-
Notifications
You must be signed in to change notification settings - Fork 24
/
algo.c
139 lines (123 loc) · 3.77 KB
/
algo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* PANDAseq -- Assemble paired FASTQ Illumina reads and strip the region between amplification primers.
Copyright (C) 2011-2013 Andre Masella
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_PTHREAD
# include <pthread.h>
#endif
#include "pandaseq.h"
#include "algo.h"
double panda_algorithm_quality_compare(
PandaAlgorithm algorithm,
const panda_qual *a,
const panda_qual *b) {
return algorithm->clazz->match_probability(&algorithm->end, (a->nt & b->nt) != '\0', a->qual, b->qual);
}
void *panda_algorithm_data(
PandaAlgorithm algo) {
return &algo->end;
}
PandaAlgorithmClass panda_algorithm_class(
PandaAlgorithm algo) {
return algo->clazz;
}
bool panda_algorithm_is_a(
PandaAlgorithm algo,
PandaAlgorithmClass clazz) {
return algo != NULL && algo->clazz == clazz;
}
PandaAlgorithm panda_algorithm_ref(
PandaAlgorithm algo) {
#ifdef HAVE_PTHREAD
pthread_mutex_lock(&algo->mutex);
#endif
algo->refcnt++;
#ifdef HAVE_PTHREAD
pthread_mutex_unlock(&algo->mutex);
#endif
return algo;
}
void panda_algorithm_unref(
PandaAlgorithm algo) {
size_t count;
if (algo == NULL)
return;
#ifdef HAVE_PTHREAD
pthread_mutex_lock(&algo->mutex);
#endif
count = --(algo->refcnt);
#ifdef HAVE_PTHREAD
pthread_mutex_unlock(&algo->mutex);
#endif
if (count == 0) {
#ifdef HAVE_PTHREAD
pthread_mutex_destroy(&algo->mutex);
#endif
if (algo->clazz->data_destroy != NULL) {
algo->clazz->data_destroy(&algo->end);
}
free(algo);
}
}
PandaAlgorithm panda_algorithm_new(
PandaAlgorithmClass clazz) {
PandaAlgorithm instance = malloc(sizeof(struct panda_algorithm) + clazz->data_size);
#ifdef HAVE_PTHREAD
pthread_mutex_init(&instance->mutex, NULL);
#endif
instance->refcnt = 1;
instance->clazz = clazz;
return instance;
}
PandaAlgorithmClass *panda_algorithms = NULL;
size_t panda_algorithms_length = 0;
static size_t algorithms_size = 10;
static int algorithm_compare(
const void *a,
const void *b) {
return strcmp(((*(PandaAlgorithmClass *) a))->name, (*((PandaAlgorithmClass *) b))->name);
}
void panda_algorithm_register(
PandaAlgorithmClass clazz) {
size_t it;
for (it = 0; it < panda_algorithms_length; it++) {
if (panda_algorithms[it] == clazz) {
return;
}
}
if (panda_algorithms_length == algorithms_size) {
algorithms_size *= 2;
panda_algorithms = realloc(panda_algorithms, algorithms_size * sizeof(PandaAlgorithmClass));
}
panda_algorithms[panda_algorithms_length++] = clazz;
qsort(panda_algorithms, panda_algorithms_length, sizeof(PandaAlgorithmClass), algorithm_compare);
}
__attribute__ ((constructor))
static void lib_init(
void) {
panda_algorithms = calloc(sizeof(PandaAlgorithmClass), algorithms_size);
panda_algorithm_register(&panda_algorithm_ea_util_class);
panda_algorithm_register(&panda_algorithm_flash_class);
panda_algorithm_register(&panda_algorithm_pear_class);
panda_algorithm_register(&panda_algorithm_rdp_mle_class);
panda_algorithm_register(&panda_algorithm_simple_bayes_class);
panda_algorithm_register(&panda_algorithm_stitch_class);
panda_algorithm_register(&panda_algorithm_uparse_class);
}
__attribute__ ((destructor))
static void lib_destroy(
void) {
free(panda_algorithms);
}