Debian Codegraph @

seagen/hitReader.C

raw symbols SCIP
1 #include "hitReader.H"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <errno.h>
5 #include <string.h>
6
7
8 static
9 int
10 hitCompareCoverage(const void *a, const void *b) {
11 const hit_s *A = (const hit_s *)a;
12 const hit_s *B = (const hit_s *)b;
13
14 if (A->coverage > B->coverage)
15 return(-1);
16 return(A->coverage < B->coverage);
17 }
18
19 static
20 int
21 hitCompareGenPos(const void *a, const void *b) {
22 const hit_s *A = (const hit_s *)a;
23 const hit_s *B = (const hit_s *)b;
24
25 if (A->a._dsIdx < B->a._dsIdx) return(-1);
26 if (A->a._dsIdx > B->a._dsIdx) return(1);
27
28 if (A->a._forward < B->a._forward) return(-1);
29 if (A->a._forward > B->a._forward) return(1);
30
31 if (A->a._dsLo < B->a._dsLo) return(-1);
32 if (A->a._dsLo > B->a._dsLo) return(1);
33
34 if (A->a._dsHi < B->a._dsHi) return(-1);
35 if (A->a._dsHi > B->a._dsHi) return(1);
36
37 return(0);
38 }
39
40
41
42
43
44
45
46 hitReader::hitReader(int m) {
47 _filesMax = m;
48 _filesLen = 0;
49 _files = new hitFile_s [_filesMax];
50
51 _listLen = 0;
52 _listMax = 1024 * 1024;
53 _list = new hit_s [_listMax];
54
55 _iid = uint32ZERO;
56 _bestScore = 0.0;
57 _worstScore = 1.0;
58 }
59
60
61 hitReader::~hitReader() {
62 for (uint32 i=0; i<_filesLen; i++)
63 //fclose(_files[i].file);
64 delete _files[i].buff;
65
66 delete [] _files;
67 delete [] _list;
68 }
69
70 void
71 hitReader::addInputFile(char *filename) {
72 errno = 0;
73
74 _files[_filesLen].stillMore = true;
75 _files[_filesLen].buff = new readBuffer(filename);
76
77 // Binary or ASCII input?
78 //
79 _files[_filesLen].isBINARY = (_files[_filesLen].buff->peek() != '-');
80
81 // Load the first hit
82 loadHit(_files+_filesLen);
83
84 _filesLen++;
85 }
86
87
88 void
89 hitReader::loadHit(hitFile_s *HF) {
90
91 if (HF->isBINARY) {
92 ahit_readBinary(&HF->a, HF->buff);
93 } else {
94 fprintf(stderr, "ERROR: hitReader::loadHit() ascii not supported right now.\n");
95 exit(1);
96 //fgets(HF->b, 1024, HF->file);
97 //ahit_parseString(&HF->a, HF->b);
98 }
99
100 if (HF->buff->eof())
101 HF->stillMore = false;
102 };
103
104
105 bool
106 hitReader::loadHits(void) {
107
108 _listLen = 0;
109 _iid = uint32ZERO;
110 _bestScore = 0.0;
111 _worstScore = 1.0;
112
113 // See if there are more hits to process.
114 //
115 bool keepGoing = false;
116 for (uint32 i=0; i<_filesLen; i++)
117 keepGoing |= _files[i].stillMore;
118
119 if (keepGoing == false)
120 return(false);
121
122 // Find the lowest ESTid
123 //
124 _iid = 1 << 30;
125 for (uint32 i=0; i<_filesLen; i++)
126 if ((_files[i].stillMore) && (_iid > _files[i].a._qsIdx))
127 _iid = _files[i].a._qsIdx;
128
129
130 // For each file, load the next hit if it's the est
131 // we're looking at
132 //
133 for (uint32 i=0; i<_filesLen; i++) {
134 while ((_files[i].stillMore) && (_files[i].a._qsIdx == _iid)) {
135 if (_listLen >= _listMax) {
136 _listMax *= 2;
137 hit_s *new_list = new hit_s [_listMax];
138 memcpy(new_list, _list, _listLen * sizeof(hit_s));
139 delete [] _list;
140 _list = new_list;
141 }
142
143 memcpy(&_list[_listLen].a, &_files[i].a, sizeof(aHit));
144
145 _list[_listLen].coverage = (double)_files[i].a._covered / (double)_files[i].a._numMers;
146 _list[_listLen].multiplicity = (double)_files[i].a._matched / (double)_files[i].a._covered;
147
148 // aHit->_covered is in bases, but aHit->_numMers is the
149 // number of mers. Possible for coverage to be > 1.0.
150 //
151 if (_list[_listLen].coverage > 1.0)
152 _list[_listLen].coverage = 1.0;
153
154 if (_list[_listLen].coverage > _bestScore)
155 _bestScore = _list[_listLen].coverage;
156
157 if (_list[_listLen].coverage < _worstScore)
158 _worstScore = _list[_listLen].coverage;
159
160 #ifdef WITH_ANSWERS
161 // Look for the answer string. If not found, set to zero.
162 //
163 _list[_listLen].mappedIdentity = 0;
164 _list[_listLen].mappedCoverage = 0;
165
166 for (int p=0; _files[i].b[p]; p++) {
167 if ((_files[i].b[p] == 'Y') || (_files[i].b[p] == 'N')) {
168 char *c = _files[i].b+p+1;
169 _list[_listLen].mappedIdentity = (uint32)strtoul(c, &c, 10);
170 _list[_listLen].mappedCoverage = (uint32)strtoul(c, &c, 10);
171 }
172 }
173 #endif
174
175 _listLen++;
176
177 loadHit(_files+i);
178 }
179 }
180
181 mergeOverlappingHits();
182
183 return(true);
184 }
185
186
187
188 void
189 hitReader::sortByCoverage(void) {
190 qsort(_list, _listLen, sizeof(hit_s), hitCompareCoverage);
191 };
192
193
194
195
196
197 // scan the list of hits (for a single EST, remember) and merge
198 // any that are overlapping
199 //
200 void
201 hitReader::mergeOverlappingHits(void) {
202
203 // Sort by the genomic position
204 //
205 qsort(_list, _listLen, sizeof(hit_s), hitCompareGenPos);
206
207 // Scan through the list, merging.
208 //
209 uint32 cur = 0; // Currently active entry
210 uint32 exa = 1; // Entry we examine for merging
211 while (exa < _listLen) {
212
213 // Do they overlap?
214 if ((_list[cur].a._dsIdx == _list[exa].a._dsIdx) &&
215 (_list[cur].a._forward == _list[exa].a._forward) &&
216 (_list[cur].a._dsHi >= _list[exa].a._dsLo)) {
217
218 // Yup, merge. Extend the current hit if it is smaller.
219
220 if ((_list[cur].a._dsLo == _list[exa].a._dsLo) &&
221 (_list[cur].a._dsHi == _list[exa].a._dsHi)) {
222 // Nop, they're the same.
223 } else if (_list[cur].a._dsHi >= _list[exa].a._dsHi) {
224 // Nop, exa is contained in cur.
225 } else {
226
227 // exa extends cur!
228
229 // If cur is contained in exa, just get rid of cur.
230 // Otherwise, we need to fudge up new scores -- but we
231 // instead just mark them as merged, and don't filter them.
232 //
233 if (_list[cur].a._dsLo == _list[exa].a._dsLo) {
234 memcpy(_list+cur, _list+exa, sizeof(hit_s));
235 } else {
236 #ifdef DEBUG_HITREADER
237 fprintf(stderr,
238 "MERGE: ("uint32FMT","uint32FMT") -e "uint32FMT" "
239 uint32FMT":"uint32FMT"-"uint32FMT"%c("uint32FMT"-"uint32FMT"-"uint32FMT") "
240 uint32FMT":"uint32FMT"-"uint32FMT"%c("uint32FMT"-"uint32FMT"-"uint32FMT")\n",
241 cur, exa,
242 _list[cur].a._qsIdx,
243 _list[cur].a._dsIdx,
244 _list[cur].a._dsLo, _list[cur].a._dsHi, _list[cur].a._forward ? 'f' : 'r',
245 _list[cur].a._covered, _list[cur].a._matched, _list[cur].a._numMers,
246 _list[exa].a._dsIdx,
247 _list[exa].a._dsLo, _list[exa].a._dsHi, _list[exa].a._forward ? 'f' : 'r',
248 _list[exa].a._covered, _list[exa].a._matched, _list[exa].a._numMers);
249 #endif
250 _list[cur].a._merged = true;
251 _list[cur].a._covered = 0;
252 _list[cur].a._matched = 0;
253 _list[cur].a._dsHi = _list[exa].a._dsHi;
254 }
255 }
256
257 // By now, we've updated cur to include all that exa did. exa is junk.
258
259 } else {
260 // Nope, copy exa to the next spot (unless they're the same)
261 // and move there.
262 //
263 cur++;
264 if (cur != exa)
265 memcpy(_list+cur, _list+exa, sizeof(hit_s));
266 }
267
268 // Move to the next examination!
269 exa++;
270 }
271
272 _listLen = cur + 1;
273 }