1 /*****************************************************************************
2 shuffleBedMain.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 "shuffleBed.h"
13 #include "version.h"
14
15 using namespace std;
16
17 // define our program name
18 #define PROGRAM_NAME "bedtools shuffle"
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 shuffle_help(void);
26
shuffle_main(int argc,char * argv[])27 int shuffle_main(int argc, char* argv[]) {
28
29 // our configuration variables
30 bool showHelp = false;
31
32 // input files
33 string bedFile = "stdin";
34 string excludeFile;
35 string includeFile;
36 string genomeFile;
37
38 bool haveBed = true;
39 bool haveGenome = false;
40 bool haveExclude = false;
41 bool haveInclude = false;
42 bool haveSeed = false;
43 float overlapFraction = 1E-9;
44 int seed = -1;
45 bool sameChrom = false;
46 bool chooseChrom = false;
47 bool isBedpe = false;
48 size_t maxTries = 1000;
49 bool noOverlapping = false;
50 bool allowBeyondChromEnd = false;
51
52
53 for(int i = 1; i < argc; i++) {
54 int parameterLength = (int)strlen(argv[i]);
55
56 if((PARAMETER_CHECK("-h", 2, parameterLength)) ||
57 (PARAMETER_CHECK("--help", 5, parameterLength))) {
58 showHelp = true;
59 }
60 }
61
62 if(showHelp) shuffle_help();
63
64 // do some parsing (all of these parameters require 2 strings)
65 for(int i = 1; i < argc; i++) {
66
67 int parameterLength = (int)strlen(argv[i]);
68
69 if(PARAMETER_CHECK("-i", 2, parameterLength)) {
70 if ((i+1) < argc) {
71 bedFile = argv[i + 1];
72 i++;
73 }
74 }
75 else if(PARAMETER_CHECK("-g", 2, parameterLength)) {
76 if ((i+1) < argc) {
77 haveGenome = true;
78 genomeFile = argv[i + 1];
79 i++;
80 }
81 }
82 else if(PARAMETER_CHECK("-excl", 5, parameterLength)) {
83 if ((i+1) < argc) {
84 haveExclude = true;
85 excludeFile = argv[i + 1];
86 i++;
87 }
88 }
89 else if(PARAMETER_CHECK("-incl", 5, parameterLength)) {
90 if ((i+1) < argc) {
91 haveInclude = true;
92 includeFile = argv[i + 1];
93 i++;
94 }
95 }
96 else if(PARAMETER_CHECK("-seed", 5, parameterLength)) {
97 if ((i+1) < argc) {
98 haveSeed = true;
99 seed = atoi(argv[i + 1]);
100 i++;
101 }
102 }
103 else if(PARAMETER_CHECK("-chrom", 6, parameterLength)) {
104 chooseChrom = true;
105 sameChrom = true;
106 }
107 else if(PARAMETER_CHECK("-chromFirst", 11, parameterLength)) {
108 chooseChrom = true;
109 }
110 else if(PARAMETER_CHECK("-f", 2, parameterLength)) {
111 if ((i+1) < argc) {
112 overlapFraction = atof(argv[i + 1]);
113 i++;
114 }
115 }
116 else if(PARAMETER_CHECK("-maxTries", 9, parameterLength)) {
117 if ((i+1) < argc) {
118 maxTries = atoi(argv[i + 1]);
119 i++;
120 }
121 }
122 else if(PARAMETER_CHECK("-bedpe", 6, parameterLength)) {
123 isBedpe = true;
124 }
125 else if(PARAMETER_CHECK("-noOverlapping", 14, parameterLength)) {
126 noOverlapping = true;
127 }
128 else if(PARAMETER_CHECK("-allowBeyondChromEnd", 20, parameterLength)) {
129 allowBeyondChromEnd = true;
130 }
131 else {
132 cerr << endl << "*****ERROR: Unrecognized parameter: " << argv[i] << " *****" << endl << endl;
133 showHelp = true;
134 }
135 }
136
137 // make sure we have both input files
138 if (!haveBed || !haveGenome) {
139 cerr << endl << "*****" << endl << "*****ERROR: Need both a BED (-i) and a genome (-g) file. " << endl << "*****" << endl;
140 showHelp = true;
141 }
142
143 if (!showHelp) {
144 BedShuffle *bc = new BedShuffle(bedFile, genomeFile, excludeFile,
145 includeFile, haveSeed, haveExclude,
146 haveInclude, sameChrom,
147 overlapFraction, seed,
148 chooseChrom, isBedpe,
149 maxTries, noOverlapping,
150 !(allowBeyondChromEnd));
151 delete bc;
152 return 0;
153 }
154 else {
155 shuffle_help();
156 }
157 return 0;
158 }
159
shuffle_help(void)160 void shuffle_help(void) {
161
162 cerr << "\nTool: bedtools shuffle (aka shuffleBed)" << endl;
163 cerr << "Version: " << VERSION << "\n";
164 cerr << "Summary: Randomly permute the locations of a feature file among a genome." << endl << endl;
165
166 cerr << "Usage: " << PROGRAM_NAME << " [OPTIONS] -i <bed/gff/vcf> -g <genome>" << endl << endl;
167
168 cerr << "Options: " << endl;
169 cerr << "\t-excl\t" << "A BED/GFF/VCF file of coordinates in which features in -i" << endl;
170 cerr << "\t\tshould not be placed (e.g. gaps.bed)." << endl << endl;
171
172 cerr << "\t-incl\t" << "Instead of randomly placing features in a genome, the -incl" << endl;
173 cerr << "\t\toptions defines a BED/GFF/VCF file of coordinates in which " << endl;
174 cerr << "\t\tfeatures in -i should be randomly placed (e.g. genes.bed). " << endl;
175 cerr << "\t\tLarger -incl intervals will contain more shuffled regions. " << endl;
176 cerr << "\t\tThis method DISABLES -chromFirst. " << endl;
177
178 cerr << "\t-chrom\t" << "Keep features in -i on the same chromosome."<< endl;
179 cerr << "\t\t- By default, the chrom and position are randomly chosen." << endl;
180 cerr << "\t\t- NOTE: Forces use of -chromFirst (see below)." << endl << endl;
181
182 cerr << "\t-seed\t" << "Supply an integer seed for the shuffling." << endl;
183 cerr << "\t\t- By default, the seed is chosen automatically." << endl;
184 cerr << "\t\t- (INTEGER)" << endl << endl;
185
186 cerr << "\t-f\t" << "Maximum overlap (as a fraction of the -i feature) with an -excl" << endl;
187 cerr << "\t\tfeature that is tolerated before searching for a new, " << endl;
188 cerr << "\t\trandomized locus. For example, -f 0.10 allows up to 10%" << endl;
189 cerr << "\t\tof a randomized feature to overlap with a given feature" << endl;
190 cerr << "\t\tin the -excl file. **Cannot be used with -incl file.**" << endl;
191 cerr << "\t\t- Default is 1E-9 (i.e., 1bp)." << endl;
192 cerr << "\t\t- FLOAT (e.g. 0.50)" << endl << endl;
193
194 cerr << "\t-chromFirst\t" << "\n\t\tInstead of choosing a position randomly among the entire" << endl;
195 cerr << "\t\tgenome (the default), first choose a chrom randomly, and then" << endl;
196 cerr << "\t\tchoose a random start coordinate on that chrom. This leads" << endl;
197 cerr << "\t\tto features being ~uniformly distributed among the chroms," << endl;
198 cerr << "\t\tas opposed to features being distribute as a function of chrom size." << endl << endl;
199
200 cerr << "\t-bedpe\t" << "Indicate that the A file is in BEDPE format." << endl << endl;
201
202
203 cerr << "\t-maxTries\t" << "\n\t\tMax. number of attempts to find a home for a shuffled interval" << endl;
204 cerr << "\t\tin the presence of -incl or -excl." << endl;
205 cerr << "\t\tDefault = 1000." << endl;
206 cerr << "\t-noOverlapping\t" << "\n\t\tDon't allow shuffled intervals to overlap." << endl;
207
208 cerr << "\t-allowBeyondChromEnd\t" << "\n\t\tAllow shuffled intervals to be relocated to a position" << endl;
209 cerr << "\t\tin which the entire original interval cannot fit w/o exceeding" << endl;
210 cerr << "\t\tthe end of the chromosome. In this case, the end coordinate of the" << endl;
211 cerr << "\t\tshuffled interval will be set to the chromosome's length." << endl;
212 cerr << "\t\tBy default, an interval's original length must be fully-contained" << endl;
213 cerr << "\t\twithin the chromosome." << endl;
214
215 cerr << "Notes: " << endl;
216 cerr << "\t(1) The genome file should tab delimited and structured as follows:" << endl;
217 cerr << "\t <chromName><TAB><chromSize>" << endl << endl;
218 cerr << "\tFor example, Human (hg19):" << endl;
219 cerr << "\tchr1\t249250621" << endl;
220 cerr << "\tchr2\t243199373" << endl;
221 cerr << "\t..." << endl;
222 cerr << "\tchr18_gl000207_random\t4262" << endl << endl;
223
224
225 cerr << "Tips: " << endl;
226 cerr << "\tOne can use the UCSC Genome Browser's MySQL database to extract" << endl;
227 cerr << "\tchromosome sizes. For example, H. sapiens:" << endl << endl;
228 cerr << "\tmysql --user=genome --host=genome-mysql.cse.ucsc.edu -A -e \\" << endl;
229 cerr << "\t\"select chrom, size from hg19.chromInfo\" > hg19.genome" << endl << endl;
230
231
232 // end the program here
233 exit(1);
234 }
235