comparison BEDTools-Version-2.14.3/src/bed12ToBed6/bed12ToBed6.cpp @ 1:bec36315bd12 default tip

Deleted selected files
author aaronquinlan
date Sat, 19 Nov 2011 14:17:03 -0500
parents dfcd8b6c1bda
children
comparison
equal deleted inserted replaced
0:dfcd8b6c1bda 1:bec36315bd12
1 /*****************************************************************************
2 bed12ToBed6.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 "lineFileUtilities.h"
13 #include "bedFile.h"
14 #include "version.h"
15
16 #include <vector>
17 #include <iostream>
18 #include <fstream>
19 #include <stdlib.h>
20
21 using namespace std;
22
23
24 // define our program name
25 #define PROGRAM_NAME "bed12ToBed6"
26
27 // define our parameter checking macro
28 #define PARAMETER_CHECK(param, paramLen, actualLen) (strncmp(argv[i], param, min(actualLen, paramLen))== 0) && (actualLen == paramLen)
29
30
31 // function declarations
32 void ShowHelp(void);
33 void DetermineBedInput(BedFile *bed);
34 void ProcessBed(istream &bedInput, BedFile *bed);
35
36
37 bool addBlockNums = false;
38
39 int main(int argc, char* argv[]) {
40
41 // our configuration variables
42 bool showHelp = false;
43
44 // input files
45 string bedFile = "stdin";
46 bool haveBed = true;
47
48 for(int i = 1; i < argc; i++) {
49 int parameterLength = (int)strlen(argv[i]);
50
51 if((PARAMETER_CHECK("-h", 2, parameterLength)) ||
52 (PARAMETER_CHECK("--help", 5, parameterLength))) {
53 showHelp = true;
54 }
55 }
56
57 if(showHelp) ShowHelp();
58
59 // do some parsing (all of these parameters require 2 strings)
60 for(int i = 1; i < argc; i++) {
61
62 int parameterLength = (int)strlen(argv[i]);
63
64 if(PARAMETER_CHECK("-i", 2, parameterLength)) {
65 if ((i+1) < argc) {
66 bedFile = argv[i + 1];
67 i++;
68 }
69 }
70 else if(PARAMETER_CHECK("-n", 2, parameterLength)) {
71 addBlockNums = true;
72 i++;
73 }
74 else {
75 cerr << endl << "*****ERROR: Unrecognized parameter: " << argv[i] << " *****" << endl << endl;
76 showHelp = true;
77 }
78 }
79
80 // make sure we have an input files
81 if (!haveBed ) {
82 cerr << endl << "*****" << endl << "*****ERROR: Need -i (BED) file. " << endl << "*****" << endl;
83 showHelp = true;
84 }
85
86 if (!showHelp) {
87 BedFile *bed = new BedFile(bedFile);
88 DetermineBedInput(bed);
89 }
90 else {
91 ShowHelp();
92 }
93 }
94
95
96 void ShowHelp(void) {
97
98 cerr << endl << "Program: " << PROGRAM_NAME << " (v" << VERSION << ")" << endl;
99
100 cerr << "Author: Aaron Quinlan (aaronquinlan@gmail.com)" << endl;
101
102 cerr << "Summary: Splits BED12 features into discrete BED6 features." << endl << endl;
103
104 cerr << "Usage: " << PROGRAM_NAME << " [OPTIONS] -i <bed12>" << endl << endl;
105
106 cerr << "Options: " << endl;
107
108 cerr << "\t-n\t" << "Force the score to be the (1-based) block number from the BED12." << endl << endl;
109
110
111 // end the program here
112 exit(1);
113 }
114
115
116 void DetermineBedInput(BedFile *bed) {
117
118 // dealing with a proper file
119 if (bed->bedFile != "stdin") {
120
121 ifstream bedStream(bed->bedFile.c_str(), ios::in);
122 if ( !bedStream ) {
123 cerr << "Error: The requested bed file (" << bed->bedFile << ") could not be opened. Exiting!" << endl;
124 exit (1);
125 }
126 ProcessBed(bedStream, bed);
127 }
128 // reading from stdin
129 else {
130 ProcessBed(cin, bed);
131 }
132 }
133
134
135 void ProcessBed(istream &bedInput, BedFile *bed) {
136
137 // process each BED entry and convert to BAM
138 BED bedEntry, nullBed;
139 int lineNum = 0;
140 BedLineStatus bedStatus;
141 // open the BED file for reading.
142 bed->Open();
143 while ((bedStatus = bed->GetNextBed(bedEntry, lineNum)) != BED_INVALID) {
144 if (bedStatus == BED_VALID) {
145
146 bedVector bedBlocks; // vec to store the discrete BED "blocks" from a
147 splitBedIntoBlocks(bedEntry, lineNum, bedBlocks);
148
149 for (int i = 0; i < (int) bedBlocks.size(); ++i) {
150 if (addBlockNums == false) {
151 printf ("%s\t%d\t%d\t%s\t%s\t%s\n", bedBlocks[i].chrom.c_str(), bedBlocks[i].start, bedBlocks[i].end, bedBlocks[i].name.c_str(),
152 bedBlocks[i].score.c_str(), bedBlocks[i].strand.c_str());
153 }
154 else {
155 if (bedBlocks[i].strand == "+")
156 printf ("%s\t%d\t%d\t%s\t%d\t%s\n", bedBlocks[i].chrom.c_str(), bedBlocks[i].start, bedBlocks[i].end, bedBlocks[i].name.c_str(),
157 i+1, bedBlocks[i].strand.c_str());
158 else
159 printf ("%s\t%d\t%d\t%s\t%d\t%s\n", bedBlocks[i].chrom.c_str(), bedBlocks[i].start, bedBlocks[i].end, bedBlocks[i].name.c_str(),
160 (int) ((bedBlocks.size()+1)-i), bedBlocks[i].strand.c_str());
161 }
162 }
163 bedEntry = nullBed;
164 }
165 }
166 // close up
167 bed->Close();
168 }