-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpaf2features.cpp
185 lines (166 loc) · 4.01 KB
/
paf2features.cpp
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
#include "myutils.h"
#include "seqdb.h"
#include "linereader.h"
#include "alpha.h"
#include "cigar.h"
static void WriteAln(FILE *f, bool Plus,
const string &QLabel, const byte *Q, uint QLo, uint QHi, uint QL,
const string &RLabel, const byte *R, uint RLo, uint RHi, uint RL,
const vector<char> &Ops, const vector<uint> &Lengths)
{
if (f == 0)
return;
const uint OpCount = SIZE(Ops);
asserta(SIZE(Lengths) == OpCount);
string QRow;
string RRow;
int QPos = int(QLo);
int RPos = int(Plus ? RLo : RHi);
for (uint i = 0; i < OpCount; ++i)
{
char Op = Ops[i];
uint n = Lengths[i];
switch (Op)
{
case 'M':
{
for (uint j = 0; j < n; ++j)
{
asserta(QPos < int(QL));
asserta(RPos >= 0 && RPos < int(RL));
QRow += Q[QPos++];
if (Plus)
RRow += R[RPos++];
else
{
byte r = R[RPos];
RRow += g_CharToCompChar[r];
--RPos;
}
}
break;
}
case 'D':
{
for (uint j = 0; j < n; ++j)
{
asserta(RPos >= 0 && RPos < int(RL));
QRow += '-';
if (Plus)
RRow += R[RPos++];
else
{
byte r = R[RPos];
RRow += g_CharToCompChar[r];
--RPos;
}
}
break;
}
case 'I':
{
for (uint j = 0; j < n; ++j)
{
asserta(QPos < int(QL));
QRow += Q[QPos++];
RRow += '-';
}
break;
}
default:
Die("Unknown op '%c'", Op);
}
}
fprintf(f, "\n");
fprintf(f, ">%s Q:%u-%u(%u), R:%u-%u(%u) %c\n",
QLabel.c_str(),
QLo, QHi, QHi - QLo + 1,
RLo, RHi, RHi - RLo + 1,
pom(Plus));
//fprintf(f, "Q: %s\n", QRow.c_str());
//fprintf(f, "R: %s\n", RRow.c_str());
const uint ColCount = SIZE(QRow);
const uint COLS = 100;
for (uint Col = 0; Col < ColCount; Col += COLS)
{
uint k = COLS;
if (Col + k > ColCount)
k = ColCount - Col;
if (Col > 0)
fprintf(f, "\n");
fprintf(f, "Q: %*.*s\n", k, k, QRow.c_str() + Col);
fprintf(f, "R: %*.*s\n", k, k, RRow.c_str() + Col);
}
}
void cmd_paf2features()
{
const string &PAFFileName = opt(paf2aln);
const string &QueryFileName = opt(input);
const string &DBFileName = opt(db);
const string &OutputFileName = opt(output);
FILE *fOut = CreateStdioFile(OutputFileName);
SeqDB Query;
SeqDB DB;
Query.FromFastx(QueryFileName);
DB.FromFasta(DBFileName);
vector<string> Fields;
t_LineBuff LB;
LineReader LR;
LR.Open(PAFFileName);
vector<char> Ops;
vector<uint> Lengths;
while (LR.ReadLine(LB))
{
const string Line = string(LB.Data);
Split(Line, Fields, '\t');
const string &QLabel = Fields[0];
uint QSeqIndex = Query.GetSeqIndex(QLabel);
uint QL = StrToUint(Fields[1]);
uint QLo = StrToUint(Fields[2]);
uint QHi = StrToUint(Fields[3]) - 1;
asserta(QLo < QHi);
asserta(QHi < QL);
const string &RLabel = Fields[5];
uint RSeqIndex = DB.GetSeqIndex(RLabel);
uint RL = StrToUint(Fields[6]);
uint RLo = StrToUint(Fields[7]);
uint RHi = StrToUint(Fields[8]) - 1;
asserta(RLo < RHi);
asserta(QHi < RL);
bool Plus;
const string &Strand = Fields[4];
if (Strand == "+")
Plus = true;
else if (Strand == "-")
Plus = false;
else
Die("Bad strand '%s'", Strand.c_str());
// s2:i:0 de:f:0.1493 rl:i:30 cg:Z:13M2I33M2D33M1D10M1...
string CIGAR;
for (uint i = 12; i < SIZE(Fields); ++i)
{
const string &Field = Fields[i];
if (StartsWith(Field, "cg:Z:"))
{
CIGAR = Field.substr(5, string::npos);
break;
}
}
if (CIGAR.empty())
Die("Missing CIGAR");
CIGARGetOps(CIGAR, Ops, Lengths);
const uint OpCount = SIZE(Ops);
asserta(SIZE(Lengths) == OpCount);
const byte *Q = Query.GetSeq(QSeqIndex);
const byte *R = DB.GetSeq(RSeqIndex);
uint QL2 = Query.GetSeqLength(QSeqIndex);
uint RL2 = DB.GetSeqLength(RSeqIndex);
asserta(QL2 == QL);
asserta(RL2 == RL);
WriteAln(g_fLog, Plus,
QLabel, Q, QLo, QHi, QL,
RLabel, R, RLo, RHi, RL,
Ops, Lengths);
}
CloseStdioFile(fOut);
}