Debian Codegraph @

src/util.c

raw symbols SCIP
1 /*
2 util.c - utility functions for scythe.
3 */
4
5 #include <assert.h>
6 #include <stdarg.h>
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <zlib.h>
11
12 #include "kseq.h"cxx . . $ `<file>/src/kseq.h`/
13 #include "scythe.h"cxx . . $ `<file>/src/scythe.h`/
14
15 __KSEQ_BASICcxx . . $ `src/kseq.h:151:9`!(static, gzFile)
16 __KS_GETCcxx . . $ `src/kseq.h:66:9`!(gzread, BUFFER_SIZEcxx . . $ `src/scythe.h:20:9`!)
17 __KS_GETUNTILcxx . . $ `src/kseq.h:91:9`!(gzread, BUFFER_SIZEcxx . . $ `src/scythe.h:20:9`!)
18 __KSEQ_READcxx . . $ `src/kseq.h:171:9`!(static)
19
20
21 void *xmalloccxx . . $ xmalloc(bfcfa4d6b7f7ef64).(size_t size) {
22 void *ret = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(size);
23 if (!ret) {
24 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Out of memory, malloc failed");
25 exitcxx . . $ exit(3ca292c338df9bb3).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!);
26 }
27 return(ret);
28 }
29
30 adapter_arraycxx . . $ adapter_array# *load_adapterscxx . . $ load_adapters(fb009b0c85c22345).(gzFilecxx . . $ gzFile# fp) {
31 adapter_arraycxx . . $ adapter_array# *aa = xmalloccxx . . $ xmalloc(bfcfa4d6b7f7ef64).(sizeof(adapter_arraycxx . . $ adapter_array#));
32 adaptercxx . . $ adapter# *adapters = xmalloccxx . . $ xmalloc(bfcfa4d6b7f7ef64).(sizeof(adaptercxx . . $ adapter#)*MAX_ADAPTERScxx . . $ `src/scythe.h:25:9`!);
33 kseq_tcxx . . $ kseq_t# *aseq;
34 int i=0, j, seq_l, header_l;
35
36 aseq = kseq_initcxx . . $ kseq_init(8b28f0577201a580).(fp);
37 /*
38 Note: the string lengths from kseq.h structs do *not* include the
39 null byte, so these have to be taken into account.
40 */
41 while ((seq_l = kseq_readcxx . . $ kseq_read(9a11b5e3a16dcc01).(aseq)) >= 0) {
42 if (!aseq->seqcxx . . $ $anonymous_type_bd94a7007b005cc0_0#seq..lcxx . . $ __kstring_t#l. || !aseq->namecxx . . $ $anonymous_type_bd94a7007b005cc0_0#name..lcxx . . $ __kstring_t#l.) {
43 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Blank FASTA header or sequence in adapters file.\n");
44 exitcxx . . $ exit(3ca292c338df9bb3).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!);
45 }
46
47 adapters[i].seqcxx . . $ adapter#seq. = (char *) xmalloccxx . . $ xmalloc(bfcfa4d6b7f7ef64).(seq_l*sizeof(char) + 1);
48 strncpycxx . . $ strncpy(255158a415b673b0).(adapters[i].seqcxx . . $ adapter#seq., aseq->seqcxx . . $ $anonymous_type_bd94a7007b005cc0_0#seq..scxx . . $ __kstring_t#s., seq_l);
49 header_l = aseq->namecxx . . $ $anonymous_type_bd94a7007b005cc0_0#name..lcxx . . $ __kstring_t#l. + aseq->commentcxx . . $ $anonymous_type_bd94a7007b005cc0_0#comment..lcxx . . $ __kstring_t#l. + 1;
50 adapters[i].namecxx . . $ adapter#name. = (char *) xmalloccxx . . $ xmalloc(bfcfa4d6b7f7ef64).(header_l*sizeof(char) + 1);
51 strncpycxx . . $ strncpy(255158a415b673b0).(adapters[i].namecxx . . $ adapter#name., aseq->namecxx . . $ $anonymous_type_bd94a7007b005cc0_0#name..scxx . . $ __kstring_t#s., aseq->namecxx . . $ $anonymous_type_bd94a7007b005cc0_0#name..lcxx . . $ __kstring_t#l.+1);
52
53 if (aseq->commentcxx . . $ $anonymous_type_bd94a7007b005cc0_0#comment..scxx . . $ __kstring_t#s.) {
54 strncatcxx . . $ strncat(255158a415b673b0).(adapters[i].namecxx . . $ adapter#name., " ", 1);
55 strncatcxx . . $ strncat(255158a415b673b0).(adapters[i].namecxx . . $ adapter#name., aseq->commentcxx . . $ $anonymous_type_bd94a7007b005cc0_0#comment..scxx . . $ __kstring_t#s., aseq->commentcxx . . $ $anonymous_type_bd94a7007b005cc0_0#comment..lcxx . . $ __kstring_t#l.+1);
56 }
57
58 adapters[i].lengthcxx . . $ adapter#length. = seq_l;
59
60 /* occurences - for recording where adapters are found */
61 adapters[i].occurrencescxx . . $ adapter#occurrences. = xmalloccxx . . $ xmalloc(bfcfa4d6b7f7ef64).(seq_l*sizeof(unsigned int));
62 for (j=0; j < seq_l; j++)
63 adapters[i].occurrencescxx . . $ adapter#occurrences.[j] = 0;
64 i++;
65 }
66
67 aa->adapterscxx . . $ adapter_array#adapters. = adapters;
68 aa->ncxx . . $ adapter_array#n. = i;
69
70 kseq_destroycxx . . $ kseq_destroy(bc2dde352bd9ac18).(aseq);
71 return(aa);
72 }
73
74 void destroy_adapterscxx . . $ destroy_adapters(d23ed8633a24bbab).(adapter_arraycxx . . $ adapter_array# *aa, int n) {
75 int i;
76 for (i = 0; i < aa->ncxx . . $ adapter_array#n.; i++) {
77 freecxx . . $ free(5d22bdacc48458e8).((aa->adapterscxx . . $ adapter_array#adapters.)[i].seqcxx . . $ adapter#seq.);
78 freecxx . . $ free(5d22bdacc48458e8).((aa->adapterscxx . . $ adapter_array#adapters.)[i].namecxx . . $ adapter#name.);
79 freecxx . . $ free(5d22bdacc48458e8).((aa->adapterscxx . . $ adapter_array#adapters.)[i].occurrencescxx . . $ adapter#occurrences.);
80 }
81 freecxx . . $ free(5d22bdacc48458e8).(aa->adapterscxx . . $ adapter_array#adapters.);
82 freecxx . . $ free(5d22bdacc48458e8).(aa);
83 }
84
85
86 char *fmt_matchescxx . . $ fmt_matches(fe5a9a6a86af7536).(const char *seqa, const char *seqb, const int *matches, const int n) {
87 /*
88 Make a string of matches. xmalloc() needs three times the
89 sequence lengths, four new line characters, and one null byte.
90 */
91 char *out = xmalloccxx . . $ xmalloc(bfcfa4d6b7f7ef64).(3*(n + 1)*sizeof(char) + 2), *ptr=out;
92 int i;
93 sprintfcxx . . $ sprintf(26c8642d26409d6d).(out, "%.*s\n", n, seqa);
94 ptr += n + 1;
95 for (i = 0; i < n; i++) {
96 if (matches[i]) {
97 sprintfcxx . . $ sprintf(26c8642d26409d6d).(ptr, "|");
98 } else {
99 sprintfcxx . . $ sprintf(26c8642d26409d6d).(ptr, " ");
100 }
101 ptr++;
102 }
103 sprintfcxx . . $ sprintf(26c8642d26409d6d).(ptr, "\n%.*s", (int) n, seqb);
104 return out;
105 }
106
107 void print_float_arraycxx . . $ print_float_array(d97132c3eefc9c07).(const float *array, int n) {
108 int i;
109 printfcxx . . $ printf(581fddd72a8db8fc).("[");
110 for (i = 0; i < n; i++) {
111 if (i != n-1)
112 printfcxx . . $ printf(581fddd72a8db8fc).("%.2f, ", array[i]);
113 else
114 printfcxx . . $ printf(581fddd72a8db8fc).("%.2f]", array[i]);
115 }
116 }
117
118 void fprint_float_arraycxx . . $ fprint_float_array(3389e3ecfa0c73b5).(FILEcxx . . $ FILE# *fp, const float *array, int n) {
119 int i;
120 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(fp, "[");
121 for (i = 0; i < n; i++) {
122 if (i != n-1)
123 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(fp, "%.2f, ", array[i]);
124 else
125 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(fp, "%.2f]", array[i]);
126 }
127 }
128
129
130 void print_int_arraycxx . . $ print_int_array(cfb464fe78c8a83b).(const int *array, int n) {
131 int i;
132 printfcxx . . $ printf(581fddd72a8db8fc).("[");
133 for (i = 0; i < n; i++) {
134 if (i != n-1)
135 printfcxx . . $ printf(581fddd72a8db8fc).("%d, ", array[i]);
136 else
137 printfcxx . . $ printf(581fddd72a8db8fc).("%d]", array[i]);
138 }
139 }
140
141
142 void print_uint_arraycxx . . $ print_uint_array(23865addb5019587).(const unsigned int *array, int n) {
143 int i;
144 printfcxx . . $ printf(581fddd72a8db8fc).("[");
145 for (i = 0; i < n; i++) {
146 if (i != n-1)
147 printfcxx . . $ printf(581fddd72a8db8fc).("%d, ", array[i]);
148 else
149 printfcxx . . $ printf(581fddd72a8db8fc).("%d]", array[i]);
150 }
151 }
152
153 void fprint_uint_arraycxx . . $ fprint_uint_array(2d8583fd46d1aa71).(FILEcxx . . $ FILE# *fp, const unsigned int *array, int n) {
154 int i;
155 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(fp, "[");
156 for (i = 0; i < n; i++) {
157 if (i != n-1)
158 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(fp, "%d, ", array[i]);
159 else
160 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(fp, "%d]", array[i]);
161 }
162 }
163
164 inline int sumcxx . . $ sum(1ec4c99060f826b5).(const int *x, size_t n) {
165 size_t i;
166 int s = 0;
167 for (i = 0; i < n; i++)
168 s += x[i];
169 return s;
170 }
171
172 void write_fastqcxx . . $ write_fastq(c1ff00be624d6f38).(FILEcxx . . $ FILE# *output_fp, kseq_tcxx . . $ kseq_t# *seq, int add_tag, int shift, int min_keep) {
173 char tag[] = ";;cut_scythe";
174 char *sequence, *qual;
175
176 /* check if we have fewer than min-match */
177 if (shift != -1 && shift <= min_keep) {
178 sequence = "N";
179 qual = "B";
180 shift = -1;
181 } else {
182 sequence = seq->seqcxx . . $ $anonymous_type_bd94a7007b005cc0_0#seq..scxx . . $ __kstring_t#s.;
183 qual = seq->qualcxx . . $ $anonymous_type_bd94a7007b005cc0_0#qual..scxx . . $ __kstring_t#s.;
184 }
185 if (shift >= 0) {
186
187 if (add_tag) {
188 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(output_fp,
189 "@%s %.*s%s-%d\n%.*s\n+\n%.*s\n", seq->namecxx . . $ $anonymous_type_bd94a7007b005cc0_0#name..scxx . . $ __kstring_t#s., (int) seq->commentcxx . . $ $anonymous_type_bd94a7007b005cc0_0#comment..lcxx . . $ __kstring_t#l.,
190 seq->commentcxx . . $ $anonymous_type_bd94a7007b005cc0_0#comment..scxx . . $ __kstring_t#s., tag, shift,
191 (int) shift, sequence,
192 (int) shift, qual);
193 } else {
194 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(output_fp,
195 "@%s %.*s\n%.*s\n+\n%.*s\n", seq->namecxx . . $ $anonymous_type_bd94a7007b005cc0_0#name..scxx . . $ __kstring_t#s., (int) seq->commentcxx . . $ $anonymous_type_bd94a7007b005cc0_0#comment..lcxx . . $ __kstring_t#l.,
196 seq->commentcxx . . $ $anonymous_type_bd94a7007b005cc0_0#comment..scxx . . $ __kstring_t#s.,
197 (int) shift, sequence,
198 (int) shift, qual);
199 }
200 } else
201 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(output_fp,
202 "@%s %.*s\n%s\n+\n%s\n", seq->namecxx . . $ $anonymous_type_bd94a7007b005cc0_0#name..scxx . . $ __kstring_t#s., (int) seq->commentcxx . . $ $anonymous_type_bd94a7007b005cc0_0#comment..lcxx . . $ __kstring_t#l.,
203 seq->commentcxx . . $ $anonymous_type_bd94a7007b005cc0_0#comment..scxx . . $ __kstring_t#s., sequence, qual);
204 }
205
206 void print_summarycxx . . $ print_summary(a6616d7498bbe84d).(adapter_arraycxx . . $ adapter_array# *aa, float prior, int uncontaminated,
207 int contaminated, int total) {
208 /* int i; */
209 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "prior: %0.3f\n", prior);
210 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "\nAdapter Trimming Complete\ncontaminated: %d, uncontaminated: %d, total: %d\n",
211 contaminated, total-contaminated, total);
212 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "contamination rate: %f\n\n", contaminated/(float) total);
213 /* for (i = 0; i < aa->n; i++) { */
214 /* fprintf(stderr, "\nAdapter %d '%s' contamination occurences:\n", i+1, aa->adapters[i].name); */
215 /* fprint_uint_array(stderr, aa->adapters[i].occurrences, aa->adapters[i].length); */
216 /* fprintf(stderr, "\n"); */
217 /* } */
218 }
219
220 void print_matchcxx . . $ print_match(a58bc8ea36e4852f).(kseq_tcxx . . $ kseq_t# *seq, matchcxx . . $ match# *match, FILEcxx . . $ FILE# *matches_fp,
221 const adapter_arraycxx . . $ adapter_array# *aa, quality_typecxx . . $ quality_type# qual_type) {
222 /* Make a string that indicates the position of the matches with "|"s. */
223 char *match_string;
224 match_string = fmt_matchescxx . . $ fmt_matches(fe5a9a6a86af7536).((aa->adapterscxx . . $ adapter_array#adapters.[match->adapter_indexcxx . . $ match#adapter_index.]).seqcxx . . $ adapter#seq.,
225 &(seq->seqcxx . . $ $anonymous_type_bd94a7007b005cc0_0#seq..scxx . . $ __kstring_t#s.)[match->shiftcxx . . $ match#shift.],
226 match->match_poscxx . . $ match#match_pos., match->lengthcxx . . $ match#length.);
227
228 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(matches_fp, "p(c|s): %f; p(!c|s): %f; adapter: %s\n%s\n%s\n%.*s\n",
229 match->pscxx . . $ match#ps.->contamcxx . . $ posterior_set#contam., match->pscxx . . $ match#ps.->randomcxx . . $ posterior_set#random.,
230 aa->adapterscxx . . $ adapter_array#adapters.[match->adapter_indexcxx . . $ match#adapter_index.].namecxx . . $ adapter#name.,
231 seq->namecxx . . $ $anonymous_type_bd94a7007b005cc0_0#name..scxx . . $ __kstring_t#s., match_string,
232 (int) match->lengthcxx . . $ match#length.,
233 &(seq->qualcxx . . $ $anonymous_type_bd94a7007b005cc0_0#qual..scxx . . $ __kstring_t#s.)[match->shiftcxx . . $ match#shift.]);
234 fprint_float_arraycxx . . $ fprint_float_array(3389e3ecfa0c73b5).(matches_fp, qual_to_probscxx . . $ qual_to_probs(a661829a82ca3f60).(&(seq->qualcxx . . $ $anonymous_type_bd94a7007b005cc0_0#qual..scxx . . $ __kstring_t#s.)[match->shiftcxx . . $ match#shift.], qual_type), match->lengthcxx . . $ match#length.);
235 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(matches_fp, "\n\n");
236 freecxx . . $ free(5d22bdacc48458e8).(match_string);
237 }