-
Notifications
You must be signed in to change notification settings - Fork 9
/
Bitmapper_main.cpp
403 lines (284 loc) · 9.37 KB
/
Bitmapper_main.cpp
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
Authors:
Haoyu Cheng
chhy@mail.ustc.edu.cn
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "Process_CommandLines.h"
#include "Auxiliary.h"
#include "Process_Reads.h"
#include "Process_sam_out.h"
#include "Index.h"
#include "Schema.h"
#include "bam_prase.h"
#include "Levenshtein_Cal.h"
int main(int argc, char *argv[])
{
_rg_name_l *chhy_ih_refGenName;//存储各条染色体的信息
bitmapper_bs_iter refChromeCont;//染色体条数
pthread_t *_r_threads;
int read_format;
if (!CommandLine_process(argc, argv))
return 1;
if(is_index)
{
createIndex(fileName[0], fileName[1]);
}
else if (is_search)
{
fprintf(stderr,"Read qualities are encoded by Phred+%d...\n", Q_base);
fprintf(stderr,"GapOpenPenalty: %d, GapExtensionPenalty: %d, MistMatchPenaltyMax: %d\n",
GapOpenPenalty, GapExtensionPenalty, MistMatchPenaltyMax);
fprintf(stderr,"MistMatchPenaltyMin: %d, N_Penalty: %d\n",
MistMatchPenaltyMin, N_Penalty);
double totalLoadingTime = 0;
double totalMappingTime = 0;
double startTime;
double loadingTime;
double mappingTime;
char outputFileName[NAME_LENGTH];
startTime = Get_T();
///这就是打开read文件
if (!initiReadAllReads(Read_File1, Read_File2, is_pairedEnd, &read_format))
{
fprintf(stderr, "Cannot open read files. \n");
return 1;
}
if(Mapped_File)
{
sprintf(outputFileName, "%s%s",Mapped_FilePath , Mapped_File);
}
else
{
outputFileName[0] = '\0';
}
if (!is_pairedEnd)
{
if (!Start_Load_Index(fileName[1]))
{
fprintf(stderr, "Load hash table index failed!\n");
return 1;
}
mappingTime = 0;
loadingTime = 0;
fprintf(stderr,"Start load hash table!\n");
Load_Index(thread_e, &chhy_ih_refGenName, &refChromeCont, fileName[1]);
totalLoadingTime += Get_T()-startTime;
fprintf(stderr, "Start alignment!\n");
if (output_methy == 0)
{
if (bam_output == 0)
{
Output_gene(outputFileName);
OutPutSAM_Nounheader(chhy_ih_refGenName, refChromeCont, argc, argv);
}
else
{
init_bam_header(outputFileName, chhy_ih_refGenName, refChromeCont, argc, argv);
}
}
Prepare_alignment(outputFileName, fileName[0], chhy_ih_refGenName, refChromeCont, read_format, is_pairedEnd);
startTime = Get_T();
if (THREAD_COUNT == 1)
{
if (pbat == 0)
{
Map_Single_Seq(0);
///Map_Single_Seq_end_to_end_cover(0);
}
else
{
Map_Single_Seq_pbat(0);
}
}
else
{
if (pbat == 0)
{
Map_Single_Seq_muti_thread(0);
}
else
{
Map_Single_Seq_pbat_muti_thread(0);
}
}
totalMappingTime += Get_T()-startTime;
}
else
{
if (output_methy == 1 && methylation_size % 2 != 0)
{
fprintf(stderr, "The variable 'methylation_size' must be an even! Please change it to be even!\n");
exit(1);
}
///_r_fp1
if (pbat == 1)
{
exchange_two_reads();
}
minDistance_pair = minDistance_pair - SEQ_LENGTH;
maxDistance_pair = maxDistance_pair - SEQ_LENGTH;
if (!Start_Load_Index(fileName[1]))
{
fprintf(stderr, "Load hash table index failed!\n");
return 1;
}
mappingTime = 0;
loadingTime = 0;
//loadHashTable = &Load_Index;
fprintf(stderr,"Start load hash table!\n");
// loadHashTable = &Load_Index;
Load_Index(thread_e, &chhy_ih_refGenName, &refChromeCont, fileName[1]);
totalLoadingTime += Get_T()-startTime;
if (is_local == 1)
{
fprintf(stderr, "Start alignment in default fast mode.\n");
}
else
{
fprintf(stderr, "Start alignment in sensitive mode.\n");
}
if (output_methy == 0)
{
if (bam_output == 0)
{
Output_gene(outputFileName);
OutPutSAM_Nounheader(chhy_ih_refGenName, refChromeCont, argc, argv);
}
else
{
init_bam_header(outputFileName, chhy_ih_refGenName, refChromeCont, argc, argv);
}
}
startTime = Get_T();
Prepare_alignment(outputFileName, fileName[0], chhy_ih_refGenName, refChromeCont, read_format, is_pairedEnd);
if(THREAD_COUNT==1)
{
Map_Pair_Seq(0);
}
else
{
Map_Pair_Seq_muti_thread(0);
}
if (output_methy == 1)
{
///out_paired_distance_statistic();
}
totalMappingTime += Get_T()-startTime;
}
if (bam_output == 1)
{
close_bam_file();
}
///这个一打开速度就奇慢,不知道为什么
///finalizeOutput();
fprintf(stderr, "-----------------------------------------------------------------------------------------------------------\n");
fprintf(stderr, "%19s%16.2f%18.2f\n\n", "Total:",totalLoadingTime, totalMappingTime);
fprintf(stderr, "%-42s%10.2f\n","Total Time:", totalMappingTime+totalLoadingTime);
long long number_of_read;
long long number_of_unique_mapped_read;
long long number_of_ambiguous_mapped_read;
long long number_of_unmapped_read;
long long number_of_mapped_bases;
long long number_of_mapped_errors;
get_mapping_informations(&number_of_read, &number_of_unique_mapped_read, &number_of_ambiguous_mapped_read,
&number_of_unmapped_read, &number_of_mapped_bases, &number_of_mapped_errors);
fprintf(stderr, "%-48s%lld\n", "No. of Reads:", number_of_read);
fprintf(stderr, "%-48s%lld (%0.2f\%)\n", "No. of Unique Mapped Reads:",
number_of_unique_mapped_read, ((double)number_of_unique_mapped_read / (double)number_of_read)*100);
fprintf(stderr, "%-48s%lld (%0.2f\%)\n", "No. of Ambiguous Mapped Reads:",
number_of_ambiguous_mapped_read, ((double)number_of_ambiguous_mapped_read / (double)number_of_read) * 100);
fprintf(stderr, "%-48s%lld (%0.2f\%)\n", "No. of Unmapped Reads:",
number_of_unmapped_read, ((double)number_of_unmapped_read / (double)number_of_read) * 100);
fprintf(stderr, "%-47s %0.2f\%\n", "Mismatch and Indel Rate:",
((double)number_of_mapped_errors / (double)number_of_mapped_bases) * 100);
if (mapstats == 1)
{
sprintf(Mapstats_File_Path + strlen(Mapstats_File_Path), "%s", Mapstats_File);
fprintf(stderr, "The statistical information will be written to %s ...\n", Mapstats_File_Path);
FILE* mapstats_fp = fopen(Mapstats_File_Path, "w");
if (mapstats_fp != NULL)
{
fprintf(mapstats_fp, "%-48s%lld\n", "No. of Reads:", number_of_read);
fprintf(mapstats_fp, "%-48s%lld (%0.2f\%)\n", "No. of Unique Mapped Reads:",
number_of_unique_mapped_read, ((double)number_of_unique_mapped_read / (double)number_of_read) * 100);
fprintf(mapstats_fp, "%-48s%lld (%0.2f\%)\n", "No. of Ambiguous Mapped Reads:",
number_of_ambiguous_mapped_read, ((double)number_of_ambiguous_mapped_read / (double)number_of_read) * 100);
fprintf(mapstats_fp, "%-48s%lld (%0.2f\%)\n", "No. of Unmapped Reads:",
number_of_unmapped_read, ((double)number_of_unmapped_read / (double)number_of_read) * 100);
fprintf(mapstats_fp, "%-47s %0.2f\%\n", "Mismatch and Indel Rate:",
((double)number_of_mapped_errors / (double)number_of_mapped_bases) * 100);
fclose(mapstats_fp);
}
}
}
else if (is_methy)
{
double total_time = 0;
double startTime = Get_T();
fprintf(stderr, "minVariantDepth: %d\n", minVariantDepth);
fprintf(stderr, "maxVariantFrac: %f\n", maxVariantFrac);
sprintf(fileName[1], "%s.methy", fileName[1]);
if (!Load_Methy_Index(thread_e, &chhy_ih_refGenName, &refChromeCont, fileName[1]))
{
fprintf(stderr, "Load hash table index failed!\n");
return 1;
}
///need_context[0]是CpG
///need_context[1]是CHG
///need_context[2]是CHH
///int need_context[3] = { 1, 0, 0 };
int need_context[3];
need_context[0] = CpG;
need_context[1] = CHG;
need_context[2] = CHH;
get_genome_cuts(Read_File1);
fprintf(stderr, "genome_cuts: %d\n", genome_cuts);
fprintf(stderr, "PE_distance: %d\n", maxDistance_pair);
char outputFileName[NAME_LENGTH];
if(Mapped_File)
{
sprintf(outputFileName, "%s%s",Mapped_FilePath , Mapped_File);
}
else
{
sprintf(outputFileName, "output");
}
///init_output_methy(Read_File1, need_context);
init_output_methy(outputFileName, need_context);
Prepare_methy(fileName[0], chhy_ih_refGenName, refChromeCont);
THREAD_COUNT = 1;
///int PE_distance = 500;
if (!is_pairedEnd)
{
fprintf(stderr, "Extract from single-end alignment ...\n");
if (THREAD_COUNT == 1)
{
methy_extract(0, Read_File1, need_context);
}
else
{
methy_extract_mutiple_thread(0, Read_File1, need_context);
}
}
else
{
fprintf(stderr, "Extract from paired-end alignment ...\n");
if (THREAD_COUNT == 1)
{
methy_extract_PE(0, Read_File1, maxDistance_pair, need_context);
}
else
{
methy_extract_PE_mutiple_thread(0, Read_File1, maxDistance_pair, need_context);
}
}
total_time += Get_T() - startTime;
fprintf(stderr, "-----------------------------------------------------------------------------------------------------------\n");
fprintf(stderr, "%19s%16.2f\n\n", "Total:", total_time);
}
return 0;
}