fastq-lib.cpp

filter: Statementstmt 0.0% 0 / 166 Branchbran 0.0% 0 / 154 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 320 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« Tidx.c ↑ Summary sparsehash/dense_hash_map »
1 /*
2 Copyright (c) 2011 Expression Analysis / Erik Aronesty
3  
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10  
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13  
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22  
23 #include "fastq-lib.h"
24  
25 #ifdef __MAIN__
26 int main(int argc, char **argv) {
27 // todo... put testing stuff in here, so the lib can be tested independently of the other componenets
28 }
29 #endif
30  
31 0 int read_line(FILE *in, struct line &l) {
32 0 return (l.n = getline(&l.s, &l.a, in));
33 }
34  
35 0 int read_fq(FILE *in, int rno, struct fq *fq, const char *name) {
36 0 read_line(in, fq->id);
37 0 ▶ if (fq->id.s && (*fq->id.s == '>')) {
Branch: 2 branches
BranchTrueFalseTotal
if (fq->id.s && (*fq->id.s == '>')) { 0 0 0
if (fq->id.s && (*fq->id.s == '>')) { 0 0 0
38 0 fq->id.s[0] = '@';
39 // read fasta instead
40 0 char c = fgetc(in);
41 0 ▶ while (c != '>' && c != EOF) {
Branch: 2 branches
BranchTrueFalseTotal
while (c != '>' && c != EOF) { 0 0 0
while (c != '>' && c != EOF) { 0 0 0
42 0 ▶ if (fq->seq.a <= (fq->seq.n+1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (fq->seq.a <= (fq->seq.n+1)) { 0 0 0
43 0 fq->seq.s=(char *)realloc(fq->seq.s, fq->seq.a=(fq->seq.a+16)*2);
44 }
45 0 ▶ if (!isspace(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!isspace(c)) 0 0 0
46 0 fq->seq.s[fq->seq.n++]=c;
47 0 c = fgetc(in);
48 }
49 0 ▶ if (c != EOF) {
Branch: 1 branch
BranchTrueFalseTotal
if (c != EOF) { 0 0 0
50 0 ungetc(c, in);
51 }
52 // make it look like a fastq
53 0 fq->qual.s=(char *)realloc(fq->qual.s, fq->qual.a=(fq->seq.n+1));
54 0 memset(fq->qual.s, 'h', fq->seq.n);
55 0 fq->qual.s[fq->qual.n=fq->seq.n]=fq->seq.s[fq->seq.n]='\0';
56 0 fq->com.s=(char *)malloc(fq->com.a=2);
57 0 fq->com.n=1;
58 0 strcpy(fq->com.s,"+");
59 0 } else {
60 0 read_line(in, fq->seq);
61 0 read_line(in, fq->com);
62 0 read_line(in, fq->qual);
63 }
64  
65 0 ▶ if (fq->qual.n <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (fq->qual.n <= 0) 0 0 0
66 0 return 0;
67 0 ▶ if (fq->id.s[0] != '@' || fq->com.s[0] != '+' || fq->seq.n != fq->qual.n) {
Branch: 3 branches
BranchTrueFalseTotal
if (fq->id.s[0] != '@' || fq->com.s[0] != '+' || fq->seq.n != fq->qual.n) { 0 0 0
if (fq->id.s[0] != '@' || fq->com.s[0] != '+' || fq->seq.n != fq->qual.n) { 0 0 0
if (fq->id.s[0] != '@' || fq->com.s[0] != '+' || fq->seq.n != fq->qual.n) { 0 0 0
68 0 ▶ const char *errtyp = (fq->seq.n != fq->qual.n) ? "length mismatch" : fq->id.s[0] != '@' ? "no '@' for id" : "no '+' for comment";
Branch: 2 branches
BranchTrueFalseTotal
const char *errtyp = (fq->seq.n != fq->qual.n) ? "length mismatch" : fq->id.s[0] != '@' ? "no '@' for id" : "no '+' for comment"; 0 0 0
const char *errtyp = (fq->seq.n != fq->qual.n) ? "length mismatch" : fq->id.s[0] != '@' ? "no '@' for id" : "no '+' for comment"; 0 0 0
69 0 ▶ if (name) {
Branch: 1 branch
BranchTrueFalseTotal
if (name) { 0 0 0
70 0 fprintf(stderr, "Malformed fastq record (%s) in file '%s', line %d\n", errtyp, name, rno*2+1);
71 } else {
72 0 fprintf(stderr, "Malformed fastq record (%s) at line %d\n", errtyp, rno*2+1);
73 }
74 0 return -1;
75 }
76 // win32-safe chomp
77 0 fq->seq.s[--fq->seq.n] = '\0';
78 0 ▶ if (fq->seq.s[fq->seq.n-1] == '\r') {
Branch: 1 branch
BranchTrueFalseTotal
if (fq->seq.s[fq->seq.n-1] == '\r') { 0 0 0
79 0 fq->seq.s[--fq->seq.n] = '\0';
80 }
81 0 fq->qual.s[--fq->qual.n] = '\0';
82 0 ▶ if (fq->qual.s[fq->qual.n-1] == '\r') {
Branch: 1 branch
BranchTrueFalseTotal
if (fq->qual.s[fq->qual.n-1] == '\r') { 0 0 0
83 0 fq->qual.s[--fq->qual.n] = '\0';
84 }
85 0 return 1;
86 }
87  
88 struct qual_str {
89 long long int cnt;
90 long long int sum;
91 long long int ssq;
92 long long int ns;
93 } quals[MAX_FILENO_QUALS+1] = {{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};
94  
95 0 int gzclose(FILE *f, bool isgz) {
96 0 ▶ return isgz ? pclose(f) : fclose(f);
Branch: 1 branch
BranchTrueFalseTotal
return isgz ? pclose(f) : fclose(f); 0 0 0
97 }
98  
99 0 FILE *gzopen(const char *f, const char *m, bool*isgz) {
100 // maybe use zlib some day?
101 FILE *h;
102 0 const char * ext = fext(f);
103 0 ▶ if (!strcmp(ext,".gz")) {
Branch: 1 branch
BranchTrueFalseTotal
if (!strcmp(ext,".gz")) { 0 0 0
104 0 char *tmp=(char *)malloc(strlen(f)+100);
105 0 ▶ if (strchr(m,'w')) {
Branch: 1 branch
BranchTrueFalseTotal
if (strchr(m,'w')) { 0 0 0
106 0 strcpy(tmp, "gzip --rsyncable > '");
107 0 strcat(tmp, f);
108 0 strcat(tmp, "'");
109 } else {
110 0 strcpy(tmp, "gunzip -c '");
111 0 strcat(tmp, f);
112 0 strcat(tmp, "'");
113 }
114 0 h = popen(tmp, m);
115 0 *isgz=1;
116 0 free(tmp);
117 0 ▶ } else if (!strcmp(ext,".zip")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!strcmp(ext,".zip")) { 0 0 0
118 0 char *tmp=(char *)malloc(strlen(f)+100);
119 0 ▶ if (strchr(m,'w')) {
Branch: 1 branch
BranchTrueFalseTotal
if (strchr(m,'w')) { 0 0 0
120 0 strcpy(tmp, "zip -q '");
121 0 strcat(tmp, f);
122 0 strcat(tmp, "' -");
123 } else {
124 0 strcpy(tmp, "unzip -p '");
125 0 strcat(tmp, f);
126 0 strcat(tmp, "'");
127 }
128 0 h = popen(tmp, m);
129 0 *isgz=1;
130 0 free(tmp);
131 } else {
132 0 h = fopen(f, m);
133 0 *isgz=0;
134 }
135 0 ▶ if (!h) {
Branch: 1 branch
BranchTrueFalseTotal
if (!h) { 0 0 0
136 0 fprintf(stderr, "Error opening file '%s': %s\n",f, strerror(errno));
137 0 exit(1);
138 }
139 0 return h;
140 }
141  
142 0 const char *fext(const char *f) {
143 0 const char *x=strrchr(f,'.');
144 0 ▶ return x ? x : "";
Branch: 1 branch
BranchTrueFalseTotal
return x ? x : ""; 0 0 0
145 }
146  
147 0 bool poorqual(int n, int l, const char *s, const char *q) {
148 0 int i=0, sum=0, ns=0;
149 0 ▶ for (i=0;i<l;++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=0;i<l;++i) { 0 0 0
150 0 ▶ if (s[i] == 'N')
Branch: 1 branch
BranchTrueFalseTotal
if (s[i] == 'N') 0 0 0
151 0 ++ns;
152 0 quals[n].cnt++;
153 0 quals[n].ssq += q[i] * q[i];
154 0 sum+=q[i];
155 }
156 0 quals[n].sum += sum;
157 0 quals[n].ns += ns;
158 0 int xmean = sum/l;
159 0 ▶ if (quals[n].cnt < 20000) {
Branch: 1 branch
BranchTrueFalseTotal
if (quals[n].cnt < 20000) { 0 0 0
160 // not enough data? use heuristic
161 0 ▶ return ((xmean-33) < 20) || (ns != 0);
Branch: 2 branches
BranchTrueFalseTotal
return ((xmean-33) < 20) || (ns != 0); 0 0 0
return ((xmean-33) < 20) || (ns != 0); 0 0 0
162 }
163 // enough data? use stdev
164 0 int pmean = quals[n].sum / quals[n].cnt; // mean q
165 0 double pdev = stdev(quals[n].cnt, quals[n].sum, quals[n].ssq); // dev q
166 0 ▶ int serr = max(2,pdev/sqrt(l)); // stderr for length l
Branch: 1 branch
BranchTrueFalseTotal
int serr = max(2,pdev/sqrt(l)); // stderr for length l 0 0 0
167 0 ▶ if (xmean < (pmean - serr)) { // off by 1 stdev?
Branch: 1 branch
BranchTrueFalseTotal
if (xmean < (pmean - serr)) { // off by 1 stdev? 0 0 0
168 0 return 1; // ditch it
169 }
170 0 ▶ if (ns > (1+(l*quals[n].ns / quals[n].cnt))) { // 1 more n than average?
Branch: 1 branch
BranchTrueFalseTotal
if (ns > (1+(l*quals[n].ns / quals[n].cnt))) { // 1 more n than average? 0 0 0
171 0 return 1; // ditch it
172 }
173 0 return 0;
174 }
175  
176 #define comp(c) ((c)=='A'?'T':(c)=='a'?'t':(c)=='C'?'G':(c)=='c'?'g':(c)=='G'?'C':(c)=='g'?'c':(c)=='T'?'A':(c)=='t'?'a':(c))
177  
178 0 void revcomp(struct fq *d, struct fq *s) {
179 0 ▶ if (!d->seq.s) {
Branch: 1 branch
BranchTrueFalseTotal
if (!d->seq.s) { 0 0 0
180 0 d->seq.s=(char *) malloc(d->seq.a=s->seq.n+1);
181 0 d->qual.s=(char *) malloc(d->qual.a=s->qual.n+1);
182 0 ▶ } else if (d->seq.a <= s->seq.n) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (d->seq.a <= s->seq.n) { 0 0 0
183 0 d->seq.s=(char *) realloc(d->seq.s, d->seq.a=(s->seq.n+1));
184 0 d->qual.s=(char *) realloc(d->qual.s, d->qual.a=(s->qual.n+1));
185 }
186 int i;
187 0 ▶ for (i=0;i<s->seq.n/2;++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=0;i<s->seq.n/2;++i) { 0 0 0
188 0 char b=s->seq.s[i];
189 0 char q=s->qual.s[i];
190 //printf("%d: %c, %c\n", i, comp(s->seq.s[s->seq.n-i-1]), s->qual.s[s->qual.n-i-1]);
191 0 ▶ d->seq.s[i]=comp(s->seq.s[s->seq.n-i-1]);
Branch: 8 branches
BranchTrueFalseTotal
d->seq.s[i]=comp(s->seq.s[s->seq.n-i-1]); 0 0 0
d->seq.s[i]=comp(s->seq.s[s->seq.n-i-1]); 0 0 0
d->seq.s[i]=comp(s->seq.s[s->seq.n-i-1]); 0 0 0
d->seq.s[i]=comp(s->seq.s[s->seq.n-i-1]); 0 0 0
d->seq.s[i]=comp(s->seq.s[s->seq.n-i-1]); 0 0 0
d->seq.s[i]=comp(s->seq.s[s->seq.n-i-1]); 0 0 0
d->seq.s[i]=comp(s->seq.s[s->seq.n-i-1]); 0 0 0
d->seq.s[i]=comp(s->seq.s[s->seq.n-i-1]); 0 0 0
192 0 d->qual.s[i]=s->qual.s[s->qual.n-i-1];
193 //printf("%d: %c, %c\n", s->seq.n-i-1, comp(b), q);
194 0 ▶ d->seq.s[s->seq.n-i-1]=comp(b);
Branch: 8 branches
BranchTrueFalseTotal
d->seq.s[s->seq.n-i-1]=comp(b); 0 0 0
d->seq.s[s->seq.n-i-1]=comp(b); 0 0 0
d->seq.s[s->seq.n-i-1]=comp(b); 0 0 0
d->seq.s[s->seq.n-i-1]=comp(b); 0 0 0
d->seq.s[s->seq.n-i-1]=comp(b); 0 0 0
d->seq.s[s->seq.n-i-1]=comp(b); 0 0 0
d->seq.s[s->seq.n-i-1]=comp(b); 0 0 0
d->seq.s[s->seq.n-i-1]=comp(b); 0 0 0
195 0 d->qual.s[s->seq.n-i-1]=q;
196 }
197 0 ▶ if (s->seq.n % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->seq.n % 2) { 0 0 0
198 //printf("%d: %c, %c\n", 1+s->seq.n/2, comp(s->seq.s[s->seq.n/2]));
199 0 ▶ d->seq.s[s->seq.n/2] = comp(s->seq.s[s->seq.n/2]);
Branch: 8 branches
BranchTrueFalseTotal
d->seq.s[s->seq.n/2] = comp(s->seq.s[s->seq.n/2]); 0 0 0
d->seq.s[s->seq.n/2] = comp(s->seq.s[s->seq.n/2]); 0 0 0
d->seq.s[s->seq.n/2] = comp(s->seq.s[s->seq.n/2]); 0 0 0
d->seq.s[s->seq.n/2] = comp(s->seq.s[s->seq.n/2]); 0 0 0
d->seq.s[s->seq.n/2] = comp(s->seq.s[s->seq.n/2]); 0 0 0
d->seq.s[s->seq.n/2] = comp(s->seq.s[s->seq.n/2]); 0 0 0
d->seq.s[s->seq.n/2] = comp(s->seq.s[s->seq.n/2]); 0 0 0
d->seq.s[s->seq.n/2] = comp(s->seq.s[s->seq.n/2]); 0 0 0
200 0 d->qual.s[s->seq.n/2] = s->qual.s[s->seq.n/2];
201 }
202 0 d->seq.n=s->seq.n;
203 0 d->qual.n=s->qual.n;
204 0 d->seq.s[s->seq.n]='\0';
205 0 d->qual.s[s->seq.n]='\0';
206 0 }
207  
208 0 void free_line(struct line *l) {
209 0 ▶ if (l) {
Branch: 1 branch
BranchTrueFalseTotal
if (l) { 0 0 0
210 0 ▶ if (l->s) free(l->s);
Branch: 1 branch
BranchTrueFalseTotal
if (l->s) free(l->s); 0 0 0
211 0 l->s=NULL;
212 }
213 0 }
214  
215 0 void free_fq(struct fq *f) {
216 0 ▶ if (f) {
Branch: 1 branch
BranchTrueFalseTotal
if (f) { 0 0 0
217 0 free_line(&f->id);
218 0 free_line(&f->seq);
219 0 free_line(&f->com);
220 0 free_line(&f->qual);
221 }
222 0 }
223  
224  
225 /* getline.c -- Replacement for GNU C library function getline
226  
227 Copyright (C) 1993 Free Software Foundation, Inc.
228  
229 This program is free software; you can redistribute it and/or
230 modify it under the terms of the GNU General Public License as
231 published by the Free Software Foundation; either version 2 of the
232 License, or (at your option) any later version.
233  
234 This program is distributed in the hope that it will be useful, but
235 WITHOUT ANY WARRANTY; without even the implied warranty of
236 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
237 General Public License for more details.
238  
239 You should have received a copy of the GNU General Public License
240 along with this program; if not, write to the Free Software
241 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
242  
243 /* Written by Jan Brittenson, bson@gnu.ai.mit.edu. */
244  
245 #include <sys/types.h>
246 #include <stdio.h>
247 #include <assert.h>
248 #include <stdlib.h>
249  
250 /* Read up to (and including) a TERMINATOR from STREAM into *LINEPTR
251 + OFFSET (and null-terminate it). *LINEPTR is a pointer returned from
252 malloc (or NULL), pointing to *N characters of space. It is realloc'd
253 as necessary. Return the number of characters read (not including the
254 null terminator), or -1 on error or EOF. */
255  
256 0 int getstr (char ** lineptr, size_t *n, FILE * stream, char terminator, int offset)
257 {
258 int nchars_avail; /* Allocated but unused chars in *LINEPTR. */
259 char *read_pos; /* Where we're reading into *LINEPTR. */
260 int ret;
261  
262 0 ▶ if (!lineptr || !n || !stream)
Branch: 3 branches
BranchTrueFalseTotal
if (!lineptr || !n || !stream) 0 0 0
if (!lineptr || !n || !stream) 0 0 0
if (!lineptr || !n || !stream) 0 0 0
263 0 return -1;
264  
265 0 ▶ if (!*lineptr)
Branch: 1 branch
BranchTrueFalseTotal
if (!*lineptr) 0 0 0
266 {
267 0 *n = 64;
268 0 *lineptr = (char *) malloc (*n);
269 0 ▶ if (!*lineptr)
Branch: 1 branch
BranchTrueFalseTotal
if (!*lineptr) 0 0 0
270 0 return -1;
271 }
272  
273 0 nchars_avail = *n - offset;
274 0 read_pos = *lineptr + offset;
275  
276 for (;;)
277 {
278 0 register int c = getc (stream);
279  
280 /* We always want at least one char left in the buffer, since we
281 always (unless we get an error while reading the first char)
282 NUL-terminate the line buffer. */
283  
284 0 ▶ assert(*n - nchars_avail == read_pos - *lineptr);
Branch: 1 branch
BranchTrueFalseTotal
assert(*n - nchars_avail == read_pos - *lineptr); 0 0 0
285 0 ▶ if (nchars_avail < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (nchars_avail < 1) 0 0 0
286 {
287 0 ▶ if (*n > 64)
Branch: 1 branch
BranchTrueFalseTotal
if (*n > 64) 0 0 0
288 0 *n *= 2;
289 else
290 0 *n += 64;
291  
292 0 nchars_avail = *n + *lineptr - read_pos;
293 0 *lineptr = (char *) realloc (*lineptr, *n);
294 0 ▶ if (!*lineptr)
Branch: 1 branch
BranchTrueFalseTotal
if (!*lineptr) 0 0 0
295 0 return -1;
296 0 read_pos = *n - nchars_avail + *lineptr;
297 0 ▶ assert(*n - nchars_avail == read_pos - *lineptr);
Branch: 1 branch
BranchTrueFalseTotal
assert(*n - nchars_avail == read_pos - *lineptr); 0 0 0
298 }
299  
300 0 ▶ if (c == EOF || ferror (stream))
Branch: 3 branches
BranchTrueFalseTotal
if (c == EOF || ferror (stream)) 0 0 0
if (c == EOF || ferror (stream)) 0 0 0
if (c == EOF || ferror (stream)) 0 0 0
301 {
302 /* Return partial line, if any. */
303 0 ▶ if (read_pos == *lineptr)
Branch: 1 branch
BranchTrueFalseTotal
if (read_pos == *lineptr) 0 0 0
304 0 return -1;
305 else
306 0 break;
307 }
308  
309 0 *read_pos++ = c;
310 0 nchars_avail--;
311  
312 0 ▶ if (c == terminator)
Branch: 1 branch
BranchTrueFalseTotal
if (c == terminator) 0 0 0
313 /* Return the line. */
314 0 break;
315 0 }
316  
317 /* Done - NUL terminate and return the number of chars read. */
318 0 *read_pos = '\0';
319  
320 0 ret = read_pos - (*lineptr + offset);
321 0 return ret;
322 }
323  
324 #if !defined(__GNUC__) || defined(__APPLE__) || defined(WIN32)
325  
326 ssize_t getline(char **lineptr, size_t *n, FILE *stream)
327 {
328 return getstr (lineptr, n, stream, '\n', 0);
329 }
330  
331  
332 #endif
« Tidx.c ↑ Summary sparsehash/dense_hash_map »