Mercurial > repos > aaronquinlan > multi_intersect
comparison BEDTools-Version-2.14.3/src/genomeCoverageBed/genomeCoverageMain.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 genomeCoverageMain.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 "genomeCoverageBed.h" | |
| 13 #include "version.h" | |
| 14 | |
| 15 using namespace std; | |
| 16 | |
| 17 // define our program name | |
| 18 #define PROGRAM_NAME "genomeCoverageBed" | |
| 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; | |
| 34 string genomeFile; | |
| 35 int max = INT_MAX; | |
| 36 float scale = 1.0; | |
| 37 | |
| 38 bool haveBed = false; | |
| 39 bool bamInput = false; | |
| 40 bool haveGenome = false; | |
| 41 bool startSites = false; | |
| 42 bool bedGraph = false; | |
| 43 bool bedGraphAll = false; | |
| 44 bool eachBase = false; | |
| 45 bool eachBaseZeroBased = false; | |
| 46 bool obeySplits = false; | |
| 47 bool haveScale = false; | |
| 48 bool filterByStrand = false; | |
| 49 bool only_5p_end = false; | |
| 50 bool only_3p_end = false; | |
| 51 bool add_gb_track_line = false; | |
| 52 string gb_track_opts; | |
| 53 string requestedStrand = "X"; | |
| 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("-i", 2, parameterLength)) { | |
| 75 if ((i+1) < argc) { | |
| 76 haveBed = true; | |
| 77 bedFile = argv[i + 1]; | |
| 78 i++; | |
| 79 } | |
| 80 } | |
| 81 else if(PARAMETER_CHECK("-ibam", 5, parameterLength)) { | |
| 82 if ((i+1) < argc) { | |
| 83 haveBed = true; | |
| 84 bamInput = true; | |
| 85 bedFile = argv[i + 1]; | |
| 86 i++; | |
| 87 } | |
| 88 } | |
| 89 else if(PARAMETER_CHECK("-g", 2, parameterLength)) { | |
| 90 if ((i+1) < argc) { | |
| 91 haveGenome = true; | |
| 92 genomeFile = argv[i + 1]; | |
| 93 i++; | |
| 94 } | |
| 95 } | |
| 96 else if(PARAMETER_CHECK("-d", 2, parameterLength)) { | |
| 97 eachBase = true; | |
| 98 } | |
| 99 else if(PARAMETER_CHECK("-dz", 3, parameterLength)) { | |
| 100 eachBase = true; | |
| 101 eachBaseZeroBased = true; | |
| 102 } | |
| 103 else if(PARAMETER_CHECK("-bg", 3, parameterLength)) { | |
| 104 bedGraph = true; | |
| 105 } | |
| 106 else if(PARAMETER_CHECK("-bga", 4, parameterLength)) { | |
| 107 bedGraphAll = true; | |
| 108 } | |
| 109 else if(PARAMETER_CHECK("-max", 4, parameterLength)) { | |
| 110 if ((i+1) < argc) { | |
| 111 max = atoi(argv[i + 1]); | |
| 112 i++; | |
| 113 } | |
| 114 } | |
| 115 else if(PARAMETER_CHECK("-scale", 6, parameterLength)) { | |
| 116 if ((i+1) < argc) { | |
| 117 haveScale = true; | |
| 118 scale = atof(argv[i + 1]); | |
| 119 i++; | |
| 120 } | |
| 121 } | |
| 122 else if(PARAMETER_CHECK("-split", 6, parameterLength)) { | |
| 123 obeySplits = true; | |
| 124 } | |
| 125 else if(PARAMETER_CHECK("-strand", 7, parameterLength)) { | |
| 126 if ((i+1) < argc) { | |
| 127 filterByStrand = true; | |
| 128 requestedStrand = argv[i+1][0]; | |
| 129 if (!(requestedStrand == "-" || requestedStrand == "+")) { | |
| 130 cerr << "*****ERROR: invalid -strand value (" << requestedStrand << "). Allowed options are + or -" << endl; | |
| 131 showHelp = true; | |
| 132 } | |
| 133 i++; | |
| 134 } | |
| 135 else { | |
| 136 cerr << "*****ERROR: -strand options requires a value: + or -" << endl; | |
| 137 showHelp = true; | |
| 138 } | |
| 139 } | |
| 140 else if(PARAMETER_CHECK("-3", 2, parameterLength)) { | |
| 141 only_3p_end = true; | |
| 142 } | |
| 143 else if(PARAMETER_CHECK("-5", 2, parameterLength)) { | |
| 144 only_5p_end = true; | |
| 145 } | |
| 146 else if(PARAMETER_CHECK("-trackline", 10, parameterLength)) { | |
| 147 add_gb_track_line = true; | |
| 148 } | |
| 149 else if(PARAMETER_CHECK("-trackopts", 10, parameterLength)) { | |
| 150 if ((i+1) < argc) { | |
| 151 add_gb_track_line = true; | |
| 152 gb_track_opts = argv[i+1]; | |
| 153 i++; | |
| 154 } else { | |
| 155 cerr << "*****ERROR: -trackopts options requires a value (UCSC/GB track definition parameters)" << endl; | |
| 156 showHelp = true; | |
| 157 } | |
| 158 } | |
| 159 else { | |
| 160 cerr << endl << "*****ERROR: Unrecognized parameter: " << argv[i] << " *****" << endl << endl; | |
| 161 showHelp = true; | |
| 162 } | |
| 163 } | |
| 164 | |
| 165 // make sure we have both input files | |
| 166 if (!haveBed && !haveGenome && !bamInput) { | |
| 167 cerr << endl << "*****" << endl << "*****ERROR: Need both a BED (-i) and a genome (-g) file. " << endl << "*****" << endl; | |
| 168 showHelp = true; | |
| 169 } | |
| 170 if (bedGraph && eachBase) { | |
| 171 cerr << endl << "*****" << endl << "*****ERROR: Use -d/-dz or -bg, not both" << endl << "*****" << endl; | |
| 172 showHelp = true; | |
| 173 } | |
| 174 if (bedGraphAll && eachBase) { | |
| 175 cerr << endl << "*****" << endl << "*****ERROR: Use -d/-dz or -bga, not both" << endl << "*****" << endl; | |
| 176 showHelp = true; | |
| 177 } | |
| 178 | |
| 179 if (only_3p_end && only_5p_end) { | |
| 180 cerr << endl << "*****" << endl << "*****ERROR: Use -3 or -5, not both " << endl << "*****" << endl; | |
| 181 showHelp = true; | |
| 182 } | |
| 183 | |
| 184 if ( (only_3p_end||only_5p_end) && obeySplits) { | |
| 185 cerr << endl << "*****" << endl << "*****ERROR: Use -split can't be used with -3 or -5." << endl << "*****" << endl; | |
| 186 showHelp = true; | |
| 187 } | |
| 188 | |
| 189 if (add_gb_track_line && !(bedGraph||bedGraphAll)) { | |
| 190 cerr << endl << "*****" << endl << "*****ERROR: Using -trackline requires bedGraph output (use -bg or -bga)." << endl << "*****" << endl; | |
| 191 showHelp = true; | |
| 192 } | |
| 193 | |
| 194 if (haveScale && !(bedGraph||bedGraphAll||eachBase)) { | |
| 195 cerr << endl << "*****" << endl << "*****ERROR: Using -scale requires bedGraph output (use -bg or -bga) or per base depth (-d)." << endl << "*****" << endl; | |
| 196 showHelp = true; | |
| 197 } | |
| 198 | |
| 199 if (!showHelp) { | |
| 200 BedGenomeCoverage *bc = new BedGenomeCoverage(bedFile, genomeFile, eachBase, | |
| 201 startSites, bedGraph, bedGraphAll, | |
| 202 max, scale, bamInput, obeySplits, | |
| 203 filterByStrand, requestedStrand, | |
| 204 only_5p_end, only_3p_end, | |
| 205 eachBaseZeroBased, | |
| 206 add_gb_track_line, gb_track_opts); | |
| 207 delete bc; | |
| 208 | |
| 209 return 0; | |
| 210 } | |
| 211 else { | |
| 212 ShowHelp(); | |
| 213 } | |
| 214 } | |
| 215 | |
| 216 void ShowHelp(void) { | |
| 217 | |
| 218 cerr << endl << "Program: " << PROGRAM_NAME << " (v" << VERSION << ")" << endl; | |
| 219 | |
| 220 cerr << "Authors: Aaron Quinlan (aaronquinlan@gmail.com)" << endl; | |
| 221 cerr << " Assaf Gordon, CSHL" << endl << endl; | |
| 222 | |
| 223 cerr << "Summary: Compute the coverage of a feature file among a genome." << endl << endl; | |
| 224 | |
| 225 cerr << "Usage: " << PROGRAM_NAME << " [OPTIONS] -i <bed/gff/vcf> -g <genome>" << endl << endl; | |
| 226 | |
| 227 cerr << "Options: " << endl; | |
| 228 | |
| 229 cerr << "\t-ibam\t\t" << "The input file is in BAM format." << endl; | |
| 230 cerr << "\t\t\tNote: BAM _must_ be sorted by position" << endl << endl; | |
| 231 | |
| 232 cerr << "\t-d\t\t" << "Report the depth at each genome position (with one-based coordinates)." << endl; | |
| 233 cerr << "\t\t\tDefault behavior is to report a histogram." << endl << endl; | |
| 234 | |
| 235 cerr << "\t-dz\t\t" << "Report the depth at each genome position (with zero-based coordinates)." << endl; | |
| 236 cerr << "\t\t\tReports only non-zero positions." << endl; | |
| 237 cerr << "\t\t\tDefault behavior is to report a histogram." << endl << endl; | |
| 238 | |
| 239 cerr << "\t-bg\t\t" << "Report depth in BedGraph format. For details, see:" << endl; | |
| 240 cerr << "\t\t\tgenome.ucsc.edu/goldenPath/help/bedgraph.html" << endl << endl; | |
| 241 | |
| 242 cerr << "\t-bga\t\t" << "Report depth in BedGraph format, as above (-bg)." << endl; | |
| 243 cerr << "\t\t\tHowever with this option, regions with zero " << endl; | |
| 244 cerr << "\t\t\tcoverage are also reported. This allows one to" << endl; | |
| 245 cerr << "\t\t\tquickly extract all regions of a genome with 0 " << endl; | |
| 246 cerr << "\t\t\tcoverage by applying: \"grep -w 0$\" to the output." << endl << endl; | |
| 247 | |
| 248 cerr << "\t-split\t\t" << "Treat \"split\" BAM or BED12 entries as distinct BED intervals." << endl; | |
| 249 cerr << "\t\t\twhen computing coverage." << endl; | |
| 250 cerr << "\t\t\tFor BAM files, this uses the CIGAR \"N\" and \"D\" operations " << endl; | |
| 251 cerr << "\t\t\tto infer the blocks for computing coverage." << endl; | |
| 252 cerr << "\t\t\tFor BED12 files, this uses the BlockCount, BlockStarts, and BlockEnds" << endl; | |
| 253 cerr << "\t\t\tfields (i.e., columns 10,11,12)." << endl << endl; | |
| 254 | |
| 255 cerr << "\t-strand\t\t" << "Calculate coverage of intervals from a specific strand." << endl; | |
| 256 cerr << "\t\t\tWith BED files, requires at least 6 columns (strand is column 6). " << endl; | |
| 257 cerr << "\t\t\t- (STRING): can be + or -" << endl << endl; | |
| 258 | |
| 259 cerr << "\t-5\t\t" << "Calculate coverage of 5\" positions (instead of entire interval)." << endl << endl; | |
| 260 | |
| 261 cerr << "\t-3\t\t" << "Calculate coverage of 3\" positions (instead of entire interval)." << endl << endl; | |
| 262 | |
| 263 cerr << "\t-max\t\t" << "Combine all positions with a depth >= max into" << endl; | |
| 264 cerr << "\t\t\ta single bin in the histogram. Irrelevant" << endl; | |
| 265 cerr << "\t\t\tfor -d and -bedGraph" << endl; | |
| 266 cerr << "\t\t\t- (INTEGER)" << endl << endl; | |
| 267 | |
| 268 cerr << "\t-scale\t\t" << "Scale the coverage by a constant factor." << endl; | |
| 269 cerr << "\t\t\tEach coverage value is multiplied by this factor before being reported." << endl; | |
| 270 cerr << "\t\t\tUseful for normalizing coverage by, e.g., reads per million (RPM)." << endl; | |
| 271 cerr << "\t\t\t- Default is 1.0; i.e., unscaled." << endl; | |
| 272 cerr << "\t\t\t- (FLOAT)" << endl << endl; | |
| 273 | |
| 274 cerr << "\t-trackline\t" << "Adds a UCSC/Genome-Browser track line definition in the first line of the output." << endl; | |
| 275 cerr <<"\t\t\t- See here for more details about track line definition:" << endl; | |
| 276 cerr <<"\t\t\t http://genome.ucsc.edu/goldenPath/help/bedgraph.html" << endl; | |
| 277 cerr <<"\t\t\t- NOTE: When adding a trackline definition, the output BedGraph can be easily" << endl; | |
| 278 cerr <<"\t\t\t uploaded to the Genome Browser as a custom track," << endl; | |
| 279 cerr <<"\t\t\t BUT CAN NOT be converted into a BigWig file (w/o removing the first line)." << endl << endl; | |
| 280 | |
| 281 cerr << "\t-trackopts\t"<<"Writes additional track line definition parameters in the first line." << endl; | |
| 282 cerr <<"\t\t\t- Example:" << endl; | |
| 283 cerr <<"\t\t\t -trackopts 'name=\"My Track\" visibility=2 color=255,30,30'" << endl; | |
| 284 cerr <<"\t\t\t Note the use of single-quotes if you have spaces in your parameters." << endl; | |
| 285 cerr <<"\t\t\t- (TEXT)" << endl << endl; | |
| 286 | |
| 287 cerr << "Notes: " << endl; | |
| 288 cerr << "\t(1) The genome file should tab delimited and structured as follows:" << endl; | |
| 289 cerr << "\t <chromName><TAB><chromSize>" << endl << endl; | |
| 290 cerr << "\tFor example, Human (hg19):" << endl; | |
| 291 cerr << "\tchr1\t249250621" << endl; | |
| 292 cerr << "\tchr2\t243199373" << endl; | |
| 293 cerr << "\t..." << endl; | |
| 294 cerr << "\tchr18_gl000207_random\t4262" << endl << endl; | |
| 295 | |
| 296 cerr << "\t(2) The input BED (-i) file must be grouped by chromosome." << endl; | |
| 297 cerr << "\t A simple \"sort -k 1,1 <BED> > <BED>.sorted\" will suffice."<< endl << endl; | |
| 298 | |
| 299 cerr << "\t(3) The input BAM (-ibam) file must be sorted by position." << endl; | |
| 300 cerr << "\t A \"samtools sort <BAM>\" should suffice."<< endl << endl; | |
| 301 | |
| 302 cerr << "Tips: " << endl; | |
| 303 cerr << "\tOne can use the UCSC Genome Browser's MySQL database to extract" << endl; | |
| 304 cerr << "\tchromosome sizes. For example, H. sapiens:" << endl << endl; | |
| 305 cerr << "\tmysql --user=genome --host=genome-mysql.cse.ucsc.edu -A -e \\" << endl; | |
| 306 cerr << "\t\"select chrom, size from hg19.chromInfo\" > hg19.genome" << endl << endl; | |
| 307 | |
| 308 | |
| 309 // end the program here | |
| 310 exit(1); | |
| 311 } | |
| 312 |
