comparison BEDTools-Version-2.14.3/src/closestBed/closestMain.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 closestMain.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 "closestBed.h"
13 #include "version.h"
14
15 using namespace std;
16
17 // define our program name
18 #define PROGRAM_NAME "closestBed"
19
20 // define our parameter checking macro
21 #define PARAMETER_CHECK(param, paramLen, actualLen) (strncmp(argv[i], param, min(actualLen, paramLen))== 0) && (actualLen == paramLen)
22
23 // function declarations
24 void ShowHelp(void);
25
26 int main(int argc, char* argv[]) {
27
28 // our configuration variables
29 bool showHelp = false;
30
31 // input files
32 string bedAFile;
33 string bedBFile;
34 string tieMode = "all";
35 string strandedDistMode = "";
36
37 bool haveBedA = false;
38 bool haveBedB = false;
39 bool haveTieMode = false;
40 bool sameStrand = false;
41 bool diffStrand = false;
42 bool ignoreOverlaps = false;
43 bool reportDistance = false;
44 bool signDistance = false;
45 bool haveStrandedDistMode = false;
46
47
48 // check to see if we should print out some help
49 if(argc <= 1) showHelp = true;
50
51 for(int i = 1; i < argc; i++) {
52 int parameterLength = (int)strlen(argv[i]);
53
54 if( (PARAMETER_CHECK("-h", 2, parameterLength)) ||
55 (PARAMETER_CHECK("--help", 5, parameterLength))) {
56 showHelp = true;
57 }
58 }
59
60 if(showHelp) ShowHelp();
61
62 // do some parsing (all of these parameters require 2 strings)
63 for(int i = 1; i < argc; i++) {
64
65 int parameterLength = (int)strlen(argv[i]);
66
67 if(PARAMETER_CHECK("-a", 2, parameterLength)) {
68 if ((i+1) < argc) {
69 haveBedA = true;
70 bedAFile = argv[i + 1];
71 i++;
72 }
73 }
74 else if(PARAMETER_CHECK("-b", 2, parameterLength)) {
75 if ((i+1) < argc) {
76 haveBedB = true;
77 bedBFile = argv[i + 1];
78 i++;
79 }
80 }
81 else if (PARAMETER_CHECK("-s", 2, parameterLength)) {
82 sameStrand = true;
83 }
84 else if (PARAMETER_CHECK("-S", 2, parameterLength)) {
85 diffStrand = true;
86 }
87 else if (PARAMETER_CHECK("-d", 2, parameterLength)) {
88 reportDistance = true;
89 }
90 else if (PARAMETER_CHECK("-D", 2, parameterLength)) {
91 if ((i+1) < argc) {
92 reportDistance = true;
93 signDistance = true;
94 haveStrandedDistMode = true;
95 strandedDistMode = argv[i + 1];
96 i++;
97 }
98 }
99 else if (PARAMETER_CHECK("-io", 3, parameterLength)) {
100 ignoreOverlaps = true;
101 }
102 else if (PARAMETER_CHECK("-t", 2, parameterLength)) {
103 if ((i+1) < argc) {
104 haveTieMode = true;
105 tieMode = argv[i + 1];
106 i++;
107 }
108 }
109 else {
110 cerr << endl << "*****ERROR: Unrecognized parameter: " << argv[i] << " *****" << endl << endl;
111 showHelp = true;
112 }
113 }
114
115 // make sure we have both input files
116 if (!haveBedA || !haveBedB) {
117 cerr << endl << "*****" << endl << "*****ERROR: Need -a and -b files. " << endl << "*****" << endl;
118 showHelp = true;
119 }
120
121 if (haveTieMode && (tieMode != "all") && (tieMode != "first")
122 && (tieMode != "last")) {
123 cerr << endl << "*****" << endl << "*****ERROR: Request \"all\" or \"first\" or \"last\" for Tie Mode (-t)" << endl << "*****" << endl;
124 showHelp = true;
125 }
126
127 if (haveStrandedDistMode && (strandedDistMode != "a") && (strandedDistMode != "b")
128 && (strandedDistMode != "ref")) {
129 cerr << endl << "*****" << endl << "*****ERROR: Request \"a\" or \"b\" or \"ref\" for Stranded Distance Mode (-D)" << endl << "*****" << endl;
130 showHelp = true;
131 }
132
133 if (sameStrand && diffStrand) {
134 cerr << endl << "*****" << endl << "*****ERROR: Request either -s OR -S, not both." << endl << "*****" << endl;
135 showHelp = true;
136 }
137
138 if (!showHelp) {
139 BedClosest *bc = new BedClosest(bedAFile, bedBFile, sameStrand, diffStrand, tieMode, reportDistance, signDistance, strandedDistMode, ignoreOverlaps);
140 delete bc;
141 return 0;
142 }
143 else {
144 ShowHelp();
145 }
146 }
147
148 void ShowHelp(void) {
149
150 cerr << endl << "Program: " << PROGRAM_NAME << " (v" << VERSION << ")" << endl;
151
152 cerr << "Authors: Aaron Quinlan (aaronquinlan@gmail.com)" << endl;
153 cerr << "\t Erik Arner, Riken" << endl << endl;
154
155 cerr << "Summary: For each feature in A, finds the closest " << endl;
156 cerr << "\t feature (upstream or downstream) in B." << endl << endl;
157
158 cerr << "Usage: " << PROGRAM_NAME << " [OPTIONS] -a <bed/gff/vcf> -b <bed/gff/vcf>" << endl << endl;
159
160 cerr << "Options: " << endl;
161 cerr << "\t-s\t" << "Require same strandedness. That is, find the closest feature in B" << endl;
162 cerr << "\t\tthat overlaps A on the _same_ strand." << endl;
163 cerr << "\t\t- By default, overlaps are reported without respect to strand." << endl << endl;
164
165 cerr << "\t-S\t" << "Require opposite strandedness. That is, find the closest feature in B" << endl;
166 cerr << "\t\tthat overlaps A on the _opposite_ strand." << endl;
167 cerr << "\t\t- By default, overlaps are reported without respect to strand." << endl << endl;
168
169 cerr << "\t-d\t" << "In addition to the closest feature in B, " << endl;
170 cerr << "\t\treport its distance to A as an extra column." << endl;
171 cerr << "\t\t- The reported distance for overlapping features will be 0." << endl << endl;
172
173 cerr << "\t-D\t" << "Like -d, report the closest feature in B, and its distance to A" << endl;
174 cerr << "\t\tas an extra column. Unlike -d, use negative distances to report" << endl;
175 cerr << "\t\tupstream features. You must specify which orientation defines \"upstream\"." << endl;
176 cerr << "\t\tThe options are:" << endl;
177 cerr << "\t\t- \"ref\" Report distance with respect to the reference genome. " << endl;
178 cerr << "\t\t B features with a lower (start, stop) are upstream" << endl;
179 cerr << "\t\t- \"a\" Report distance with respect to A." << endl;
180 cerr << "\t\t When A is on the - strand, \"upstream\" means B has a higher (start,stop)." << endl;
181 cerr << "\t\t- \"b\" Report distance with respect to B." << endl;
182 cerr << "\t\t When B is on the - strand, \"upstream\" means A has a higher (start,stop)." << endl << endl;
183
184 cerr << "\t-io\t" << "Ignore features in B that overlap A. That is, we want close, but " << endl;
185 cerr << "\t\tnot touching features only." << endl << endl;
186
187 cerr << "\t-t\t" << "How ties for closest feature are handled. This occurs when two" << endl;
188 cerr << "\t\tfeatures in B have exactly the same \"closeness\" with A." << endl;
189 cerr << "\t\tBy default, all such features in B are reported." << endl;
190 cerr << "\t\tHere are all the options:" << endl;
191 cerr << "\t\t- \"all\" Report all ties (default)." << endl;
192 cerr << "\t\t- \"first\" Report the first tie that occurred in the B file." << endl;
193 cerr << "\t\t- \"last\" Report the last tie that occurred in the B file." << endl << endl;
194
195 cerr << "Notes: " << endl;
196 cerr << "\tReports \"none\" for chrom and \"-1\" for all other fields when a feature" << endl;
197 cerr << "\tis not found in B on the same chromosome as the feature in A." << endl;
198 cerr << "\tE.g. none\t-1\t-1" << endl << endl;
199
200 // end the program here
201 exit(1);
202 }