forked from MichaelStromberg-KTH/Forage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSequence.cpp
More file actions
198 lines (147 loc) · 5.39 KB
/
Copy pathSequence.cpp
File metadata and controls
198 lines (147 loc) · 5.39 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
#include "Sequence.h"
CSequence::CSequence()
: m_Name(NULL)
, m_IsReverseComplement(false)
, m_Offset(0)
, m_Filename(NULL)
, m_Bases(NULL)
, m_Qualities(NULL)
, m_IsAligned(false)
, m_ClipStart(0)
, m_ClipEnd(0)
, m_NumBases(0)
{}
CSequence::~CSequence(void) {
//printf("Sequence destructor: %s.\n",m_Name);
// delete the name
if(m_Name) delete [] m_Name;
// delete the filename
if(m_Filename) delete [] m_Filename;
// delete the bases
if(m_Bases) delete [] m_Bases;
// delete the qualities
if(m_Qualities) delete [] m_Qualities;
}
// Aligns the sequence (old version) with respect to the contig
void CSequence::AlignOldVersion(unsigned int alignBases) {
// TODO: make sure the qualities have been read
if(m_IsAligned) {
printf("ERROR: Sequence is already aligned.\n");
exit(1);
}
//printf("Offset: %d\n",m_Offset);
// adjust the offset
int offset = m_Offset;
// the consensus sequence starts at position 1
unsigned int consensusLength = alignBases + 1;
// how many bases should we copy from the original
unsigned int numBasesToCopy = m_NumBases;
int startPosition = 0;
int newStartPosition = offset;
int endPosition = numBasesToCopy;
int newEndPosition = numBasesToCopy + offset;
// if sequence starts before the consensus, adjust the beginning of the sequence
if(newStartPosition < 0) {
newStartPosition = 1;
newEndPosition += offset;
numBasesToCopy = newEndPosition - newStartPosition;
}
// if sequence is longer than consensus, adjust the end of the sequence
if(newEndPosition > (signed)consensusLength) {
newEndPosition = consensusLength;
numBasesToCopy = newEndPosition - newStartPosition;
}
// align the bases
char* pStartPos = m_Bases + startPosition;
char* pNewStartPos = m_Bases + newStartPosition;
memmove(pNewStartPos,pStartPos,numBasesToCopy);
// align the qualities
pStartPos = m_Qualities + startPosition;
pNewStartPos = m_Qualities + newStartPosition;
memmove(pNewStartPos,pStartPos,numBasesToCopy);
unsigned int numBaseTrimBegin = newStartPosition;
unsigned int numBaseTrimEnd = consensusLength - newEndPosition;
if(numBaseTrimBegin > 0) {
memset(m_Bases,0,sizeof(char)*numBaseTrimBegin);
memset(m_Qualities,0,sizeof(char)*numBaseTrimBegin);
}
if(numBaseTrimEnd > 0) {
memset(m_Bases+newEndPosition,0,sizeof(char)*numBaseTrimEnd);
memset(m_Qualities+newEndPosition,0,sizeof(char)*numBaseTrimEnd);
}
// perform base filtration
if(CRuntimeParameters::EnableBaseFiltration) {
unsigned int threshold = CRuntimeParameters::BaseFilterThreshold;
for(unsigned int i=0;i<alignBases;i++)
if(m_Qualities[i] < (signed)threshold) {
m_Bases[i] = 0;
m_Qualities[i] = 0;
}
}
// set the aligned flag
m_IsAligned = true;
// set the new length
m_NumBases = alignBases;
}
// Aligns the sequence with respect to the contig
void CSequence::Align(unsigned int alignBases) {
// the consensus sequence starts at position 1
unsigned int consensusLength = alignBases + 1;
// how many bases should we copy from the original
unsigned int numBasesToCopy = m_NumBases;
// all contigs begin at position 1
int contigStartPos = 1;
// this is where the sequence starts on the contig scale
contigStartPos += m_Offset;
//printf("Contig start position: %u\n",contigStartPos);
// this is where the sequence starts on the sequence scale
int sequenceStartPos = 1;
// if the starting position is off the scale, adjust
if(contigStartPos < 1) {
contigStartPos = 1;
sequenceStartPos -= m_Offset;
}
unsigned int sequencePosDiff = numBasesToCopy - sequenceStartPos;
unsigned int contigPosDiff = consensusLength - contigStartPos;
if(sequencePosDiff < contigPosDiff) numBasesToCopy = sequencePosDiff;
else numBasesToCopy = contigPosDiff;
//printf("consensus length: %u, sequence length: %u, bases being copied: %u.\n",alignBases,m_NumBases,numBasesToCopy);
//for(unsigned int i=0;i<consensusLength;i++) printf("%c",m_Bases[i]);
//printf("\n\n");
// align the bases
char* pSeqStartPos = m_Bases + sequenceStartPos;
char* pContigStartPos = m_Bases + contigStartPos;
//printf("* first base: %c, second base: %c, third base: %c\n",m_Bases[0],m_Bases[1],m_Bases[2]);
memmove(pContigStartPos,pSeqStartPos,numBasesToCopy);
// align the qualities
pSeqStartPos = m_Qualities + sequenceStartPos;
pContigStartPos = m_Qualities + contigStartPos;
memmove(pContigStartPos,pSeqStartPos,numBasesToCopy);
//for(unsigned int i=0;i<consensusLength;i++) printf("%c",m_Bases[i]);
//printf("\n\n");
unsigned int endPos = contigStartPos+numBasesToCopy;
//printf("start: %u, end: %u, consensus length: %u\n",contigStartPos,contigStartPos+numBasesToCopy,consensusLength);
unsigned int numBaseTrimBegin = contigStartPos;
unsigned int numBaseTrimEnd = consensusLength - endPos;
if(numBaseTrimBegin > 0) {
memset(m_Bases,0,sizeof(char)*numBaseTrimBegin);
memset(m_Qualities,0,sizeof(char)*numBaseTrimBegin);
}
if(numBaseTrimEnd > 0) {
memset(m_Bases+endPos,0,sizeof(char)*numBaseTrimEnd);
memset(m_Qualities+endPos,0,sizeof(char)*numBaseTrimEnd);
}
// perform base filtration
if(CRuntimeParameters::EnableBaseFiltration) {
unsigned int threshold = CRuntimeParameters::BaseFilterThreshold;
for(unsigned int i=0;i<alignBases;i++)
if(m_Qualities[i] < (signed)threshold) {
m_Bases[i] = 0;
m_Qualities[i] = 0;
}
}
// set the aligned flag
m_IsAligned = true;
// set the new length
m_NumBases = alignBases;
}