-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCigarString.C
236 lines (179 loc) · 4.32 KB
/
CigarString.C
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
/****************************************************************
CigarString.C
Copyright (C)2015 William H. Majoros ([email protected]).
This is OPEN SOURCE SOFTWARE governed by the Gnu General Public
License (GPL) version 3, as described at www.opensource.org.
****************************************************************/
#include <iostream>
#include <fstream>
#include "CigarString.H"
#include "Exceptions.H"
using namespace std;
using namespace BOOM;
Regex CigarString::re("^(\\d+)(\\D)(.*)");
ostream &BOOM::operator<<(ostream &os,const CigarOpType &t)
{
switch(t)
{
case CIGAR_MATCH: os<<"M"; break;
case CIGAR_INSERT:os<<"I"; break;
case CIGAR_DELETE:os<<"D"; break;
case CIGAR_SOFT_MASK:os<<"S"; break;
default: INTERNAL_ERROR;
}
return os;
}
ostream &BOOM::operator<<(ostream &os,const CigarOp &op)
{
os<<op.rep;
os<<op.type;
return os;
}
ostream &BOOM::operator<<(ostream &os,const CigarString &S)
{
S.printOn(os);
return os;
}
CigarOp::CigarOp(CigarOpType t,int r)
: type(t), rep(r)
{
// ctor
}
CigarString::CigarString(const String &S)
{
parse(S);
}
int CigarString::length() const
{
return ops.size();
}
CigarOp &CigarString::operator[](int i)
{
return ops[i];
}
Vector<CigarOp> &CigarString::asVector()
{
return ops;
}
void CigarString::parse(const String &S)
{
ops.clear();
String local=S;
local.trimWhitespace();
while(!local.isEmpty()) {
if(!re.match(local)) throw S+" : cannot parse CIGAR string";
String repStr=re[1], opStr=re[2], rest=re[3];
int rep=repStr.asInt();
char opChar=opStr[0];
CigarOpType type=charToOpType(opChar);
ops.push_back(CigarOp(type,rep));
local=rest;
}
}
CigarOpType CigarString::charToOpType(char c)
{
switch(c)
{
case 'M': case '=': case 'X': return CIGAR_MATCH;
case 'I': return CIGAR_INSERT;
case 'D': case 'N': return CIGAR_DELETE;
case 'S': return CIGAR_SOFT_MASK;
default: throw String("unrecognized CIGAR operator: ")+c;
}
}
void CigarString::printOn(ostream &os) const
{
for(Vector<CigarOp>::const_iterator cur=ops.begin(), end=ops.end() ;
cur!=end ; ++cur)
os<<*cur;
}
CigarAlignment *CigarString::getAlignment()
{
int len=0;
for(Vector<CigarOp>::const_iterator cur=ops.begin(), end=ops.end() ;
cur!=end ; ++cur) {
CigarOp op=*cur;
if(op.type!=CIGAR_INSERT && op.type!=CIGAR_SOFT_MASK) len+=op.rep;
}
CigarAlignment &A=*new CigarAlignment(len);
int fromPos=0, toPos=0;
for(Vector<CigarOp>::const_iterator cur=ops.begin(), end=ops.end() ;
cur!=end ; ++cur) {
CigarOp op=*cur;
switch(op.type)
{
case CIGAR_MATCH:
for(int i=0 ; i<op.rep ; ++i) A[fromPos++]=toPos++;
break;
case CIGAR_DELETE:
for(int i=0 ; i<op.rep ; ++i) A[fromPos++]=CIGAR_UNDEFINED;
break;
case CIGAR_INSERT:
toPos+=op.rep;
break;
case CIGAR_SOFT_MASK:
toPos+=op.rep;
//throw RootException("soft-masking not implemented in CigarString::getAlignment()");
break;
}
}
return &A;
}
void CigarString::load(const String &filename)
{
ifstream is(filename.c_str());
String line;
is>>line;
parse(line);
}
CigarString *CigarString::unrollMatches() const
{
CigarString *unrolled=new CigarString();
CigarOp match(CIGAR_MATCH,1);
for(Vector<CigarOp>::const_iterator cur=ops.begin(), end=ops.end() ;
cur!=end ; ++cur) {
CigarOp op=*cur;
if(op.type==CIGAR_MATCH)
for(int i=0 ; i<op.rep ; ++i) unrolled->ops.push_back(match);
else unrolled->ops.push_back(op);
}
return unrolled;
}
bool CigarOp::advanceInQuery()
{
switch(type) {
case CIGAR_MATCH:
case CIGAR_INSERT:
case CIGAR_SOFT_MASK:
return true;
}
return false;
}
bool CigarOp::advanceInRef()
{
switch(type) {
case CIGAR_MATCH:
case CIGAR_DELETE:
return true;
}
return false;
}
void CigarString::computeIntervals(int refPos)
{
const int n=ops.size();
int begin1=0, begin2=refPos;
for(int i=0 ; i<n ; ++i) {
CigarOp &op=ops[i];
const int L=op.getLength();
int end1=begin1, end2=begin2;
if(op.advanceInQuery()) end1+=L;
if(op.advanceInRef()) end2+=L;
op.interval1=Interval(begin1,end1);
op.interval2=Interval(begin2,end2);
begin1=end1; begin2=end2;
}
}
bool CigarString::completeMatch()
{
return ops.size()==1 && ops[0].getOp()==CIGAR_MATCH;
}