0
|
1 /*****************************************************************************
|
|
2 mergeMain.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 "mergeBed.h"
|
|
13 #include "version.h"
|
|
14
|
|
15 using namespace std;
|
|
16
|
|
17 // define our program name
|
|
18 #define PROGRAM_NAME "mergeBed"
|
|
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 = "stdin";
|
|
34 int maxDistance = 0;
|
|
35 string scoreOp = "";
|
|
36
|
|
37 // input arguments
|
|
38 bool haveBed = true;
|
|
39 bool numEntries = false;
|
|
40 bool haveMaxDistance = false;
|
|
41 bool forceStrand = false;
|
|
42 bool reportNames = false;
|
|
43 bool reportScores = false;
|
|
44
|
|
45 for(int i = 1; i < argc; i++) {
|
|
46 int parameterLength = (int)strlen(argv[i]);
|
|
47
|
|
48 if((PARAMETER_CHECK("-h", 2, parameterLength)) ||
|
|
49 (PARAMETER_CHECK("--help", 5, parameterLength))) {
|
|
50 showHelp = true;
|
|
51 }
|
|
52 }
|
|
53
|
|
54 if(showHelp) ShowHelp();
|
|
55
|
|
56 // do some parsing (all of these parameters require 2 strings)
|
|
57 for(int i = 1; i < argc; i++) {
|
|
58
|
|
59 int parameterLength = (int)strlen(argv[i]);
|
|
60
|
|
61 if(PARAMETER_CHECK("-i", 2, parameterLength)) {
|
|
62 if ((i+1) < argc) {
|
|
63 bedFile = argv[i + 1];
|
|
64 i++;
|
|
65 }
|
|
66 }
|
|
67 else if(PARAMETER_CHECK("-n", 2, parameterLength)) {
|
|
68 numEntries = true;
|
|
69 }
|
|
70 else if(PARAMETER_CHECK("-d", 2, parameterLength)) {
|
|
71 if ((i+1) < argc) {
|
|
72 haveMaxDistance = true;
|
|
73 maxDistance = atoi(argv[i + 1]);
|
|
74 i++;
|
|
75 }
|
|
76 }
|
|
77 else if (PARAMETER_CHECK("-s", 2, parameterLength)) {
|
|
78 forceStrand = true;
|
|
79 }
|
|
80 else if (PARAMETER_CHECK("-nms", 4, parameterLength)) {
|
|
81 reportNames = true;
|
|
82 }
|
|
83 else if (PARAMETER_CHECK("-scores", 7, parameterLength)) {
|
|
84 reportScores = true;
|
|
85 if ((i+1) < argc) {
|
|
86 scoreOp = argv[i + 1];
|
|
87 i++;
|
|
88 }
|
|
89 }
|
|
90 else {
|
|
91 cerr << endl << "*****ERROR: Unrecognized parameter: " << argv[i] << " *****" << endl << endl;
|
|
92 showHelp = true;
|
|
93 }
|
|
94 }
|
|
95
|
|
96 // make sure we have both input files
|
|
97 if (!haveBed) {
|
|
98 cerr << endl << "*****" << endl << "*****ERROR: Need -i BED file. " << endl << "*****" << endl;
|
|
99 showHelp = true;
|
|
100 }
|
|
101 if (reportNames && numEntries) {
|
|
102 cerr << endl << "*****" << endl << "*****ERROR: Request either -n OR -nms, not both." << endl << "*****" << endl;
|
|
103 showHelp = true;
|
|
104 }
|
|
105 if ((reportScores == true) && (scoreOp != "sum") && (scoreOp != "max") && (scoreOp != "min") && (scoreOp != "mean") &&
|
|
106 (scoreOp != "mode") && (scoreOp != "median") && (scoreOp != "antimode") && (scoreOp != "collapse"))
|
|
107 {
|
|
108 cerr << endl << "*****" << endl << "*****ERROR: Invalid scoreOp selection \"" << scoreOp << endl << "\" *****" << endl;
|
|
109 showHelp = true;
|
|
110 }
|
|
111
|
|
112 if (!showHelp) {
|
|
113 BedMerge *bm = new BedMerge(bedFile, numEntries, maxDistance, forceStrand, reportNames, reportScores, scoreOp);
|
|
114 delete bm;
|
|
115 return 0;
|
|
116 }
|
|
117 else {
|
|
118 ShowHelp();
|
|
119 }
|
|
120 }
|
|
121
|
|
122 void ShowHelp(void) {
|
|
123
|
|
124 cerr << endl << "Program: " << PROGRAM_NAME << " (v" << VERSION << ")" << endl;
|
|
125
|
|
126 cerr << "Author: Aaron Quinlan (aaronquinlan@gmail.com)" << endl;
|
|
127
|
|
128 cerr << "Summary: Merges overlapping BED/GFF/VCF entries into a single interval." << endl << endl;
|
|
129
|
|
130 cerr << "Usage: " << PROGRAM_NAME << " [OPTIONS] -i <bed/gff/vcf>" << endl << endl;
|
|
131
|
|
132 cerr << "Options: " << endl;
|
|
133 cerr << "\t-s\t" << "Force strandedness. That is, only merge features" << endl;
|
|
134 cerr << "\t\tthat are the same strand." << endl;
|
|
135 cerr << "\t\t- By default, merging is done without respect to strand." << endl << endl;
|
|
136
|
|
137 cerr << "\t-n\t" << "Report the number of BED entries that were merged." << endl;
|
|
138 cerr << "\t\t- Note: \"1\" is reported if no merging occurred." << endl << endl;
|
|
139
|
|
140
|
|
141 cerr << "\t-d\t" << "Maximum distance between features allowed for features" << endl;
|
|
142 cerr << "\t\tto be merged." << endl;
|
|
143 cerr << "\t\t- Def. 0. That is, overlapping & book-ended features are merged." << endl;
|
|
144 cerr << "\t\t- (INTEGER)" << endl << endl;
|
|
145
|
|
146 cerr << "\t-nms\t" << "Report the names of the merged features separated by semicolons." << endl << endl;
|
|
147
|
|
148 cerr << "\t-scores\t" << "Report the scores of the merged features. Specify one of " << endl;
|
|
149 cerr << "\t\tthe following options for reporting scores:" << endl;
|
|
150 cerr << "\t\t sum, min, max," << endl;
|
|
151 cerr << "\t\t mean, median, mode, antimode," << endl;
|
|
152 cerr << "\t\t collapse (i.e., print a semicolon-separated list)," << endl;
|
|
153 cerr << "\t\t- (INTEGER)" << endl << endl;
|
|
154
|
|
155 cerr << "Notes: " << endl;
|
|
156 cerr << "\t(1) All output, regardless of input type (e.g., GFF or VCF)" << endl;
|
|
157 cerr << "\t will in BED format with zero-based starts" << endl << endl;
|
|
158
|
|
159
|
|
160 // end the program here
|
|
161 exit(1);
|
|
162
|
|
163 }
|