-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbam_trie.c
261 lines (210 loc) · 8.27 KB
/
bam_trie.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include "bam_trie.h"
void generate_logs(const char *file, cp_trie *trie);
cp_trie* dna_bam_to_trie(char * file) {
// Create trie
cp_trie *trie = cp_trie_create(0);
int read_bytes;
// char* bam_string;
int cont = 0;
bam1_t* bam_p = bam_init1();
// Open bam file for read
bamFile bam_fd = bam_open(file, "r");
//header for BAM file has been done in the opening
bam_header_t* bam_header_p = bam_header_read(bam_fd);
while ((read_bytes = bam_read1(bam_fd, bam_p)) > 0) {
add_region_to_trie(trie, bam_p, bam_header_p);
cont++;
}
// Free Memory
bam_close(bam_fd);
bam_destroy1(bam_p);
return trie;
}
cp_trie *dna_dataset_to_trie(char * file) {
char* buffer = (char*) calloc(1, MAX_DATASET_LINE_LENGTH);
const char delimiters[] = "\t";
char* token = NULL;
char* id = "";
char s;
int pos;
short int tam;
FILE* fd = fopen(file, "r");
FILE* fb = fopen("id.tmp", "wb");
// Create trie
cp_trie* trie = cp_trie_create(0);
// cp_trie *trie = cp_trie_create_trie(COLLECTION_MODE_DEEP,NULL,(cp_destructor_fn) region_free);
dna_map_region_t* region;
if (fd == NULL || fb == NULL) { // Mejorar esta gestión de errores
printf("Fallo al abrir el fichero");
return NULL;
}
while (fgets(buffer, MAX_DATASET_LINE_LENGTH, fd)) {
pos = 0;
token = strtok(buffer, delimiters);
region = (dna_map_region_t*) calloc(1, sizeof(dna_map_region_t));
while (token != NULL) {
switch (pos) {
case 0:
region->chromosome = (char*) calloc(strlen(token) + 1, sizeof(char));
strcpy(region->chromosome, token);
break;
case 1:
sscanf(token, "%i", ®ion->start_position);
break;
case 2:
sscanf(token, "%i", ®ion->end_position);
break;
case 3:
sscanf(token, "%c", &s);
region->strand = (s == '0' ? 0 : 1);
// printf("%c%i\n",s,region->strand);
break;
case 4:
id = (char*) calloc(strlen(token) + 1, sizeof(char));
sscanf(token, "%s", id);
break;
}
token = strtok(NULL, delimiters);
pos++;
}
tam = strlen(id) + 1;
fwrite(&tam, sizeof(short int), 1, fb);
fwrite(id, tam, 1, fb);
cp_trie_add(trie, id, region);
free(id);
}
//close and free resources
free(buffer);
fclose(fd);
fclose(fb);
return trie;
}
int add_region_to_trie(cp_trie *trie, bam1_t *bam_line, bam_header_t *header) {
// Create new region
dna_map_region_t * region = (dna_map_region_t *) calloc(1, sizeof(dna_map_region_t));
// Add chromosome info
int tid = bam_line->core.tid;
if (tid == -1) {
region->chromosome = "*";
} else {
region->chromosome = (char*) calloc(strlen(header->target_name[tid]) + 1, sizeof(char));
strcpy(region->chromosome, header->target_name[tid]);
}
// Add start position info
region->start_position = bam_line->core.pos; // TODO mirar el FLAG si es 1-base o 0-base
// Add end position info
region->end_position = (int) bam_calend(&bam_line->core, bam1_cigar(bam_line));
// Add strand info
uint32_t flag = (uint32_t) bam_line->core.flag;
region->strand = (flag & BAM_FREVERSE) ? 1 : 0;
// Get the Seq name Id
char * id = (char*) calloc(bam_line->core.l_qname, sizeof(char));
strcpy(id, bam1_qname(bam_line));
cp_trie_add(trie, id, region);
// free(id);
return 1;
}
void dna_intersection(cp_trie *trie, char * filename, char * log_file, char *unmapped_bam, unsigned char align_bam, unsigned char soft_hard, trie_result_t* result) {
int read_bytes;
// int log = 0;
unsigned char equal_res;
char *id;
dna_map_region_t* region;
dna_map_region_t* region_trie = NULL;
FILE* ftlog = fopen(log_file, "w");
bam1_t* bam_line = bam_init1();
// Open bam file for read
bamFile bam_fd = bam_open(filename, "r");
bamFile f_unmapped = bam_open(unmapped_bam, "w");
//header for BAM file has been done in the opening
bam_header_t* bam_header_p = bam_header_read(bam_fd);
bam_header_write(f_unmapped, bam_header_p);
// Create new region
region = (dna_map_region_t *) calloc(1, sizeof(dna_map_region_t));
while ((read_bytes = bam_read1(bam_fd, bam_line)) > 0) {
id = (char*) calloc(bam_line->core.l_qname, sizeof(char));
strcpy(id, bam1_qname(bam_line));
if (bam_line->core.flag == 4) {
if (align_bam == 1) {
if (equal_res) {
result->right_mapped++;
region_trie->rwmapped = 1;
} else {
printf("entra");
result->wrong_mapped++;
fprintf(ftlog, "Query: %s\n", id);
fprintf(ftlog, "Case 1: ");
dna_fprint_region(ftlog, region_trie);
fprintf(ftlog, "Case 2: ");
dna_fprint_region(ftlog, region);
}
result->mapped++;
} else {
result->not_mapped++;
}
} else if ((region_trie = cp_trie_exact_match(trie, bam1_qname(bam_line))) == NULL) {
result->not_mapped++;
bam_write1(f_unmapped, bam_line);
} else {
result->mapped++;
region_trie->mapped = 1;
int tid = bam_line->core.tid;
region->chromosome = (char*) calloc(strlen(bam_header_p->target_name[tid]) + 1, sizeof(char));
strcpy(region->chromosome, bam_header_p->target_name[tid]);
// Add start position info
region->start_position = bam_line->core.pos;
// Add end position info
region->end_position = (int) bam_calend(&bam_line->core, bam1_cigar(bam_line)) ;
// region->end_position = region->start_position + (int) bam_cigar2qlen(&bam_line->core, bam1_cigar(bam_line)) -1;
//printf("trie: %d - %d\n", region_trie->start_position, region_trie->end_position);
//printf("bam: %d - %d\n\n", region->start_position, region->end_position);
// Add strand info
uint32_t flag = (uint32_t) bam_line->core.flag;
region->strand = (flag & BAM_FREVERSE) ? 1 : 0;
equal_res = (soft_hard == 0) ? dna_map_region_equal_soft(region_trie, region) : dna_map_region_equal_hard(region_trie, region);
if (equal_res) {
result->right_mapped++;
region_trie->rwmapped = 1;
} else {
result->wrong_mapped++;
fprintf(ftlog, "Query: %s\n", id);
fprintf(ftlog, "Case 1: ");
dna_fprint_region(ftlog, region_trie);
fprintf(ftlog, "Case 2: ");
dna_fprint_region(ftlog, region);
}
free(region->chromosome);
}
free(id);
}
result->filename = (char*) calloc(strlen(filename) + 1, sizeof(char));
strcpy(result->filename, filename);
// Free Memory
free(region);
// free(region_trie);
bam_close(bam_fd);
bam_close(f_unmapped);
bam_destroy1(bam_line);
fclose(ftlog);
}
void print_result(trie_result_t *result, int log) {
double total = result->mapped + (double)result->not_mapped;
if (log == 0) {
printf("File: %s\n", result->filename);
printf("Mapped: %d\n", result->mapped);
printf("\tRight mapped: %d\n", (result->right_mapped));
printf("\tWrong mapped: %d\n", (result->wrong_mapped));
printf("Not mapped: %d \n\n", result->not_mapped);
} else {
printf("%s\t%d\t%lf\t%d\t%lf\t%d\t%lf\t%d\t%lf\n", result->filename,
(result->mapped),
(result->mapped / total),
(result->not_mapped),
(result->not_mapped / total),
(result->right_mapped),
(result->right_mapped / total),
(result->wrong_mapped),
(result->wrong_mapped / total)
);
}
}