Debian Codegraph @

src/match.c

raw symbols SCIP
1 /*
2 matches.c - sequence match scoring for scythe.
3 */
4 #include <assert.h>
5 #include <limits.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include "scythe.h"
10
11 int *score_sequence(int *matches, const char *seq, const char *pattern, size_t n) {
12 /*
13 Score a string using constant match and mismatch scores. Assumes
14 seq is longer than or of equal length to pattern. Only the first
15 n characters of pattern and seq are scored. We explicitly error
16 out of in the case of a null byte.
17
18
19 [----------seq-------------------]
20 |----pattern-----|
21
22 */
23 size_t i;
24 /* These are addressed by the assert below
25 assert(strlen(seq) >= n);
26 assert(strlen(pattern) >= n);
27 */
28 for (i = 0; i < n; i++) {
29 assert(seq[i] && pattern[i]); /* no string termination */
30 if (seq[i] == pattern[i])
31 matches[i] = MATCH_SCORE;
32 else
33 matches[i] = MISMATCH_SCORE;
34 }
35 return matches;
36 }
37
38 match *find_best_match(const adapter_array *aa, const char *read,
39 float *p_quals, float prior, float p_match, int min_l) {
40 /*
41 Take an adapter array, and check the read against all
42 adapters. Brute force string matching is used. This is to avoid
43 approximate matching algorithms which required an a priori
44 specified number mismatches.
45
46 */
47
48 match *best_match=NULL;
49 int i, shift, max_shift, found_contam=0;
50 size_t al, rl = strlen(read);
51 int *best_arr=xmalloc(sizeof(int) * rl), best_adapter=0, best_length=0, best_shift=0, best_score=INT_MIN;
52 int curr_score, *curr_arr=xmalloc(sizeof(int) * rl);
53 posterior_set *ps=NULL;
54 float *best_p_quals=NULL;
55
56 max_shift = rl - min_l;
57 for (shift = 0; shift < max_shift; shift++) {
58 for (i = 0; i < aa->n; i++) {
59 if (min_l >= aa->adapters[i].length) {
60 fprintf(stderr, "Minimum match length (option -n) greater than or " \
61 "equal to length of adapter.\n");
62 exit(EXIT_FAILURE);
63 }
64 al = min(aa->adapters[i].length, rl - shift);
65 score_sequence(curr_arr, &(read)[shift], (aa->adapters[i]).seq, al);
66 curr_score = sum(curr_arr, al);
67 if (curr_score > best_score) {
68 best_score = curr_score;
69 best_length = al;
70 best_shift = shift;
71 best_p_quals = &(p_quals)[shift];
72 for (size_t iii = 0; iii < al; iii++)
73 best_arr[iii] = curr_arr[iii];
74 best_adapter = i;
75 ps = posterior(best_arr, best_p_quals, prior, 0.25, best_length);
76 found_contam = ps->is_contam;
77 if (found_contam) {
78 break;
79 } else {
80 free(ps);
81 ps=NULL;
82 }
83 }
84 }
85 if (found_contam)
86 break;
87 }
88 free(curr_arr);
89 curr_arr = NULL;
90
91 if (!found_contam) {
92 /* no match found */
93 free(best_arr);
94 best_arr = NULL;
95 return NULL;
96 }
97 best_arr = realloc(best_arr, sizeof(*best_arr) * best_length);
98 /* save this match */
99 best_match = xmalloc(sizeof(match));
100 best_match->match = best_arr;
101 best_match->shift = best_shift;
102 best_match->length = best_length;
103 best_match->ps = ps;
104 best_match->score = best_score;
105 best_match->adapter_index = best_adapter;
106 best_match->p_quals = best_p_quals;
107 best_match->match_pos = calloc(best_length, sizeof(int));
108 for (i = 0; i < best_length; i++)
109 best_match->match_pos[i] = best_match->match[i] == MATCH_SCORE;
110 return best_match;
111 }
112
113 void destroy_match(match *m) {
114 /*
115 Free all memory taken by this match object. m->p_quals is not
116 freed, as this is points to an array allocated within main().
117 */
118 free(m->match);
119 free(m->match_pos);
120 free(m->ps);
121 free(m);
122 }
123