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