comparison BEDTools-Version-2.14.3/src/sortBed/sortMain.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 sortBedMain.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 "sortBed.h"
13 #include "version.h"
14
15 using namespace std;
16
17 // define our program name
18 #define PROGRAM_NAME "sortBed"
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 bool haveBed = true;
35 int sortChoices = 0;
36
37 bool sortBySizeAsc = false;
38 bool sortBySizeDesc = false;
39 bool sortByChromThenSizeAsc = false;
40 bool sortByChromThenSizeDesc = false;
41 bool sortByChromThenScoreAsc = false;
42 bool sortByChromThenScoreDesc = false;
43
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("-sizeA", 6, parameterLength)) {
68 sortBySizeAsc = true;
69 sortChoices++;
70 }
71 else if(PARAMETER_CHECK("-sizeD", 6, parameterLength)) {
72 sortBySizeDesc = true;
73 sortChoices++;
74 }
75 else if(PARAMETER_CHECK("-chrThenSizeA", 13, parameterLength)) {
76 sortByChromThenSizeAsc = true;
77 sortChoices++;
78 }
79 else if(PARAMETER_CHECK("-chrThenSizeD", 13, parameterLength)) {
80 sortByChromThenSizeDesc = true;
81 sortChoices++;
82 }
83 else if(PARAMETER_CHECK("-chrThenScoreA", 14, parameterLength)) {
84 sortByChromThenScoreAsc = true;
85 sortChoices++;
86 }
87 else if(PARAMETER_CHECK("-chrThenScoreD", 14, parameterLength)) {
88 sortByChromThenScoreDesc = true;
89 sortChoices++;
90 }
91 else {
92 cerr << endl << "*****ERROR: Unrecognized parameter: " << argv[i] << " *****" << endl << endl;
93 showHelp = true;
94 }
95 }
96
97 // make sure we have both input files
98 if (!haveBed) {
99 cerr << endl << "*****" << endl << "*****ERROR: Need -i BED file. " << endl << "*****" << endl;
100 showHelp = true;
101 }
102 if (sortChoices > 1) {
103 cerr << endl << "*****" << endl << "*****ERROR: Sorting options are mutually exclusive. Please choose just one. " << endl << "*****" << endl;
104 showHelp = true;
105 }
106
107
108 if (!showHelp) {
109 BedSort *bm = new BedSort(bedFile);
110
111 if (sortBySizeAsc) {
112 bm->SortBedBySizeAsc();
113 }
114 else if (sortBySizeDesc) {
115 bm->SortBedBySizeDesc();
116 }
117 else if (sortByChromThenSizeAsc) {
118 bm->SortBedByChromThenSizeAsc();
119 }
120 else if (sortByChromThenSizeDesc) {
121 bm->SortBedByChromThenSizeDesc();
122 }
123 else if (sortByChromThenScoreAsc) {
124 bm->SortBedByChromThenScoreAsc();
125 }
126 else if (sortByChromThenScoreDesc) {
127 bm->SortBedByChromThenScoreDesc();
128 }
129 else {
130 bm->SortBed();
131 }
132 return 0;
133 }
134 else {
135 ShowHelp();
136 }
137 }
138
139 void ShowHelp(void) {
140
141 cerr << endl << "Program: " << PROGRAM_NAME << " (v" << VERSION << ")" << endl;
142
143 cerr << "Author: Aaron Quinlan (aaronquinlan@gmail.com)" << endl;
144 cerr << "Summary: Sorts a feature file in various and useful ways." << endl << endl;
145 cerr << "Usage: " << PROGRAM_NAME << " [OPTIONS] -i <bed/gff/vcf>" << endl << endl;
146
147 cerr << "Options: " << endl;
148 cerr << "\t" << "-sizeA\t\t" << "Sort by feature size in ascending order." << endl;
149 cerr << "\t" << "-sizeD\t\t" << "Sort by feature size in descending order." << endl;
150 cerr << "\t" << "-chrThenSizeA\t" << "Sort by chrom (asc), then feature size (asc)." << endl;
151 cerr << "\t" << "-chrThenSizeD\t" << "Sort by chrom (asc), then feature size (desc)." << endl;
152 cerr << "\t" << "-chrThenScoreA\t" << "Sort by chrom (asc), then score (asc)." << endl;
153 cerr << "\t" << "-chrThenScoreD\t" << "Sort by chrom (asc), then score (desc)." << endl << endl;
154
155 exit(1);
156
157 }