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"
13 #include "scythe.h"
14
15 __KSEQ_BASIC(static, gzFile)
16 __KS_GETC(gzread, BUFFER_SIZE)
17 __KS_GETUNTIL(gzread, BUFFER_SIZE)
18 __KSEQ_READ(static)
19
20
21 void *xmalloc(size_t size) {
22 void *ret = malloc(size);
23 if (!ret) {
24 fprintf(stderr, "Out of memory, malloc failed");
25 exit(EXIT_FAILURE);
26 }
27 return(ret);
28 }
29
30 adapter_array *load_adapters(gzFile fp) {
31 adapter_array *aa = xmalloc(sizeof(adapter_array));
32 adapter *adapters = xmalloc(sizeof(adapter)*MAX_ADAPTERS);
33 kseq_t *aseq;
34 int i=0, j, seq_l, header_l;
35
36 aseq = kseq_init(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_read(aseq)) >= 0) {
42 if (!aseq->seq.l || !aseq->name.l) {
43 fprintf(stderr, "Blank FASTA header or sequence in adapters file.\n");
44 exit(EXIT_FAILURE);
45 }
46
47 adapters[i].seq = (char *) xmalloc(seq_l*sizeof(char) + 1);
48 strncpy(adapters[i].seq, aseq->seq.s, seq_l);
49 header_l = aseq->name.l + aseq->comment.l + 1;
50 adapters[i].name = (char *) xmalloc(header_l*sizeof(char) + 1);
51 strncpy(adapters[i].name, aseq->name.s, aseq->name.l+1);
52
53 if (aseq->comment.s) {
54 strncat(adapters[i].name, " ", 1);
55 strncat(adapters[i].name, aseq->comment.s, aseq->comment.l+1);
56 }
57
58 adapters[i].length = seq_l;
59
60 /* occurences - for recording where adapters are found */
61 adapters[i].occurrences = xmalloc(seq_l*sizeof(unsigned int));
62 for (j=0; j < seq_l; j++)
63 adapters[i].occurrences[j] = 0;
64 i++;
65 }
66
67 aa->adapters = adapters;
68 aa->n = i;
69
70 kseq_destroy(aseq);
71 return(aa);
72 }
73
74 void destroy_adapters(adapter_array *aa, int n) {
75 int i;
76 for (i = 0; i < aa->n; i++) {
77 free((aa->adapters)[i].seq);
78 free((aa->adapters)[i].name);
79 free((aa->adapters)[i].occurrences);
80 }
81 free(aa->adapters);
82 free(aa);
83 }
84
85
86 char *fmt_matches(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 = xmalloc(3*(n + 1)*sizeof(char) + 2), *ptr=out;
92 int i;
93 sprintf(out, "%.*s\n", n, seqa);
94 ptr += n + 1;
95 for (i = 0; i < n; i++) {
96 if (matches[i]) {
97 sprintf(ptr, "|");
98 } else {
99 sprintf(ptr, " ");
100 }
101 ptr++;
102 }
103 sprintf(ptr, "\n%.*s", (int) n, seqb);
104 return out;
105 }
106
107 void print_float_array(const float *array, int n) {
108 int i;
109 printf("[");
110 for (i = 0; i < n; i++) {
111 if (i != n-1)
112 printf("%.2f, ", array[i]);
113 else
114 printf("%.2f]", array[i]);
115 }
116 }
117
118 void fprint_float_array(FILE *fp, const float *array, int n) {
119 int i;
120 fprintf(fp, "[");
121 for (i = 0; i < n; i++) {
122 if (i != n-1)
123 fprintf(fp, "%.2f, ", array[i]);
124 else
125 fprintf(fp, "%.2f]", array[i]);
126 }
127 }
128
129
130 void print_int_array(const int *array, int n) {
131 int i;
132 printf("[");
133 for (i = 0; i < n; i++) {
134 if (i != n-1)
135 printf("%d, ", array[i]);
136 else
137 printf("%d]", array[i]);
138 }
139 }
140
141
142 void print_uint_array(const unsigned int *array, int n) {
143 int i;
144 printf("[");
145 for (i = 0; i < n; i++) {
146 if (i != n-1)
147 printf("%d, ", array[i]);
148 else
149 printf("%d]", array[i]);
150 }
151 }
152
153 void fprint_uint_array(FILE *fp, const unsigned int *array, int n) {
154 int i;
155 fprintf(fp, "[");
156 for (i = 0; i < n; i++) {
157 if (i != n-1)
158 fprintf(fp, "%d, ", array[i]);
159 else
160 fprintf(fp, "%d]", array[i]);
161 }
162 }
163
164 inline int sum(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_fastq(FILE *output_fp, 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->seq.s;
183 qual = seq->qual.s;
184 }
185 if (shift >= 0) {
186
187 if (add_tag) {
188 fprintf(output_fp,
189 "@%s %.*s%s-%d\n%.*s\n+\n%.*s\n", seq->name.s, (int) seq->comment.l,
190 seq->comment.s, tag, shift,
191 (int) shift, sequence,
192 (int) shift, qual);
193 } else {
194 fprintf(output_fp,
195 "@%s %.*s\n%.*s\n+\n%.*s\n", seq->name.s, (int) seq->comment.l,
196 seq->comment.s,
197 (int) shift, sequence,
198 (int) shift, qual);
199 }
200 } else
201 fprintf(output_fp,
202 "@%s %.*s\n%s\n+\n%s\n", seq->name.s, (int) seq->comment.l,
203 seq->comment.s, sequence, qual);
204 }
205
206 void print_summary(adapter_array *aa, float prior, int uncontaminated,
207 int contaminated, int total) {
208 /* int i; */
209 fprintf(stderr, "prior: %0.3f\n", prior);
210 fprintf(stderr, "\nAdapter Trimming Complete\ncontaminated: %d, uncontaminated: %d, total: %d\n",
211 contaminated, total-contaminated, total);
212 fprintf(stderr, "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_match(kseq_t *seq, match *match, FILE *matches_fp,
221 const adapter_array *aa, 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_matches((aa->adapters[match->adapter_index]).seq,
225 &(seq->seq.s)[match->shift],
226 match->match_pos, match->length);
227
228 fprintf(matches_fp, "p(c|s): %f; p(!c|s): %f; adapter: %s\n%s\n%s\n%.*s\n",
229 match->ps->contam, match->ps->random,
230 aa->adapters[match->adapter_index].name,
231 seq->name.s, match_string,
232 (int) match->length,
233 &(seq->qual.s)[match->shift]);
234 fprint_float_array(matches_fp, qual_to_probs(&(seq->qual.s)[match->shift], qual_type), match->length);
235 fprintf(matches_fp, "\n\n");
236 free(match_string);
237 }