-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctions.cpp
More file actions
executable file
·362 lines (327 loc) · 8.56 KB
/
functions.cpp
File metadata and controls
executable file
·362 lines (327 loc) · 8.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
using namespace std;
#include "functions.h"
#include <string>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
#include <math.h>
#include "wigreader.h"
string revcomp(const string &dna, bool reverse) {
string ans = dna;
for (int i=0; i<dna.length(); i++) {
// find number for character
char base;
if (dna[i] == 'A') { base = 'T'; }
else if (dna[i] == 'C') { base = 'G'; }
else if (dna[i] == 'G') { base = 'C'; }
else if (dna[i] == 'T') { base = 'A'; }
else if (dna[i] == 'N') { base = 'N'; }
if (reverse) {
ans[dna.length() - 1 - i] = base;
} else {
ans[i] = base;
}
}
return ans;
}
int stringToInt(string &str) {
return atoi(str.c_str());
}
int stringToLongInt(string &str) {
return atol(str.c_str());
}
string intToString(int number)
{
stringstream ss;//create a stringstream
ss << number;//add number to the stream
return ss.str();//return a string with the contents of the stream
}
void FindAndReplace( std::string& source, const char* find, const char* replace )
{
size_t findLen = strlen(find);
size_t replaceLen = strlen(replace);
size_t pos = 0;
//search for the next occurrence of find within source
while ((pos = source.find( find, pos)) != std::string::npos)
{
//replace the found string with the replacement
source.replace( pos, findLen, replace );
//the next line keeps you from searching your replace string,
//so your could replace "hello" with "hello world"
//and not have it blow chunks.
pos += replaceLen;
}
}
string basename(string file) {
vector<string> tokens = split(file, "/");
return tokens.back();
}
string enclosing_directory(string file){
vector<string> tokens = split(file, "/");
tokens.erase(tokens.end());
std::stringstream ss;
for(size_t i = 0; i < tokens.size(); ++i)
{
if(i == 0 && file[0] == '/') {
ss << "/";
}
ss << tokens[i];
ss << "/";
}
ss << "/";
return ss.str();
}
/* removes .bed */
string bed_prefix(string file) {
vector<string> tokens = split(file, ".bed");
return tokens.front();
}
string file_prefix(string file) {
vector<string> tokens = split(file, ".");
return tokens.front();
}
/* Returns all but final extension */
/* e.g. file.5000.help.txt => file.5000.help */
string file_complete_prefix(string file) {
vector<string> tokens = split(file, ".");
tokens.erase(tokens.end());
std::stringstream ss;
for(size_t i = 0; i < tokens.size(); ++i)
{
if(i != 0)
ss << ".";
ss << tokens[i];
}
return ss.str();
}
string file_postfix(string file) {
vector<string> tokens = split(file, ".");
tokens.erase(tokens.begin());
std::stringstream ss;
for(size_t i = 0; i < tokens.size(); ++i)
{
if(i != 0)
ss << ".";
ss << tokens[i];
}
return ss.str();
}
vector<string> split(string line, const char *delim) {
FindAndReplace(line, delim, " ");
istringstream stm(line);
vector<string> tokens;
for (;;) {
string word;
if (!(stm >> word)) break;
tokens.push_back(word);
}
return tokens;
}
void ReadBedFileIntoModelsVector(string file, vector<MODEL> &models, int min_size) {
cout << "Reading and Parsing bed file into models file: " << file << endl;
vector<BEDLINE> bedlines;
readBedFile(file, bedlines);
for(vector<BEDLINE>::iterator it = bedlines.begin(); it != bedlines.end(); ++it) {
MODEL m;
vector<string> tokens = split((*it).gene, "|");
m.gene_id = tokens[0];
m.identifier = tokens[1];
m.chr = (*it).chr;
m.strand = (*it).strand;
m.start = (*it).start;
m.end = (*it).end;
if((m.end - m.start) < min_size){
continue;
}
models.push_back(m);
}
cout << "Done reading and Parsing bed file into models file: " << file << endl;
}
void ReadModelFile(string file, vector<MODEL> &models, int min_size) {
// e.g. ENSMUSG00000072821 ENSMUSE00000694008 5 + 94505411 94505464 0
cout << "Reading and Parsing model file: " << file << endl;
string line;
string word;
ifstream modelfilestream (file.c_str());
if (modelfilestream.is_open()) {
getline (modelfilestream,line); // header
while ( modelfilestream.good() )
{
getline (modelfilestream,line);
if(line.compare("") == 0) {
continue;
}
MODEL model;
FindAndReplace(line, "\t", " ");
istringstream stm(line);
stm >> model.gene_id;
stm >> model.identifier;
stm >> model.chr;
stm >> word;
model.strand = word.c_str()[0]; // Converts "+" or "-" to '+' or '-'
stm >> word;
model.start = atoi(word.c_str());
stm >> word;
model.end = atoi(word.c_str());
if((model.end - model.start) < min_size){
continue;
}
models.push_back(model);
}
modelfilestream.close();
}
else {
cerr << "Unable to open file: " << file << endl;
exit(1);
}
cout << "Done Reading and Parsing model file: " << file << endl;
}
vector<string> tokenizeHTSymbolMapGenesFile(std::string l)
{
FindAndReplace(l,"\t"," ");
FindAndReplace(l,","," ");
//std::replace(l.begin(), l.end(), '\t', ' ');
//std::replace(l.begin(), l.end(), ',', ' ');
istringstream stm(l);
vector<string> tokens;
for (;;) {
string word;
if (!(stm >> word)) break;
tokens.push_back(word);
}
return tokens;
}
void readAndParseHCGenesFile(string file, vector<string> &hcg) {
cout << "Reading and Parsing HT Genes file" << endl;
string line;
ifstream genefilestream (file.c_str());
if (genefilestream.is_open()) {
while ( genefilestream.good() )
{
getline (genefilestream,line);
vector<string> tokens = tokenizeHTSymbolMapGenesFile(line);
tokens.erase (tokens.begin());
hcg.insert(hcg.end(),tokens.begin(), tokens.end());
}
genefilestream.close();
}
else {
cerr << "Unable to open file: " << file << endl;
exit(1);
}
}
void removeChrs(vector<MODEL> &models, string chr) {
for (int i=0; i<models.size(); i++) {
if(models[i].chr == chr) {
//cout << models[i].chr << ' ' << chr << endl;
models.erase(models.begin() + i);
i--;
}
}
}
void removeLtLength(vector<MODEL> &models, int length) {
for (int i=0; i<models.size(); i++) {
if((models[i].end - models[i].start) < length) {
//cout << "Removing: " << models[i].end - models[i].start << endl;
models.erase(models.begin() + i);
i--;
}
}
}
void removeIfNotInGeneList(vector<MODEL> &models, const vector<string>&gene_list) {
for (int i=0; i<models.size(); i++) {
bool del = true;
for (int j=0; j<gene_list.size(); j++) {
if(models[i].gene_id == gene_list[j]) {
del = false;
break;
}
}
if(del) {
models.erase(models.begin() + i);
i--;
}
}
}
void removeIfInGeneList(vector<MODEL> &models, const vector<string>&gene_list) {
for (int i=0; i<models.size(); i++) {
for (int j=0; j<gene_list.size(); j++) {
if(models[i].gene_id == gene_list[j]) {
//cout << "Removing: " << s << endl;
models.erase(models.begin() + i);
i--;
break;
}
}
}
}
void bedlinesVectorToMapByChr(vector<BEDLINE> &bedlines, map<string,vector<BEDLINE> > &bedlinesByChr) {
for(vector<BEDLINE>::iterator it = bedlines.begin(); it != bedlines.end(); ++it) {
bedlinesByChr[(*it).chr].push_back(*it);
}
}
void readBedFile(string file, map<string, BEDLINE> &bedlines) {
cout << "Reading Bed File (map)" << endl;
string line;
ifstream bedfilestream (file.c_str());
if (bedfilestream.is_open()) {
while ( bedfilestream.good() )
{
getline (bedfilestream,line);
if(line == "") {
continue;
}
vector<string> tokens = split(line,"\t");
BEDLINE bedline;
bedline.chr = tokens[0];
bedline.start = stringToLongInt(tokens[1]);
bedline.end = stringToLongInt(tokens[2]);
bedline.gene = tokens[3];
// then there is a "value" field that we skip.
if(tokens.size() >= 6) {
bedline.strand = tokens[5].c_str()[0];
}
bedlines[bedline.gene] = bedline;
}
bedfilestream.close();
}
else {
cerr << "Unable to open file: " << file << endl;
exit(1);
}
cout << "Done reading Bed File" << endl;
}
void readBedFile(string file, vector<BEDLINE> &bedlines) {
cout << "Reading Bed File (vector)" << endl;
string line;
ifstream bedfilestream (file.c_str());
if (bedfilestream.is_open()) {
while ( bedfilestream.good() )
{
getline (bedfilestream,line);
if(line == "") {
continue;
}
vector<string> tokens = split(line,"\t");
BEDLINE bedline;
bedline.chr = tokens[0];
bedline.start = stringToLongInt(tokens[1]);
bedline.end = stringToLongInt(tokens[2]);
bedline.gene = tokens[3];
// then there is a "value" field that we skip.
if(tokens.size() >= 6) {
bedline.strand = tokens[5].c_str()[0];
}
bedlines.push_back(bedline);
}
bedfilestream.close();
}
else {
cerr << "Unable to open file: " << file << endl;
exit(1);
}
cout << "Done reading Bed File" << endl;
}