0
|
1 /*****************************************************************************
|
|
2 intersectMain.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 "intersectBed.h"
|
|
13 #include "version.h"
|
|
14
|
|
15 using namespace std;
|
|
16
|
|
17 // define our program name
|
|
18 #define PROGRAM_NAME "intersectBed"
|
|
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 bedAFile;
|
|
34 string bedBFile;
|
|
35
|
|
36 // input arguments
|
|
37 float overlapFraction = 1E-9;
|
|
38
|
|
39 bool haveBedA = false;
|
|
40 bool haveBedB = false;
|
|
41 bool noHit = false;
|
|
42 bool anyHit = false;
|
|
43 bool writeA = false;
|
|
44 bool writeB = false;
|
|
45 bool writeCount = false;
|
|
46 bool writeOverlap = false;
|
|
47 bool writeAllOverlap = false;
|
|
48 bool haveFraction = false;
|
|
49 bool reciprocalFraction = false;
|
|
50 bool sameStrand = false;
|
|
51 bool diffStrand = false;
|
|
52 bool obeySplits = false;
|
|
53 bool inputIsBam = false;
|
|
54 bool outputIsBam = true;
|
|
55 bool uncompressedBam = false;
|
|
56 bool sortedInput = false;
|
|
57 // check to see if we should print out some help
|
|
58 if(argc <= 1) showHelp = true;
|
|
59
|
|
60 for(int i = 1; i < argc; i++) {
|
|
61 int parameterLength = (int)strlen(argv[i]);
|
|
62
|
|
63 if((PARAMETER_CHECK("-h", 2, parameterLength)) ||
|
|
64 (PARAMETER_CHECK("--help", 5, parameterLength))) {
|
|
65 showHelp = true;
|
|
66 }
|
|
67 }
|
|
68
|
|
69 if(showHelp) ShowHelp();
|
|
70
|
|
71 // do some parsing (all of these parameters require 2 strings)
|
|
72 for(int i = 1; i < argc; i++) {
|
|
73
|
|
74 int parameterLength = (int)strlen(argv[i]);
|
|
75
|
|
76 if(PARAMETER_CHECK("-a", 2, parameterLength)) {
|
|
77 if ((i+1) < argc) {
|
|
78 haveBedA = true;
|
|
79 outputIsBam = false;
|
|
80 bedAFile = argv[i + 1];
|
|
81 i++;
|
|
82 }
|
|
83 }
|
|
84 else if(PARAMETER_CHECK("-abam", 5, parameterLength)) {
|
|
85 if ((i+1) < argc) {
|
|
86 haveBedA = true;
|
|
87 inputIsBam = true;
|
|
88 bedAFile = argv[i + 1];
|
|
89 i++;
|
|
90 }
|
|
91 }
|
|
92 else if(PARAMETER_CHECK("-b", 2, parameterLength)) {
|
|
93 if ((i+1) < argc) {
|
|
94 haveBedB = true;
|
|
95 bedBFile = argv[i + 1];
|
|
96 i++;
|
|
97 }
|
|
98 }
|
|
99 else if(PARAMETER_CHECK("-bed", 4, parameterLength)) {
|
|
100 outputIsBam = false;
|
|
101 }
|
|
102 else if(PARAMETER_CHECK("-u", 2, parameterLength)) {
|
|
103 anyHit = true;
|
|
104 }
|
|
105 else if(PARAMETER_CHECK("-f", 2, parameterLength)) {
|
|
106 if ((i+1) < argc) {
|
|
107 haveFraction = true;
|
|
108 overlapFraction = atof(argv[i + 1]);
|
|
109 i++;
|
|
110 }
|
|
111 }
|
|
112 else if(PARAMETER_CHECK("-wa", 3, parameterLength)) {
|
|
113 writeA = true;
|
|
114 }
|
|
115 else if(PARAMETER_CHECK("-wb", 3, parameterLength)) {
|
|
116 writeB = true;
|
|
117 }
|
|
118 else if(PARAMETER_CHECK("-wo", 3, parameterLength)) {
|
|
119 writeOverlap = true;
|
|
120 }
|
|
121 else if(PARAMETER_CHECK("-wao", 4, parameterLength)) {
|
|
122 writeAllOverlap = true;
|
|
123 writeOverlap = true;
|
|
124 }
|
|
125 else if(PARAMETER_CHECK("-c", 2, parameterLength)) {
|
|
126 writeCount = true;
|
|
127 }
|
|
128 else if(PARAMETER_CHECK("-r", 2, parameterLength)) {
|
|
129 reciprocalFraction = true;
|
|
130 }
|
|
131 else if (PARAMETER_CHECK("-v", 2, parameterLength)) {
|
|
132 noHit = true;
|
|
133 }
|
|
134 else if (PARAMETER_CHECK("-s", 2, parameterLength)) {
|
|
135 sameStrand = true;
|
|
136 }
|
|
137 else if (PARAMETER_CHECK("-S", 2, parameterLength)) {
|
|
138 diffStrand = true;
|
|
139 }
|
|
140 else if (PARAMETER_CHECK("-split", 6, parameterLength)) {
|
|
141 obeySplits = true;
|
|
142 }
|
|
143 else if(PARAMETER_CHECK("-ubam", 5, parameterLength)) {
|
|
144 uncompressedBam = true;
|
|
145 }
|
|
146 else if(PARAMETER_CHECK("-sorted", 7, parameterLength)) {
|
|
147 sortedInput = true;
|
|
148 }
|
|
149 else {
|
|
150 cerr << endl << "*****ERROR: Unrecognized parameter: " << argv[i] << " *****" << endl << endl;
|
|
151 showHelp = true;
|
|
152 }
|
|
153 }
|
|
154
|
|
155 // make sure we have both input files
|
|
156 if (!haveBedA || !haveBedB) {
|
|
157 cerr << endl << "*****" << endl << "*****ERROR: Need -a and -b files. " << endl << "*****" << endl;
|
|
158 showHelp = true;
|
|
159 }
|
|
160
|
|
161 if (anyHit && noHit) {
|
|
162 cerr << endl << "*****" << endl << "*****ERROR: Request either -u OR -v, not both." << endl << "*****" << endl;
|
|
163 showHelp = true;
|
|
164 }
|
|
165
|
|
166 if (writeB && writeCount) {
|
|
167 cerr << endl << "*****" << endl << "*****ERROR: Request either -wb OR -c, not both." << endl << "*****" << endl;
|
|
168 showHelp = true;
|
|
169 }
|
|
170
|
|
171 if (writeCount && writeOverlap) {
|
|
172 cerr << endl << "*****" << endl << "*****ERROR: Request either -wb OR -wo, not both." << endl << "*****" << endl;
|
|
173 showHelp = true;
|
|
174 }
|
|
175
|
|
176 if (writeA && writeOverlap) {
|
|
177 cerr << endl << "*****" << endl << "*****ERROR: Request either -wa OR -wo, not both." << endl << "*****" << endl;
|
|
178 showHelp = true;
|
|
179 }
|
|
180
|
|
181 if (writeB && writeOverlap) {
|
|
182 cerr << endl << "*****" << endl << "*****ERROR: Request either -wb OR -wo, not both." << endl << "*****" << endl;
|
|
183 showHelp = true;
|
|
184 }
|
|
185
|
|
186 if (reciprocalFraction && !haveFraction) {
|
|
187 cerr << endl << "*****" << endl << "*****ERROR: If using -r, you need to define -f." << endl << "*****" << endl;
|
|
188 showHelp = true;
|
|
189 }
|
|
190
|
|
191 if (anyHit && writeCount) {
|
|
192 cerr << endl << "*****" << endl << "*****ERROR: Request either -u OR -c, not both." << endl << "*****" << endl;
|
|
193 showHelp = true;
|
|
194 }
|
|
195
|
|
196 if (anyHit && writeB) {
|
|
197 cerr << endl << "*****" << endl << "*****ERROR: Request either -u OR -wb, not both." << endl << "*****" << endl;
|
|
198 showHelp = true;
|
|
199 }
|
|
200
|
|
201 if (anyHit && writeOverlap) {
|
|
202 cerr << endl << "*****" << endl << "*****ERROR: Request either -u OR -wo, not both." << endl << "*****" << endl;
|
|
203 showHelp = true;
|
|
204 }
|
|
205
|
|
206 if (sameStrand && diffStrand) {
|
|
207 cerr << endl << "*****" << endl << "*****ERROR: Request either -s OR -S, not both." << endl << "*****" << endl;
|
|
208 showHelp = true;
|
|
209 }
|
|
210
|
|
211 if (!showHelp) {
|
|
212
|
|
213 BedIntersect *bi = new BedIntersect(bedAFile, bedBFile, anyHit, writeA, writeB, writeOverlap,
|
|
214 writeAllOverlap, overlapFraction, noHit, writeCount, sameStrand, diffStrand,
|
|
215 reciprocalFraction, obeySplits, inputIsBam, outputIsBam, uncompressedBam, sortedInput);
|
|
216 delete bi;
|
|
217 return 0;
|
|
218 }
|
|
219 else {
|
|
220 ShowHelp();
|
|
221 }
|
|
222 }
|
|
223
|
|
224 void ShowHelp(void) {
|
|
225
|
|
226 cerr << endl << "Program: " << PROGRAM_NAME << " (v" << VERSION << ")" << endl;
|
|
227
|
|
228 cerr << "Author: Aaron Quinlan (aaronquinlan@gmail.com)" << endl;
|
|
229
|
|
230 cerr << "Summary: Report overlaps between two feature files." << endl << endl;
|
|
231
|
|
232 cerr << "Usage: " << PROGRAM_NAME << " [OPTIONS] -a <bed/gff/vcf> -b <bed/gff/vcf>" << endl << endl;
|
|
233
|
|
234 cerr << "Options: " << endl;
|
|
235
|
|
236 cerr << "\t-abam\t" << "The A input file is in BAM format. Output will be BAM as well." << endl << endl;
|
|
237
|
|
238 cerr << "\t-ubam\t" << "Write uncompressed BAM output. Default is to write compressed BAM." << endl << endl;
|
|
239
|
|
240 cerr << "\t-bed\t" << "When using BAM input (-abam), write output as BED. The default" << endl;
|
|
241 cerr << "\t\tis to write output in BAM when using -abam." << endl << endl;
|
|
242
|
|
243 cerr << "\t-wa\t" << "Write the original entry in A for each overlap." << endl << endl;
|
|
244
|
|
245 cerr << "\t-wb\t" << "Write the original entry in B for each overlap." << endl;
|
|
246 cerr << "\t\t- Useful for knowing _what_ A overlaps. Restricted by -f and -r." << endl << endl;
|
|
247
|
|
248 cerr << "\t-wo\t" << "Write the original A and B entries plus the number of base" << endl;
|
|
249 cerr << "\t\tpairs of overlap between the two features." << endl;
|
|
250 cerr << "\t\t- Overlaps restricted by -f and -r." << endl;
|
|
251 cerr << "\t\t Only A features with overlap are reported." << endl << endl;
|
|
252
|
|
253 cerr << "\t-wao\t" << "Write the original A and B entries plus the number of base" << endl;
|
|
254 cerr << "\t\tpairs of overlap between the two features." << endl;
|
|
255 cerr << "\t\t- Overlapping features restricted by -f and -r." << endl;
|
|
256 cerr << "\t\t However, A features w/o overlap are also reported" << endl;
|
|
257 cerr << "\t\t with a NULL B feature and overlap = 0." << endl << endl;
|
|
258
|
|
259 cerr << "\t-u\t" << "Write the original A entry _once_ if _any_ overlaps found in B." << endl;
|
|
260 cerr << "\t\t- In other words, just report the fact >=1 hit was found." << endl;
|
|
261 cerr << "\t\t- Overlaps restricted by -f and -r." << endl << endl;
|
|
262
|
|
263 cerr << "\t-c\t" << "For each entry in A, report the number of overlaps with B." << endl;
|
|
264 cerr << "\t\t- Reports 0 for A entries that have no overlap with B." << endl;
|
|
265 cerr << "\t\t- Overlaps restricted by -f and -r." << endl << endl;
|
|
266
|
|
267 cerr << "\t-v\t" << "Only report those entries in A that have _no overlaps_ with B." << endl;
|
|
268 cerr << "\t\t- Similar to \"grep -v\" (an homage)." << endl << endl;
|
|
269
|
|
270 cerr << "\t-f\t" << "Minimum overlap required as a fraction of A." << endl;
|
|
271 cerr << "\t\t- Default is 1E-9 (i.e., 1bp)." << endl;
|
|
272 cerr << "\t\t- FLOAT (e.g. 0.50)" << endl << endl;
|
|
273
|
|
274 cerr << "\t-r\t" << "Require that the fraction overlap be reciprocal for A and B." << endl;
|
|
275 cerr << "\t\t- In other words, if -f is 0.90 and -r is used, this requires" << endl;
|
|
276 cerr << "\t\t that B overlap 90% of A and A _also_ overlaps 90% of B." << endl << endl;
|
|
277
|
|
278 cerr << "\t-s\t" << "Require same strandedness. That is, only report hits in B that" << endl;
|
|
279 cerr << "\t\toverlap A on the _same_ strand." << endl;
|
|
280 cerr << "\t\t- By default, overlaps are reported without respect to strand." << endl << endl;
|
|
281
|
|
282 cerr << "\t-S\t" << "Require different strandedness. That is, only report hits in B that" << endl;
|
|
283 cerr << "\t\toverlap A on the _opposite_ strand." << endl;
|
|
284 cerr << "\t\t- By default, overlaps are reported without respect to strand." << endl << endl;
|
|
285
|
|
286 cerr << "\t-split\t" << "Treat \"split\" BAM or BED12 entries as distinct BED intervals." << endl << endl;
|
|
287
|
|
288 cerr << "\t-sorted\t" << "Use the \"chromsweep\" algorithm for sorted (-k1,1 -k2,2n) input" << endl;
|
|
289 cerr << "\t\tNOTE: this will trust, but not enforce that data is sorted. Caveat emptor." << endl << endl;
|
|
290
|
|
291 // end the program here
|
|
292 exit(1);
|
|
293
|
|
294 }
|