comparison BEDTools-Version-2.14.3/src/slopBed/slopBedMain.cpp @ 0:dfcd8b6c1bda

Uploaded
author aaronquinlan
date Thu, 03 Nov 2011 10:25:04 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:dfcd8b6c1bda
1 /*****************************************************************************
2 slopBedMain.cpp
3
4 (c) 2009 - Aaron Quinlan
5 Hall Laboratory
6 Department of Biochemistry and Molecular Genetics
7 University of Virginia
8 aaronquinlan@gmail.com
9
10 Licenced under the GNU General Public License 2.0 license.
11 ******************************************************************************/
12 #include "slopBed.h"
13 #include "version.h"
14
15 using namespace std;
16
17 // define our program name
18 #define PROGRAM_NAME "slopBed"
19
20
21 // define our parameter checking macro
22 #define PARAMETER_CHECK(param, paramLen, actualLen) (strncmp(argv[i], param, min(actualLen, paramLen))== 0) && (actualLen == paramLen)
23
24 // function declarations
25 void ShowHelp(void);
26
27 int main(int argc, char* argv[]) {
28
29 // our configuration variables
30 bool showHelp = false;
31
32 // input files
33 string bedFile = "stdin";
34 string genomeFile;
35
36 bool haveBed = true;
37 bool haveGenome = false;
38 bool haveLeft = false;
39 bool haveRight = false;
40 bool haveBoth = false;
41
42 bool forceStrand = false;
43 float leftSlop = 0.0;
44 float rightSlop = 0.0;
45 bool fractional = false;
46
47 for(int i = 1; i < argc; i++) {
48 int parameterLength = (int)strlen(argv[i]);
49
50 if((PARAMETER_CHECK("-h", 2, parameterLength)) ||
51 (PARAMETER_CHECK("--help", 5, parameterLength))) {
52 showHelp = true;
53 }
54 }
55
56 if(showHelp) ShowHelp();
57
58 // do some parsing (all of these parameters require 2 strings)
59 for(int i = 1; i < argc; i++) {
60
61 int parameterLength = (int)strlen(argv[i]);
62
63 if(PARAMETER_CHECK("-i", 2, parameterLength)) {
64 if ((i+1) < argc) {
65 bedFile = argv[i + 1];
66 i++;
67 }
68 }
69 else if(PARAMETER_CHECK("-g", 2, parameterLength)) {
70 if ((i+1) < argc) {
71 haveGenome = true;
72 genomeFile = argv[i + 1];
73 i++;
74 }
75 }
76 else if(PARAMETER_CHECK("-l", 2, parameterLength)) {
77 if ((i+1) < argc) {
78 haveLeft = true;
79 leftSlop = atof(argv[i + 1]);
80 i++;
81 }
82 }
83 else if(PARAMETER_CHECK("-r", 2, parameterLength)) {
84 if ((i+1) < argc) {
85 haveRight = true;
86 rightSlop = atof(argv[i + 1]);
87 i++;
88 }
89 }
90 else if(PARAMETER_CHECK("-b", 2, parameterLength)) {
91 if ((i+1) < argc) {
92 haveBoth = true;
93 leftSlop = atof(argv[i + 1]);
94 rightSlop = atof(argv[i + 1]);
95 i++;
96 }
97 }
98 else if(PARAMETER_CHECK("-s", 2, parameterLength)) {
99 forceStrand = true;
100 }
101 else if(PARAMETER_CHECK("-pct", 4, parameterLength)) {
102 fractional = true;
103 }
104 else {
105 cerr << endl << "*****ERROR: Unrecognized parameter: " << argv[i] << " *****" << endl << endl;
106 showHelp = true;
107 }
108 }
109
110 // make sure we have both input files
111 if (!haveBed || !haveGenome) {
112 cerr << endl << "*****" << endl << "*****ERROR: Need both a BED (-i) and a genome (-g) file. " << endl << "*****" << endl;
113 showHelp = true;
114 }
115 if (!haveLeft && !haveRight && !haveBoth) {
116 cerr << endl << "*****" << endl << "*****ERROR: Need -l and -r together or -b alone. " << endl << "*****" << endl;
117 showHelp = true;
118 }
119 if ((!haveLeft && haveRight) || (haveLeft && !haveRight)) {
120 cerr << endl << "*****" << endl << "*****ERROR: Need both -l and -r. " << endl << "*****" << endl;
121 showHelp = true;
122 }
123 if (forceStrand && (!(haveLeft) || !(haveRight))) {
124 cerr << endl << "*****" << endl << "*****ERROR: Must supply -l and -r with -s. " << endl << "*****" << endl;
125 showHelp = true;
126 }
127
128 if (!showHelp) {
129 BedSlop *bc = new BedSlop(bedFile, genomeFile, forceStrand, leftSlop, rightSlop, fractional);
130 delete bc;
131
132 return 0;
133 }
134 else {
135 ShowHelp();
136 }
137 }
138
139 void ShowHelp(void) {
140
141 cerr << endl << "Program: " << PROGRAM_NAME << " (v" << VERSION << ")" << endl;
142
143 cerr << "Author: Aaron Quinlan (aaronquinlan@gmail.com)" << endl;
144
145 cerr << "Summary: Add requested base pairs of \"slop\" to each feature." << endl << endl;
146
147 cerr << "Usage: " << PROGRAM_NAME << " [OPTIONS] -i <bed/gff/vcf> -g <genome> [-b <int> or (-l and -r)]" << endl << endl;
148
149 cerr << "Options: " << endl;
150 cerr << "\t-b\t" << "Increase the BED/GFF/VCF entry by -b base pairs in each direction." << endl;
151 cerr << "\t\t- (Integer) or (Float, e.g. 0.1) if used with -pct." << endl << endl;
152
153 cerr << "\t-l\t" << "The number of base pairs to subtract from the start coordinate." << endl;
154 cerr << "\t\t- (Integer) or (Float, e.g. 0.1) if used with -pct." << endl << endl;
155
156 cerr << "\t-r\t" << "The number of base pairs to add to the end coordinate." << endl;
157 cerr << "\t\t- (Integer) or (Float, e.g. 0.1) if used with -pct." << endl << endl;
158
159 cerr << "\t-s\t" << "Define -l and -r based on strand." << endl;
160 cerr << "\t\tE.g. if used, -l 500 for a negative-stranded feature, " << endl;
161 cerr << "\t\tit will add 500 bp downstream. Default = false." << endl << endl;
162
163 cerr << "\t-pct\t" << "Define -l and -r as a fraction of the feature's length." << endl;
164 cerr << "\t\tE.g. if used on a 1000bp feature, -l 0.50, " << endl;
165 cerr << "\t\twill add 500 bp \"upstream\". Default = false." << endl << endl;
166
167 cerr << "Notes: " << endl;
168 cerr << "\t(1) Starts will be set to 0 if options would force it below 0." << endl;
169 cerr << "\t(2) Ends will be set to the chromosome length if requested slop would" << endl;
170 cerr << "\tforce it above the max chrom length." << endl;
171
172 cerr << "\t(3) The genome file should tab delimited and structured as follows:" << endl;
173 cerr << "\n\t<chromName><TAB><chromSize>" << endl << endl;
174 cerr << "\tFor example, Human (hg19):" << endl;
175 cerr << "\tchr1\t249250621" << endl;
176 cerr << "\tchr2\t243199373" << endl;
177 cerr << "\t..." << endl;
178 cerr << "\tchr18_gl000207_random\t4262" << endl << endl;
179
180
181 cerr << "Tips: " << endl;
182 cerr << "\tOne can use the UCSC Genome Browser's MySQL database to extract" << endl;
183 cerr << "\tchromosome sizes. For example, H. sapiens:" << endl << endl;
184 cerr << "\tmysql --user=genome --host=genome-mysql.cse.ucsc.edu -A -e \\" << endl;
185 cerr << "\t\"select chrom, size from hg19.chromInfo\" > hg19.genome" << endl << endl;
186
187
188 // end the program here
189 exit(1);
190 }