-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBedFile.cpp
More file actions
136 lines (107 loc) · 2.68 KB
/
BedFile.cpp
File metadata and controls
136 lines (107 loc) · 2.68 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
/*
* BedFile.cpp
* Spanner
*
* Created by Chip Stewart on 1/28/10.
* Copyright 2010 Boston College. All rights reserved.
*
*/
#include "BedFile.h"
bool comparePair (pair<int,short> p1, pair<int, short> p2)
{
if (p1.first<p2.first) {return true;}
if (p1.second<p2.second) {return true;}
return false;
}
//------------------------------------------------------------------------------
// Bed class constructor
//------------------------------------------------------------------------------
C_BedChr::C_BedChr() {
N = 0;
plim[0] =INT_MAX;
plim[1] =0;
name= "";
header= "";
}
C_BedChr::C_BedChr(string & fn, string &chr1) { // load bed record for chr
bool sort=false;
this->filename=fn;
if (fn.length()==0) {
return;
}
this->chr=chr1;
if (chr1.length()==0) {
return;
}
// input line
string line;
// open
ifstream bed(this->filename.c_str(), ios::in);
if (!bed) {
cerr << "Unable to open file: " << this->filename<< endl;
exit(1);
}
this->header = "";
int nl=0;
while (getline(bed, line)) {
nl++;
if (line.compare(0,3,"chr") != 0) {
this->header=this->header+"\n"+line;
} else {
// construct a stream from the string
stringstream strstr(line);
// use stream iterators to copy the stream to the vector as whitespace separated strings
istream_iterator<string> it(strstr);
istream_iterator<string> end;
vector<string> f(it, end);
if (f.size()<3) {
cerr << fn << " too few fields:\n " << line << endl;
continue;
}
string c=f[0];
if (c.compare(3,c.size()-3,chr1)==0) {
int p1= atoi(f[1].c_str());
int p2= atoi(f[2].c_str());
int l1 = p2-p1;
if (p.size()>0) {
if (p1<p[p.size()-1]) { sort=true;};
}
this->p.push_back(p1);
this->l.push_back(l1);
if (p1<plim[0]) { plim[0]=p1;};
if (p2>plim[1]) { plim[1]=p2;};
}
}
}
bed.close();
if (sort) {
pair <int,short> q1;
list < pair <int,short> > q;
list < pair <int,short> >::iterator iq;
for (int i=0; i<int(p.size()); i++) {
q1.first=p[i];
q1.second=l[i];
q.push_back(q1);
}
q.sort(comparePair);
int i=0;
for (iq=q.begin(); iq!=q.end() ; iq++) {
p[i]=(*iq).first;
l[i]=(*iq).second;
i++;
}
}
N=p.size();
}
C_BedChr& C_BedChr::operator=(const C_BedChr &rhs) {
N=rhs.N ;
chr=rhs.chr;
name=rhs.name;
header=rhs.header;
filename=rhs.header;
plim[0]=rhs.plim[0];
plim[1]=rhs.plim[1];
p=rhs.p ;
l=rhs.l ;
return *this;
}