From 4ac9907e4963e5274f3a210ba2b1f7bb3af20d4d Mon Sep 17 00:00:00 2001 From: Haoyu Cheng Date: Thu, 26 Dec 2019 02:26:24 -0500 Subject: [PATCH] clean code --- Assembly.cpp | 1998 ++--------- Assembly.h | 7 +- CommandLines.cpp | 239 +- CommandLines.h | 44 +- Correct.cpp | 7272 +++----------------------------------- Correct.h | 322 +- Hash_Table.cpp | 2651 +------------- Hash_Table.h | 228 +- Levenshtein_distance.cpp | 805 ----- Levenshtein_distance.h | 292 +- Makefile | 13 +- Output.cpp | 9 +- Overlaps.cpp | 3247 ++--------------- Overlaps.h | 45 +- POA.cpp | 592 +--- POA.h | 386 +- Process_Read.cpp | 275 +- Process_Read.h | 29 +- debug_assembly.sh | 17 - edlib.cpp | 1461 -------- edlib.h | 258 -- khash.h | 15 +- kmer.h | 7 +- ksw2.h | 177 - ksw2_extz2_sse.cpp | 305 -- main.cpp | 216 +- 26 files changed, 1585 insertions(+), 19325 deletions(-) delete mode 100644 debug_assembly.sh delete mode 100644 edlib.cpp delete mode 100644 edlib.h delete mode 100644 ksw2.h delete mode 100644 ksw2_extz2_sse.cpp diff --git a/Assembly.cpp b/Assembly.cpp index 2872496..43529c0 100644 --- a/Assembly.cpp +++ b/Assembly.cpp @@ -13,27 +13,10 @@ Total_Count_Table TCB; Total_Pos_Table PCB; All_reads R_INF; -Assembly_Graph assembly; - pthread_mutex_t statistics; -long long total_matched_overlap_0 = 0; -long long total_matched_overlap_1 = 0; -long long total_potiental_matched_overlap_0 = 0; -long long total_potiental_matched_overlap_1 = 0; -long long total_num_read_base = 0; -long long total_num_correct_base = 0; -long long total_second_num_correct_base = 0; -int roundID = 0; - - -long long complete_threads = 0; - - void* Perform_Counting(void* arg) { - int thr_ID = *((int*)arg); - int i = 0; HPC_seq HPC_read; @@ -65,7 +48,6 @@ void* Perform_Counting(void* arg) for (i = 0; i < curr_sub_block.num; i++) { - ///forward strand init_HPC_seq(&HPC_read, curr_sub_block.read[i].seq.s, curr_sub_block.read[i].seq.l); init_Hash_code(&k_code); @@ -76,93 +58,43 @@ void* Perform_Counting(void* arg) { if(code < 4) { - k_mer_append(&k_code,code,k_mer_length); + k_mer_append(&k_code,code, asm_opt.k_mer_length); avalible_k++; - if (avalible_k>=k_mer_length) + if (avalible_k >= asm_opt.k_mer_length) { - ///插入 - if(insert_Total_Count_Table(&TCB, &k_code, k_mer_length)) + if(insert_Total_Count_Table(&TCB, &k_code, asm_opt.k_mer_length)) { select_k_mer_number++; } k_mer_number++; - } - } else { avalible_k = 0; init_Hash_code(&k_code); } - } - - - /** - ///reverse complement strand - reverse_complement(curr_sub_block.read[i].seq.s, curr_sub_block.read[i].seq.l); - init_HPC_seq(&HPC_read, curr_sub_block.read[i].seq.s, curr_sub_block.read[i].seq.l); - init_Hash_code(&k_code); - - avalible_k = 0; - - while ((code = get_HPC_code(&HPC_read, &end_pos)) != 6) - { - if(code < 4) - { - k_mer_append(&k_code,code,k_mer_length); - avalible_k++; - if (avalible_k>=k_mer_length) - { - ///插入 - if(insert_Total_Count_Table(&TCB, &k_code, k_mer_length)) - { - select_k_mer_number++; - } - - k_mer_number++; - - } - - } - else - { - avalible_k = 0; - init_Hash_code(&k_code); - } - - } - **/ - - } - - } destory_R_buffer_block(&curr_sub_block); - ///free(arg); + free(arg); + return NULL; } - - void* Perform_Counting_non_first(void* arg) { int thr_ID = *((int*)arg); - int i = 0; + uint64_t i = 0; HPC_seq HPC_read; - - long long read_number = 0; long long select_k_mer_number = 0 ; long long k_mer_number = 0 ; - int file_flag = 1; - uint64_t code; uint64_t end_pos; @@ -174,7 +106,7 @@ void* Perform_Counting_non_first(void* arg) UC_Read g_read; init_UC_Read(&g_read); - for (i = thr_ID; i < R_INF.total_reads; i = i + thread_num) + for (i = thr_ID; i < R_INF.total_reads; i = i + asm_opt.thread_num) { recover_UC_Read(&g_read, &R_INF, i); ///forward strand @@ -187,44 +119,37 @@ void* Perform_Counting_non_first(void* arg) { if(code < 4) { - k_mer_append(&k_code,code,k_mer_length); + k_mer_append(&k_code,code, asm_opt.k_mer_length); avalible_k++; - if (avalible_k>=k_mer_length) + if (avalible_k >= asm_opt.k_mer_length) { - ///插入 - if(insert_Total_Count_Table(&TCB, &k_code, k_mer_length)) + if(insert_Total_Count_Table(&TCB, &k_code, asm_opt.k_mer_length)) { select_k_mer_number++; } - k_mer_number++; - } - } else { avalible_k = 0; init_Hash_code(&k_code); } - } } - + destory_UC_Read(&g_read); + free(arg); + + return NULL; } - - void* Build_hash_table_non_first(void* arg) { int thr_ID = *((int*)arg); - int i = 0; + uint64_t i = 0; HPC_seq HPC_read; - - int file_flag = 1; - uint64_t code; uint64_t end_pos; @@ -237,7 +162,7 @@ void* Build_hash_table_non_first(void* arg) UC_Read g_read; init_UC_Read(&g_read); - for (i = thr_ID; i < R_INF.total_reads; i = i + thread_num) + for (i = thr_ID; i < R_INF.total_reads; i = i + asm_opt.thread_num) { recover_UC_Read(&g_read, &R_INF, i); ///forward strand @@ -252,43 +177,34 @@ void* Build_hash_table_non_first(void* arg) { if(code < 4) { - k_mer_append(&k_code,code,k_mer_length); + k_mer_append(&k_code,code, asm_opt.k_mer_length); avalible_k++; - if (avalible_k>=k_mer_length) + if (avalible_k >= asm_opt.k_mer_length) { - - ///选取的k-mer满足两个要求 - ///1. hash(k-mer) % 101 <= 3 - ///2. occ(k-mer)要满足范围 - ///TCB表中的元素仅满足第一个要求,而PCB表中的元素满足两个要求 - ///所以如果当前k-mer在PCB表中存在,则他的位置一定要加入到候选位置中去 - ///insert_Total_Pos_Table(&PCB, &k_code, k_mer_length, curr_sub_block.read[i].ID, HPC_base - k_mer_length + 1, FORWARD); - insert_Total_Pos_Table(&PCB, &k_code, k_mer_length, - i, end_pos); + ///there are two requirements + ///1. hash(k-mer) + ///2. occ(k-mer) + ///TCB just meet the first requirement,while PCB needs to meet both of them + insert_Total_Pos_Table(&PCB, &k_code, asm_opt.k_mer_length, i, end_pos); } - } else { avalible_k = 0; init_Hash_code(&k_code); } - - ///HPC_base++; } } destory_UC_Read(&g_read); + free(arg); + + return NULL; } - - - - void* Build_hash_table(void* arg) { - int thr_ID = *((int*)arg); int i = 0; HPC_seq HPC_read; @@ -330,34 +246,24 @@ void* Build_hash_table(void* arg) { if(code < 4) { - k_mer_append(&k_code,code,k_mer_length); + k_mer_append(&k_code,code, asm_opt.k_mer_length); avalible_k++; - if (avalible_k>=k_mer_length) + if (avalible_k >= asm_opt.k_mer_length) { - - ///选取的k-mer满足两个要求 - ///1. hash(k-mer) % 101 <= 3 - ///2. occ(k-mer)要满足范围 - ///TCB表中的元素仅满足第一个要求,而PCB表中的元素满足两个要求 - ///所以如果当前k-mer在PCB表中存在,则他的位置一定要加入到候选位置中去 - ///insert_Total_Pos_Table(&PCB, &k_code, k_mer_length, curr_sub_block.read[i].ID, HPC_base - k_mer_length + 1, FORWARD); - insert_Total_Pos_Table(&PCB, &k_code, k_mer_length, - curr_sub_block.read[i].ID, end_pos); + ///there are two requirements + ///1. hash(k-mer) + ///2. occ(k-mer) + ///TCB just meet the first requirement,while PCB needs to meet both of them + insert_Total_Pos_Table(&PCB, &k_code, asm_opt.k_mer_length, curr_sub_block.read[i].ID, end_pos); } - } else { avalible_k = 0; init_Hash_code(&k_code); } - - ///HPC_base++; } - - - ///load read compress_base(Get_READ(R_INF, curr_sub_block.read[i].ID), curr_sub_block.read[i].seq.s, curr_sub_block.read[i].seq.l, @@ -365,102 +271,50 @@ void* Build_hash_table(void* arg) memcpy(R_INF.name+R_INF.name_index[curr_sub_block.read[i].ID], curr_sub_block.read[i].name.s, curr_sub_block.read[i].name.l); - - - /** - ///reverse complement strand - reverse_complement(curr_sub_block.read[i].seq.s, curr_sub_block.read[i].seq.l); - - init_HPC_seq(&HPC_read, curr_sub_block.read[i].seq.s, curr_sub_block.read[i].seq.l); - init_Hash_code(&k_code); - - avalible_k = 0; - - HPC_base = 0; - - while ((code = get_HPC_code(&HPC_read, &end_pos)) != 6) - { - if(code < 4) - { - k_mer_append(&k_code,code,k_mer_length); - avalible_k++; - if (avalible_k>=k_mer_length) - { - - ///选取的k-mer满足两个要求 - ///1. hash(k-mer) % 101 <= 3 - ///2. occ(k-mer)要满足范围 - ///TCB表中的元素仅满足第一个要求,而PCB表中的元素满足两个要求 - ///所以如果当前k-mer在PCB表中存在,则他的位置一定要加入到候选位置中去 - insert_Total_Pos_Table(&PCB, &k_code, k_mer_length, - curr_sub_block.read[i].ID, HPC_base - k_mer_length + 1, REVERSE_COMPLEMENT); - } - - } - else - { - avalible_k = 0; - init_Hash_code(&k_code); - } - - HPC_base++; - } - **/ - } - - } destory_R_buffer_block(&curr_sub_block); - ///free(arg); - - + free(arg); + return NULL; } - - - - - - - void Counting_multiple_thr() { double start_time = Get_T(); - fprintf(stdout, "Begin Counting ...... \n"); + fprintf(stderr, "Begin Counting... \n"); - init_Total_Count_Table(k_mer_length, &TCB); + init_Total_Count_Table(asm_opt.k_mer_length, &TCB); pthread_t inputReadsHandle; int *is_insert = (int*)malloc(sizeof(*is_insert)); *is_insert = 1; - if (roundID == 0) + if (asm_opt.roundID == 0) { - init_kseq(read_file_name); + init_kseq(asm_opt.read_file_name); init_All_reads(&R_INF); - init_R_buffer(thread_num); + init_R_buffer(asm_opt.thread_num); pthread_create(&inputReadsHandle, NULL, input_reads_muti_threads, (void*)is_insert); } pthread_t *_r_threads; - _r_threads = (pthread_t *)malloc(sizeof(pthread_t)*thread_num); + _r_threads = (pthread_t *)malloc(sizeof(pthread_t)*asm_opt.thread_num); int i = 0; - for (i = 0; i < thread_num; i++) + for (i = 0; i < asm_opt.thread_num; i++) { int *arg = (int*)malloc(sizeof(*arg)); *arg = i; - if (roundID == 0) + if (asm_opt.roundID == 0) { pthread_create(_r_threads + i, NULL, Perform_Counting, (void*)arg); } @@ -471,10 +325,7 @@ void Counting_multiple_thr() } - - - - for (i = 0; i=k_mer_length) - { - list_length = locate_Total_Pos_Table(&PCB, &k_code, &list, k_mer_length, &sub_ID); - merge_Candidates_list(&l, list, list_length, end_pos, 0); - //merge_Candidates_list_version(&l, list, list_length, end_pos, 0); - } - } - else - { - avalible_k = 0; - init_Hash_code(&k_code); - } - - ///HPC_base++; - } - - - - ///reverse complement strand - reverse_complement(g_read.seq, g_read.length); - init_HPC_seq(&HPC_read, g_read.seq, g_read.length); - init_Hash_code(&k_code); - avalible_k = 0; - - while ((code = get_HPC_code(&HPC_read, &end_pos)) != 6) - { - if(code < 4) - { - k_mer_append(&k_code,code,k_mer_length); - avalible_k++; - if (avalible_k>=k_mer_length) - { - list_length = locate_Total_Pos_Table(&PCB, &k_code, &list, k_mer_length, &sub_ID); - merge_Candidates_list(&l, list, list_length, end_pos, 1); - //merge_Candidates_list_version(&l, list, list_length, end_pos, 1); - } - } - else - { - avalible_k = 0; - init_Hash_code(&k_code); - } - - ///HPC_base++; - } - - } - - destory_Candidates_list(&l); -} - - - - -void debug_merge_result(Candidates_list* x, Candidates_list* y) -{ - uint64_t i; - if (x->length != y->length) - { - fprintf(stderr, "ERROR: different list\n"); - fprintf(stderr, "x->length: %llu, y->length: %llu\n", x->length, y->length); - } - - for (i = 0; i < x->length; i++) - { - if (x->list[i].offset != y->list[i].offset - || - x->list[i].readID != y->list[i].readID - || - x->list[i].self_offset != y->list[i].self_offset - || - x->list[i].strand != y->list[i].strand - ) - { - fprintf(stderr, "ERROR: different pos\n"); - } - - } - -} - - - - - - -void* Overlap_calculate(void* arg) -{ - - int thr_ID = *((int*)arg); - - long long i = 0; - int avalible_k = 0; - - UC_Read g_read; - init_UC_Read(&g_read); - HPC_seq HPC_read; - Hash_code k_code; - uint64_t code; - uint64_t end_pos; - k_mer_pos* list; - uint64_t list_length; - uint64_t sub_ID; - - Candidates_list l; - //Candidates_list debug_l; - - init_Candidates_list(&l); - //init_Candidates_list(&debug_l); - - k_mer_pos_list_alloc array_list; - init_k_mer_pos_list_alloc(&array_list); - - - for (i = thr_ID; i < R_INF.total_reads; i = i + thread_num) - ///for (i = thr_ID; i < R_INF.total_reads/50; i = i + thread_num) - { - /** - if (i % 1000 == 0) - { - fprintf(stderr, "i: %llu\n", i); - } - **/ - - - - clear_Candidates_list(&l); - ///clear_Candidates_list(&debug_l); - - clear_k_mer_pos_list_alloc(&array_list); - recover_UC_Read(&g_read, &R_INF, i); - - - ///forward strand - init_HPC_seq(&HPC_read, g_read.seq, g_read.length); - init_Hash_code(&k_code); - avalible_k = 0; - - while ((code = get_HPC_code(&HPC_read, &end_pos)) != 6) - { - if(code < 4) - { - k_mer_append(&k_code,code,k_mer_length); - avalible_k++; - if (avalible_k>=k_mer_length) - { - list_length = locate_Total_Pos_Table(&PCB, &k_code, &list, k_mer_length, &sub_ID); - - if (list_length != 0) - { - append_k_mer_pos_list_alloc(&array_list, list, list_length, end_pos, 0); - } - ///merge_Candidates_list(&l, list, list_length, end_pos, 0); - ///merge_Candidates_list_version(&debug_l, list, list_length, end_pos, 0); - } - } - else - { - avalible_k = 0; - init_Hash_code(&k_code); - } - - ///HPC_base++; - } - - - - ///reverse complement strand - reverse_complement(g_read.seq, g_read.length); - init_HPC_seq(&HPC_read, g_read.seq, g_read.length); - init_Hash_code(&k_code); - avalible_k = 0; - - while ((code = get_HPC_code(&HPC_read, &end_pos)) != 6) - { - if(code < 4) - { - k_mer_append(&k_code,code,k_mer_length); - avalible_k++; - if (avalible_k>=k_mer_length) - { - list_length = locate_Total_Pos_Table(&PCB, &k_code, &list, k_mer_length, &sub_ID); - if (list_length != 0) - { - append_k_mer_pos_list_alloc(&array_list, list, list_length, end_pos, 1); - } - ///merge_Candidates_list(&l, list, list_length, end_pos, 1); - ///merge_Candidates_list_version(&debug_l, list, list_length, end_pos, 1); - } - } - else - { - avalible_k = 0; - init_Hash_code(&k_code); - } - - ///HPC_base++; - } - - merge_k_mer_pos_list_alloc(&array_list, &l); - - ///debug_merge_result(&l, &debug_l); - - } - - destory_Candidates_list(&l); - ///destory_Candidates_list(&debug_l); - - - destory_k_mer_pos_list_alloc(&array_list); -} - -inline void output_read_to_buffer(long long readID, All_reads* R_INF, char* corrected_read, long long correct_read_length, -Output_buffer_sub_block* current_sub_buffer) -{ - /** - if (seq->name.l - != Get_NAME_LENGTH(R_INF, read_number)) - { - fprintf(stderr, "name error\n"); - } - - - if(memcmp(seq->name.s, Get_NAME(R_INF, read_number), seq->name.l)) - { - fprintf(stderr, "name error\n"); - } - **/ - - - ///先清空 - current_sub_buffer->length = 0; - - ///一个是>一个是\n - add_base_to_sub_buffer(current_sub_buffer, '>'); - add_segment_to_sub_buffer(current_sub_buffer, Get_NAME((*R_INF), readID), Get_NAME_LENGTH((*R_INF), readID)); - add_base_to_sub_buffer(current_sub_buffer, '\n'); - add_segment_to_sub_buffer(current_sub_buffer, corrected_read, correct_read_length); - add_base_to_sub_buffer(current_sub_buffer, '\n'); - push_results_to_buffer(current_sub_buffer); - } @@ -900,31 +440,25 @@ char* new_read, int* new_length) int diff_char_i = 0; - for (i = 0; i < cigar->length; i++) + for (i = 0; i < (long long)cigar->length; i++) { operation = Get_Cigar_Type(cigar->record[i]); operation_length = Get_Cigar_Length(cigar->record[i]); if (operation == 0) { - ///fprintf(stderr, "0 new_i: %d\n", new_i); memcpy(new_read + new_i, pre_read + pre_i, operation_length); pre_i = pre_i + operation_length; new_i = new_i + operation_length; - ///fprintf(stderr, "0 new_i: %d\n", new_i); } else if (operation == 1) { for (j = 0; j < operation_length; j++) { - // fprintf(stderr, "1 new_i: %d, diff_char_i: %d, lost_base_length: %d, lost_base: %d\n", - // new_i, diff_char_i, cigar->lost_base_length, cigar->lost_base[diff_char_i]); - new_read[new_i] = Get_MisMatch_Base(cigar->lost_base[diff_char_i]); new_i++; diff_char_i++; - ///fprintf(stderr, "1 new_i: %d\n", new_i); } pre_i = pre_i + operation_length; } @@ -935,23 +469,14 @@ char* new_read, int* new_length) } else if (operation == 2) { - ///fprintf(stderr, "2 new_i: %d\n", new_i); memcpy(new_read + new_i, cigar->lost_base + diff_char_i, operation_length); new_i = new_i + operation_length; diff_char_i = diff_char_i + operation_length; - ///fprintf(stderr, "2 new_i: %d\n", new_i); } } *new_length = new_i; - - // if(pre_i != pre_length) - // { - // fprintf(stderr, "error\n"); - // } - ///0xffffffff; } - void get_uncorrected_read_from_cigar(Cigar_record* cigar, char* new_read, int new_length, char* pre_read, int* pre_length) { int i, j; @@ -961,7 +486,7 @@ void get_uncorrected_read_from_cigar(Cigar_record* cigar, char* new_read, int ne int diff_char_i = 0; - for (i = 0; i < cigar->length; i++) + for (i = 0; i < (long long)cigar->length; i++) { operation = Get_Cigar_Type(cigar->record[i]); operation_length = Get_Cigar_Length(cigar->record[i]); @@ -1000,19 +525,17 @@ void get_uncorrected_read_from_cigar(Cigar_record* cigar, char* new_read, int ne *pre_length = pre_i; } - inline int get_cigar_errors(Cigar_record* cigar) { int i; int total_errors = 0; - for (i = 0; i < cigar->length; i++) + for (i = 0; i < (long long)cigar->length; i++) { if (Get_Cigar_Type(cigar->record[i]) > 0) { total_errors = total_errors + Get_Cigar_Length(cigar->record[i]); } } - return total_errors; } @@ -1021,7 +544,7 @@ char* new_read, int new_length, int correct_base) { int i; int total_errors = 0; - for (i = 0; i < cigar->length; i++) + for (i = 0; i < (long long)cigar->length; i++) { if (Get_Cigar_Type(cigar->record[i]) > 0) { @@ -1041,7 +564,7 @@ char* new_read, int new_length, int correct_base) pre_i = new_i = 0; - for (i = 0; i < cigar->length; i++) + for (i = 0; i < (long long)cigar->length; i++) { operation = Get_Cigar_Type(cigar->record[i]); operation_length = Get_Cigar_Length(cigar->record[i]); @@ -1121,17 +644,13 @@ char* new_read, int new_length, int correct_base) free(tmp_seq); - if(cigar->new_read_length != new_length) + if((int)cigar->new_read_length != new_length) { fprintf(stderr, "cigar->new_read_length: %d, new_length: %d\n", cigar->new_read_length, new_length); } - - - } - inline void push_cigar(Compressed_Cigar_record* records, long long ID, Cigar_record* input) { @@ -1152,8 +671,6 @@ inline void push_cigar(Compressed_Cigar_record* records, long long ID, Cigar_rec memcpy(records[ID].lost_base, input->lost_base, input->lost_base_length); records[ID].new_length = input->new_read_length; - - } @@ -1163,7 +680,7 @@ All_reads* R_INF, int if_reverse) long long i = 0, xLen, yLen; ma_hit_t tmp; clear_ma_hit_t_alloc(paf); - for (i = 0; i < overlap_list->length; i++) + for (i = 0; i < (long long)overlap_list->length; i++) { if (overlap_list->list[i].is_match == flag) { @@ -1204,27 +721,6 @@ All_reads* R_INF, int if_reverse) } -int check_weak_overlap(ma_hit_t_alloc* reverse_paf_list, -overlap_region_alloc* overlap_list, long long weakID) -{ - long long i = 0; - long long strongID, index; - for (i = 0; i < overlap_list->length; i++) - { - ///if this is a matched strong overlap - if (overlap_list->list[i].is_match == 1 && overlap_list->list[i].strong == 1) - { - strongID = overlap_list->list[i].y_id; - index = get_specific_overlap(&(reverse_paf_list[strongID]), strongID, weakID); - if(index != -1) - { - return 0; - } - } - } - - return 1; -} int if_exact_match(char* x, long long xLen, char* y, long long yLen, long long xBeg, long long xEnd, long long yBeg, long long yEnd) @@ -1259,9 +755,8 @@ overlap_region_alloc* overlap_list, int flag) long long available_overlaps = 0; ma_hit_t tmp; clear_ma_hit_t_alloc(paf); - for (i = 0; i < overlap_list->length; i++) + for (i = 0; i < (long long)overlap_list->length; i++) { - ///if (overlap_list->list[i].is_match == 1) if (overlap_list->list[i].is_match == flag) { available_overlaps++; @@ -1312,428 +807,6 @@ overlap_region_alloc* overlap_list, int flag) } -int fix_overlap_region_by_cigar(long long* r_beg, long long* r_end, Cigar_record* cigar, long long new_read_length, -long long pre_read_length, long long y_ID) -{ - long long pre_r_beg = (*r_beg); - long long pre_r_end = (*r_end); - (*r_beg) = (*r_end) = -1; - long long pre_i, new_i, cigar_i; - pre_i = new_i = cigar_i = 0; - int operation, operationLen; - - for (cigar_i = 0; cigar_i < cigar->length; cigar_i++) - { - operation = Get_Cigar_Type(cigar->record[cigar_i]); - operationLen = Get_Cigar_Length(cigar->record[cigar_i]); - if (operation == 0 || operation == 1) - { - if(pre_r_beg >= pre_i && pre_r_beg < pre_i + operationLen) - { - (*r_beg) = new_i + (pre_r_beg - pre_i); - } - - - if(pre_r_end >= pre_i && pre_r_end < pre_i + operationLen) - { - (*r_end) = new_i + (pre_r_end - pre_i); - } - - if((*r_beg) != -1 && (*r_end) != -1) - { - return 1; - } - - - new_i += operationLen; - pre_i += operationLen; - } - else if (operation == 2) ///2是x缺字符(y多字符) - { - new_i += operationLen; - }///3是y缺字符(x多字符) - else if (operation == 3) - { - - if(pre_r_beg >= pre_i && pre_r_beg < pre_i + operationLen) - { - (*r_beg) = new_i - 1; - if((*r_beg) < 0) - { - (*r_beg) = 0; - } - } - - - if(pre_r_end >= pre_i && pre_r_end < pre_i + operationLen) - { - (*r_end) = new_i; - if((*r_end) >= new_read_length) - { - (*r_end) = new_read_length - 1; - } - } - - if((*r_beg) != -1 && (*r_end) != -1) - { - return 1; - } - - pre_i += operationLen; - } - } - - if(pre_i != pre_read_length) - { - if(memcmp("m64013_190412_043951/172426111/ccs", Get_NAME(R_INF, y_ID), - Get_NAME_LENGTH(R_INF, y_ID)) == 0) - { - fprintf(stderr, "error, pre_i: %d, new_i:%d, pre_read_length: %d, new_read_length:%d\n", - pre_i, new_i, pre_read_length, new_read_length); - for (cigar_i = 0; cigar_i < cigar->length; cigar_i++) - { - operation = Get_Cigar_Type(cigar->record[cigar_i]); - operationLen = Get_Cigar_Length(cigar->record[cigar_i]); - fprintf(stderr, "operation: %d, operationLen: %d\n", operation, operationLen); - } - - } - ///fprintf(stderr, "error, pre_i: %d, pre_read_length: %d\n", pre_i, pre_read_length); - return 0; - } - - ///return; - - if((*r_end) == -1 && pre_r_end >= new_read_length) - { - (*r_end) = new_read_length - 1; - } - - if((*r_beg) == -1 && pre_r_beg >= new_read_length) - { - (*r_beg) = new_read_length - 1; - } - - return 1; - - // if((*r_beg) == -1 || (*r_end) == -1) - // { - // fprintf(stderr, "pre_r_beg: %d, pre_r_end: %d, pre_read_length: %d, new_read_length: %d\n", - // pre_r_beg, pre_r_end, pre_read_length, new_read_length); - // for (cigar_i = 0; cigar_i < cigar->length; cigar_i++) - // { - // operation = Get_Cigar_Type(cigar->record[cigar_i]); - // operationLen = Get_Cigar_Length(cigar->record[cigar_i]); - // fprintf(stderr, "operation: %d, operationLen: %d\n", operation, operationLen); - // } - // } -} - -void convert_kmer(k_v* kv, Hash_code* k_code, uint64_t end_pos) -{ - kv->key.x[0] = k_code->x[0]; - kv->key.x[1] = k_code->x[1]; - kv->value = end_pos; -} - - -void debug_sort_small_hash(small_hash_table* Table) -{ - long long i; - for (i = 1; i < Table->length; i++) - { - if(compare_k_mer(&Table->buffer[i], &Table->buffer[i-1]) < 0) - { - fprintf(stderr, "error\n"); - } - - if(compare_k_mer(&Table->buffer[i], &Table->buffer[i-1]) == 0) - { - if(Table->buffer[i].value < Table->buffer[i-1].value) - { - fprintf(stderr, "error\n"); - } - } - } - - for (i = 0; i < Table->length; i++) - { - long long left, right; - - if(query_small_hash_table(Table, &Table->buffer[i], &left, &right) == 0) - { - fprintf(stderr, "Table->length: %d, i: %d, x[1]: %llu, x[0]: %llu, value: %llu\n", - Table->length, i, - Table->buffer[i].key.x[1], Table->buffer[i].key.x[0], - Table->buffer[i].value); - } - - long long single_count = 0; - long long first_i = -1; - - for (long long j = 0; j < Table->length; j++) - { - if(compare_k_mer(&Table->buffer[i], &Table->buffer[j]) == 0) - { - single_count++; - if(first_i == -1) - { - first_i = j; - } - } - else if(compare_k_mer(&Table->buffer[i], &Table->buffer[j]) < 0) - { - break; - } - - } - - if(left != first_i) - { - fprintf(stderr, "error left\n"); - } - - if(right - left + 1 != single_count) - { - fprintf(stderr, "error right\n"); - } - - } -} - -void get_candidates_from_existing_overlaps(long long readID, UC_Read* g_read, UC_Read* overlap_read, -overlap_region_alloc* overlap_list, k_mer_pos_list_alloc* array_list, -HeapSq* heap, Candidates_list* l, small_hash_table* forward, small_hash_table* reverse) -{ - HPC_seq HPC_read; - Hash_code k_code; - long long avalible_k; - uint64_t code; - uint64_t end_pos; - k_mer_pos* list; - uint64_t list_length; - uint64_t sub_ID; - k_v k_mer_kv; - - clear_Heap(heap); - clear_Candidates_list(l); - - clear_k_mer_pos_list_alloc(array_list); - clear_overlap_region_alloc(overlap_list); - - clear_small_hash_table(forward); - clear_small_hash_table(reverse); - - recover_UC_Read(g_read, &R_INF, readID); - - ///forward strand - init_HPC_seq(&HPC_read, g_read->seq, g_read->length); - init_Hash_code(&k_code); - avalible_k = 0; - - while ((code = get_HPC_code(&HPC_read, &end_pos)) != 6) - { - if(code < 4) - { - k_mer_append(&k_code,code, k_mer_length); - avalible_k++; - if (avalible_k>= k_mer_length) - { - if(if_k_mer_available(&k_code, k_mer_length)) - { - convert_kmer(&k_mer_kv, &k_code, end_pos); - add_small_hash_table(forward, &k_mer_kv); - } - } - } - else - { - avalible_k = 0; - init_Hash_code(&k_code); - } - } - - sort_small_hash_table(forward); - - ///debug_sort_small_hash(forward); - - - - /** - ///reverse complement strand - reverse_complement(g_read->seq, g_read->length); - init_HPC_seq(&HPC_read, g_read->seq, g_read->length); - init_Hash_code(&k_code); - avalible_k = 0; - - while ((code = get_HPC_code(&HPC_read, &end_pos)) != 6) - { - if(code < 4) - { - k_mer_append(&k_code,code, k_mer_length); - avalible_k++; - if (avalible_k>= k_mer_length) - { - if(if_k_mer_available(&k_code, k_mer_length)) - { - convert_kmer(&k_mer_kv, &k_code, end_pos); - add_small_hash_table(reverse, &k_mer_kv); - } - } - } - else - { - avalible_k = 0; - init_Hash_code(&k_code); - } - } - - sort_small_hash_table(reverse); - - ///debug_sort_small_hash(reverse); - **/ - - long long i; - long long y_start, y_end, y_strand, y_ID, y_overlapLen, extraLen, y_readLen, tmp; - char* new_y_string; - long long new_y_length; - long long result_left, result_right, result_occ; - long long total_result_occ = 0; - Cigar_record cigar; - for (i = 0; i < R_INF.paf[readID].length; i++) - { - y_ID = R_INF.paf[readID].buffer[i].tn; - y_start = R_INF.paf[readID].buffer[i].ts; - y_end = R_INF.paf[readID].buffer[i].te; - y_strand = R_INF.paf[readID].buffer[i].rev; - y_readLen = R_INF.read_length[y_ID]; - - - - y_overlapLen = y_end - y_start + 1; - extraLen = y_overlapLen * 0.1; - y_start = y_start - extraLen; - if(y_start < 0) - { - y_start = 0; - } - y_end = y_end + extraLen; - if(extraLen >= y_readLen) - { - y_end = y_readLen - 1; - } - - - y_start = y_readLen - y_start - 1; - y_end = y_readLen - y_end - 1; - tmp = y_start; - y_start = y_end; - y_end = tmp; - - - - - if(y_strand == 0) - { - recover_UC_Read(overlap_read, &R_INF, y_ID); - } - else - { - recover_UC_Read_RC(overlap_read, &R_INF, y_ID); - } - - new_y_string = overlap_read->seq + y_start; - new_y_length = y_end - y_start + 1; - - /** - new_y_string = overlap_read->seq; - new_y_length = overlap_read->length; - **/ - total_result_occ = 0; - clear_Candidates_list(l); - - - init_HPC_seq(&HPC_read, new_y_string, new_y_length); - init_Hash_code(&k_code); - avalible_k = 0; - while ((code = get_HPC_code(&HPC_read, &end_pos)) != 6) - { - if(code < 4) - { - k_mer_append(&k_code,code, k_mer_length); - avalible_k++; - if (avalible_k>= k_mer_length) - { - if(if_k_mer_available(&k_code, k_mer_length)) - { - convert_kmer(&k_mer_kv, &k_code, end_pos); - result_occ = query_small_hash_table(forward, &k_mer_kv, &result_left, &result_right); - if(result_occ > 0) - { - total_result_occ += result_occ; - - insert_kv_list_to_candidates(forward->buffer + result_left, result_occ, y_ID, - end_pos + y_start, y_strand, l); - - /** - insert_kv_list_to_candidates(forward->buffer + result_left, result_occ, y_ID, - end_pos, y_strand, l); - **/ - } - - } - } - } - else - { - avalible_k = 0; - init_Hash_code(&k_code); - } - } - - ///要在这里整理出一个candidate位置,然后插入进overlap_list - sort_candidates(l, readID, overlap_list, &R_INF); - - // fprintf(stderr, "x_pos_s: (pre: %d), x_pos_e: (pre: %d), y_pos_s: (pre: %d), y_pos_e: (pre: %d), y_strand: (pre: %d)\n\n", - // (uint32_t)(R_INF.paf[readID].buffer[i].qns), R_INF.paf[readID].buffer[i].qe, R_INF.paf[readID].buffer[i].ts, R_INF.paf[readID].buffer[i].te, - // R_INF.paf[readID].buffer[i].rev); - - // fprintf(stderr, "x_id: %d, y_ID:%d, x_len: %d, y_len: %d, overlap_list->length: %d\n", readID, y_ID, - // R_INF.read_length[readID], R_INF.read_length[y_ID], overlap_list->length); - // fprintf(stderr, "x_pos_s: %d (pre: %d), x_pos_e: %d (pre: %d), y_pos_s: %d (pre: %d), y_pos_e: %d (pre: %d), x_strand: %d, y_strand: %d (pre: %d)\n\n\n", - // overlap_list->list[overlap_list->length - 1].x_pos_s, (uint32_t)(R_INF.paf[readID].buffer[i].qns), - // overlap_list->list[overlap_list->length - 1].x_pos_e, R_INF.paf[readID].buffer[i].qe, - // overlap_list->list[overlap_list->length - 1].y_pos_s, R_INF.paf[readID].buffer[i].ts, - // overlap_list->list[overlap_list->length - 1].y_pos_e, R_INF.paf[readID].buffer[i].te, - // overlap_list->list[overlap_list->length - 1].x_pos_strand, overlap_list->list[overlap_list->length - 1].y_pos_strand, - // R_INF.paf[readID].buffer[i].rev); - - - - // if(l->length != total_result_occ) - // { - // fprintf(stderr, "error\n"); - // } - ///fprintf(stderr, "i:%d, total_result_occ: %d, y_strand: %d, new_y_length: %d\n", i, total_result_occ, y_strand, new_y_length); - } - - - qsort(overlap_list->list, overlap_list->length, sizeof(overlap_region), cmp_by_x_pos_s); - - reverse_complement(g_read->seq, g_read->length); - - - // for (long long i = 0; i < overlap_list->length; i++) - // { - // fprintf(stderr, "****x_pos_s: %d, x_pos_e: %d, y_pos_s: %d, y_pos_e: %d, x_strand: %d, y_strand: %d, x_id: %d, y_id: %d\n", - // overlap_list->list[i].x_pos_s, overlap_list->list[i].x_pos_e, - // overlap_list->list[i].y_pos_s, overlap_list->list[i].y_pos_e, - // overlap_list->list[i].x_pos_strand, overlap_list->list[i].y_pos_strand, - // overlap_list->list[i].x_id, overlap_list->list[i].y_id); - // } - -} - void get_new_candidates(long long readID, UC_Read* g_read, overlap_region_alloc* overlap_list, k_mer_pos_list_alloc* array_list, HeapSq* heap, Candidates_list* l, double band_width_threshold, int keep_whole_chain) { @@ -1765,11 +838,11 @@ HeapSq* heap, Candidates_list* l, double band_width_threshold, int keep_whole_ch { if(code < 4) { - k_mer_append(&k_code,code,k_mer_length); + k_mer_append(&k_code,code, asm_opt.k_mer_length); avalible_k++; - if (avalible_k>=k_mer_length) + if (avalible_k >= asm_opt.k_mer_length) { - list_length = locate_Total_Pos_Table(&PCB, &k_code, &list, k_mer_length, &sub_ID); + list_length = locate_Total_Pos_Table(&PCB, &k_code, &list, asm_opt.k_mer_length, &sub_ID); if (list_length != 0) { @@ -1796,11 +869,11 @@ HeapSq* heap, Candidates_list* l, double band_width_threshold, int keep_whole_ch { if(code < 4) { - k_mer_append(&k_code,code,k_mer_length); + k_mer_append(&k_code,code, asm_opt.k_mer_length); avalible_k++; - if (avalible_k>=k_mer_length) + if (avalible_k >= asm_opt.k_mer_length) { - list_length = locate_Total_Pos_Table(&PCB, &k_code, &list, k_mer_length, &sub_ID); + list_length = locate_Total_Pos_Table(&PCB, &k_code, &list, asm_opt.k_mer_length, &sub_ID); if (list_length != 0) { append_k_mer_pos_list_alloc(array_list, list, list_length, end_pos, 1); @@ -1814,40 +887,22 @@ HeapSq* heap, Candidates_list* l, double band_width_threshold, int keep_whole_ch } } - - merge_k_mer_pos_list_alloc_heap_sort(array_list, l, heap); - ///以x_pos_e,即结束位置为主元排序 - ///calculate_overlap_region(l, overlap_list, readID, g_read->length, &R_INF); calculate_overlap_region_by_chaining(l, overlap_list, readID, g_read->length, &R_INF, band_width_threshold, keep_whole_chain); } - - void* Overlap_calculate_heap_merge(void* arg) { - /************需要注释掉**********/ - // long long debug_overlap = 0; - // long long filtered_debug_overlap = 0; - /************需要注释掉**********/ - - long long matched_overlap_0 = 0; - long long matched_overlap_1 = 0; - long long potiental_matched_overlap_0 = 0; - long long potiental_matched_overlap_1 = 0; long long num_read_base = 0; long long num_correct_base = 0; - long long num_second_correct_base = 0; - long long j; + long long num_recorrect_base = 0; int fully_cov, abnormal; int thr_ID = *((int*)arg); - uint64_t POA_i; long long i = 0; - int avalible_k = 0; UC_Read g_read; init_UC_Read(&g_read); @@ -1855,28 +910,12 @@ void* Overlap_calculate_heap_merge(void* arg) UC_Read overlap_read; init_UC_Read(&overlap_read); - HPC_seq HPC_read; - Hash_code k_code; - uint64_t code; - uint64_t end_pos; - k_mer_pos* list; - uint64_t list_length; - uint64_t sub_ID; - long long total_shared_seed = 0; - long long candidate_overlap_reads = 0; - - Candidates_list l; - //Candidates_list debug_l; Graph POA_Graph; Graph DAGCon; init_Graph(&DAGCon); init_Graph(&POA_Graph); - - init_Candidates_list(&l); - //init_Candidates_list(&debug_l); - k_mer_pos_list_alloc array_list; init_k_mer_pos_list_alloc(&array_list); @@ -1901,22 +940,10 @@ void* Overlap_calculate_heap_merge(void* arg) haplotype_evdience_alloc hap; InitHaplotypeEvdience(&hap); - Round2_alignment second_round; init_Round2_alignment(&second_round); - small_hash_table forward, reverse; - init_small_hash_table(&forward); - init_small_hash_table(&reverse); - - - uint8_t c2n[256]; - memset(c2n, 4, 256); - c2n['A'] = c2n['a'] = 0; c2n['C'] = c2n['c'] = 1; - c2n['G'] = c2n['g'] = 2; c2n['T'] = c2n['t'] = 3; // build the encoding table - - - for (i = thr_ID; i < R_INF.total_reads; i = i + thread_num) + for (i = thr_ID; i < (long long)R_INF.total_reads; i = i + asm_opt.thread_num) { ///get_new_candidates(i, &g_read, &overlap_list, &array_list, &heap, &l, THRESHOLD_RATE*1.5); get_new_candidates(i, &g_read, &overlap_list, &array_list, &heap, &l, 0.02, 1); @@ -1925,24 +952,20 @@ void* Overlap_calculate_heap_merge(void* arg) clear_Round2_alignment(&second_round); correct_overlap(&overlap_list, &R_INF, &g_read, &correct, &overlap_read, &POA_Graph, &DAGCon, - &matched_overlap_0, &matched_overlap_1, &potiental_matched_overlap_0, &potiental_matched_overlap_1, - ¤t_cigar, &hap, &second_round, 0, 1, &fully_cov, &abnormal, c2n); + ¤t_cigar, &hap, &second_round, 0, 1, &fully_cov, &abnormal); - num_read_base = num_read_base + g_read.length; - num_correct_base = num_correct_base + correct.corrected_base; - num_second_correct_base = num_second_correct_base + second_round.dumy.corrected_base; + num_read_base += g_read.length; + num_correct_base += correct.corrected_base; + num_recorrect_base += second_round.dumy.corrected_base; push_cigar(R_INF.cigars, i, ¤t_cigar); push_cigar(R_INF.second_round_cigar, i, &(second_round.cigar)); - R_INF.paf[i].is_fully_corrected = 0; if(fully_cov) { - if( - get_cigar_errors(¤t_cigar) == 0 - && + if(get_cigar_errors(¤t_cigar) == 0 && get_cigar_errors(&second_round.cigar) == 0) { R_INF.paf[i].is_fully_corrected = 1; @@ -1950,100 +973,50 @@ void* Overlap_calculate_heap_merge(void* arg) } R_INF.paf[i].is_abnormal = abnormal; - - - - - push_overlaps(&(R_INF.paf[i]), &overlap_list, 1, &R_INF, roundID%2); - push_overlaps(&(R_INF.reverse_paf[i]), &overlap_list, 2, &R_INF, roundID%2); + push_overlaps(&(R_INF.paf[i]), &overlap_list, 1, &R_INF, asm_opt.roundID%2); + push_overlaps(&(R_INF.reverse_paf[i]), &overlap_list, 2, &R_INF, asm_opt.roundID%2); } - /************需要注释掉**********/ - // fprintf(stderr, "debug_overlap: %llu\n", debug_overlap); - // fprintf(stderr, "filtered_debug_overlap: %llu\n", filtered_debug_overlap); - /************需要注释掉**********/ - finish_output_buffer(); - destory_buffer_sub_block(¤t_sub_buffer); - - - destory_Candidates_list(&l); destory_overlap_region_alloc(&overlap_list); - destory_Heap(&heap); destory_k_mer_pos_list_alloc(&array_list); - destory_Graph(&POA_Graph); destory_Graph(&DAGCon); - - destory_UC_Read(&g_read); destory_UC_Read(&overlap_read); destory_Cigar_record(¤t_cigar); - destory_Correct_dumy(&correct); - destoryHaplotypeEvdience(&hap); - destory_Round2_alignment(&second_round); - - destory_small_hash_table(&forward); - destory_small_hash_table(&reverse); - + pthread_mutex_lock(&statistics); - total_matched_overlap_0 += matched_overlap_0; - total_matched_overlap_1 += matched_overlap_1; - total_potiental_matched_overlap_0 += potiental_matched_overlap_0; - total_potiental_matched_overlap_1 += potiental_matched_overlap_1; - total_num_read_base += num_read_base; - total_num_correct_base += num_correct_base; - total_second_num_correct_base +=num_second_correct_base; + asm_opt.num_bases += num_read_base; + asm_opt.num_corrected_bases += num_correct_base; + asm_opt.num_recorrected_bases += num_recorrect_base; - complete_threads++; - if(complete_threads == thread_num) + asm_opt.complete_threads++; + if(asm_opt.complete_threads == asm_opt.thread_num) { - fprintf(stderr, "total_matched_overlap_0: %llu\n", total_matched_overlap_0); - fprintf(stderr, "total_matched_overlap_1: %llu\n", total_matched_overlap_1); - fprintf(stderr, "total_potiental_matched_overlap_0: %llu\n", total_potiental_matched_overlap_0); - fprintf(stderr, "total_potiental_matched_overlap_1: %llu\n", total_potiental_matched_overlap_1); - fprintf(stderr, "total_num_read_base: %llu\n", total_num_read_base); - fprintf(stderr, "total_num_correct_base: %llu\n", total_num_correct_base); - fprintf(stderr, "total_second_num_correct_base: %llu\n", total_second_num_correct_base); - + fprintf(stderr, "total bases #: %lld\n", asm_opt.num_bases); + fprintf(stderr, "total corrected bases: %lld\n", asm_opt.num_corrected_bases); + fprintf(stderr, "total recorrected bases: %lld\n", asm_opt.num_recorrected_bases); } pthread_mutex_unlock(&statistics); - free(arg); + + return NULL; } - - void* Output_related_reads(void* arg) { - /************需要注释掉**********/ - // long long debug_overlap = 0; - // long long filtered_debug_overlap = 0; - /************需要注释掉**********/ - - long long matched_overlap_0 = 0; - long long matched_overlap_1 = 0; - long long potiental_matched_overlap_0 = 0; - long long potiental_matched_overlap_1 = 0; - long long num_read_base = 0; - long long num_correct_base = 0; - long long num_second_correct_base = 0; - long long j; - int fully_cov; - int thr_ID = *((int*)arg); - uint64_t POA_i; long long i = 0; - int avalible_k = 0; UC_Read g_read; init_UC_Read(&g_read); @@ -2051,19 +1024,7 @@ void* Output_related_reads(void* arg) UC_Read overlap_read; init_UC_Read(&overlap_read); - HPC_seq HPC_read; - Hash_code k_code; - uint64_t code; - uint64_t end_pos; - k_mer_pos* list; - uint64_t list_length; - uint64_t sub_ID; - long long total_shared_seed = 0; - long long candidate_overlap_reads = 0; - - Candidates_list l; - //Candidates_list debug_l; Graph POA_Graph; Graph DAGCon; init_Graph(&DAGCon); @@ -2101,34 +1062,31 @@ void* Output_related_reads(void* arg) Round2_alignment second_round; init_Round2_alignment(&second_round); - small_hash_table forward, reverse; - init_small_hash_table(&forward); - init_small_hash_table(&reverse); - long long required_read_name_length = strlen(required_read_name); - for (i = thr_ID; i < R_INF.total_reads; i = i + thread_num) + long long required_read_name_length = strlen(asm_opt.required_read_name); + for (i = thr_ID; i < (long long)R_INF.total_reads; i = i + asm_opt.thread_num) { - if(required_read_name_length == Get_NAME_LENGTH((R_INF),i) + if(required_read_name_length == (long long)Get_NAME_LENGTH((R_INF),i) && - memcmp(required_read_name, Get_NAME((R_INF), i), Get_NAME_LENGTH((R_INF),i)) == 0) + memcmp(asm_opt.required_read_name, Get_NAME((R_INF), i), Get_NAME_LENGTH((R_INF),i)) == 0) { ////get_new_candidates(i, &g_read, &overlap_list, &array_list, &heap, &l, THRESHOLD_RATE*1.5); get_new_candidates(i, &g_read, &overlap_list, &array_list, &heap, &l, 0.02, 1); - fprintf(stderr, ">%.*s\n", Get_NAME_LENGTH((R_INF), i), + fprintf(stderr, ">%.*s\n", (int)Get_NAME_LENGTH((R_INF), i), Get_NAME((R_INF), i)); recover_UC_Read(&g_read, &R_INF, i); - fprintf(stderr, "%.*s\n", g_read.length, g_read.seq); + fprintf(stderr, "%.*s\n", (int)g_read.length, g_read.seq); - long long k; + uint64_t k; for (k = 0; k < overlap_list.length; k++) { - fprintf(stderr, ">%.*s\n", Get_NAME_LENGTH((R_INF),overlap_list.list[k].y_id), + fprintf(stderr, ">%.*s\n", (int)Get_NAME_LENGTH((R_INF),overlap_list.list[k].y_id), Get_NAME((R_INF),overlap_list.list[k].y_id)); recover_UC_Read(&g_read, &R_INF, overlap_list.list[k].y_id); - fprintf(stderr, "%.*s\n", g_read.length, g_read.seq); + fprintf(stderr, "%.*s\n", (int)g_read.length, g_read.seq); } } @@ -2137,53 +1095,36 @@ void* Output_related_reads(void* arg) finish_output_buffer(); destory_buffer_sub_block(¤t_sub_buffer); - - - - destory_Candidates_list(&l); destory_overlap_region_alloc(&overlap_list); - //destory_Candidates_list(&debug_l); - destory_Heap(&heap); destory_k_mer_pos_list_alloc(&array_list); - ///destory_k_mer_pos_list_alloc_prefilter(&array_list); - destory_Graph(&POA_Graph); destory_Graph(&DAGCon); - - destory_UC_Read(&g_read); destory_UC_Read(&overlap_read); destory_Cigar_record(¤t_cigar); - destory_Correct_dumy(&correct); - destoryHaplotypeEvdience(&hap); - destory_Round2_alignment(&second_round); - - destory_small_hash_table(&forward); - destory_small_hash_table(&reverse); + free(arg); + + return NULL; } - - inline long long get_N_occ(char* seq, long long length) { long long N_occ = 0; long long j; for (j = 0; j < length; j++) { - if(seq_nt6_table[seq[j]] >= 4) + if(seq_nt6_table[(uint8_t)seq[j]] >= 4) { - ///fprintf(stderr, "seq[%d]: %c\n", j, seq[j]); N_occ++; } } - return N_occ; } @@ -2191,7 +1132,7 @@ inline long long get_N_occ(char* seq, long long length) void* Save_corrected_reads(void* arg) { int thr_ID = *((int*)arg); - long long i, j; + long long i; UC_Read g_read; init_UC_Read(&g_read); @@ -2209,24 +1150,17 @@ void* Save_corrected_reads(void* arg) char* new_read; int new_read_length; - for (i = thr_ID; i < R_INF.total_reads; i = i + thread_num) + for (i = thr_ID; i < (long long)R_INF.total_reads; i = i + asm_opt.thread_num) { recover_UC_Read(&g_read, &R_INF, i); - - /********************************1 round******************************/ - if(R_INF.cigars[i].new_length > first_round_read_size) + if((long long)R_INF.cigars[i].new_length > first_round_read_size) { first_round_read_size = R_INF.cigars[i].new_length; first_round_read = (char*)realloc(first_round_read, first_round_read_size); } - - - - - cigar.length = R_INF.cigars[i].length; cigar.lost_base_length = R_INF.cigars[i].lost_base_length; cigar.record = R_INF.cigars[i].record; @@ -2234,19 +1168,10 @@ void* Save_corrected_reads(void* arg) get_corrected_read_from_cigar(&cigar, g_read.seq, g_read.length, first_round_read, &first_round_read_length); - - - - - /********************debug********************/ - // N_occ = get_N_occ(first_round_read, first_round_read_length); - // fprintf(stderr, "i: %d, first N_occ: %d\n", i, N_occ); - // fflush(stderr); - /********************debug********************/ /********************************1 round******************************/ /********************************2 round******************************/ - if(R_INF.second_round_cigar[i].new_length > second_round_read_size) + if((long long)R_INF.second_round_cigar[i].new_length > second_round_read_size) { second_round_read_size = R_INF.second_round_cigar[i].new_length; second_round_read = (char*)realloc(second_round_read, second_round_read_size); @@ -2258,12 +1183,6 @@ void* Save_corrected_reads(void* arg) get_corrected_read_from_cigar(&cigar, first_round_read, first_round_read_length, second_round_read, &second_round_read_length); - - /********************debug********************/ - // N_occ = get_N_occ(second_round_read, second_round_read_length); - // fprintf(stderr, "i: %d, second N_occ: %d\n\n\n", i, N_occ); - // fflush(stderr); - /********************debug********************/ /********************************2 round******************************/ @@ -2273,12 +1192,12 @@ void* Save_corrected_reads(void* arg) new_read_length = second_round_read_length; - if (roundID != number_of_round - 1) + if (asm_opt.roundID != asm_opt.number_of_round - 1) { ///need modification reverse_complement(new_read, new_read_length); } - else if(number_of_round % 2 == 0) + else if(asm_opt.number_of_round % 2 == 0) { ///need modification reverse_complement(new_read, new_read_length); @@ -2288,7 +1207,7 @@ void* Save_corrected_reads(void* arg) N_occ = get_N_occ(new_read, new_read_length); - if(R_INF.read_size[i] < new_read_length) + if((long long)R_INF.read_size[i] < new_read_length) { R_INF.read_size[i] = new_read_length; R_INF.read_sperate[i] = (uint8_t*)realloc(R_INF.read_sperate[i], R_INF.read_size[i]/4+1); @@ -2306,18 +1225,18 @@ void* Save_corrected_reads(void* arg) free(first_round_read); free(second_round_read); free(arg); -} + return NULL; +} void Output_corrected_reads() { - long long i, j; + long long i; UC_Read g_read; init_UC_Read(&g_read); - FILE* output_file = fopen(output_file_name, "w"); + FILE* output_file = fopen(asm_opt.output_file_name, "w"); - - for (i = 0; i < R_INF.total_reads; i++) + for (i = 0; i < (long long)R_INF.total_reads; i++) { recover_UC_Read(&g_read, &R_INF, i); fwrite(">", 1, 1, output_file); @@ -2326,65 +1245,28 @@ void Output_corrected_reads() fwrite(g_read.seq, 1, g_read.length, output_file); fwrite("\n", 1, 1, output_file); } - - /** - if(number_of_round % 2 == 0) - { - for (i = 0; i < R_INF.total_reads; i++) - { - recover_UC_Read(&g_read, &R_INF, i); - fwrite(">", 1, 1, output_file); - fwrite(Get_NAME(R_INF, i), 1, Get_NAME_LENGTH(R_INF, i), output_file); - fwrite("\n", 1, 1, output_file); - fwrite(g_read.seq, 1, g_read.length, output_file); - fwrite("\n", 1, 1, output_file); - } - } - else - { - for (i = 0; i < R_INF.total_reads; i++) - { - recover_UC_Read_RC(&g_read, &R_INF, i); - fwrite(">", 1, 1, output_file); - fwrite(Get_NAME(R_INF, i), 1, Get_NAME_LENGTH(R_INF, i), output_file); - fwrite("\n", 1, 1, output_file); - fwrite(g_read.seq, 1, g_read.length, output_file); - fwrite("\n", 1, 1, output_file); - } - } - **/ - - - - destory_UC_Read(&g_read); fclose(output_file); } - - void Overlap_calculate_multipe_thr() { - - - double start_time = Get_T(); - pthread_t outputResultSinkHandle; - fprintf(stdout, "Begin Overlap Calculate ...... \n"); + fprintf(stderr, "Begin calculating overlaps... \n"); pthread_t *_r_threads; - _r_threads = (pthread_t *)malloc(sizeof(pthread_t)*thread_num); + _r_threads = (pthread_t *)malloc(sizeof(pthread_t)*asm_opt.thread_num); int i = 0; - for (i = 0; i < thread_num; i++) + for (i = 0; i < asm_opt.thread_num; i++) { int *arg = (int*)malloc(sizeof(*arg)); *arg = i; - if(!required_read_name) + if(!asm_opt.required_read_name) { pthread_create(_r_threads + i, NULL, Overlap_calculate_heap_merge, (void*)arg); } @@ -2392,33 +1274,31 @@ void Overlap_calculate_multipe_thr() { pthread_create(_r_threads + i, NULL, Output_related_reads, (void*)arg); } - - } - for (i = 0; iseq.s, seq->seq.l); - init_Hash_code(&k_code); - - avalible_k = 0; - - while ((code = get_HPC_code(&HPC_read, &end_pos)) != 6) - { - if(code < 4) - { - k_mer_append(&k_code,code,k_mer_length); - avalible_k++; - if (avalible_k>=k_mer_length) - { - if(verify_Total_Count_Table(&TCB, &k_code, k_mer_length) == -1) - { - fprintf(stderr, "ERROR when subtracting!\n"); - } - } - - } - else - { - avalible_k = 0; - init_Hash_code(&k_code); - } - - } - - - - - read_number++; - } - - fprintf(stdout, "read_number: %lld\n",read_number); - - fprintf(stdout, "Start Traversing ...\n"); - - Traverse_Total_Count_Table(&TCB); - - fprintf(stdout, "Finish Traversing!\n"); - - -} - - - -/********************************for debug*****************************************/ -void verify_Position_hash_table() -{ - - init_kseq(read_file_name); - - kseq_t *seq = (kseq_t*)calloc(1, sizeof(kseq_t)); - - - long long read_number = 0; - ///long long HPC_base; - - HPC_seq HPC_read; - uint64_t code; - uint64_t end_pos; - Hash_code k_code; - int avalible_k = 0; - k_mer_pos* list; - uint64_t sub_ID; - - UC_Read g_read; - init_UC_Read(&g_read); - - fprintf(stdout, "Start Verifying Position Table...\n"); - - while (get_read(seq, adapterLen)) - { - - - if (seq->seq.l - != Get_READ_LENGTH(R_INF, read_number)) - { - fprintf(stderr, "seq error\n"); - } - - - recover_UC_Read(&g_read, &R_INF, read_number); - - - - if(memcmp(seq->seq.s, g_read.seq, seq->seq.l)) - { - fprintf(stderr, "\nseq error ID: %llu, length: %llu\n",read_number, seq->seq.l); - - } - - if (seq->name.l - != Get_NAME_LENGTH(R_INF, read_number)) - { - fprintf(stderr, "name error\n"); - } - - - if(memcmp(seq->name.s, Get_NAME(R_INF, read_number), seq->name.l)) - { - fprintf(stderr, "name error\n"); - } - - - init_HPC_seq(&HPC_read, seq->seq.s, seq->seq.l); - init_Hash_code(&k_code); - - avalible_k = 0; - ///HPC_base = 0; - - while ((code = get_HPC_code(&HPC_read, &end_pos)) != 6) - { - - - if(code < 4) - { - k_mer_append(&k_code,code,k_mer_length); - avalible_k++; - if (avalible_k>=k_mer_length) - { - - ///uint64_t count1 = get_Total_Count_Table(&TCB, &k_code, k_mer_length); - uint64_t count2 = count_Total_Pos_Table(&PCB, &k_code, k_mer_length); - /** - if(count1>=k_mer_min_freq && count1<= k_mer_max_freq && count1!=count2) - { - fprintf(stderr, "count1: %lld\n",count1); - fprintf(stderr, "count2: %lld\n",count2); - } - **/ - - - - if(locate_Total_Pos_Table(&PCB, &k_code, &list, k_mer_length, &sub_ID) == count2) - { - int j = 0; - for(j=1; jseq, g_read->length); - init_Hash_code(&k_code); - avalible_k = 0; - - // if(g_read->length != R_INF.read_length[readID]) - // { - // fprintf(stderr, "error\n"); - // } - - while ((code = get_HPC_code(&HPC_read, &end_pos)) != 6) - { - if(code < 4) - { - k_mer_append(&k_code,code,k_mer_length); - avalible_k++; - if (avalible_k>=k_mer_length) - { - list_length = locate_Total_Pos_Table(&PCB, &k_code, &list, k_mer_length, &sub_ID); - - if (list_length != 0) - { - append_k_mer_pos_list_alloc(array_list, list, list_length, end_pos, 0); - } - } - } - else - { - avalible_k = 0; - init_Hash_code(&k_code); - } - } - - - - ///reverse complement strand - reverse_complement(g_read->seq, g_read->length); - init_HPC_seq(&HPC_read, g_read->seq, g_read->length); - init_Hash_code(&k_code); - avalible_k = 0; - - while ((code = get_HPC_code(&HPC_read, &end_pos)) != 6) - { - if(code < 4) - { - k_mer_append(&k_code,code,k_mer_length); - avalible_k++; - if (avalible_k>=k_mer_length) - { - list_length = locate_Total_Pos_Table(&PCB, &k_code, &list, k_mer_length, &sub_ID); - if (list_length != 0) - { - append_k_mer_pos_list_alloc(array_list, list, list_length, end_pos, 1); - } - } - } - else - { - avalible_k = 0; - init_Hash_code(&k_code); - } - } - - - - merge_k_mer_pos_list_alloc_heap_sort(array_list, l, heap); - calculate_inexact_overlap_region(l, overlap_list, readID, g_read->length, &R_INF); - ///calculate_overlap_region(l, overlap_list, readID, g_read->length, &R_INF); - - - - - overlap_region_sort_y_id(overlap_list->list, overlap_list->length); - ma_hit_sort_tn(R_INF.paf[readID].buffer, R_INF.paf[readID].length); - overlap_list->mapped_overlaps_length = 0; - long long inner_j = 0; - long long j = 0; - while (j < overlap_list->length && inner_j < R_INF.paf[readID].length) - { - if(overlap_list->list[j].y_id < R_INF.paf[readID].buffer[inner_j].tn) - { - j++; - } - else if(overlap_list->list[j].y_id > R_INF.paf[readID].buffer[inner_j].tn) - { - inner_j++; - } - else - { - if(overlap_list->list[j].y_pos_strand == R_INF.paf[readID].buffer[inner_j].rev) - { - overlap_list->list[j].is_match = 1; - overlap_list->mapped_overlaps_length++; - } - j++; - inner_j++; - } - } -} - - - - - -void debug_print_overlap(char* y_name, overlap_region_alloc* overlap_list, All_reads* R_INF, char* func) -{ - fprintf(stderr, "\nafter %s\n", func); - long long i, j; - for (i = 0; i < overlap_list->length; i++) - { - if(y_name == NULL || memcmp(y_name, Get_NAME((*R_INF), overlap_list->list[i].y_id), - Get_NAME_LENGTH((*R_INF), overlap_list->list[i].y_id)) == 0) - { - fprintf(stderr, "****************x_name: %.*s, x_id: %d****************\n", - Get_NAME_LENGTH((*R_INF), overlap_list->list[i].x_id), - Get_NAME((*R_INF), overlap_list->list[i].x_id), overlap_list->list[i].x_id); - - fprintf(stderr, "y_name: %.*s, y_id: %d, is_match: %d\n", - Get_NAME_LENGTH((*R_INF),overlap_list->list[i].y_id), - Get_NAME((*R_INF),overlap_list->list[i].y_id), overlap_list->list[i].y_id, - overlap_list->list[i].is_match); - - fprintf(stderr, "alignLen: %d, x_s: %d, x_e: %d, y_s: %d, y_e: %d, y_dir: %d, strong: %d\n", - overlap_list->list[i].align_length, - overlap_list->list[i].x_pos_s, - overlap_list->list[i].x_pos_e, - overlap_list->list[i].y_pos_s, - overlap_list->list[i].y_pos_e, - overlap_list->list[i].y_pos_strand, - overlap_list->list[i].strong); - - fprintf(stderr, "i: %d, %.*s, x_s: %d, x_e: %d, y_s: %d, y_end: %d, w_list_length: %d, dir: %d, strong: %d, is_match: %d\n", - i, Get_NAME_LENGTH((*R_INF),overlap_list->list[i].y_id), - Get_NAME((*R_INF),overlap_list->list[i].y_id), - overlap_list->list[i].x_pos_s, - overlap_list->list[i].x_pos_e, - overlap_list->list[i].y_pos_s, - overlap_list->list[i].y_pos_e, - overlap_list->list[i].w_list_length, - overlap_list->list[i].y_pos_strand, - overlap_list->list[i].strong, - overlap_list->list[i].is_match); - - for (j = 0; j < overlap_list->list[i].w_list_length; j++) - { - fprintf(stderr, "************************\ncigar_j: %d, x_s: %d, x_e: %d, y_s: %d, y_end: %d\n", - j, overlap_list->list[i].w_list[j].x_start, - overlap_list->list[i].w_list[j].x_end, - overlap_list->list[i].w_list[j].y_start, - overlap_list->list[i].w_list[j].y_end); - if(overlap_list->list[i].w_list[j].y_end == -1) - { - fprintf(stderr, "not match\n"); - } - else - { - int cigar_i, operation, operationLen; - CIGAR* cigar = &(overlap_list->list[i].w_list[j].cigar); - fprintf(stderr, "length: %d\n", cigar->length); - for (cigar_i = 0; cigar_i < cigar->length; cigar_i++) - { - operation = cigar->C_C[cigar_i]; - operationLen = cigar->C_L[cigar_i]; - fprintf(stderr, "oper: %d, Len: %d\n", operation, operationLen); - } - } - - } - } - } -} - -int debug_diff(int a, int b) -{ - if(DIFF(a, b)!= abs(a-b)) - { - fprintf(stderr, "sbsbsbsb\n"); - } -} - void update_overlaps(overlap_region_alloc* overlap_list, ma_hit_t_alloc* paf, UC_Read* g_read, UC_Read* overlap_read, int is_match, int is_exact) { - long long inner_j = 0; - long long j = 0; + uint64_t inner_j = 0; + uint64_t j = 0; long long x_overlapLen, y_overlapLen; while (j < overlap_list->length && inner_j < paf->length) { @@ -2920,13 +1365,6 @@ UC_Read* g_read, UC_Read* overlap_read, int is_match, int is_exact) if(x_overlapLen < y_overlapLen) x_overlapLen = y_overlapLen; x_overlapLen = x_overlapLen * 0.1; - // debug_diff(overlap_list->list[j].x_pos_s, Get_qs(paf->buffer[inner_j])); - // debug_diff(overlap_list->list[j].x_pos_e, Get_qe(paf->buffer[inner_j])); - // debug_diff(overlap_list->list[j].y_pos_s, Get_ts(paf->buffer[inner_j])); - // debug_diff(overlap_list->list[j].y_pos_e, Get_te(paf->buffer[inner_j])); - - ///fprintf(stderr, "hehe\n"); - // if( // ((DIFF(overlap_list->list[j].x_pos_s, Get_qs(paf->buffer[inner_j])) < x_overlapLen) // && (DIFF(overlap_list->list[j].x_pos_e, Get_qe(paf->buffer[inner_j])) < x_overlapLen)) @@ -2934,11 +1372,11 @@ UC_Read* g_read, UC_Read* overlap_read, int is_match, int is_exact) // ((DIFF(overlap_list->list[j].y_pos_s, Get_ts(paf->buffer[inner_j])) < x_overlapLen) // && (DIFF(overlap_list->list[j].y_pos_e, Get_te(paf->buffer[inner_j])) < x_overlapLen))) if( - ((DIFF(overlap_list->list[j].x_pos_s, Get_qs(paf->buffer[inner_j])) < x_overlapLen) - && (DIFF(overlap_list->list[j].x_pos_e, Get_qe(paf->buffer[inner_j])) < x_overlapLen)) + ((DIFF(overlap_list->list[j].x_pos_s, Get_qs(paf->buffer[inner_j])) < (uint64_t)x_overlapLen) + && (DIFF(overlap_list->list[j].x_pos_e, Get_qe(paf->buffer[inner_j])) < (uint64_t)x_overlapLen)) || - ((DIFF(overlap_list->list[j].y_pos_s, Get_ts(paf->buffer[inner_j])) < x_overlapLen) - && (DIFF(overlap_list->list[j].y_pos_e, Get_te(paf->buffer[inner_j])) < x_overlapLen)) + ((DIFF(overlap_list->list[j].y_pos_s, Get_ts(paf->buffer[inner_j])) < (uint64_t)x_overlapLen) + && (DIFF(overlap_list->list[j].y_pos_e, Get_te(paf->buffer[inner_j])) < (uint64_t)x_overlapLen)) ) { overlap_list->list[j].is_match = is_match; @@ -2984,7 +1422,7 @@ UC_Read* g_read, UC_Read* overlap_read, int is_match, int is_exact) void update_exact_overlaps(overlap_region_alloc* overlap_list, UC_Read* g_read, UC_Read* overlap_read) { - long long j; + uint64_t j; for (j = 0; j < overlap_list->length; j++) { if (overlap_list->list[j].is_match != 1) @@ -3033,15 +1471,15 @@ void statistic(ma_hit_t_alloc* paf, ma_hit_t_alloc* rev_paf, long long readNum) } - fprintf(stdout, "****************statistic for overlaps****************\n"); - fprintf(stdout, "overlaps #: %lld\n", forward); - fprintf(stdout, "strong overlaps #: %lld\n", strong); - fprintf(stdout, "weak overlaps #: %lld\n", weak); - fprintf(stdout, "exact overlaps #: %lld\n", exact); - fprintf(stdout, "inexact overlaps #: %lld\n", forward - exact); - fprintf(stdout, "overlaps without large indels#: %lld\n", no_l_indel); - fprintf(stdout, "reverse overlaps #: %lld\n", reverse); - fprintf(stdout, "****************statistic for overlaps****************\n"); + fprintf(stderr, "****************statistic for overlaps****************\n"); + fprintf(stderr, "overlaps #: %lld\n", forward); + fprintf(stderr, "strong overlaps #: %lld\n", strong); + fprintf(stderr, "weak overlaps #: %lld\n", weak); + fprintf(stderr, "exact overlaps #: %lld\n", exact); + fprintf(stderr, "inexact overlaps #: %lld\n", forward - exact); + fprintf(stderr, "overlaps without large indels#: %lld\n", no_l_indel); + fprintf(stderr, "reverse overlaps #: %lld\n", reverse); + fprintf(stderr, "****************statistic for overlaps****************\n"); } @@ -3049,7 +1487,7 @@ void fill_chain(Fake_Cigar* chain, char* x_string, char* y_string, long long xBe long long x_readLen, long long y_readLen, Cigar_record* cigar, uint8_t* c2n) { long long i, xOffset, yOffset, xRegionLen, yRegionLen, /**bandLen,**/ maxXpos, maxYpos, mapScore, zdroped; - float band_rate = 0.08; + ///float band_rate = 0.08; int endbouns; if(chain->length <= 0) return; @@ -3074,25 +1512,25 @@ long long x_readLen, long long y_readLen, Cigar_record* cigar, uint8_t* c2n) // if(bandLen == 0) bandLen = MIN(xRegionLen, yRegionLen); ///do alignment backward - kv_resize(uint8_t, x_num, xRegionLen); - kv_resize(uint8_t, y_num, yRegionLen); + kv_resize(uint8_t, x_num, (uint64_t)xRegionLen); + kv_resize(uint8_t, y_num, (uint64_t)yRegionLen); ///text is x, query is y afine_gap_alignment(x_string, x_num.a, xRegionLen, y_string, y_num.a, yRegionLen, c2n, BACKWARD_KSW, MATCH_SCORE_KSW, MISMATCH_SCORE_KSW, GAP_OPEN_KSW, GAP_EXT_KSW, /**bandLen,**/BAND_KSW, Z_DROP_KSW, endbouns, &maxXpos, &maxYpos, &mapScore, &zdroped); - fprintf(stderr, "* xOffset: %d, yOffset: %d, xRegionLen: %d, yRegionLen: %d, bandLen: %d, maxXpos: %d, maxYpos: %d, zdroped: %d\n", - xOffset, yOffset, xRegionLen, yRegionLen, BAND_KSW, maxXpos, maxYpos, zdroped); + // fprintf(stderr, "* xOffset: %lld, yOffset: %lld, xRegionLen: %lld, yRegionLen: %lld, bandLen: %lld, maxXpos: %lld, maxYpos: %lld, zdroped: %lld\n", + // xOffset, yOffset, xRegionLen, yRegionLen, BAND_KSW, maxXpos, maxYpos, zdroped); } ///align forward - for (i = 0; i < chain->length; i++) + for (i = 0; i < (long long)chain->length; i++) { // xOffset = get_fake_gap_pos(chain, i); // yOffset = xOffset + get_fake_gap_shift(chain, i); xOffset = get_fake_gap_pos(chain, i); yOffset = (xOffset - xBeg) + yBeg + get_fake_gap_shift(chain, i); ///last region - if(i == chain->length - 1) + if(i == (long long)(chain->length - 1)) { endbouns = 0; xRegionLen = x_readLen - xOffset; @@ -3115,14 +1553,14 @@ long long x_readLen, long long y_readLen, Cigar_record* cigar, uint8_t* c2n) ///do alignment forward - kv_resize(uint8_t, x_num, xRegionLen); - kv_resize(uint8_t, y_num, yRegionLen); + kv_resize(uint8_t, x_num, (uint64_t)xRegionLen); + kv_resize(uint8_t, y_num, (uint64_t)yRegionLen); ///text is x, query is y afine_gap_alignment(x_string+xOffset, x_num.a, xRegionLen, y_string+yOffset, y_num.a, yRegionLen, c2n, FORWARD_KSW, MATCH_SCORE_KSW, MISMATCH_SCORE_KSW, GAP_OPEN_KSW, GAP_EXT_KSW, /**bandLen,**/BAND_KSW, Z_DROP_KSW, endbouns, &maxXpos, &maxYpos, &mapScore, &zdroped); - fprintf(stderr, "# xOffset: %d, yOffset: %d, xRegionLen: %d, yRegionLen: %d, bandLen: %d, maxXpos: %d, maxYpos: %d, zdroped: %d\n", - xOffset, yOffset, xRegionLen, yRegionLen, BAND_KSW, maxXpos, maxYpos, zdroped); + // fprintf(stderr, "# xOffset: %lld, yOffset: %lld, xRegionLen: %lld, yRegionLen: %lld, bandLen: %lld, maxXpos: %lld, maxYpos: %lld, zdroped: %lld\n", + // xOffset, yOffset, xRegionLen, yRegionLen, BAND_KSW, maxXpos, maxYpos, zdroped); } @@ -3132,7 +1570,7 @@ long long x_readLen, long long y_readLen, Cigar_record* cigar, uint8_t* c2n) void Final_phasing(overlap_region_alloc* overlap_list, Cigar_record_alloc* cigarline, UC_Read* g_read, UC_Read* overlap_read, uint8_t* c2n) { - long long i, xLen, yLen, yStrand; + uint64_t i, xLen, yStrand; char* x_string; char* y_string; Cigar_record* cigar; @@ -3147,7 +1585,6 @@ UC_Read* g_read, UC_Read* overlap_read, uint8_t* c2n) overlap_list->list[i].is_match == 3) { xLen = overlap_list->list[i].x_pos_e - overlap_list->list[i].x_pos_s + 1; - yLen = overlap_list->list[i].y_pos_e - overlap_list->list[i].y_pos_s + 1; yStrand = overlap_list->list[i].y_pos_strand; cigar = &(cigarline->buffer[i]); ///has already been matched exactly @@ -3182,7 +1619,7 @@ UC_Read* g_read, UC_Read* overlap_read, uint8_t* c2n) void* Final_overlap_calculate_heap_merge(void* arg) { int thr_ID = *((int*)arg); - long long i = 0; + uint64_t i = 0; UC_Read g_read; init_UC_Read(&g_read); @@ -3214,7 +1651,7 @@ void* Final_overlap_calculate_heap_merge(void* arg) - for (i = thr_ID; i < R_INF.total_reads; i = i + thread_num) + for (i = thr_ID; i < R_INF.total_reads; i = i + asm_opt.thread_num) { get_new_candidates(i, &g_read, &overlap_list, &array_list, &heap, &l, 0.001, 0); @@ -3222,7 +1659,6 @@ void* Final_overlap_calculate_heap_merge(void* arg) correct_overlap(&overlap_list, &R_INF, &g_read, &correct, &overlap_read, &POA_Graph, &DAGCon, &matched_overlap_0, &matched_overlap_1, &potiental_matched_overlap_0, &potiental_matched_overlap_1, ¤t_cigar, &hap, &second_round, 0, 0); - push_final_overlaps(&(R_INF.paf[i]), &overlap_list); **/ @@ -3239,7 +1675,6 @@ void* Final_overlap_calculate_heap_merge(void* arg) ///Final_phasing(&overlap_list, &cigarline, &g_read, &overlap_read, c2n); - push_final_overlaps(&(R_INF.paf[i]), R_INF.reverse_paf, &overlap_list, 1); push_final_overlaps(&(R_INF.reverse_paf[i]), R_INF.reverse_paf, @@ -3248,9 +1683,6 @@ void* Final_overlap_calculate_heap_merge(void* arg) } - - - finish_output_buffer(); destory_Candidates_list(&l); @@ -3263,24 +1695,29 @@ void* Final_overlap_calculate_heap_merge(void* arg) pthread_mutex_lock(&statistics); - complete_threads++; - if(complete_threads == thread_num) + asm_opt.complete_threads++; + if(asm_opt.complete_threads == asm_opt.thread_num) { - statistic(R_INF.paf, R_INF.reverse_paf, R_INF.total_reads); + if(VERBOSE >= 1) + { + statistic(R_INF.paf, R_INF.reverse_paf, R_INF.total_reads); + } } pthread_mutex_unlock(&statistics); free(arg); + + return NULL; } void Output_PAF() { - fprintf(stdout, "Writing PAF to disk ...... \n"); - char* paf_name = (char*)malloc(strlen(output_file_name)+5); - sprintf(paf_name, "%s.paf", output_file_name); + fprintf(stderr, "Writing PAF to disk ...... \n"); + char* paf_name = (char*)malloc(strlen(asm_opt.output_file_name)+5); + sprintf(paf_name, "%s.paf", asm_opt.output_file_name); FILE* output_file = fopen(paf_name, "w"); - long long i, j; + uint64_t i, j; ma_hit_t_alloc* sources = R_INF.paf; @@ -3292,7 +1729,7 @@ void Output_PAF() fwrite(Get_NAME(R_INF, Get_qn(sources[i].buffer[j])), 1, Get_NAME_LENGTH(R_INF, Get_qn(sources[i].buffer[j])), output_file); fwrite("\t", 1, 1, output_file); - fprintf(output_file, "%d\t", Get_READ_LENGTH(R_INF, Get_qn(sources[i].buffer[j]))); + fprintf(output_file, "%lu\t", Get_READ_LENGTH(R_INF, Get_qn(sources[i].buffer[j]))); fprintf(output_file, "%d\t", Get_qs(sources[i].buffer[j])); fprintf(output_file, "%d\t", Get_qe(sources[i].buffer[j])); if(sources[i].buffer[j].rev) @@ -3306,7 +1743,7 @@ void Output_PAF() fwrite(Get_NAME(R_INF, Get_tn(sources[i].buffer[j])), 1, Get_NAME_LENGTH(R_INF, Get_tn(sources[i].buffer[j])), output_file); fwrite("\t", 1, 1, output_file); - fprintf(output_file, "%d\t", Get_READ_LENGTH(R_INF, Get_tn(sources[i].buffer[j]))); + fprintf(output_file, "%lu\t", Get_READ_LENGTH(R_INF, Get_tn(sources[i].buffer[j]))); fprintf(output_file, "%d\t", Get_ts(sources[i].buffer[j])); fprintf(output_file, "%d\t", Get_te(sources[i].buffer[j])); fprintf(output_file, "%d\t", sources[i].buffer[j].ml); @@ -3359,8 +1796,6 @@ int check_cluster(uint64_t* list, long long listLen, ma_hit_t_alloc* paf, float } - - void rescue_edges(ma_hit_t_alloc* paf, ma_hit_t_alloc* rev_paf, long long readNum, long long rescue_threshold, float cluster_threshold) { @@ -3396,11 +1831,11 @@ long long readNum, long long rescue_threshold, float cluster_threshold) ///the read itself has these overlaps, but all related reads do not have ///we need to remove all overlaps from paf[i], and then add all overlaps to rev_paf[i] - if(edge_vector.n >= rescue_threshold && + if((long long)edge_vector.n >= rescue_threshold && check_cluster(edge_vector.a, edge_vector.n, paf, cluster_threshold) == 1) { - fprintf(stderr,"\nremove following %d edges...\n", edge_vector.n); - print_revise_edges(&(paf[i]), edge_vector_index.a, edge_vector_index.n); + // fprintf(stderr,"\nremove following %u edges...\n", edge_vector.n); + // print_revise_edges(&(paf[i]), edge_vector_index.a, edge_vector_index.n); add_overlaps(&(paf[i]), &(rev_paf[i]), edge_vector_index.a, edge_vector_index.n); remove_overlaps(&(paf[i]), edge_vector_index.a, edge_vector_index.n); @@ -3426,11 +1861,11 @@ long long readNum, long long rescue_threshold, float cluster_threshold) ///the read itself do not have these overlaps, but all related reads have ///we need to remove all overlaps from rev_paf[i], and then add all overlaps to paf[i] - if(edge_vector.n >= rescue_threshold && + if((long long)edge_vector.n >= rescue_threshold && check_cluster(edge_vector.a, edge_vector.n, paf, cluster_threshold) == 1) { - fprintf(stderr,"\nadd following %d edges...\n", edge_vector.n); - print_revise_edges(&(rev_paf[i]), edge_vector_index.a, edge_vector_index.n); + // fprintf(stderr,"\nadd following %u edges...\n", edge_vector.n); + // print_revise_edges(&(rev_paf[i]), edge_vector_index.a, edge_vector_index.n); remove_overlaps(&(rev_paf[i]), edge_vector_index.a, edge_vector_index.n); @@ -3447,24 +1882,22 @@ long long readNum, long long rescue_threshold, float cluster_threshold) } - void generate_overlaps(int last_round) { double start_time = Get_T(); - roundID = number_of_round - last_round; - fprintf(stdout, "Calculting final overlaps ...\n"); + asm_opt.roundID = asm_opt.number_of_round - last_round; + fprintf(stderr, "Begin calculting final overlaps ...\n"); Counting_multiple_thr(); Build_hash_table_multiple_thr(); - ///thread_num = 1; pthread_t *_r_threads; - _r_threads = (pthread_t *)malloc(sizeof(pthread_t)*thread_num); + _r_threads = (pthread_t *)malloc(sizeof(pthread_t) * asm_opt.thread_num); int i = 0; - for (i = 0; i < thread_num; i++) + for (i = 0; i < asm_opt.thread_num; i++) { int *arg = (int*)malloc(sizeof(*arg)); *arg = i; @@ -3472,59 +1905,42 @@ void generate_overlaps(int last_round) } - for (i = 0; i +#define VERSION "0.0.0.1" -char* read_file_name = NULL; -char* output_file_name = NULL; -int thread_num = 1; -int k_mer_length = 40; -int coverage = -1; -//int k_mer_min_freq = 9; -int k_mer_min_freq = 3; -//int k_mer_min_freq = 2; -int k_mer_max_freq = 66; -int load_index_from_disk = 0; -int write_index_to_disk = 0; -int number_of_round = 1; -int read_graph = 0; -int c_round = 4; -int adapterLen = 0; -char* required_read_name = NULL; +hifiasm_opt_t asm_opt; double Get_T(void) { @@ -29,66 +15,197 @@ double Get_T(void) return t.tv_sec+t.tv_usec/1000000.0; } -void Print_H() +void Print_H(hifiasm_opt_t* asm_opt) { - fprintf(stderr, "Incorrect options.\n"); - fprintf(stderr, "./ccs_assembly -w -l -q NA12878_chr1_10M.fq -o output_NA12878_v11_2.fa -c 31 -k 40 -t 32 -r 2 -a 4 -z 0\n"); + fprintf(stderr, "Usage: hifiasm [options] -q -o \n"); + fprintf(stderr, "Options:\n"); + fprintf(stderr, " -q FILE input in the fastq(.gz)/fasta(.gz) formats\n"); + fprintf(stderr, " -k FILE output assembly (in gfa format) and corrected reads (in fasta format)\n"); + fprintf(stderr, " -t INT number of threads [%d]\n", asm_opt->thread_num); + fprintf(stderr, " -r INT round of correction [%d]\n", asm_opt->number_of_round); + fprintf(stderr, " -a INT round of assembly cleaning [%d]\n", asm_opt->clean_round); + fprintf(stderr, " -k INT k-mer length [%d] (must be < 64)\n", asm_opt->k_mer_length); + fprintf(stderr, " -w write all overlaps to disk, can accelerate assembly next time\n"); + fprintf(stderr, " -l load all overlaps from disk, can avoid overlap calculation\n"); + fprintf(stderr, " -z INT length of adapters that should be removed [%d]\n", asm_opt->adapterLen); + fprintf(stderr, " -p INT size of popped bubbles [%lld]\n", asm_opt->pop_bubble_size); + fprintf(stderr, " -x FLOAT max overlap drop ratio [%.2g]\n", asm_opt->max_drop_rate); + fprintf(stderr, " -y FLOAT min overlap drop ratio [%.2g]\n", asm_opt->min_drop_rate); + fprintf(stderr, " -v show version number\n"); + fprintf(stderr, " -h show help information\n"); + fprintf(stderr, "Example: ./hifiasm -w -l -q NA12878.fq.gz -o NA12878.asm -k 40 -t 32 -r 2 -a 4 -z 0\n"); + } - - - -int CommandLine_process (int argc, char *argv[]) +void init_opt(hifiasm_opt_t* asm_opt) { + asm_opt->read_file_name = NULL; + asm_opt->output_file_name = NULL; + asm_opt->required_read_name = NULL; + asm_opt->thread_num = 1; + asm_opt->k_mer_length = 40; + asm_opt->k_mer_min_freq = 3; + asm_opt->k_mer_max_freq = 66; + asm_opt->load_index_from_disk = 0; + asm_opt->write_index_to_disk = 0; + asm_opt->number_of_round = 2; + asm_opt->adapterLen = 0; + asm_opt->clean_round = 4; + asm_opt->complete_threads = 0; + asm_opt->pop_bubble_size = 100000; + asm_opt->min_drop_rate = 0.2; + asm_opt->max_drop_rate = 0.8; +} - static ko_longopt_t longopts[] = { - { "help", ko_no_argument, 100}, - { "seq", ko_required_argument, 101}, - { "output", ko_required_argument, 102}, - { "thread", ko_required_argument, 103}, - { "k_mer_min_freq", ko_required_argument, 104}, - { "k_mer_max_freq", ko_required_argument, 105}, - { "round", ko_required_argument, 106}, - { NULL, 0, 0 } - }; +void clear_opt(hifiasm_opt_t* asm_opt, int last_round) +{ + asm_opt->complete_threads = 0; + asm_opt->num_bases = 0; + asm_opt->num_corrected_bases = 0; + asm_opt->num_recorrected_bases = 0; + asm_opt->roundID = asm_opt->number_of_round - last_round; +} - ketopt_t opt = KETOPT_INIT; - int i, c; - while ((c = ketopt(&opt, argc, argv, 1, "ht:o:q:k:lwm:n:r:c:a:b:z:", longopts)) >= 0) { - if (c == 100 || c == 'h') Print_H(); - else if (c == 103 || c == 't') thread_num = atoi(opt.arg); - else if (c == 102 || c == 'o') output_file_name = opt.arg; - else if (c == 101 || c == 'q') read_file_name = opt.arg; - else if (c == 104 || c == 'n') k_mer_min_freq = atoi(opt.arg); - else if (c == 105 || c == 'm') k_mer_max_freq = atoi(opt.arg); - else if (c == 106 || c == 'r') number_of_round = atoi(opt.arg); - else if (c == 'k') k_mer_length = atoi(opt.arg); - else if (c == 'l') load_index_from_disk = 1; - else if (c == 'w') write_index_to_disk = 1; - else if (c == 'c') coverage = atoi(opt.arg); - else if (c == 'a') c_round = atoi(opt.arg); - else if (c == 'z') adapterLen = atoi(opt.arg); - else if (c == 'b') required_read_name = opt.arg; - else if (c == '?') printf("unknown opt: -%c\n", opt.opt? opt.opt : ':'); - else if (c == ':') printf("missing arg: -%c\n", opt.opt? opt.opt : ':'); +int check_option(hifiasm_opt_t* asm_opt) +{ + if(asm_opt->read_file_name == NULL) + { + fprintf(stderr, "[ERROR] missing input: please specify a read file\n"); + return 0; } + if(asm_opt->output_file_name == NULL) + { + fprintf(stderr, "[ERROR] missing output: please specify the output name\n"); + return 0; + } + + if(asm_opt->thread_num < 1) + { + fprintf(stderr, "[ERROR] the number of threads must be > 0\n"); + return 0; + } + + + if(asm_opt->number_of_round < 1) + { + fprintf(stderr, "[ERROR] the number of rounds for correction must be > 0\n"); + return 0; + } + + if(asm_opt->clean_round < 1) + { + fprintf(stderr, "[ERROR] the number of rounds for assembly cleaning must be > 0\n"); + return 0; + } + + if(asm_opt->adapterLen < 0) + { + fprintf(stderr, "[ERROR] the length of removed adapters must be >= 0\n"); + return 0; + } + + + if(asm_opt->k_mer_length >= 64) + { + fprintf(stderr, "[ERROR] the length of k_mer must be < 64\n"); + return 0; + } + + + if(asm_opt->max_drop_rate < 0 || asm_opt->max_drop_rate >= 1 ) + { + fprintf(stderr, "[ERROR] max overlap drop ratio must be [0.0, 1.0)\n"); + return 0; + } + + + if(asm_opt->min_drop_rate < 0 || asm_opt->min_drop_rate >= 1) + { + fprintf(stderr, "[ERROR] min overlap drop ratio must be [0.0, 1.0)\n"); + return 0; + } + + if(asm_opt->max_drop_rate <= asm_opt->min_drop_rate) + { + fprintf(stderr, "[ERROR] min overlap drop ratio must be less than max overlap drop ratio\n"); + return 0; + } + + if(asm_opt->pop_bubble_size < 0) + { + fprintf(stderr, "[ERROR] the size of popped bubbles must be >= 0\n"); + return 0; + } + + + // fprintf(stderr, "input file: %s\n", asm_opt->read_file_name); + // fprintf(stderr, "output file: %s\n", asm_opt->output_file_name); + // fprintf(stderr, "number of threads: %d\n", asm_opt->thread_num); + // fprintf(stderr, "number of rounds for correction: %d\n", asm_opt->number_of_round); + // fprintf(stderr, "number of rounds for assembly cleaning: %d\n", asm_opt->clean_round); + // fprintf(stderr, "length of removed adapters: %d\n", asm_opt->adapterLen); + // fprintf(stderr, "length of k_mer: %d\n", asm_opt->k_mer_length); + // fprintf(stderr, "min overlap drop ratio: %.2g\n", asm_opt->min_drop_rate); + // fprintf(stderr, "max overlap drop ratio: %.2g\n", asm_opt->max_drop_rate); + // fprintf(stderr, "size of popped bubbles: %lld\n", asm_opt->pop_bubble_size); + + return 1; +} + + +int CommandLine_process(int argc, char *argv[], hifiasm_opt_t* asm_opt) +{ + ketopt_t opt = KETOPT_INIT; + + int c; + + while ((c = ketopt(&opt, argc, argv, 1, "hvt:o:q:k:lwm:n:r:a:b:z:x:y:p:", 0)) >= 0) { + if (c == 'h') + { + Print_H(asm_opt); + return 0; + } + else if (c == 'v') + { + fprintf(stderr, "[Version] %s\n", VERSION); + return 0; + } + else if (c == 't') asm_opt->thread_num = atoi(opt.arg); + else if (c == 'o') asm_opt->output_file_name = opt.arg; + else if (c == 'q') asm_opt->read_file_name = opt.arg; + else if (c == 'n') asm_opt->k_mer_min_freq = atoi(opt.arg); + else if (c == 'm') asm_opt->k_mer_max_freq = atoi(opt.arg); + else if (c == 'r') asm_opt->number_of_round = atoi(opt.arg); + else if (c == 'k') asm_opt->k_mer_length = atoi(opt.arg); + else if (c == 'l') asm_opt->load_index_from_disk = 1; + else if (c == 'w') asm_opt->write_index_to_disk = 1; + else if (c == 'a') asm_opt->clean_round = atoi(opt.arg); + else if (c == 'z') asm_opt->adapterLen = atoi(opt.arg); + else if (c == 'b') asm_opt->required_read_name = opt.arg; + else if (c == 'x') asm_opt->max_drop_rate = atof(opt.arg); + else if (c == 'y') asm_opt->min_drop_rate = atof(opt.arg); + else if (c == 'p') asm_opt->pop_bubble_size = atoll(opt.arg); + else if (c == ':') + { + fprintf(stderr, "[ERROR] missing option argument in \"%s\"\n", argv[opt.i - 1]); + return 0; + } + else if (c == '?') + { + fprintf(stderr, "[ERROR] unknown option in \"%s\"\n", argv[opt.i - 1]); + return 0; + } + } if (argc == 1) { - Print_H(); - return 0; - } - - if(coverage == -1) - { - fprintf(stdout, "Please set -c!\n"); - Print_H(); + Print_H(asm_opt); return 0; } - return 1; + return check_option(asm_opt); } \ No newline at end of file diff --git a/CommandLines.h b/CommandLines.h index b433ab9..f451c7f 100644 --- a/CommandLines.h +++ b/CommandLines.h @@ -3,25 +3,37 @@ #include +#define VERBOSE 0 -extern char* read_file_name; -extern char* output_file_name; -extern int thread_num; -extern int k_mer_length; -extern int k_mer_min_freq; -extern int k_mer_max_freq; -extern int load_index_from_disk; -extern int write_index_to_disk; -extern int number_of_round; -extern int roundID; -extern int coverage; -extern int read_graph; -extern int c_round; -extern char* required_read_name; -extern int adapterLen; +typedef struct { + char* read_file_name; + char* output_file_name; + char* required_read_name; + int thread_num; + int k_mer_length; + int k_mer_min_freq; + int k_mer_max_freq; + int load_index_from_disk; + int write_index_to_disk; + int number_of_round; + int adapterLen; + int clean_round; + int complete_threads; + int roundID; + float min_drop_rate; + float max_drop_rate; + long long pop_bubble_size; + long long num_bases; + long long num_corrected_bases; + long long num_recorrected_bases; +} hifiasm_opt_t; -int CommandLine_process (int argc, char *argv[]); +extern hifiasm_opt_t asm_opt; + +void init_opt(hifiasm_opt_t* asm_opt); +void clear_opt(hifiasm_opt_t* asm_opt, int last_round); +int CommandLine_process (int argc, char *argv[], hifiasm_opt_t* asm_opt); double Get_T(void); #endif \ No newline at end of file diff --git a/Correct.cpp b/Correct.cpp index 148b73b..22ff237 100644 --- a/Correct.cpp +++ b/Correct.cpp @@ -4,83 +4,13 @@ #include #include "Correct.h" #include "Levenshtein_distance.h" -#include "edlib.h" #include "Assembly.h" #include "CommandLines.h" -#include "ksw2.h" - -long long T_total_match=0; -long long T_total_unmatch=0; -long long T_total_mis=0; -pthread_mutex_t debug_statistics ; +///#include "ksw2.h" -void align(const char *tseq, const char *qseq, const int tl, const int ql, -const uint8_t *c, int sc_mch, int sc_mis, int gapo, int gape, int bandLen, int zdrop, -int* max_q_pos, int* max_t, int* score) -{ - int i, a = sc_mch, b = sc_mis < 0? sc_mis : -sc_mis; // a>0 and b<0 - int8_t mat[25] = {a,b,b,b,0, b,a,b,b,0, b,b,a,b,0, b,b,b,a,0, 0,0,0,0,0}; - uint8_t *ts, *qs; - ksw_extz_t ez; - - memset(&ez, 0, sizeof(ksw_extz_t)); - ts = (uint8_t*)malloc(tl); - qs = (uint8_t*)malloc(ql); - for (i = 0; i < tl; ++i) ts[i] = c[(uint8_t)tseq[i]]; // encode to 0/1/2/3 - for (i = 0; i < ql; ++i) qs[i] = c[(uint8_t)qseq[i]]; - ///ksw_extz(0, ql, qs, tl, ts, 5, mat, gapo, gape, -1, -1, 0, &ez); - ksw_extz2_sse(0, ql, qs, tl, ts, 5, mat, gapo, gape, bandLen, zdrop, sc_mch, 0, &ez); - /** - for (i = 0; i < ez.n_cigar; ++i) // print CIGAR - printf("%d%c", ez.cigar[i]>>4, "MID"[ez.cigar[i]&0xf]); - putchar('\n'); - **/ - free(ez.cigar); free(ts); free(qs); - (*score) = ez.max; -} - - -void afine_gap_alignment(const char *tseq, uint8_t* tnum, const int tl, -const char *qseq, uint8_t* qnum, const int ql, const uint8_t *c2n, const int strand, -int sc_mch, int sc_mis, int gapo, int gape, int bandLen, int zdrop, int end_bonus, -long long* max_t_pos, long long* max_q_pos, long long* score, long long* droped) -{ - (*max_t_pos) = (*max_q_pos) = -1; - int i, a = sc_mch, b = sc_mis < 0? sc_mis : -sc_mis; // a>0 and b<0 - int8_t mat[25] = {a,b,b,b,0, b,a,b,b,0, b,b,a,b,0, b,b,b,a,0, 0,0,0,0,0}; - ksw_extz_t ez; - memset(&ez, 0, sizeof(ksw_extz_t)); - - if(strand == FORWARD_KSW) - { - for (i = 0; i < tl; ++i) tnum[i] = c2n[(uint8_t)tseq[i]]; // encode to 0/1/2/3 - for (i = 0; i < ql; ++i) qnum[i] = c2n[(uint8_t)qseq[i]]; - } - else if(strand == BACKWARD_KSW) - { - for (i = 0; i < tl; ++i) tnum[i] = c2n[(uint8_t)tseq[tl - i - 1]]; // encode to 0/1/2/3 - for (i = 0; i < ql; ++i) qnum[i] = c2n[(uint8_t)qseq[ql - i - 1]]; - } - - ksw_extz2_sse(0, ql, qnum, tl, tnum, 5, mat, gapo, gape, bandLen, zdrop, end_bonus, 0, &ez); - - (*score) = ez.max; - (*max_t_pos) = ez.max_t; - (*max_q_pos) = ez.max_q; - (*droped) = ez.zdropped; - - /** - for (i = 0; i < ez.n_cigar; ++i) // print CIGAR - printf("%d%c", ez.cigar[i]>>4, "MID"[ez.cigar[i]&0xf]); - putchar('\n'); - **/ - free(ez.cigar); -} - - void clear_Round2_alignment(Round2_alignment* h) { clear_Correct_dumy_pure(&(h->dumy)); @@ -106,216 +36,31 @@ void destory_Round2_alignment(Round2_alignment* h) -///y_length > x_length -unsigned int edit_distance_normal_test_banded(char* y, int y_length, char* x, int x_length, int error_cut, int matrix[1000][1000] ) -{ memset(matrix, 0, sizeof(matrix)); - - int i, j; - for (i = 0; i <= x_length; i++) - { - matrix[i][0] = i; - } - - int digonal, up, left; - unsigned int min; - - ///一列列算的 - for (i = 0; i < x_length; i++) - { - for (j = 0; j < y_length; j++) - { - ///matrix[i + 1][j + 1] - digonal = matrix[i][j] + (x[i] != y[j]); - up = matrix[i + 1][j] + 1; - left = matrix[i][j + 1] + 1; - min = digonal; - if (up < min) - { - min = up; - } - - if (left< min) - { - min = left; - } - - matrix[i + 1][j + 1] = min; - } - } - - min = (unsigned int)-1; - for (j = x_length; j <= y_length; j++) - { - if (matrix[i][j] < min) - { - min = matrix[i][j]; - } - } - - - return min <= error_cut?min:(unsigned int)(-1); -} - -void verify_get_interval(long long window_start, long long window_end, overlap_region_alloc* overlap_list,Correct_dumy* dumy) -{ - long long i; - long long match_length = 0; - long long match_lengthNT = 0; - long long Len; - - for (i = 0; i < overlap_list->length; i++) - { - if((Len = OVERLAP(window_start, window_end, overlap_list->list[i].x_pos_s, overlap_list->list[i].x_pos_e)) > 0) - { - if (Len == WINDOW) - { - match_length++; - long long j; - for (j = 0; j < dumy->length; j++) - { - if (i==dumy->overlapID[j]) - { - break; - } - } - - if (j >= dumy->length) - { - fprintf(stderr, "+ERROR interval\n"); - - fprintf(stderr, "i: %u, window_start: %u, window_end: %u, x_pos_s: %u, x_pos_e: %u\n", - i, window_start, window_end, overlap_list->list[i].x_pos_s, overlap_list->list[i].x_pos_e); - } - } - else - { - match_lengthNT++; - long long j; - for (j = 0; j < dumy->lengthNT; j++) - { - if (i==dumy->overlapID[dumy->size - j - 1]) - { - break; - } - } - - if (j >= dumy->lengthNT) - { - fprintf(stderr, "-ERROR interval\n"); - - fprintf(stderr, "i: %u, window_start: %u, window_end: %u, x_pos_s: %u, x_pos_e: %u, dumy->lengthNT: %u\n", - i, window_start, window_end, overlap_list->list[i].x_pos_s, overlap_list->list[i].x_pos_e, dumy->lengthNT); - } - } - - - - - - } - } - - if (match_length != dumy->length || match_lengthNT != dumy->lengthNT) - { - fprintf(stderr, "****************ERROR interval length*******************\n"); - fprintf(stderr, "match_length: %u\n", match_length); - fprintf(stderr, "dumy->length: %u\n", dumy->length); - fprintf(stderr, "window_start: %u, window_end: %u\n", window_start, window_end); - } - -} - - -inline int get_interval_back(long long window_start, long long window_end, overlap_region_alloc* overlap_list, Correct_dumy* dumy) -{ - long long i; - int flag = 0; - - for (i = dumy->start_i; i < overlap_list->length; i++) - { - ///只会发生在这个interval比list里所有元素都小的情况 - ///这种情况下一个interval需要从0开始 - if (window_start < overlap_list->list[i].x_pos_s) - { - dumy->start_i = 0; - dumy->length = 0; - return -1; - } - else if(window_start >= overlap_list->list[i].x_pos_s && window_start <= overlap_list->list[i].x_pos_e) - { - dumy->start_i = i; - break; - } - } - - ///只会发生在这个window比list里所有元素都大的情况 - ///这种情况下一个window也无需遍历了 - if (i >= overlap_list->length) - { - dumy->start_i = overlap_list->length; - dumy->length = 0; - return -2; - } - - ///走到这里的时候,至少window_start的要求是满足了 - dumy->length = 0; - - for (; i < overlap_list->length; i++) - { - if(overlap_list->list[i].x_pos_s <= window_start && overlap_list->list[i].x_pos_e >= window_end) - { - dumy->overlapID[dumy->length] = i; - dumy->length++; - } - else if(overlap_list->list[i].x_pos_s > window_start) - { - break; - } - } - - if ( dumy->length == 0) - { - return 0; - } - else - { - return 1; - } -} - - inline int get_interval(long long window_start, long long window_end, overlap_region_alloc* overlap_list, Correct_dumy* dumy) { - long long i; - int flag = 0; - long long Begin, End, Len; - - // fprintf(stderr, "overlap_list->length: %d, dumy->size: %d, dumy->start_i: %d\n", - // overlap_list->length, dumy->size, dumy->start_i); - // fflush(stderr); + uint64_t i; + long long Len; for (i = dumy->start_i; i < overlap_list->length; i++) { - ///只会发生在这个interval比list里所有元素都小的情况 - ///这种情况下一个interval需要从0开始 - if (window_end < overlap_list->list[i].x_pos_s) + ///this interval is smaller than all overlaps + ///in this case, the next interval should start from 0 + if (window_end < (long long)overlap_list->list[i].x_pos_s) { dumy->start_i = 0; dumy->length = 0; dumy->lengthNT = 0; return 0; } - else ///只要window_end >= overlap_list->list[i].x_pos_s,就有可能重叠 + else ///if window_end >= overlap_list->list[i].x_pos_s,this overlap might be overlapped with current interval { dumy->start_i = i; break; } } - - ///只会发生在这个window比list里所有元素都大的情况 - ///这种情况下一个window也无需遍历了 + ///this interval is larger than all overlaps, so we don't need to scan next overlap if (i >= overlap_list->length) { dumy->start_i = overlap_list->length; @@ -329,16 +74,7 @@ inline int get_interval(long long window_start, long long window_end, overlap_re for (; i < overlap_list->length; i++) { - // fprintf(stderr, "inner i: %d, x_pos_s: %d, x_pos_e: %d, window_start: %d, window_end: %d\n", - // i, overlap_list->list[i].x_pos_s, overlap_list->list[i].x_pos_e, - // window_start, window_end); - // fflush(stderr); - - // fprintf(stderr, "dumy->length: %d, dumy->lengthNT: %d, dumy->size: %d\n", - // dumy->length, dumy->lengthNT, dumy->size); - // fflush(stderr); - - if((Len = OVERLAP(window_start, window_end, overlap_list->list[i].x_pos_s, overlap_list->list[i].x_pos_e)) > 0) + if((Len = OVERLAP(window_start, window_end, (long long)overlap_list->list[i].x_pos_s, (long long)overlap_list->list[i].x_pos_e)) > 0) { ///sometimes the length of window > WINDOW, but overlap length == WINDOW if (Len == WINDOW && window_end - window_start + 1 == WINDOW) @@ -353,7 +89,7 @@ inline int get_interval(long long window_start, long long window_end, overlap_re } } - if(overlap_list->list[i].x_pos_s > window_end) + if((long long)overlap_list->list[i].x_pos_s > window_end) { break; } @@ -372,32 +108,29 @@ inline int get_interval(long long window_start, long long window_end, overlap_re inline int get_available_interval(long long window_start, long long window_end, overlap_region_alloc* overlap_list, Correct_dumy* dumy) { - long long i; - int flag = 0; - long long Begin, End, Len; - long long overlap_length; + uint64_t i; + long long Len; for (i = dumy->start_i; i < overlap_list->length; i++) { - ///只会发生在这个interval比list里所有元素都小的情况 - ///这种情况下一个interval需要从0开始 - if (window_end < overlap_list->list[i].x_pos_s) + ///this interval is smaller than all overlaps + ///in this case, the next interval should start from 0 + if (window_end < (long long)overlap_list->list[i].x_pos_s) { dumy->start_i = 0; dumy->length = 0; dumy->lengthNT = 0; return 0; } - else ///只要window_end >= overlap_list->list[i].x_pos_s,就有可能重叠 + else ///if window_end >= overlap_list->list[i].x_pos_s,this overlap might be overlapped with current interval { dumy->start_i = i; break; } } - ///只会发生在这个window比list里所有元素都大的情况 - ///这种情况下一个window也无需遍历了 + ///this interval is larger than all overlaps, so we don't need to scan next overlap if (i >= overlap_list->length) { dumy->start_i = overlap_list->length; @@ -415,14 +148,13 @@ inline int get_available_interval(long long window_start, long long window_end, for (; i < overlap_list->length; i++) { - ///是否重叠 - if((Len = OVERLAP(window_start, window_end, overlap_list->list[i].x_pos_s, overlap_list->list[i].x_pos_e)) > 0) + ///check if the interval is overlapped with current overlap + if((Len = OVERLAP(window_start, window_end, (long long)overlap_list->list[i].x_pos_s, (long long)overlap_list->list[i].x_pos_e)) > 0) { - ///重叠数量 + ///number of overlaps fake_length++; - ///重叠是否有效 - overlap_length = overlap_list->list[i].x_pos_e - overlap_list->list[i].x_pos_s + 1; + ///check if this overlap is available if (overlap_list->list[i].is_match == 1) { dumy->overlapID[dumy->length] = i; @@ -430,13 +162,13 @@ inline int get_available_interval(long long window_start, long long window_end, } } - if(overlap_list->list[i].x_pos_s > window_end) + if((long long)overlap_list->list[i].x_pos_s > window_end) { break; } } - ///fake_length是重叠的数量,而不是有效重叠的数量 + ///fake_length is the number of overlaps, instead of the number of available overlaps if (fake_length == 0) { return 0; @@ -451,7 +183,8 @@ inline int get_available_interval(long long window_start, long long window_end, void print_string(char* s, int l) { - for (size_t i = 0; i < l; i++) + int i; + for (i = 0; i < l; i++) { fprintf(stderr, "%c", s[i]); } @@ -460,96 +193,7 @@ void print_string(char* s, int l) } -void test_edit_distance_by_edlib(char* x_string, char* y_string, long long x_len, -long long o_len, int threashold, int error, long long* total_mis) -{ - EdlibAlignResult result = edlibAlign(x_string, x_len, y_string, o_len, - edlibNewAlignConfig(threashold, EDLIB_MODE_HW, EDLIB_TASK_PATH, NULL, 0)); - if (result.status == EDLIB_STATUS_OK) { - if (result.editDistance != error) - { - - (*total_mis)++; - - - if ((int)error != -1 && result.editDistance==-1) - { - fprintf(stderr, "ERROR1\n"); - } - - if ((int)error < result.editDistance==-1 && - (int)error != -1 && result.editDistance!=-1) - { - fprintf(stderr, "ERROR2\n"); - } - - int up_length = o_len - result.endLocations[0] - 1; - int left_length = result.endLocations[0] - x_len; - - - char* cigar = edlibAlignmentToCigar(result.alignment, result.alignmentLength, EDLIB_CIGAR_STANDARD); - int cigar_length = strlen(cigar); - - int i = cigar_length - 1; - int j = 0; - char tmp; - - - while (i >= 0) - { - if (up_length < 0 || left_length < 0) - { - break; - } - - if (cigar[i] == 'I' || cigar[i] == 'D') - { - tmp = cigar[i]; - cigar[i] = '\0'; - j = i - 1; - while (cigar[j] <= '9' && cigar[j] >= '0' && j >= 0) - { - j--; - } - j++; - int Len = atoi(cigar + j); - cigar[i] = tmp; - i = j - 1; - - if (tmp == 'I') - { - up_length = up_length - Len; - left_length = left_length + Len; - } - else - { - left_length = left_length - Len; - up_length = up_length + Len; - } - } - else - { - i--; - } - } - - - if (up_length >= 0 && left_length >= 0) - { - fprintf(stderr, "****\nedlib: %d, alignmentLength: %d, startLocations: %d, endLocations: %d\n", - result.editDistance, result.alignmentLength, result.startLocations[0], result.endLocations[0]); - fprintf(stderr,"%s\n", cigar); - print_string(x_string, x_len); - print_string(y_string, o_len); - fprintf(stderr, "BPM: %d\n", error); - } - - free(cigar); - } - } - edlibFreeAlignResult(result); -} void fill_subregion(char* r, long long start_pos, long long length, uint8_t strand, All_reads* R_INF, long long ID, @@ -611,7 +255,6 @@ All_reads* R_INF) int extra_begin, extra_end, x_len, threshold; long long y_start; long long Window_Len, o_len; - int end_site; unsigned int error; @@ -646,12 +289,7 @@ All_reads* R_INF) x_string = x_buffer; y_string = y_buffer; - end_site = Reserve_Banded_BPM(y_string, Window_Len, x_string, x_len, threshold, &error); - - // if(y_id == 439960 && (x_id == 439950 || x_id == 5598234)) - // { - // fprintf(stderr, "y_id: %d, x_id: %d, error: %llu", y_id, x_id, error); - // } + Reserve_Banded_BPM(y_string, Window_Len, x_string, x_len, threshold, &error); if (error!=(unsigned int)-1) { @@ -664,9 +302,8 @@ All_reads* R_INF) void verify_window(long long window_start, long long window_end, overlap_region_alloc* overlap_list,Correct_dumy* dumy, All_reads* R_INF, char* r_string) { - long long i; - long long currentID, currentIDLen; + long long currentID; long long x_start, y_start, o_len; long long Window_Len = WINDOW + (THRESHOLD << 1); char* x_string = NULL; @@ -685,15 +322,15 @@ char* r_string) int extra_begin; int extra_end; - ///这些是整个window被完全覆盖的 - for (i = 0; i < dumy->length; i++) + ///here are overlaps fully covered by WINDOW + for (i = 0; i < (long long)dumy->length; i++) { extra_begin = extra_end = 0; - ///整个window被覆盖的话,read本身上的区间就是[window_start, window_end] + ///if the window has been fully covered, the interval at x is [window_start, window_end] x_len = WINDOW; currentID = dumy->overlapID[i]; x_start = window_start; - ///y上的相对位置 + ///offset of y y_start = (x_start - overlap_list->list[currentID].x_pos_s) + overlap_list->list[currentID].y_pos_s; /****************************may have bugs********************************/ y_start += y_start_offset(x_start, &(overlap_list->list[currentID].f_cigar)); @@ -711,9 +348,6 @@ char* r_string) fill_subregion(dumy->overlap_region_group[groupLen], y_start, o_len, overlap_list->list[currentID].y_pos_strand, R_INF, overlap_list->list[currentID].y_id, extra_begin, extra_end); - // fprintf(stderr, "!i: %d\n", i); - // fflush(stderr); - y_extra_begin[groupLen] = extra_begin; y_extra_end[groupLen] = extra_end; overlapID[groupLen] = currentID; @@ -792,16 +426,10 @@ char* r_string) } } - // fprintf(stderr, "(1) dumy->size: %d\n", dumy->size); - // fflush(stderr); - - if (groupLen == 1) { end_site = Reserve_Banded_BPM(dumy->overlap_region_group[0], Window_Len, x_string, WINDOW, THRESHOLD, &error); - - if (error!=(unsigned int)-1) { overlap_list->list[overlapID[0]].align_length += x_len; @@ -842,70 +470,30 @@ char* r_string) groupLen = 0; } - - // fprintf(stderr, "(2) dumy->size: %d\n", dumy->size); - // fflush(stderr); - - long long reverse_i = dumy->size - 1; int threshold; - ///这些是整个window被部分覆盖的 - for (i = 0; i < dumy->lengthNT; i++) + ///here are overlaps partially covered by WINDOW + for (i = 0; i < (long long)dumy->lengthNT; i++) { - extra_begin = extra_end = 0; currentID = dumy->overlapID[reverse_i--]; - x_start = MAX(window_start, overlap_list->list[currentID].x_pos_s); - x_end = MIN(window_end, overlap_list->list[currentID].x_pos_e); + x_start = MAX(window_start, (long long)overlap_list->list[currentID].x_pos_s); + x_end = MIN(window_end, (long long)overlap_list->list[currentID].x_pos_e); - - ///这个是和当前窗口重叠的长度 + ///overlap length between [window_start, window_end] x_len = x_end - x_start + 1; threshold = x_len * THRESHOLD_RATE; /****************************may have bugs********************************/ threshold = Adjust_Threshold(threshold, x_len); /****************************may have bugs********************************/ - - ///y上的相对位置 + ///offset of y y_start = (x_start - overlap_list->list[currentID].x_pos_s) + overlap_list->list[currentID].y_pos_s; /****************************may have bugs********************************/ y_start += y_start_offset(x_start, &(overlap_list->list[currentID].f_cigar)); /****************************may have bugs********************************/ - - - - - - // fprintf(stderr, "lengthNT: %d, i: %d, window_start: %d, window_end: %d, x_start: %d, x_end: %d\n", - // dumy->lengthNT, i, window_start, window_end, x_start, x_end); - // fflush(stderr); - - // fprintf(stderr, "x_pos_s: %d, x_pos_e: %d, y_pos_s: %d, y_pos_e: %d\n", - // overlap_list->list[currentID].x_pos_s, - // overlap_list->list[currentID].x_pos_e, - // overlap_list->list[currentID].y_pos_s, - // overlap_list->list[currentID].y_pos_e); - - // fprintf(stderr, "x_id: %d, x_length: %d, y_id: %d, y_length: %d, y_pos_strand: %d\n", - // overlap_list->list[currentID].x_id, - // Get_READ_LENGTH((*R_INF), overlap_list->list[currentID].x_id), - // overlap_list->list[currentID].y_id, - // Get_READ_LENGTH((*R_INF), overlap_list->list[currentID].y_id), - // overlap_list->list[currentID].y_pos_strand); - // fflush(stderr); - - // fprintf(stderr, "y_start: %d, y_start_offset: %d\n", y_start, - // y_start_offset(x_start, &(overlap_list->list[currentID].f_cigar))); - // print_fake_gap(&(overlap_list->list[currentID].f_cigar)); - // fflush(stderr); - - // fprintf(stderr, "(31) i: %d, dumy->size: %d, y_start: %d, yLen: %d\n", i, dumy->size, y_start, - // Get_READ_LENGTH((*R_INF), overlap_list->list[currentID].y_id)); - // fflush(stderr); - Window_Len = x_len + (threshold << 1); if(!determine_overlap_region(threshold, y_start, overlap_list->list[currentID].y_id, Window_Len, R_INF, @@ -915,49 +503,15 @@ char* r_string) -1, -1, -1, -1, -1, -1); continue; } - - /** - if(overlap_list->list[currentID].x_id == 18390 - && overlap_list->list[currentID].y_id == 18419) - { - fprintf(stderr, "x_start: %d, x_len: %d, y_start: %d, y_offset: %d, extra_begin: %d, extra_end: %d, o_len: %d\n", - x_start, x_len, y_start, - y_start_offset(x_start, &(overlap_list->list[currentID].f_cigar)), - extra_begin, extra_end, o_len); - - } - **/ - - - // fprintf(stderr, "(32) i: %d, dumy->size: %d, y_start: %d, o_len: %d, extra_begin: %d, extra_end: %d\n", - // i, dumy->size, y_start, o_len, extra_begin, extra_end); - // if(o_len == -13) - // { - // print_fake_gap(&(overlap_list->list[currentID].f_cigar)); - // } - // fflush(stderr); - fill_subregion(dumy->overlap_region, y_start, o_len, overlap_list->list[currentID].y_pos_strand, - R_INF, overlap_list->list[currentID].y_id, extra_begin, extra_end); - - - - // fprintf(stderr, "(333332) i: %d, dumy->size: %d, y_start: %d, yLen: %d, threshold: %d, Window_Len: %d\n", i, dumy->size, y_start, - // Get_READ_LENGTH((*R_INF), overlap_list->list[currentID].y_id), threshold, Window_Len); - // fflush(stderr); - + R_INF, overlap_list->list[currentID].y_id, extra_begin, extra_end); x_string = r_string + x_start; y_string = dumy->overlap_region; - end_site = Reserve_Banded_BPM(y_string, Window_Len, x_string, x_len, threshold, &error); - - // fprintf(stderr, "(33) dumy->size: %d\n", dumy->size); - // fflush(stderr); - if (error!=(unsigned int)-1) { overlap_list->list[currentID].align_length += x_len; @@ -970,49 +524,6 @@ char* r_string) extra_begin, extra_end, threshold); } } - - // fprintf(stderr, "(3) dumy->size: %d\n", dumy->size); - // fflush(stderr); - - - // fprintf(stderr, "************groupLen: %d\n", groupLen); - // fflush(stderr); -} - -void debug_stats(overlap_region_alloc* overlap_list, All_reads* R_INF, - UC_Read* g_read, Correct_dumy* dumy, UC_Read* overlap_read, - long long* matched_overlap_0, long long* matched_overlap_1) -{ - long long j; - long long Len_x; - int threshold; - long long y_start; - long long Len_y; - long long currentIDLen; - for (j = 0; j < overlap_list->length; j++) - { - Len_x = overlap_list->list[j].x_pos_e - overlap_list->list[j].x_pos_s + 1; - - if (overlap_list->list[j].is_match == 1) - { - if (overlap_list->list[j].y_pos_strand == 0) - { - (*matched_overlap_0)++; - ///(*matched_overlap_0) = (*matched_overlap_0) + overlap_list->list[j].align_length; - ///(*matched_overlap_0) = (*matched_overlap_0) + Len_x; - } - else - { - (*matched_overlap_1)++; - ///(*matched_overlap_1) = (*matched_overlap_1) + overlap_list->list[j].align_length; - ///(*matched_overlap_1) = (*matched_overlap_1) + Len_x; - } - } - } - - - - } inline double trim_error_rate(overlap_region_alloc* overlap_list, long long ID) @@ -1123,7 +634,7 @@ UC_Read* g_read) end_site = Reserve_Banded_BPM(y_string, SubWindowLen, x_string, SubLen, SubThreshold, &error); - ///error等于-1说明没匹配 + ///if error = -1, unmatched if (error!=(unsigned int)-1) { T_error = T_error + error; @@ -1232,9 +743,9 @@ All_reads* R_INF, Correct_dumy* dumy, UC_Read* g_read) int x_len = subWinLen; int threshold = double_error_threshold(overlap_list->list[ID].w_list[i].error_threshold, x_len); int Window_Len = x_len + (threshold << 1); - unsigned int r_error_left; + unsigned int r_error_left = 0; int r_x_end_left, r_y_end_left, aligned_xLen_left; - unsigned int r_error_right; + unsigned int r_error_right = 0; int r_x_end_right, r_y_end_right, aligned_xLen_right; long long y_beg_left, y_beg_right; @@ -1254,7 +765,7 @@ All_reads* R_INF, Correct_dumy* dumy, UC_Read* g_read) } ///if the next window is mapped - if(i < overlap_list->list[ID].w_list_length - 1 && overlap_list->list[ID].w_list[i + 1].y_end != -1) + if(i < (long long)(overlap_list->list[ID].w_list_length - 1) && overlap_list->list[ID].w_list[i + 1].y_end != -1) { y_beg_right = 1 + overlap_list->list[ID].w_list[i + 1].y_start - 1 - x_len; } @@ -1296,19 +807,6 @@ All_reads* R_INF, Correct_dumy* dumy, UC_Read* g_read) threshold, 1, &r_error_right, &r_y_end_right, &r_x_end_right, &aligned_xLen_right); } - /** - if(i == 0 || i == subWinNum - 1) - { - if(((aligned_xLen_left + aligned_xLen_right) < x_len) - && - (x_len - (aligned_xLen_left + aligned_xLen_right) > 20)) - { - return 1.0; - } - } - **/ - - ///aligned in both direction if(aligned_xLen_left != 0 && aligned_xLen_right != 0) { @@ -1343,34 +841,6 @@ All_reads* R_INF, Correct_dumy* dumy, UC_Read* g_read) return error_rate; } - - -void mark_duplicate(overlap_region_alloc* overlap_list, All_reads* R_INF, - UC_Read* g_read, Correct_dumy* dumy, UC_Read* overlap_read) -{ - long long j, overlapLen; - double rate; - - - for (j = 0; j < overlap_list->length; j++) - { - if(overlap_list->list[j].is_match == 1) - { - rate = trim_error_rate(overlap_list, j); - - if(rate > 0.01) - { - overlapLen = overlap_list->list[j].x_pos_e - overlap_list->list[j].x_pos_s + 1; - overlap_list->mapped_overlaps_length -= overlapLen; - overlap_list->list[j].is_match = 0; - } - } - } - - -} - - int calculate_hpm_errors(char* x, int x_len, char* y, int y_len, CIGAR* cigar, int error) { int x_i, y_i, cigar_i; @@ -1383,9 +853,7 @@ int calculate_hpm_errors(char* x, int x_len, char* y, int y_len, CIGAR* cigar, i int cigar_error = 0; int hpm_error = 0; - ///0 is match, 1 is mismatch, 2 is up, 3 is left - ///2是x缺字符(y多字符),而3是y缺字符(x多字符) - ///while (x_i < x_len && y_i < y_len && cigar_i < cigar->length) + while (cigar_i < cigar->length) { operation = cigar->C_C[cigar_i]; @@ -1409,30 +877,17 @@ int calculate_hpm_errors(char* x, int x_len, char* y, int y_len, CIGAR* cigar, i x_i++; y_i++; } - }///2是x缺字符(y多字符) + } else if (operation == 2) { if(if_is_homopolymer_repeat(x_i, x, x_len) || if_is_homopolymer_repeat(y_i, y, y_len)) { hpm_error++; - }/** - else - { - if(x_i - 5 > 0 && x_i + 5 <= x_len - && - y_i - 5 > 0 && y_i + 5 <= y_len) - { - fprintf(stderr, "x: %.*s\ny: %.*s\n\n", 10, x + x_i - 5, 10, y + y_i - 5); - } - } - **/ - - cigar_error += operationLen; y_i += operationLen; - }///3是y缺字符(x多字符) + } else if (operation == 3) { @@ -1447,15 +902,10 @@ int calculate_hpm_errors(char* x, int x_len, char* y, int y_len, CIGAR* cigar, i cigar_i++; } - - - return hpm_error; - } - void count_no_HPM_errors(overlap_region_alloc* overlap_list, All_reads* R_INF, UC_Read* g_read, Correct_dumy* dumy, UC_Read* overlap_read, long long* total_errors, long long* total_hpm_errors) @@ -1463,7 +913,7 @@ void count_no_HPM_errors(overlap_region_alloc* overlap_list, All_reads* R_INF, long long j, i; - long long y_id, y_strand, y_readLen; + long long y_id, y_strand; long long x_start, x_end, x_len, y_start, y_end, y_len, error; char* x_string; char* y_string; @@ -1472,16 +922,15 @@ void count_no_HPM_errors(overlap_region_alloc* overlap_list, All_reads* R_INF, (*total_errors) = 0; (*total_hpm_errors) = 0; - for (j = 0; j < overlap_list->length; j++) + for (j = 0; j < (long long)overlap_list->length; j++) { y_id = overlap_list->list[j].y_id; y_strand = overlap_list->list[j].y_pos_strand; - y_readLen = Get_READ_LENGTH((*R_INF), y_id); if (overlap_list->list[j].is_match == 1) { ///for (i = 0; i < overlap_list->list[j].w_list_length; i++) - for (i = 1; i < overlap_list->list[j].w_list_length - 1; i++) + for (i = 1; i < (long long)overlap_list->list[j].w_list_length - 1; i++) { if(overlap_list->list[j].w_list[i].y_end != -1) { @@ -1518,56 +967,6 @@ void count_no_HPM_errors(overlap_region_alloc* overlap_list, All_reads* R_INF, } -void debug_output_overlaps(overlap_region_alloc* overlap_list, All_reads* R_INF, - UC_Read* g_read, Correct_dumy* dumy, UC_Read* overlap_read, - long long* matched_overlap_0, long long* matched_overlap_1) -{ - long long j; - long long Len_x; - int threshold; - long long y_start; - long long Len_y; - long long currentIDLen = 0; - fprintf(stderr, "overlap_list->length: %d\n", overlap_list->length); - for (j = 0; j < overlap_list->length; j++) - { - if(memcmp("m64013_190412_043951/108332093/ccs", Get_NAME((*R_INF),overlap_list->list[j].y_id), - Get_NAME_LENGTH((*R_INF), overlap_list->list[j].y_id)) == 0) - { - fprintf(stderr, "******************************x_id: %d, y_id: %d, y_name: %.*s, error_rate: %f, is_match: %d*******************************\n", - overlap_list->list[j].x_id, - overlap_list->list[j].y_id, - Get_NAME_LENGTH((*R_INF), overlap_list->list[j].y_id), Get_NAME((*R_INF),overlap_list->list[j].y_id), - trim_error_rate(overlap_list, j), overlap_list->list[j].is_match); - - print_fake_gap(&overlap_list->list[j].f_cigar); - } - - Len_x = overlap_list->list[j].x_pos_e - overlap_list->list[j].x_pos_s + 1; - - if (overlap_list->list[j].is_match == 1) - { - currentIDLen++; - fprintf(stderr, "y_name: %.*s\n", - Get_NAME_LENGTH((*R_INF), overlap_list->list[j].y_id), Get_NAME((*R_INF),overlap_list->list[j].y_id)); - } - // else - // { - // fprintf(stderr, "not match, y_name: %.*s\n", - // Get_NAME_LENGTH((*R_INF), overlap_list->list[j].y_id), Get_NAME((*R_INF),overlap_list->list[j].y_id)); - // } - - } - - fprintf(stderr, "currentIDLen: %d\n\n", currentIDLen); - -} - - - - - - int verify_cigar(char* x, int x_len, char* y, int y_len, CIGAR* cigar, int error) { int x_i, y_i, cigar_i; @@ -1581,8 +980,7 @@ int verify_cigar(char* x, int x_len, char* y, int y_len, CIGAR* cigar, int error int flag_error = 0; ///0 is match, 1 is mismatch, 2 is up, 3 is left - ///2是x缺字符(y多字符),而3是y缺字符(x多字符) - ///while (x_i < x_len && y_i < y_len && cigar_i < cigar->length) + ///2 means there are more y, 3 means there are more x while (cigar_i < cigar->length) { operation = cigar->C_C[cigar_i]; @@ -1616,12 +1014,12 @@ int verify_cigar(char* x, int x_len, char* y, int y_len, CIGAR* cigar, int error x_i++; y_i++; } - }///2是x缺字符(y多字符) + } else if (operation == 2) { cigar_error += operationLen; y_i += operationLen; - }///3是y缺字符(x多字符) + } else if (operation == 3) { cigar_error += operationLen; @@ -1631,9 +1029,6 @@ int verify_cigar(char* x, int x_len, char* y, int y_len, CIGAR* cigar, int error cigar_i++; } - - endloop: - ///return; if (cigar_error != error) { @@ -1869,12 +1264,12 @@ int scan_cigar_interval(CIGAR* cigar, int* get_error, int scanXbeg, int scanXend return 1; } } - }///2是x缺字符(y多字符) + } else if (operation == 2) { cigar_error += operationLen; y_i += operationLen; - }///3是y缺字符(x多字符) + } else if (operation == 3) { for (i = 0; i < operationLen; i++) @@ -1884,7 +1279,6 @@ int scan_cigar_interval(CIGAR* cigar, int* get_error, int scanXbeg, int scanXend cigar_error = 0; } - x_i++; cigar_error++; @@ -1897,7 +1291,6 @@ int scan_cigar_interval(CIGAR* cigar, int* get_error, int scanXbeg, int scanXend } } - (*get_error) = cigar_error; return 0; } @@ -2137,104 +1530,6 @@ inline void generate_cigar( // { // fprintf(stderr, "error\n"); // } - - - - /** - int x_i, y_i; - x_i = 0; - y_i = 0; - int new_error = error; - ///0 is match, 1 is mismatch, 2 is up, 3 is left - ///2是x缺字符(y多字符),而3是y缺字符(x多字符) - ///while (x_i < x_len && y_i < y_len && cigar_i < cigar->length) - for (i = path_length - 1; i >= 0; i--) - { - if(path[i] == 0) - { - x_i++; - y_i++; - } - else if(path[i] == 1) - { - x_i++; - y_i++; - } - else if(path[i] == 2) - { - move_gap_greedy(path, i, path_length, x, x_i, y, y_i, &new_error); - y_i++; - } - else if(path[i] == 3) - { - move_gap_greedy(path, i, path_length, x, x_i, y, y_i, &new_error); - x_i++; - } - } - - - - - - - - CIGAR new_cigar; - - new_cigar.length = 0; - pre_ciga = 5; - pre_ciga_length = 0; - - - for (i = path_length - 1; i >= 0; i--) - { - - if (pre_ciga != path[i]) - { - if (pre_ciga_length != 0) - { - new_cigar.C_L[new_cigar.length] = pre_ciga_length; - new_cigar.C_C[new_cigar.length] = pre_ciga; - new_cigar.length++; - } - - pre_ciga = path[i]; - pre_ciga_length = 1; - } - else - { - pre_ciga_length++; - } - } - - if (pre_ciga_length != 0) - { - new_cigar.C_L[new_cigar.length] = pre_ciga_length; - new_cigar.C_C[new_cigar.length] = pre_ciga; - new_cigar.length++; - } - - - - - - if(verify_cigar(x, x_len, y, (*end) - (*start) + 1, &new_cigar, new_error)) - { - - fprintf(stderr, "x_string: %.*s\n", x_len, x); - fprintf(stderr, "y_string: %.*s\n\n", (*end) - (*start) + 1 , y); - - for (int j = 0; j < result->cigar.length; j++) - { - fprintf(stderr, "oper: %d, len: %d\n", result->cigar.C_C[j], result->cigar.C_L[j]); - } - - for (int j = 0; j < new_cigar.length; j++) - { - fprintf(stderr, "new_cigar.oper: %d, new_cigar.len: %d\n", new_cigar.C_C[j], new_cigar.C_L[j]); - } - - } - **/ } @@ -2252,9 +1547,8 @@ int verify_cigar_2(char* x, int x_len, char* y, int y_len, Cigar_record* cigar, int diff_i = 0; ///0 is match, 1 is mismatch, 2 is up, 3 is left - ///2是x缺字符(y多字符),而3是y缺字符(x多字符) - ///while (x_i < x_len && y_i < y_len && cigar_i < cigar->length) - while (cigar_i < cigar->length) + ///2 means there are more y, 3 means there are more x + while (cigar_i < (long long)cigar->length) { operation = Get_Cigar_Type(cigar->record[cigar_i]); operationLen = Get_Cigar_Length(cigar->record[cigar_i]); @@ -2266,7 +1560,7 @@ int verify_cigar_2(char* x, int x_len, char* y, int y_len, Cigar_record* cigar, if (x[x_i]!=y[y_i]) { - fprintf(stderr, "error match\n"); + ///fprintf(stderr, "error match\n"); flag_error = 1; } x_i++; @@ -2281,21 +1575,21 @@ int verify_cigar_2(char* x, int x_len, char* y, int y_len, Cigar_record* cigar, if (x[x_i]==y[y_i]) { - fprintf(stderr, "error mismatch, cigar_i: %d, x_i: %d, y_i: %d\n",cigar_i, x_i, y_i); + ///fprintf(stderr, "error mismatch, cigar_i: %d, x_i: %d, y_i: %d\n",cigar_i, x_i, y_i); flag_error = 1; } if(Get_MisMatch_Base(cigar->lost_base[diff_i]) != y[y_i]) { - fprintf(stderr, "mismatch x: %c, y: %c, mis[%d]: %c\n", x[x_i],y[y_i],diff_i, - Get_MisMatch_Base(cigar->lost_base[diff_i])); + // fprintf(stderr, "mismatch x: %c, y: %c, mis[%d]: %c\n", x[x_i],y[y_i],diff_i, + // Get_MisMatch_Base(cigar->lost_base[diff_i])); } if(Get_Match_Base(cigar->lost_base[diff_i]) != x[x_i]) { - fprintf(stderr, "match x: %c, y: %c, deletion[%d]: %c\n", - x[x_i],y[y_i],diff_i, - Get_Match_Base(cigar->lost_base[diff_i])); + // fprintf(stderr, "match x: %c, y: %c, deletion[%d]: %c\n", + // x[x_i],y[y_i],diff_i, + // Get_Match_Base(cigar->lost_base[diff_i])); } @@ -2305,7 +1599,7 @@ int verify_cigar_2(char* x, int x_len, char* y, int y_len, Cigar_record* cigar, y_i++; diff_i++; } - }///2是x缺字符(y多字符) + } else if (operation == 2) { cigar_error += operationLen; @@ -2313,13 +1607,13 @@ int verify_cigar_2(char* x, int x_len, char* y, int y_len, Cigar_record* cigar, { if(cigar->lost_base[diff_i] != y[y_i]) { - fprintf(stderr, "insertion x: %c, y: %c, insertion[%d]: %c\n", x[x_i],y[y_i],diff_i, - cigar->lost_base[diff_i]); + ///fprintf(stderr, "insertion x: %c, y: %c, insertion[%d]: %c\n", x[x_i],y[y_i],diff_i, + ///cigar->lost_base[diff_i]); } y_i++; diff_i++; } - }///3是y缺字符(x多字符) + } else if (operation == 3) { cigar_error += operationLen; @@ -2328,21 +1622,18 @@ int verify_cigar_2(char* x, int x_len, char* y, int y_len, Cigar_record* cigar, { if(cigar->lost_base[diff_i] != x[x_i]) { - fprintf(stderr, "deletion x: %c, y: %c, deletion[%d]: %c\n", x[x_i],y[y_i],diff_i, - cigar->lost_base[diff_i]); + ///fprintf(stderr, "deletion x: %c, y: %c, deletion[%d]: %c\n", x[x_i],y[y_i],diff_i, + ///cigar->lost_base[diff_i]); } x_i++; diff_i++; } - // x_i += operationLen; - // diff_i += operationLen; } cigar_i++; } - endloop: ///return; /** if (cigar_error != error) @@ -2363,12 +1654,12 @@ int verify_cigar_2(char* x, int x_len, char* y, int y_len, Cigar_record* cigar, { print_string(x, x_len); print_string(y, y_len); - fprintf(stderr, "x_len: %d, y_len: %d, cigar_len: %d, error: %d\n", x_len, y_len, cigar->length, error); - for (i = 0; i < cigar->length; i++) + ///fprintf(stderr, "x_len: %d, y_len: %d, cigar_len: %d, error: %d\n", x_len, y_len, cigar->length, error); + for (i = 0; i < (long long)cigar->length; i++) { operation = Get_Cigar_Type(cigar->record[i]); operationLen = Get_Cigar_Length(cigar->record[i]); - fprintf(stderr, "%u: %u\n", operationLen, operation); + ///fprintf(stderr, "%u: %u\n", operationLen, operation); } } @@ -2498,1262 +1789,11 @@ int* r_extra_begin, int* r_extra_end, unsigned int* r_error) } - - - -inline void recalcate_window_back(overlap_region_alloc* overlap_list, All_reads* R_INF, - UC_Read* g_read, Correct_dumy* dumy, UC_Read* overlap_read) -{ - long long j, k, i; - long long Len_x; - int threshold; - long long y_len; - long long currentIDLen; - long long matches; - long long y_id; - int y_strand; - long long y_readLen; - long long x_start; - long long x_end; - long long x_len; - long long total_y_start; - long long total_y_end; - long long y_start; - long long y_end; - long long Window_Len; - char* x_string; - char* y_string; - int end_site; - unsigned int error; - int real_y_start; - long long overlap_length; - int extra_begin, extra_end; - long long o_len; - - - ///j负责遍历整个overlap list - for (j = 0; j < overlap_list->length; j++) - { - - y_id = overlap_list->list[j].y_id; - y_strand = overlap_list->list[j].y_pos_strand; - y_readLen = Get_READ_LENGTH((*R_INF), y_id); - - - - - - //i负责每个overlap里面的window - //倒着找 - //倒着用结束位置矫正 - for (i = overlap_list->list[j].w_list_length - 1; i >= 0; i--) - { - ///找到第一个匹配的window - if(overlap_list->list[j].w_list[i].y_end != -1) - { - ///note!!! need notification - ///total_y_start = overlap_list->list[j].w_list[i].y_end + 1; - ///this is the actual end postion in ystring - total_y_start = overlap_list->list[j].w_list[i].y_end - overlap_list->list[j].w_list[i].extra_begin + 1; - - - - ///k遍历匹配window右侧所有不匹配的window - ///如果i匹配,则k从i+1开始 - ///知道第一个匹配的window结束 - for (k = i + 1; k < overlap_list->list[j].w_list_length && overlap_list->list[j].w_list[k].y_end == -1; k++) - { - /** - if(memcmp("m64013_190324_024932/92733922/ccs", Get_NAME((*R_INF),overlap_list->list[j].x_id), - Get_NAME_LENGTH((*R_INF), overlap_list->list[j].x_id)) == 0) - { - if(memcmp("m64013_190324_024932/123996697/ccs", Get_NAME((*R_INF),overlap_list->list[j].y_id), - Get_NAME_LENGTH((*R_INF), overlap_list->list[j].y_id)) == 0) - { - fprintf(stderr, "total_y_start: %d, x_start: %lld, x_end: %lld, y_start: %lld, y_end: %lld, match: %d, cigar.length: %d, error: %d\n", - total_y_start, overlap_list->list[j].w_list[i].x_start, overlap_list->list[j].w_list[i].x_end, - overlap_list->list[j].w_list[i].y_start, overlap_list->list[j].w_list[i].y_end, - overlap_list->list[j].w_list[i].y_end - overlap_list->list[j].w_list[i].y_start + 1, - overlap_list->list[j].w_list[i].cigar.length, overlap_list->list[j].w_list[i].error); - } - } - **/ - - - extra_begin = extra_end = 0; - - ///y_start有可能大于y_readLen - ///这多发于最后一个window长度仅为几,而前面一个window的结束位置也超过了y_readLen-1 - ///这个时候做动态规划会给超过的部分补N - if (total_y_start >= y_readLen) - { - break; - } - - ///there is no problem for x - x_start = overlap_list->list[j].w_list[k].x_start; - x_end = overlap_list->list[j].w_list[k].x_end; - x_len = x_end - x_start + 1; - // /****************************may have bugs********************************/ - // ///threshold = x_len * THRESHOLD_RATE; - // threshold = overlap_list->list[j].w_list[k].error_threshold; - // /****************************may have bugs********************************/ - // /****************************may have bugs********************************/ - // threshold = Adjust_Threshold(threshold, x_len); - // /****************************may have bugs********************************/ - threshold = double_error_threshold(overlap_list->list[j].w_list[k].error_threshold, x_len); - - - y_start = total_y_start; - Window_Len = x_len + (threshold << 1); - determine_overlap_region(threshold, y_start, y_id, Window_Len, R_INF, - &extra_begin, &extra_end, &y_start, &o_len); - - if(o_len + threshold < x_len) - { - break; - } - - fill_subregion(dumy->overlap_region, y_start, o_len, y_strand, - R_INF, y_id, extra_begin, extra_end); - - x_string = g_read->seq + x_start; - y_string = dumy->overlap_region; - - ///note!!! need notification - end_site = Reserve_Banded_BPM(y_string, Window_Len, x_string, x_len, threshold, &error); - - ///error等于-1说明没匹配 - if (error!=(unsigned int)-1) - { - overlap_list->list[j].w_list[k].cigar.length = -1; - overlap_list->list[j].w_list[k].y_start = y_start; - overlap_list->list[j].w_list[k].y_end = y_start + end_site; - overlap_list->list[j].w_list[k].error = (int)error; - ///note!!! need notification - overlap_list->list[j].w_list[k].extra_begin = extra_begin; - overlap_list->list[j].w_list[k].extra_end = extra_end; - overlap_list->list[j].w_list[k].error_threshold = threshold; - - overlap_list->list[j].align_length += x_len; - } - else - { - break; - } - ///note!!! need notification - ///total_y_start = y_start + end_site + 1; - total_y_start = y_start + end_site - extra_begin + 1; - - } - - } - - } - - - - - - ///continue; - ///i负责每个overlap里面的window - ///正着找 - ///用起始位置矫正 - for (i = 0; i < overlap_list->list[j].w_list_length; i++) - { - ///找到第一个匹配的window - ///首先这个window要匹配 - ///其次不要是第一个window,这没意义 - ///最后他之前的那个window必须是不匹配,如果之前那个window匹配,也没意义 - if(overlap_list->list[j].w_list[i].y_end != -1 && i != 0 && overlap_list->list[j].w_list[i - 1].y_end == -1) - { - - ///判断这个匹配的window的起始位置有没有被计算出来 - ///如果没有,就需要重新计算 - if(overlap_list->list[j].w_list[i].cigar.length == -1) - { - ///there is no problem for x - x_start = overlap_list->list[j].w_list[i].x_start; - x_end = overlap_list->list[j].w_list[i].x_end; - x_len = x_end - x_start + 1; - /****************************may have bugs********************************/ - ///threshold = x_len * THRESHOLD_RATE; - threshold = overlap_list->list[j].w_list[i].error_threshold; - /****************************may have bugs********************************/ - /****************************may have bugs********************************/ - threshold = Adjust_Threshold(threshold, x_len); - /****************************may have bugs********************************/ - Window_Len = x_len + (threshold << 1); - - - ///y_start is the real y_start - y_start = overlap_list->list[j].w_list[i].y_start; - extra_begin = overlap_list->list[j].w_list[i].extra_begin; - extra_end = overlap_list->list[j].w_list[i].extra_end; - o_len = Window_Len - extra_end - extra_begin; - fill_subregion(dumy->overlap_region, y_start, o_len, y_strand, - R_INF, y_id, extra_begin, extra_end); - x_string = g_read->seq + x_start; - y_string = dumy->overlap_region; - - ///note!!! need notification - end_site = Reserve_Banded_BPM_PATH(y_string, Window_Len, x_string, x_len, threshold, &error, &real_y_start, - &(dumy->path_length), dumy->matrix_bit, dumy->path, - overlap_list->list[j].w_list[i].error, overlap_list->list[j].w_list[i].y_end - y_start); - - - - - - - - ///到这里y_start已经被正确计算出来了 - if (error != (unsigned int)-1) - { - - if (end_site == Window_Len - 1 || real_y_start == 0) - { - - if(fix_boundary(x_string, x_len, threshold, y_start, real_y_start, end_site, - extra_begin, extra_end, y_id, Window_Len, R_INF, dumy, y_strand, error, - &y_start, &real_y_start, &end_site, - &extra_begin, &extra_end, &error)) - { - ///fprintf(stderr, "old_error: %d, new_error: %d\n", overlap_list->list[j].w_list[i].error, error); - overlap_list->list[j].w_list[i].error = error; - overlap_list->list[j].w_list[i].extra_begin = extra_begin; - overlap_list->list[j].w_list[i].extra_end = extra_end; - } - - - } - - - - generate_cigar(dumy->path, dumy->path_length, &(overlap_list->list[j].w_list[i]), - &real_y_start, &end_site, &error, x_string, x_len, y_string); - - if(real_y_start < extra_begin || end_site >= Window_Len - extra_end) - { - fprintf(stderr, "\nreal_y_start: %d, extra_begin: %d\n", - real_y_start, extra_begin); - - fprintf(stderr, "end_site: %d, Window_Len: %d, extra_end: %d\n", - end_site, Window_Len, extra_end); - } - - - ///note!!! need notification - ///real_y_start = y_start + real_y_start; - real_y_start = y_start + real_y_start - extra_begin; - overlap_list->list[j].w_list[i].y_start = real_y_start; - overlap_list->list[j].w_list[i].y_end = y_start + end_site; - overlap_list->list[j].w_list[i].error = error; - } - else - { - fprintf(stderr, "error\n"); - } - - - } - else - { - real_y_start = overlap_list->list[j].w_list[i].y_start; - } - - - - ///再次矫正的基础位置就是real_y_start - total_y_end = real_y_start - 1; - ///k遍历匹配window左侧所有不匹配的window - ///如果i匹配,则k从i-1开始 - ///直到第一个匹配的window结束 - ///因为i!=0,所以k的大小不用担心 - for (k = i - 1; k >= 0 && overlap_list->list[j].w_list[k].y_end == -1; k--) - { - ///there is no problem in x - x_start = overlap_list->list[j].w_list[k].x_start; - x_end = overlap_list->list[j].w_list[k].x_end; - x_len = x_end - x_start + 1; - // /****************************may have bugs********************************/ - // ///threshold = x_len * THRESHOLD_RATE; - // threshold = overlap_list->list[j].w_list[k].error_threshold; - // /****************************may have bugs********************************/ - // /****************************may have bugs********************************/ - // threshold = Adjust_Threshold(threshold, x_len); - // /****************************may have bugs********************************/ - threshold = double_error_threshold(overlap_list->list[j].w_list[k].error_threshold, x_len); - - Window_Len = x_len + (threshold << 1); - - if(total_y_end <= 0) - { - break; - } - - ///y_start may less than 0 - y_start = total_y_end - x_len + 1; - determine_overlap_region(threshold, y_start, y_id, Window_Len, R_INF, - &extra_begin, &extra_end, &y_start, &o_len); - - if(o_len + threshold < x_len) - { - break; - } - - fill_subregion(dumy->overlap_region, y_start, o_len, y_strand, - R_INF, y_id, extra_begin, extra_end); - x_string = g_read->seq + x_start; - y_string = dumy->overlap_region; - - ///note!!! need notification - end_site = Reserve_Banded_BPM_PATH(y_string, Window_Len, x_string, x_len, threshold, &error, &real_y_start, - &(dumy->path_length), dumy->matrix_bit, dumy->path, -1, -1); - - - ///error等于-1说明没匹配 - if (error!=(unsigned int)-1) - { - - if (end_site == Window_Len - 1 || real_y_start == 0) - { - fix_boundary(x_string, x_len, threshold, y_start, real_y_start, end_site, - extra_begin, extra_end, y_id, Window_Len, R_INF, dumy, y_strand, error, - &y_start, &real_y_start, &end_site, - &extra_begin, &extra_end, &error); - } - - generate_cigar(dumy->path, dumy->path_length, &(overlap_list->list[j].w_list[k]), - &real_y_start, &end_site, &error, x_string, x_len, y_string); - - if(real_y_start < extra_begin || end_site >= Window_Len - extra_end) - { - fprintf(stderr, "\nreal_y_start: %d, extra_begin: %d\n", - real_y_start, extra_begin); - - fprintf(stderr, "end_site: %d, Window_Len: %d, extra_end: %d\n", - end_site, Window_Len, extra_end); - } - - ///y_start has no shift, but y_end has shift - overlap_list->list[j].w_list[k].y_start = y_start + real_y_start - extra_begin; - overlap_list->list[j].w_list[k].y_end = y_start + end_site; - overlap_list->list[j].w_list[k].error = error; - overlap_list->list[j].align_length += x_len; - ///note!!! need notification - overlap_list->list[j].w_list[k].extra_begin = extra_begin; - overlap_list->list[j].w_list[k].extra_end = extra_end; - overlap_list->list[j].w_list[k].error_threshold = threshold; - } - else - { - break; - } - - total_y_end = y_start + real_y_start - 1 - extra_begin; - } - - } - } - - - } - - - - overlap_list->mapped_overlaps_length = 0; - - int pre_threshold; - long long tLen, tError; - double error_rate; - ///j负责遍历整个overlap list - for (j = 0; j < overlap_list->length; j++) - { - y_id = overlap_list->list[j].y_id; - y_strand = overlap_list->list[j].y_pos_strand; - y_readLen = Get_READ_LENGTH((*R_INF), y_id); - overlap_length = overlap_list->list[j].x_pos_e - overlap_list->list[j].x_pos_s + 1; - overlap_list->list[j].is_match = 0; - - /** - if(memcmp("m64013_190324_024932/92733922/ccs", Get_NAME((*R_INF),overlap_list->list[j].x_id), - Get_NAME_LENGTH((*R_INF), overlap_list->list[j].x_id)) == 0) - { - if(memcmp("m64013_190324_024932/123996697/ccs", Get_NAME((*R_INF),overlap_list->list[j].y_id), - Get_NAME_LENGTH((*R_INF), overlap_list->list[j].y_id)) == 0) - { - fprintf(stderr, "##############y_name: %.*s, error_rate: %f, is_match: %d, overlap_length: %d, align_length: %d##################\n", - Get_NAME_LENGTH((*R_INF), overlap_list->list[j].y_id), Get_NAME((*R_INF),overlap_list->list[j].y_id), - trim_error_rate(overlap_list, j), overlap_list->list[j].is_match, overlap_length, - overlap_list->list[j].align_length); - for (i = 0; i < overlap_list->list[j].w_list_length; i++) - { - fprintf(stderr, "x_start: %lld, x_end: %lld, y_start: %lld, y_end: %lld, match: %d, cigar.length: %d, error: %d\n", - overlap_list->list[j].w_list[i].x_start, overlap_list->list[j].w_list[i].x_end, - overlap_list->list[j].w_list[i].y_start, overlap_list->list[j].w_list[i].y_end, - overlap_list->list[j].w_list[i].y_end - overlap_list->list[j].w_list[i].y_start + 1, - overlap_list->list[j].w_list[i].cigar.length, overlap_list->list[j].w_list[i].error); - - } - } - } - **/ - - - - ///only calculate cigar for high quality overlaps - if (overlap_length * OVERLAP_THRESHOLD_FILTER <= overlap_list->list[j].align_length) - { - - for (i = 0; i < overlap_list->list[j].w_list_length; i++) - { - ///判断cigar是否被计算 - ///没被计算过就重算 - ///第一个条件是判断这个窗口是否匹配 - if(overlap_list->list[j].w_list[i].y_end != -1) - { - if(overlap_list->list[j].w_list[i].cigar.length == -1) - { - ///there is no problem for x - x_start = overlap_list->list[j].w_list[i].x_start; - x_end = overlap_list->list[j].w_list[i].x_end; - x_len = x_end - x_start + 1; - /****************************may have bugs********************************/ - ///threshold = x_len * THRESHOLD_RATE; - threshold = overlap_list->list[j].w_list[i].error_threshold; - /****************************may have bugs********************************/ - /****************************may have bugs********************************/ - threshold = Adjust_Threshold(threshold, x_len); - /****************************may have bugs********************************/ - Window_Len = x_len + (threshold << 1); - - - ///y_start is the real y_start - y_start = overlap_list->list[j].w_list[i].y_start; - extra_begin = overlap_list->list[j].w_list[i].extra_begin; - extra_end = overlap_list->list[j].w_list[i].extra_end; - o_len = Window_Len - extra_end - extra_begin; - fill_subregion(dumy->overlap_region, y_start, o_len, y_strand, - R_INF, y_id, extra_begin, extra_end); - x_string = g_read->seq + x_start; - y_string = dumy->overlap_region; - - - ///note!!! need notification - end_site = Reserve_Banded_BPM_PATH(y_string, Window_Len, x_string, x_len, threshold, &error, &real_y_start, - &(dumy->path_length), dumy->matrix_bit, dumy->path, - overlap_list->list[j].w_list[i].error, overlap_list->list[j].w_list[i].y_end - y_start); - - - ///到这里y_start已经被正确计算出来了 - if (error != (unsigned int)-1) - { - - if (end_site == Window_Len - 1 || real_y_start == 0) - { - - if(fix_boundary(x_string, x_len, threshold, y_start, real_y_start, end_site, - extra_begin, extra_end, y_id, Window_Len, R_INF, dumy, y_strand, error, - &y_start, &real_y_start, &end_site, - &extra_begin, &extra_end, &error)) - { - ///fprintf(stderr, "old_error: %d, new_error: %d\n", overlap_list->list[j].w_list[i].error, error); - overlap_list->list[j].w_list[i].error = error; - overlap_list->list[j].w_list[i].extra_begin = extra_begin; - overlap_list->list[j].w_list[i].extra_end = extra_end; - } - - } - - generate_cigar(dumy->path, dumy->path_length, &(overlap_list->list[j].w_list[i]), - &real_y_start, &end_site, &error, x_string, x_len, y_string); - - - if(real_y_start < extra_begin || end_site >= Window_Len - extra_end) - { - fprintf(stderr, "\nreal_y_start: %d, extra_begin: %d\n", - real_y_start, extra_begin); - - fprintf(stderr, "end_site: %d, Window_Len: %d, extra_end: %d\n", - end_site, Window_Len, extra_end); - } - - - - ///note!!! need notification - ///real_y_start = y_start + real_y_start; - real_y_start = y_start + real_y_start - extra_begin; - overlap_list->list[j].w_list[i].y_start = real_y_start; - overlap_list->list[j].w_list[i].y_end = y_start + end_site - extra_begin; - overlap_list->list[j].w_list[i].error = error; - } - else - { - fprintf(stderr, "error\n"); - } - } - else - { - overlap_list->list[j].w_list[i].y_end -= overlap_list->list[j].w_list[i].extra_begin; - } - - - } - else ///try to calculate using higher threshold - { - ///there is no problem for x - ///there is no problem for x - x_start = overlap_list->list[j].w_list[i].x_start; - x_end = overlap_list->list[j].w_list[i].x_end; - x_len = x_end - x_start + 1; - ///double the threshold - pre_threshold = overlap_list->list[j].w_list[i].error_threshold; - threshold = double_error_threshold(pre_threshold, x_len); - // /****************************may have bugs********************************/ - // ///pre_threshold = x_len * THRESHOLD_RATE; - // pre_threshold = overlap_list->list[j].w_list[i].error_threshold; - // /****************************may have bugs********************************/ - // /****************************may have bugs********************************/ - // pre_threshold = Adjust_Threshold(pre_threshold, x_len); - // /****************************may have bugs********************************/ - // threshold = pre_threshold * 2; - // ///may have some bugs - // if(x_len >= 300 && threshold < THRESHOLD_MAX_SIZE) - // { - // threshold = THRESHOLD_MAX_SIZE; - // } - // if(threshold > THRESHOLD_MAX_SIZE) - // { - // threshold = THRESHOLD_MAX_SIZE; - // } - Window_Len = x_len + (threshold << 1); - - - - ///if the previous window is mapped - if(i > 0 && overlap_list->list[j].w_list[i - 1].y_end != -1) - { - y_start = overlap_list->list[j].w_list[i - 1].y_end + 1; - determine_overlap_region(threshold, y_start, y_id, Window_Len, R_INF, - &extra_begin, &extra_end, &y_start, &o_len); - }///if the next window is mapped - else if(i < overlap_list->list[j].w_list_length - 1 && overlap_list->list[j].w_list[i + 1].y_end != -1) - { - y_start = overlap_list->list[j].w_list[i + 1].y_start - 1 - x_len + 1; - determine_overlap_region(threshold, y_start, y_id, Window_Len, R_INF, - &extra_begin, &extra_end, &y_start, &o_len); - } - else///if the previous window and next window are not mapped, using the y_start itself - { - ///y_start is the real y_start - y_start = overlap_list->list[j].w_list[i].y_start; - /// since y_start has already substacted pre_threshold - ///here we just need to substact threshold - pre_threshold - determine_overlap_region(threshold - pre_threshold, y_start, y_id, Window_Len, R_INF, - &extra_begin, &extra_end, &y_start, &o_len); - } - - - fill_subregion(dumy->overlap_region, y_start, o_len, y_strand, R_INF, y_id, extra_begin, extra_end); - - x_string = g_read->seq + x_start; - y_string = dumy->overlap_region; - - ///note!!! need notification - end_site = Reserve_Banded_BPM_PATH(y_string, Window_Len, x_string, x_len, threshold, &error, &real_y_start, - &(dumy->path_length), dumy->matrix_bit, dumy->path, -1, -1); - - if (error!=(unsigned int)-1) - { - - if (end_site == Window_Len - 1 || real_y_start == 0) - { - fix_boundary(x_string, x_len, threshold, y_start, real_y_start, end_site, - extra_begin, extra_end, y_id, Window_Len, R_INF, dumy, y_strand, error, - &y_start, &real_y_start, &end_site, - &extra_begin, &extra_end, &error); - } - - generate_cigar(dumy->path, dumy->path_length, &(overlap_list->list[j].w_list[i]), - &real_y_start, &end_site, &error, x_string, x_len, y_string); - - - if(real_y_start < extra_begin || end_site >= Window_Len - extra_end) - { - fprintf(stderr, "\nreal_y_start: %d, extra_begin: %d, error: %d, Window_Len: %d, x_len: %d\n", - real_y_start, extra_begin, error, Window_Len, x_len); - - fprintf(stderr, "end_site: %d, Window_Len: %d, extra_end: %d\n", - end_site, Window_Len, extra_end); - } - - real_y_start = y_start + real_y_start - extra_begin; - overlap_list->list[j].w_list[i].y_start = real_y_start; - overlap_list->list[j].w_list[i].y_end = y_start + end_site - extra_begin; - overlap_list->list[j].w_list[i].error = error; - overlap_list->list[j].align_length += x_len; - overlap_list->list[j].w_list[i].extra_begin = extra_begin; - overlap_list->list[j].w_list[i].extra_end = extra_end; - } - } - } - - - error_rate = trim_error_rate(overlap_list, j); - - - - ///if(error_rate <= 0.015) - if(error_rate <= 0.025) - { - ///overlap_list->mapped_overlaps++; - overlap_list->mapped_overlaps_length += overlap_length; - overlap_list->list[j].is_match = 1; - } - } - } - - - - - - /** - ///j负责遍历整个overlap list - for (j = 0; j < overlap_list->length; j++) - { - y_id = overlap_list->list[j].y_id; - y_strand = overlap_list->list[j].y_pos_strand; - y_readLen = Get_READ_LENGTH((*R_INF), y_id); - overlap_length = overlap_list->list[j].x_pos_e - overlap_list->list[j].x_pos_s + 1; - - ///only calculate cigar for high quality overlaps - if (overlap_length * OVERLAP_THRESHOLD_FILTER <= overlap_list->list[j].align_length) - { - for (i = 0; i < overlap_list->list[j].w_list_length; i++) - { - if(overlap_list->list[j].w_list[i].y_end != -1) - { - ///there is no problem for x - x_start = overlap_list->list[j].w_list[i].x_start; - x_end = overlap_list->list[j].w_list[i].x_end; - x_len = x_end - x_start + 1; - - x_string = g_read->seq + x_start; - - y_start = overlap_list->list[j].w_list[i].y_start; - y_end = overlap_list->list[j].w_list[i].y_end; - y_len = y_end - y_start + 1; - - recover_UC_Read_sub_region(dumy->overlap_region, y_start, y_len, y_strand, R_INF, y_id); - y_string = dumy->overlap_region; - - - if(verify_cigar(x_string, x_len, y_string, y_len, &overlap_list->list[j].w_list[i].cigar, - overlap_list->list[j].w_list[i].error)) - { - fprintf(stderr, "j: %d, i: %d, y_id: %d, y_start: %d, y_end: %d\n", j, i, y_id, y_start, y_end); - } - } - - } - } - - - // for (i = 0; i < overlap_list->list[j].w_list_length; i++) - // { - - // if(overlap_list->list[j].w_list[i].x_end - overlap_list->list[j].w_list[i].x_start + 1 != WINDOW && - // overlap_list->list[j].w_list[i].x_end != overlap_list->list[j].x_pos_e && - // overlap_list->list[j].w_list[i].x_start != overlap_list->list[j].x_pos_s) - // { - // fprintf(stderr, "x_start:%d, x_end: %d, g_read->length: %d, x_pos_s: %d, x_pos_e: %d\n", - // overlap_list->list[j].w_list[i].x_start, - // overlap_list->list[j].w_list[i].x_end, - // g_read->length, - // overlap_list->list[j].x_pos_s, - // overlap_list->list[j].x_pos_e); - // } - // } - } - **/ - - - - - -} - - -inline void recalcate_window_simple(overlap_region_alloc* overlap_list, All_reads* R_INF, - UC_Read* g_read, Correct_dumy* dumy, UC_Read* overlap_read) -{ - long long j, k, i; - long long Len_x; - int threshold; - long long y_len; - long long currentIDLen; - long long matches; - long long y_id; - int y_strand; - long long y_readLen; - long long x_start; - long long x_end; - long long x_len; - long long total_y_start; - long long total_y_end; - long long y_start; - long long y_end; - long long Window_Len; - char* x_string; - char* y_string; - int end_site; - unsigned int error; - int real_y_start; - long long overlap_length; - int extra_begin, extra_end; - long long o_len; - - - ///j负责遍历整个overlap list - for (j = 0; j < overlap_list->length; j++) - { - - y_id = overlap_list->list[j].y_id; - y_strand = overlap_list->list[j].y_pos_strand; - y_readLen = Get_READ_LENGTH((*R_INF), y_id); - - - - - - //i负责每个overlap里面的window - //倒着找 - //倒着用结束位置矫正 - for (i = overlap_list->list[j].w_list_length - 1; i >= 0; i--) - { - ///找到第一个匹配的window - if(overlap_list->list[j].w_list[i].y_end != -1) - { - ///note!!! need notification - ///total_y_start = overlap_list->list[j].w_list[i].y_end + 1; - ///this is the actual end postion in ystring - total_y_start = overlap_list->list[j].w_list[i].y_end - overlap_list->list[j].w_list[i].extra_begin + 1; - - - - ///k遍历匹配window右侧所有不匹配的window - ///如果i匹配,则k从i+1开始 - ///知道第一个匹配的window结束 - for (k = i + 1; k < overlap_list->list[j].w_list_length && overlap_list->list[j].w_list[k].y_end == -1; k++) - { - extra_begin = extra_end = 0; - - ///y_start有可能大于y_readLen - ///这多发于最后一个window长度仅为几,而前面一个window的结束位置也超过了y_readLen-1 - ///这个时候做动态规划会给超过的部分补N - if (total_y_start >= y_readLen) - { - break; - } - - ///there is no problem for x - x_start = overlap_list->list[j].w_list[k].x_start; - x_end = overlap_list->list[j].w_list[k].x_end; - x_len = x_end - x_start + 1; - // /****************************may have bugs********************************/ - // ///threshold = x_len * THRESHOLD_RATE; - // threshold = overlap_list->list[j].w_list[k].error_threshold; - // /****************************may have bugs********************************/ - // /****************************may have bugs********************************/ - // threshold = Adjust_Threshold(threshold, x_len); - // /****************************may have bugs********************************/ - threshold = double_error_threshold(overlap_list->list[j].w_list[k].error_threshold, x_len); - - - y_start = total_y_start; - Window_Len = x_len + (threshold << 1); - if(!determine_overlap_region(threshold, y_start, y_id, Window_Len, R_INF, - &extra_begin, &extra_end, &y_start, &o_len)) - { - break; - } - - if(o_len + threshold < x_len) - { - break; - } - - fill_subregion(dumy->overlap_region, y_start, o_len, y_strand, - R_INF, y_id, extra_begin, extra_end); - - x_string = g_read->seq + x_start; - y_string = dumy->overlap_region; - - ///note!!! need notification - end_site = Reserve_Banded_BPM(y_string, Window_Len, x_string, x_len, threshold, &error); - - ///error等于-1说明没匹配 - if (error!=(unsigned int)-1) - { - overlap_list->list[j].w_list[k].cigar.length = -1; - overlap_list->list[j].w_list[k].y_start = y_start; - overlap_list->list[j].w_list[k].y_end = y_start + end_site; - overlap_list->list[j].w_list[k].error = (int)error; - ///note!!! need notification - overlap_list->list[j].w_list[k].extra_begin = extra_begin; - overlap_list->list[j].w_list[k].extra_end = extra_end; - overlap_list->list[j].w_list[k].error_threshold = threshold; - - overlap_list->list[j].align_length += x_len; - } - else - { - break; - } - ///note!!! need notification - ///total_y_start = y_start + end_site + 1; - total_y_start = y_start + end_site - extra_begin + 1; - - } - - } - - } - - - - - - ///continue; - ///i负责每个overlap里面的window - ///正着找 - ///用起始位置矫正 - for (i = 0; i < overlap_list->list[j].w_list_length; i++) - { - ///找到第一个匹配的window - ///首先这个window要匹配 - ///其次不要是第一个window,这没意义 - ///最后他之前的那个window必须是不匹配,如果之前那个window匹配,也没意义 - if(overlap_list->list[j].w_list[i].y_end != -1 && i != 0 && overlap_list->list[j].w_list[i - 1].y_end == -1) - { - - ///判断这个匹配的window的起始位置有没有被计算出来 - ///如果没有,就需要重新计算 - if(overlap_list->list[j].w_list[i].cigar.length == -1) - { - ///there is no problem for x - x_start = overlap_list->list[j].w_list[i].x_start; - x_end = overlap_list->list[j].w_list[i].x_end; - x_len = x_end - x_start + 1; - /****************************may have bugs********************************/ - ///threshold = x_len * THRESHOLD_RATE; - threshold = overlap_list->list[j].w_list[i].error_threshold; - /****************************may have bugs********************************/ - /****************************may have bugs********************************/ - threshold = Adjust_Threshold(threshold, x_len); - /****************************may have bugs********************************/ - Window_Len = x_len + (threshold << 1); - - - ///y_start is the real y_start - y_start = overlap_list->list[j].w_list[i].y_start; - extra_begin = overlap_list->list[j].w_list[i].extra_begin; - extra_end = overlap_list->list[j].w_list[i].extra_end; - o_len = Window_Len - extra_end - extra_begin; - fill_subregion(dumy->overlap_region, y_start, o_len, y_strand, - R_INF, y_id, extra_begin, extra_end); - x_string = g_read->seq + x_start; - y_string = dumy->overlap_region; - - ///note!!! need notification - end_site = Reserve_Banded_BPM_PATH(y_string, Window_Len, x_string, x_len, threshold, &error, &real_y_start, - &(dumy->path_length), dumy->matrix_bit, dumy->path, - overlap_list->list[j].w_list[i].error, overlap_list->list[j].w_list[i].y_end - y_start); - - - - - - - - ///到这里y_start已经被正确计算出来了 - if (error != (unsigned int)-1) - { - - if (end_site == Window_Len - 1 || real_y_start == 0) - { - - if(fix_boundary(x_string, x_len, threshold, y_start, real_y_start, end_site, - extra_begin, extra_end, y_id, Window_Len, R_INF, dumy, y_strand, error, - &y_start, &real_y_start, &end_site, - &extra_begin, &extra_end, &error)) - { - ///fprintf(stderr, "old_error: %d, new_error: %d\n", overlap_list->list[j].w_list[i].error, error); - overlap_list->list[j].w_list[i].error = error; - overlap_list->list[j].w_list[i].extra_begin = extra_begin; - overlap_list->list[j].w_list[i].extra_end = extra_end; - } - - - } - - - - generate_cigar(dumy->path, dumy->path_length, &(overlap_list->list[j].w_list[i]), - &real_y_start, &end_site, &error, x_string, x_len, y_string); - - if(real_y_start < extra_begin || end_site >= Window_Len - extra_end) - { - fprintf(stderr, "\nreal_y_start: %d, extra_begin: %d\n", - real_y_start, extra_begin); - - fprintf(stderr, "end_site: %d, Window_Len: %d, extra_end: %d\n", - end_site, Window_Len, extra_end); - } - - - ///note!!! need notification - ///real_y_start = y_start + real_y_start; - real_y_start = y_start + real_y_start - extra_begin; - overlap_list->list[j].w_list[i].y_start = real_y_start; - overlap_list->list[j].w_list[i].y_end = y_start + end_site; - overlap_list->list[j].w_list[i].error = error; - } - else - { - fprintf(stderr, "error\n"); - } - - - } - else - { - real_y_start = overlap_list->list[j].w_list[i].y_start; - } - - - - ///再次矫正的基础位置就是real_y_start - total_y_end = real_y_start - 1; - ///k遍历匹配window左侧所有不匹配的window - ///如果i匹配,则k从i-1开始 - ///直到第一个匹配的window结束 - ///因为i!=0,所以k的大小不用担心 - for (k = i - 1; k >= 0 && overlap_list->list[j].w_list[k].y_end == -1; k--) - { - ///there is no problem in x - x_start = overlap_list->list[j].w_list[k].x_start; - x_end = overlap_list->list[j].w_list[k].x_end; - x_len = x_end - x_start + 1; - // /****************************may have bugs********************************/ - // ///threshold = x_len * THRESHOLD_RATE; - // threshold = overlap_list->list[j].w_list[k].error_threshold; - // /****************************may have bugs********************************/ - // /****************************may have bugs********************************/ - // threshold = Adjust_Threshold(threshold, x_len); - // /****************************may have bugs********************************/ - threshold = double_error_threshold(overlap_list->list[j].w_list[k].error_threshold, x_len); - - Window_Len = x_len + (threshold << 1); - - if(total_y_end <= 0) - { - break; - } - - ///y_start may less than 0 - y_start = total_y_end - x_len + 1; - if(!determine_overlap_region(threshold, y_start, y_id, Window_Len, R_INF, - &extra_begin, &extra_end, &y_start, &o_len)) - { - break; - } - - if(o_len + threshold < x_len) - { - break; - } - - fill_subregion(dumy->overlap_region, y_start, o_len, y_strand, - R_INF, y_id, extra_begin, extra_end); - x_string = g_read->seq + x_start; - y_string = dumy->overlap_region; - - ///note!!! need notification - end_site = Reserve_Banded_BPM_PATH(y_string, Window_Len, x_string, x_len, threshold, &error, &real_y_start, - &(dumy->path_length), dumy->matrix_bit, dumy->path, -1, -1); - - - ///error等于-1说明没匹配 - if (error!=(unsigned int)-1) - { - - if (end_site == Window_Len - 1 || real_y_start == 0) - { - fix_boundary(x_string, x_len, threshold, y_start, real_y_start, end_site, - extra_begin, extra_end, y_id, Window_Len, R_INF, dumy, y_strand, error, - &y_start, &real_y_start, &end_site, - &extra_begin, &extra_end, &error); - } - - generate_cigar(dumy->path, dumy->path_length, &(overlap_list->list[j].w_list[k]), - &real_y_start, &end_site, &error, x_string, x_len, y_string); - - if(real_y_start < extra_begin || end_site >= Window_Len - extra_end) - { - fprintf(stderr, "\nreal_y_start: %d, extra_begin: %d\n", - real_y_start, extra_begin); - - fprintf(stderr, "end_site: %d, Window_Len: %d, extra_end: %d\n", - end_site, Window_Len, extra_end); - } - - ///y_start has no shift, but y_end has shift - overlap_list->list[j].w_list[k].y_start = y_start + real_y_start - extra_begin; - overlap_list->list[j].w_list[k].y_end = y_start + end_site; - overlap_list->list[j].w_list[k].error = error; - overlap_list->list[j].align_length += x_len; - ///note!!! need notification - overlap_list->list[j].w_list[k].extra_begin = extra_begin; - overlap_list->list[j].w_list[k].extra_end = extra_end; - overlap_list->list[j].w_list[k].error_threshold = threshold; - } - else - { - break; - } - - total_y_end = y_start + real_y_start - 1 - extra_begin; - } - - } - } - - - } - - - - overlap_list->mapped_overlaps_length = 0; - - int pre_threshold; - long long tLen, tError; - double error_rate; - ///j负责遍历整个overlap list - for (j = 0; j < overlap_list->length; j++) - { - y_id = overlap_list->list[j].y_id; - y_strand = overlap_list->list[j].y_pos_strand; - y_readLen = Get_READ_LENGTH((*R_INF), y_id); - overlap_length = overlap_list->list[j].x_pos_e - overlap_list->list[j].x_pos_s + 1; - overlap_list->list[j].is_match = 0; - - - ///only calculate cigar for high quality overlaps - if (overlap_length * OVERLAP_THRESHOLD_FILTER <= overlap_list->list[j].align_length) - { - - for (i = 0; i < overlap_list->list[j].w_list_length; i++) - { - ///判断cigar是否被计算 - ///没被计算过就重算 - ///第一个条件是判断这个窗口是否匹配 - if(overlap_list->list[j].w_list[i].y_end != -1) - { - if(overlap_list->list[j].w_list[i].cigar.length == -1) - { - ///there is no problem for x - x_start = overlap_list->list[j].w_list[i].x_start; - x_end = overlap_list->list[j].w_list[i].x_end; - x_len = x_end - x_start + 1; - /****************************may have bugs********************************/ - ///threshold = x_len * THRESHOLD_RATE; - threshold = overlap_list->list[j].w_list[i].error_threshold; - /****************************may have bugs********************************/ - /****************************may have bugs********************************/ - threshold = Adjust_Threshold(threshold, x_len); - /****************************may have bugs********************************/ - Window_Len = x_len + (threshold << 1); - - - ///y_start is the real y_start - y_start = overlap_list->list[j].w_list[i].y_start; - extra_begin = overlap_list->list[j].w_list[i].extra_begin; - extra_end = overlap_list->list[j].w_list[i].extra_end; - o_len = Window_Len - extra_end - extra_begin; - fill_subregion(dumy->overlap_region, y_start, o_len, y_strand, - R_INF, y_id, extra_begin, extra_end); - x_string = g_read->seq + x_start; - y_string = dumy->overlap_region; - - - ///note!!! need notification - end_site = Reserve_Banded_BPM_PATH(y_string, Window_Len, x_string, x_len, threshold, &error, &real_y_start, - &(dumy->path_length), dumy->matrix_bit, dumy->path, - overlap_list->list[j].w_list[i].error, overlap_list->list[j].w_list[i].y_end - y_start); - - // if(error != overlap_list->list[j].w_list[i].error) - // { - // fprintf(stderr, "error\n"); - // } - - ///到这里y_start已经被正确计算出来了 - if (error != (unsigned int)-1) - { - - if (end_site == Window_Len - 1 || real_y_start == 0) - { - - if(fix_boundary(x_string, x_len, threshold, y_start, real_y_start, end_site, - extra_begin, extra_end, y_id, Window_Len, R_INF, dumy, y_strand, error, - &y_start, &real_y_start, &end_site, - &extra_begin, &extra_end, &error)) - { - ///fprintf(stderr, "old_error: %d, new_error: %d\n", overlap_list->list[j].w_list[i].error, error); - overlap_list->list[j].w_list[i].error = error; - overlap_list->list[j].w_list[i].extra_begin = extra_begin; - overlap_list->list[j].w_list[i].extra_end = extra_end; - } - - } - - generate_cigar(dumy->path, dumy->path_length, &(overlap_list->list[j].w_list[i]), - &real_y_start, &end_site, &error, x_string, x_len, y_string); - - - if(real_y_start < extra_begin || end_site >= Window_Len - extra_end) - { - fprintf(stderr, "\nreal_y_start: %d, extra_begin: %d\n", - real_y_start, extra_begin); - - fprintf(stderr, "end_site: %d, Window_Len: %d, extra_end: %d\n", - end_site, Window_Len, extra_end); - } - - - - ///note!!! need notification - ///real_y_start = y_start + real_y_start; - real_y_start = y_start + real_y_start - extra_begin; - overlap_list->list[j].w_list[i].y_start = real_y_start; - overlap_list->list[j].w_list[i].y_end = y_start + end_site - extra_begin; - overlap_list->list[j].w_list[i].error = error; - } - else - { - fprintf(stderr, "error\n"); - } - } - else - { - overlap_list->list[j].w_list[i].y_end -= overlap_list->list[j].w_list[i].extra_begin; - } - - - } - } - - ///error_rate = trim_error_rate(overlap_list, j); - error_rate = non_trim_error_rate(overlap_list, j, R_INF, dumy, g_read); - - - ///if(error_rate <= 0.015) - if(error_rate <= 0.03) - { - ///overlap_list->mapped_overlaps++; - overlap_list->mapped_overlaps_length += overlap_length; - overlap_list->list[j].is_match = 1; - } - else if(error_rate <= 0.045) - { - overlap_list->list[j].is_match = 3; - } - } - } - - - - - - /** - ///j负责遍历整个overlap list - for (j = 0; j < overlap_list->length; j++) - { - y_id = overlap_list->list[j].y_id; - y_strand = overlap_list->list[j].y_pos_strand; - y_readLen = Get_READ_LENGTH((*R_INF), y_id); - overlap_length = overlap_list->list[j].x_pos_e - overlap_list->list[j].x_pos_s + 1; - - ///only calculate cigar for high quality overlaps - if (overlap_length * OVERLAP_THRESHOLD_FILTER <= overlap_list->list[j].align_length) - { - for (i = 0; i < overlap_list->list[j].w_list_length; i++) - { - if(overlap_list->list[j].w_list[i].y_end != -1) - { - ///there is no problem for x - x_start = overlap_list->list[j].w_list[i].x_start; - x_end = overlap_list->list[j].w_list[i].x_end; - x_len = x_end - x_start + 1; - - x_string = g_read->seq + x_start; - - y_start = overlap_list->list[j].w_list[i].y_start; - y_end = overlap_list->list[j].w_list[i].y_end; - y_len = y_end - y_start + 1; - - recover_UC_Read_sub_region(dumy->overlap_region, y_start, y_len, y_strand, R_INF, y_id); - y_string = dumy->overlap_region; - - - if(verify_cigar(x_string, x_len, y_string, y_len, &overlap_list->list[j].w_list[i].cigar, - overlap_list->list[j].w_list[i].error)) - { - fprintf(stderr, "j: %d, i: %d, y_id: %d, y_start: %d, y_end: %d\n", j, i, y_id, y_start, y_end); - } - } - - } - } - - - // for (i = 0; i < overlap_list->list[j].w_list_length; i++) - // { - - // if(overlap_list->list[j].w_list[i].x_end - overlap_list->list[j].w_list[i].x_start + 1 != WINDOW && - // overlap_list->list[j].w_list[i].x_end != overlap_list->list[j].x_pos_e && - // overlap_list->list[j].w_list[i].x_start != overlap_list->list[j].x_pos_s) - // { - // fprintf(stderr, "x_start:%d, x_end: %d, g_read->length: %d, x_pos_s: %d, x_pos_e: %d\n", - // overlap_list->list[j].w_list[i].x_start, - // overlap_list->list[j].w_list[i].x_end, - // g_read->length, - // overlap_list->list[j].x_pos_s, - // overlap_list->list[j].x_pos_e); - // } - // } - } - **/ - - - - - -} - - - - inline void recalcate_window(overlap_region_alloc* overlap_list, All_reads* R_INF, UC_Read* g_read, Correct_dumy* dumy, UC_Read* overlap_read) { long long j, k, i; - long long Len_x; int threshold; - long long y_len; - long long currentIDLen; - long long matches; long long y_id; int y_strand; long long y_readLen; @@ -3763,7 +1803,6 @@ inline void recalcate_window(overlap_region_alloc* overlap_list, All_reads* R_IN long long total_y_start; long long total_y_end; long long y_start; - long long y_end; long long Window_Len; char* x_string; char* y_string; @@ -3775,8 +1814,7 @@ inline void recalcate_window(overlap_region_alloc* overlap_list, All_reads* R_IN long long o_len; - ///j负责遍历整个overlap list - for (j = 0; j < overlap_list->length; j++) + for (j = 0; j < (long long)overlap_list->length; j++) { y_id = overlap_list->list[j].y_id; @@ -3799,7 +1837,7 @@ inline void recalcate_window(overlap_region_alloc* overlap_list, All_reads* R_IN ///k corresponding to all unmatched windows at the right side of overlap_list->list[j].w_list[i] ///so k starts from i + 1, and end to the first matched window - for (k = i + 1; k < overlap_list->list[j].w_list_length && overlap_list->list[j].w_list[k].y_end == -1; k++) + for (k = i + 1; k < (long long)overlap_list->list[j].w_list_length && overlap_list->list[j].w_list[k].y_end == -1; k++) { extra_begin = extra_end = 0; @@ -3841,7 +1879,6 @@ inline void recalcate_window(overlap_region_alloc* overlap_list, All_reads* R_IN ///note!!! need notification end_site = Reserve_Banded_BPM(y_string, Window_Len, x_string, x_len, threshold, &error); - ///error等于-1说明没匹配 if (error!=(unsigned int)-1) { overlap_list->list[j].w_list[k].cigar.length = -1; @@ -3872,7 +1909,7 @@ inline void recalcate_window(overlap_region_alloc* overlap_list, All_reads* R_IN //i corresponding to each window of a overlap //utilize the the start pos of next window in forward - for (i = 0; i < overlap_list->list[j].w_list_length; i++) + for (i = 0; i < (long long)overlap_list->list[j].w_list_length; i++) { ///find the first matched window, which should not be the first window ///the pre-window of this matched window must be unmatched @@ -3966,10 +2003,6 @@ inline void recalcate_window(overlap_region_alloc* overlap_list, All_reads* R_IN ///the end pos for pre window is real_y_start - 1 total_y_end = real_y_start - 1; - ///k遍历匹配window左侧所有不匹配的window - ///如果i匹配,则k从i-1开始 - ///直到第一个匹配的window结束 - ///因为i!=0,所以k的大小不用担心 for (k = i - 1; k >= 0 && overlap_list->list[j].w_list[k].y_end == -1; k--) { ///there is no problem in x @@ -4058,10 +2091,8 @@ inline void recalcate_window(overlap_region_alloc* overlap_list, All_reads* R_IN overlap_list->mapped_overlaps_length = 0; - int pre_threshold; - long long tLen, tError; double error_rate; - for (j = 0; j < overlap_list->length; j++) + for (j = 0; j < (long long)overlap_list->length; j++) { y_id = overlap_list->list[j].y_id; y_strand = overlap_list->list[j].y_pos_strand; @@ -4074,7 +2105,7 @@ inline void recalcate_window(overlap_region_alloc* overlap_list, All_reads* R_IN if (overlap_length * OVERLAP_THRESHOLD_FILTER <= overlap_list->list[j].align_length) { - for (i = 0; i < overlap_list->list[j].w_list_length; i++) + for (i = 0; i < (long long)overlap_list->list[j].w_list_length; i++) { ///first we need to check if this window is matched if(overlap_list->list[j].w_list[i].y_end != -1) @@ -4191,8 +2222,8 @@ inline void recalcate_window(overlap_region_alloc* overlap_list, All_reads* R_IN void debug_scan_cigar(overlap_region* sub_list) { long long i; - int f_err, b_err, fLen, bLen, xLen; - for (i = 0; i < sub_list->w_list_length; i++) + int f_err, b_err, fLen, xLen; + for (i = 0; i < (long long)sub_list->w_list_length; i++) { if(sub_list->w_list[i].y_end == -1 || sub_list->w_list[i].cigar.length == -1) { @@ -4207,7 +2238,7 @@ void debug_scan_cigar(overlap_region* sub_list) if(b_err != sub_list->w_list[i].error || f_err != sub_list->w_list[i].error) { - fprintf(stderr, "sbsbsbsbsb1\n"); + fprintf(stderr, "error\n"); } scan_cigar(&(sub_list->w_list[i].cigar), &b_err, @@ -4217,13 +2248,13 @@ void debug_scan_cigar(overlap_region* sub_list) if(b_err != sub_list->w_list[i].error || f_err != sub_list->w_list[i].error) { - fprintf(stderr, "sbsbsbsbsb2\n"); + fprintf(stderr, "error\n"); } scan_cigar_interval(&(sub_list->w_list[i].cigar), &b_err, 0, xLen-1); if(b_err != sub_list->w_list[i].error) { - fprintf(stderr, "sbsbsbsbsb3\n"); + fprintf(stderr, "error\n"); } fLen = xLen / 3; @@ -4231,7 +2262,7 @@ void debug_scan_cigar(overlap_region* sub_list) scan_cigar_interval(&(sub_list->w_list[i].cigar), &b_err, 0, fLen-1); if(f_err != b_err) { - fprintf(stderr, "sbsbsbsbsb4\n"); + fprintf(stderr, "error\n"); } fLen = xLen / 3; @@ -4239,7 +2270,7 @@ void debug_scan_cigar(overlap_region* sub_list) scan_cigar_interval(&(sub_list->w_list[i].cigar), &b_err, xLen-fLen, xLen-1); if(f_err != b_err) { - fprintf(stderr, "\nsbsbsbsbsb5\n"); + fprintf(stderr, "\nerror\n"); fprintf(stderr, "b_err: %d, f_err: %d\n",b_err, f_err); long long j; for (j = 0; j < sub_list->w_list[i].cigar.length; j++) @@ -4276,10 +2307,8 @@ void calculate_boundary_cigars(overlap_region* sub_list, All_reads* R_INF, Corre UC_Read* g_read) { resize_window_list_alloc(&(sub_list->boundary_cigars), sub_list->w_list_length - 1); - int x_id = sub_list->x_id; int y_id = sub_list->y_id; int y_strand = sub_list->y_pos_strand; - long long x_readLen = Get_READ_LENGTH((*R_INF), x_id); long long y_readLen = Get_READ_LENGTH((*R_INF), y_id); long long i, y_distance; int f_err, b_err, m_error; @@ -4290,7 +2319,7 @@ UC_Read* g_read) long long L_useless_side, R_useless_side; int alpha = 1; long long y_start, x_start; - long long y_end, x_end; + long long x_end; long long yLen, xLen; long long leftLen, rightLen; long long threshold; @@ -4305,7 +2334,7 @@ UC_Read* g_read) ///the (i)-th boundary between the (i)-th window and the (i+1)-th window ///that means it includes (the tail of (i)-th window) and (the header of (i+1)-th window) ///note the (i)-th boundary is calculated at the (i)-th window - for (i = 0; i + 1 < sub_list->w_list_length; i++) + for (i = 0; i + 1 < (long long)sub_list->w_list_length; i++) { ///if both of the two windows are not aligned ///it is not necessary to calculate the boundary @@ -4367,8 +2396,8 @@ UC_Read* g_read) ///up to now, x_start and y_start are not -1 ///leftLen does not include x_start itself, rightLen does ///gnerally speaking, rightLen should be always larger than leftLen - leftLen = MIN(MIN((x_start - sub_list->w_list[i].x_start), y_start), single_sideLen); - rightLen = MIN(MIN((sub_list->w_list[i+1].x_end + 1 - x_start), y_readLen - y_start), + leftLen = MIN(MIN((x_start - (long long)sub_list->w_list[i].x_start), y_start), single_sideLen); + rightLen = MIN(MIN(((long long)sub_list->w_list[i+1].x_end + 1 - x_start), y_readLen - y_start), single_sideLen); ///xLen should be the sum length of two windows @@ -4440,13 +2469,13 @@ UC_Read* g_read) L_useless_side = R_useless_side = force_useless_side; ///first window - if((i == 0) && (x_start == sub_list->w_list[0].x_start)) + if((i == 0) && (x_start == (long long)sub_list->w_list[0].x_start)) { L_useless_side = 0; } ///last window - if((i == sub_list->w_list_length - 2) && - (x_end == sub_list->w_list[sub_list->w_list_length - 1].x_end)) + if((i == (long long)(sub_list->w_list_length - 2)) && + (x_end == (long long)(sub_list->w_list[sub_list->w_list_length - 1].x_end))) { R_useless_side = 0; } @@ -4500,7 +2529,7 @@ UC_Read* g_read) void debug_window_cigar(overlap_region_alloc* overlap_list, UC_Read* g_read, Correct_dumy* dumy, All_reads* R_INF, int test_window, int test_boundary) { - long long i, j, y_id, y_strand; + uint64_t i, j, y_id, y_strand; char* x_string; char* y_string; long long x_start; @@ -4541,7 +2570,7 @@ All_reads* R_INF, int test_window, int test_boundary) if(verify_cigar(x_string, x_len, y_string, y_len, &overlap_list->list[j].w_list[i].cigar, overlap_list->list[j].w_list[i].error)) { - fprintf(stderr, "j: %d, i: %d, y_id: %d, y_start: %d, y_end: %d\n", j, i, y_id, y_start, y_end); + fprintf(stderr, "error\n"); } } } @@ -4551,7 +2580,7 @@ All_reads* R_INF, int test_window, int test_boundary) if(test_boundary == 1) { - for (i = 0; i < overlap_list->list[j].boundary_cigars.length; i++) + for (i = 0; i < (uint64_t)overlap_list->list[j].boundary_cigars.length; i++) { if(overlap_list->list[j].boundary_cigars.buffer[i].y_end != -1) { @@ -4573,7 +2602,7 @@ All_reads* R_INF, int test_window, int test_boundary) &overlap_list->list[j].boundary_cigars.buffer[i].cigar, overlap_list->list[j].boundary_cigars.buffer[i].error)) { - fprintf(stderr, "j: %d, i: %d, y_id: %d, y_start: %d, y_end: %d\n", j, i, y_id, y_start, y_end); + fprintf(stderr, "error\n"); } } } @@ -4583,10 +2612,9 @@ All_reads* R_INF, int test_window, int test_boundary) if(test_window == 1 && test_boundary == 1) { if(overlap_list->list[j].w_list_length != - overlap_list->list[j].boundary_cigars.length + 1) + (uint64_t)(overlap_list->list[j].boundary_cigars.length + 1)) { - fprintf(stderr, "w_list_length: %d, boundary_cigars.length: %d\n", - overlap_list->list[j].w_list_length, overlap_list->list[j].boundary_cigars.length); + fprintf(stderr, "error\n"); } } @@ -4598,11 +2626,7 @@ inline void recalcate_window_advance(overlap_region_alloc* overlap_list, All_rea UC_Read* g_read, Correct_dumy* dumy, UC_Read* overlap_read) { long long j, k, i; - long long Len_x; int threshold; - long long y_len; - long long currentIDLen; - long long matches; long long y_id; int y_strand; long long y_readLen; @@ -4612,7 +2636,6 @@ inline void recalcate_window_advance(overlap_region_alloc* overlap_list, All_rea long long total_y_start; long long total_y_end; long long y_start; - long long y_end; long long Window_Len; char* x_string; char* y_string; @@ -4624,8 +2647,7 @@ inline void recalcate_window_advance(overlap_region_alloc* overlap_list, All_rea long long o_len; - ///j负责遍历整个overlap list - for (j = 0; j < overlap_list->length; j++) + for (j = 0; j < (long long)overlap_list->length; j++) { y_id = overlap_list->list[j].y_id; @@ -4644,11 +2666,9 @@ inline void recalcate_window_advance(overlap_region_alloc* overlap_list, All_rea total_y_start = overlap_list->list[j].w_list[i].y_end - overlap_list->list[j].w_list[i].extra_begin + 1; - - ///k corresponding to all unmatched windows at the right side of overlap_list->list[j].w_list[i] ///so k starts from i + 1, and end to the first matched window - for (k = i + 1; k < overlap_list->list[j].w_list_length && overlap_list->list[j].w_list[k].y_end == -1; k++) + for (k = i + 1; k < (long long)overlap_list->list[j].w_list_length && overlap_list->list[j].w_list[k].y_end == -1; k++) { extra_begin = extra_end = 0; @@ -4690,7 +2710,7 @@ inline void recalcate_window_advance(overlap_region_alloc* overlap_list, All_rea ///note!!! need notification end_site = Reserve_Banded_BPM(y_string, Window_Len, x_string, x_len, threshold, &error); - ///error等于-1说明没匹配 + ///if error==-1, unmatched if (error!=(unsigned int)-1) { overlap_list->list[j].w_list[k].cigar.length = -1; @@ -4721,7 +2741,7 @@ inline void recalcate_window_advance(overlap_region_alloc* overlap_list, All_rea //i corresponding to each window of a overlap //utilize the the start pos of next window in forward - for (i = 0; i < overlap_list->list[j].w_list_length; i++) + for (i = 0; i < (long long)overlap_list->list[j].w_list_length; i++) { ///find the first matched window, which should not be the first window ///the pre-window of this matched window must be unmatched @@ -4777,22 +2797,10 @@ inline void recalcate_window_advance(overlap_region_alloc* overlap_list, All_rea overlap_list->list[j].w_list[i].extra_end = extra_end; } } - - - + generate_cigar(dumy->path, dumy->path_length, &(overlap_list->list[j].w_list[i]), &real_y_start, &end_site, &error, x_string, x_len, y_string); - // if(real_y_start < extra_begin || end_site >= Window_Len - extra_end) - // { - // fprintf(stderr, "\nreal_y_start: %d, extra_begin: %d\n", - // real_y_start, extra_begin); - - // fprintf(stderr, "end_site: %d, Window_Len: %d, extra_end: %d\n", - // end_site, Window_Len, extra_end); - // } - - ///note!!! need notification real_y_start = y_start + real_y_start - extra_begin; overlap_list->list[j].w_list[i].y_start = real_y_start; @@ -4811,14 +2819,11 @@ inline void recalcate_window_advance(overlap_region_alloc* overlap_list, All_rea real_y_start = overlap_list->list[j].w_list[i].y_start; } - ///the end pos for pre window is real_y_start - 1 total_y_end = real_y_start - 1; - ///k遍历匹配window左侧所有不匹配的window - ///如果i匹配,则k从i-1开始 - ///直到第一个匹配的window结束 - ///因为i!=0,所以k的大小不用担心 + ///find the unmatched window on the left of current matched window + ///k starts from i - 1 for (k = i - 1; k >= 0 && overlap_list->list[j].w_list[k].y_end == -1; k--) { ///there is no problem in x @@ -4874,15 +2879,6 @@ inline void recalcate_window_advance(overlap_region_alloc* overlap_list, All_rea generate_cigar(dumy->path, dumy->path_length, &(overlap_list->list[j].w_list[k]), &real_y_start, &end_site, &error, x_string, x_len, y_string); - // if(real_y_start < extra_begin || end_site >= Window_Len - extra_end) - // { - // fprintf(stderr, "\nreal_y_start: %d, extra_begin: %d\n", - // real_y_start, extra_begin); - - // fprintf(stderr, "end_site: %d, Window_Len: %d, extra_end: %d\n", - // end_site, Window_Len, extra_end); - // } - ///y_start has no shift, but y_end has shift overlap_list->list[j].w_list[k].y_start = y_start + real_y_start - extra_begin; overlap_list->list[j].w_list[k].y_end = y_start + end_site; @@ -4907,10 +2903,8 @@ inline void recalcate_window_advance(overlap_region_alloc* overlap_list, All_rea overlap_list->mapped_overlaps_length = 0; - int pre_threshold; - long long tLen, tError; double error_rate; - for (j = 0; j < overlap_list->length; j++) + for (j = 0; j < (long long)overlap_list->length; j++) { y_id = overlap_list->list[j].y_id; y_strand = overlap_list->list[j].y_pos_strand; @@ -4924,7 +2918,7 @@ inline void recalcate_window_advance(overlap_region_alloc* overlap_list, All_rea if (overlap_length * OVERLAP_THRESHOLD_FILTER <= overlap_list->list[j].align_length) { - for (i = 0; i < overlap_list->list[j].w_list_length; i++) + for (i = 0; i < (long long)overlap_list->list[j].w_list_length; i++) { ///first we need to check if this window is matched if(overlap_list->list[j].w_list[i].y_end != -1) @@ -4964,11 +2958,6 @@ inline void recalcate_window_advance(overlap_region_alloc* overlap_list, All_rea &(dumy->path_length), dumy->matrix_bit, dumy->path, overlap_list->list[j].w_list[i].error, overlap_list->list[j].w_list[i].y_end - y_start); - // if(error != overlap_list->list[j].w_list[i].error) - // { - // fprintf(stderr, "error\n"); - // } - if (error != (unsigned int)-1) { if (end_site == Window_Len - 1 || real_y_start == 0) @@ -4989,16 +2978,6 @@ inline void recalcate_window_advance(overlap_region_alloc* overlap_list, All_rea generate_cigar(dumy->path, dumy->path_length, &(overlap_list->list[j].w_list[i]), &real_y_start, &end_site, &error, x_string, x_len, y_string); - - // if(real_y_start < extra_begin || end_site >= Window_Len - extra_end) - // { - // fprintf(stderr, "\nreal_y_start: %d, extra_begin: %d\n", - // real_y_start, extra_begin); - - // fprintf(stderr, "end_site: %d, Window_Len: %d, extra_end: %d\n", - // end_site, Window_Len, extra_end); - // } - ///note!!! need notification real_y_start = y_start + real_y_start - extra_begin; overlap_list->list[j].w_list[i].y_start = real_y_start; @@ -5042,31 +3021,6 @@ inline void recalcate_window_advance(overlap_region_alloc* overlap_list, All_rea } - - - -/** -inline void adjust_alignment_windows(overlap_region* overlap, All_reads* R_INF) -{ - long long y_id = overlap->y_id; - long long y_strand = overlap->y_pos_strand; - long long y_readLen = Get_READ_LENGTH((*R_INF), y_id); - long long overlap_length = overlap->x_pos_e - overlap->x_pos_s + 1; - long long i; - ///note here we start from i = 1, instead of i = 0 - for (i = 1; i < overlap->w_list_length; i++) - { - ///that means we have both start pos and end pos - if(overlap->w_list[i].y_end != -1 && overlap->w_list[i].cigar.length != -1) - { - ; - } - } -} -**/ - - - inline void add_base_to_correct_read_directly(Correct_dumy* dumy, char base) { @@ -5084,10 +3038,9 @@ inline void add_base_to_correct_read_directly(Correct_dumy* dumy, char base) inline void add_base_to_correct_read(Correct_dumy* dumy, char base, int is_error) { - ///deletion就不要管 + ///don't need to deal with deletion if (base != 'D') { - if (dumy->corrected_read_length + 2 > dumy->corrected_read_size) { dumy->corrected_read_size = dumy->corrected_read_size * 2; @@ -5124,7 +3077,7 @@ inline void add_segment_to_correct_read(Correct_dumy* dumy, char* segment, long -///返回下一个backbone节点上的ID +///return the ID of next node at backbone long long inline add_path_to_correct_read(Graph* backbone, Correct_dumy* dumy, long long currentNodeID, long long type, long long edgeID, Cigar_record* current_cigar, char* self_string) { @@ -5134,58 +3087,30 @@ long long type, long long edgeID, Cigar_record* current_cigar, char* self_string ///Note: currentNodeID must be a backbone node ///currentNodeID = 0 means a fake node ///currentNodeID = i means self_string[i - 1] - ///包括匹配和误配两种情况 + ///include match/mismatch if (type == MISMATCH) { - ///这是match的情况 + ///match if(backbone->g_nodes.list[currentNodeID].mismatch_edges.list[edgeID].length == 0) { nodeID = backbone->g_nodes.list[currentNodeID].mismatch_edges.list[edgeID].out_node; add_base_to_correct_read_directly(dumy, backbone->g_nodes.list[nodeID].base); - ///match所以dumy->corrected_base不要+1 - ///nodeID = i means self_string[i - 1] ///add_cigar_record(self_string+nodeID-1, 1, current_cigar, 0); add_cigar_record(&(backbone->g_nodes.list[nodeID].base), 1, current_cigar, 0); - - /***********需要注释掉********* */ - if (nodeID != currentNodeID + 1) - { - fprintf(stderr, "error match\n"); - } - /***********需要注释掉********* */ - return nodeID; } - else ///这是mismatch的情况 + else ///mismatch { - - nodeID = backbone->g_nodes.list[currentNodeID].mismatch_edges.list[edgeID].out_node; add_base_to_correct_read_directly(dumy, backbone->g_nodes.list[nodeID].base); dumy->corrected_base++; - - char merge_base = 0; merge_base = seq_nt6_table[(uint8_t)backbone->g_nodes.list[nodeID].base]; merge_base = merge_base << 3; - ///这种中间节点只有一个元素,所以直接list[0] nodeID = backbone->g_nodes.list[nodeID].mismatch_edges.list[0].out_node; merge_base = merge_base | seq_nt6_table[(uint8_t)backbone->g_nodes.list[nodeID].base]; add_cigar_record(&merge_base, 1, current_cigar, 1); - - /** - add_cigar_record(&(backbone->g_nodes.list[nodeID].base), 1, current_cigar, 1); - nodeID = backbone->g_nodes.list[nodeID].mismatch_edges.list[0].out_node; - **/ - - /***********需要注释掉********* */ - if (nodeID != currentNodeID + 1) - { - fprintf(stderr, "error mismatch\n"); - } - /***********需要注释掉********* */ - return nodeID; } } @@ -5193,34 +3118,12 @@ long long type, long long edgeID, Cigar_record* current_cigar, char* self_string { nodeID = backbone->g_nodes.list[currentNodeID].deletion_edges.list[edgeID].out_node; dumy->corrected_base += nodeID - currentNodeID; - - // if(nodeID - currentNodeID != 1) - // { - // fprintf(stderr, "error\n"); - // } - - ///currentNodeID = i means self_string[i - 1] add_cigar_record(self_string + currentNodeID, nodeID - currentNodeID, current_cigar, DELETION); - - - - /***********需要注释掉********* */ - if (!(nodeID >= backbone->s_start_nodeID && nodeID <= backbone->s_end_nodeID)) - { - fprintf(stderr, "error deletion 1\n"); - } - if (nodeID <= currentNodeID) - { - fprintf(stderr, "error deletion 2\n"); - } - - /***********需要注释掉********* */ - return nodeID; } else if (type == INSERTION) { - ///这个一定要变成0 + ///pay attention to this line backbone->g_nodes.list[currentNodeID].num_insertions = 0; nodeID = backbone->g_nodes.list[currentNodeID].insertion_edges.list[edgeID].out_node; @@ -5230,21 +3133,9 @@ long long type, long long edgeID, Cigar_record* current_cigar, char* self_string { add_base_to_correct_read_directly(dumy, backbone->g_nodes.list[nodeID].base); add_cigar_record(&backbone->g_nodes.list[nodeID].base, 1, current_cigar, INSERTION); - ///只有一条边 nodeID = backbone->g_nodes.list[nodeID].insertion_edges.list[0].out_node; } dumy->corrected_base += step; - - ///currentNodeID = i means self_string[i - 1] - ///add_cigar_record(self_string + currentNodeID, step, current_cigar, INSERTION); - - /***********需要注释掉********* */ - if (nodeID != currentNodeID) - { - fprintf(stderr, "error insertion\n"); - } - /***********需要注释掉********* */ - return nodeID; } else @@ -5255,7 +3146,7 @@ long long type, long long edgeID, Cigar_record* current_cigar, char* self_string } -///返回下一个backbone节点上的ID +///return the ID of next node at backbone long long inline add_path_to_correct_read_new(Graph* backbone, Graph* DAGCon, Correct_dumy* dumy, long long currentNodeID, long long type, long long edgeID, Cigar_record* current_cigar, char* self_string) { @@ -5265,58 +3156,30 @@ long long type, long long edgeID, Cigar_record* current_cigar, char* self_string ///Note: currentNodeID must be a backbone node ///currentNodeID = 0 means a fake node ///currentNodeID = i means self_string[i - 1] - ///包括匹配和误配两种情况 + ///include match/mismatch if (type == MISMATCH) { - ///这是match的情况 + ///match if(backbone->g_nodes.list[currentNodeID].mismatch_edges.list[edgeID].length == 0) { nodeID = backbone->g_nodes.list[currentNodeID].mismatch_edges.list[edgeID].out_node; add_base_to_correct_read_directly(dumy, backbone->g_nodes.list[nodeID].base); - ///match所以dumy->corrected_base不要+1 - ///nodeID = i means self_string[i - 1] ///add_cigar_record(self_string+nodeID-1, 1, current_cigar, 0); add_cigar_record(&(backbone->g_nodes.list[nodeID].base), 1, current_cigar, 0); - - /***********需要注释掉********* */ - if (nodeID != currentNodeID + 1) - { - fprintf(stderr, "error match\n"); - } - /***********需要注释掉********* */ - return nodeID; } - else ///这是mismatch的情况 + else ///mismatch { - - nodeID = backbone->g_nodes.list[currentNodeID].mismatch_edges.list[edgeID].out_node; add_base_to_correct_read_directly(dumy, backbone->g_nodes.list[nodeID].base); dumy->corrected_base++; - - char merge_base = 0; merge_base = seq_nt6_table[(uint8_t)backbone->g_nodes.list[nodeID].base]; merge_base = merge_base << 3; - ///这种中间节点只有一个元素,所以直接list[0] nodeID = backbone->g_nodes.list[nodeID].mismatch_edges.list[0].out_node; merge_base = merge_base | seq_nt6_table[(uint8_t)backbone->g_nodes.list[nodeID].base]; add_cigar_record(&merge_base, 1, current_cigar, 1); - - /** - add_cigar_record(&(backbone->g_nodes.list[nodeID].base), 1, current_cigar, 1); - nodeID = backbone->g_nodes.list[nodeID].mismatch_edges.list[0].out_node; - **/ - - /***********需要注释掉********* */ - if (nodeID != currentNodeID + 1) - { - fprintf(stderr, "error mismatch\n"); - } - /***********需要注释掉********* */ - return nodeID; } } @@ -5324,34 +3187,12 @@ long long type, long long edgeID, Cigar_record* current_cigar, char* self_string { nodeID = backbone->g_nodes.list[currentNodeID].deletion_edges.list[edgeID].out_node; dumy->corrected_base += nodeID - currentNodeID; - - // if(nodeID - currentNodeID != 1) - // { - // fprintf(stderr, "error\n"); - // } - - ///currentNodeID = i means self_string[i - 1] add_cigar_record(self_string + currentNodeID, nodeID - currentNodeID, current_cigar, DELETION); - - - - /***********需要注释掉********* */ - if (!(nodeID >= backbone->s_start_nodeID && nodeID <= backbone->s_end_nodeID)) - { - fprintf(stderr, "error deletion 1\n"); - } - if (nodeID <= currentNodeID) - { - fprintf(stderr, "error deletion 2\n"); - } - - /***********需要注释掉********* */ - return nodeID; } else if (type == INSERTION) { - ///这个一定要变成0 + ///pay attention to this line backbone->g_nodes.list[currentNodeID].num_insertions = 0; long long str; char str_c; @@ -5364,590 +3205,16 @@ long long type, long long edgeID, Cigar_record* current_cigar, char* self_string } return currentNodeID; - - // nodeID = backbone->g_nodes.list[currentNodeID].insertion_edges.list[edgeID].out_node; - // long long step = backbone->g_nodes.list[currentNodeID].insertion_edges.list[edgeID].length; - // long long i; - // for (i = 0; i < step; i++) - // { - // add_base_to_correct_read_directly(dumy, backbone->g_nodes.list[nodeID].base); - // add_cigar_record(&backbone->g_nodes.list[nodeID].base, 1, current_cigar, INSERTION); - // ///只有一条边 - // nodeID = backbone->g_nodes.list[nodeID].insertion_edges.list[0].out_node; - // } - // dumy->corrected_base += step; - // /***********需要注释掉********* */ - // if (nodeID != currentNodeID) - // { - // fprintf(stderr, "error insertion\n"); - // } - // /***********需要注释掉********* */ - // return nodeID; } else { fprintf(stderr, "error type\n"); } -} - - - -void test_single_path(Graph* DAGCon, Graph* backbone, int debug_node_in_backbone) -{ - char forward[1000]; - char reverse[1000]; - char pre[1000]; - long long i, j, outNode, inputNode, preNode, string_i, path_weight, step; - - if((Output_Edges(G_Node(*DAGCon, DAGCon->s_start_nodeID)).length != Input_Edges(G_Node(*DAGCon, DAGCon->s_end_nodeID)).length) - || (G_Node(*backbone, debug_node_in_backbone).insertion_edges.length != Output_Edges(G_Node(*DAGCon, DAGCon->s_start_nodeID)).length)) - { - fprintf(stderr, "s_out: %d, s_end: %d\n", - Output_Edges(G_Node(*DAGCon, DAGCon->s_start_nodeID)).length, - Input_Edges(G_Node(*DAGCon, DAGCon->s_end_nodeID)).length); - } - - - - - - for (i = 0; i < Output_Edges(G_Node(*DAGCon, DAGCon->s_start_nodeID)).length; i++) - { - outNode = Output_Edges(G_Node(*DAGCon, DAGCon->s_start_nodeID)).list[i].out_node; - string_i = 0; - path_weight = Output_Edges(G_Node(*DAGCon, DAGCon->s_start_nodeID)).list[i].weight; - - while(outNode != DAGCon->s_end_nodeID) - { - forward[string_i++] = G_Node(*DAGCon, outNode).base; - if(Output_Edges(G_Node(*DAGCon, outNode)).list[0].weight != path_weight) - { - fprintf(stderr, "error1\n"); - } - outNode = Output_Edges(G_Node(*DAGCon, outNode)).list[0].out_node; - } - forward[string_i] = '\0'; - - - inputNode = Input_Edges(G_Node(*DAGCon, DAGCon->s_end_nodeID)).list[i].in_node; - string_i = 0; - while(inputNode != DAGCon->s_start_nodeID) - { - reverse[string_i++] = G_Node(*DAGCon, inputNode).base; - - - - if(Input_Edges(G_Node(*DAGCon, inputNode)).list[0].weight != path_weight) - { - fprintf(stderr, "error2\n"); - } - inputNode = Input_Edges(G_Node(*DAGCon, inputNode)).list[0].in_node; - } - reverse[string_i] = '\0'; - - for(j = 0; j < string_i/2; j ++) - { - char k = reverse[j]; - reverse[j] = reverse[string_i - j - 1]; - reverse[string_i - j - 1] = k; - } - - if(strcmp(forward, reverse)) - { - fprintf(stderr, "f: %s\n, r: %s\n\n", forward, reverse); - } - - - - - if(G_Node(*backbone, debug_node_in_backbone).insertion_edges.list[i].weight != path_weight) - { - fprintf(stderr, "error3\n"); - } - - if(G_Node(*backbone, debug_node_in_backbone).insertion_edges.list[i].length != string_i) - { - fprintf(stderr, "error4\n"); - } - - step = G_Node(*backbone, debug_node_in_backbone).insertion_edges.list[i].length; - if(step != 0) - { - string_i = 0; - preNode = G_Node(*backbone, debug_node_in_backbone).insertion_edges.list[i].out_node; - - for (j = 0; j < step; j++) - { - pre[string_i++] = G_Node(*backbone, preNode).base; - preNode = G_Node(*backbone, preNode).insertion_edges.list[0].out_node; - } - } - - pre[string_i] = '\0'; - - - if(strcmp(forward, pre)) - { - fprintf(stderr, "f: %s, r: %s, p: %s\n\n", forward, reverse, pre); - } - - } -} - - - - -void test_single_path_new(Graph* DAGCon, Graph* backbone, int debug_node_in_backbone) -{ - char forward[1000]; - char reverse[1000]; - char pre[1000]; - long long i, j, preNode, string_i, path_weight, step; - Node* outNode; - Node* inputNode; - Node* currentStartNode; - Node* currentEndNode; - - if( - (Real_Length(Output_Edges(G_Node(*DAGCon, DAGCon->s_start_nodeID))) - != - Real_Length(Input_Edges(G_Node(*DAGCon, DAGCon->s_end_nodeID)))) - || - (G_Node(*backbone, debug_node_in_backbone).insertion_edges.length - != - Real_Length(Output_Edges(G_Node(*DAGCon, DAGCon->s_start_nodeID))))) - { - fprintf(stderr, "s_out: %d, s_end: %d, insertion_edges.length: %d\n", - Real_Length(Output_Edges(G_Node(*DAGCon, DAGCon->s_start_nodeID))), - Real_Length(Input_Edges(G_Node(*DAGCon, DAGCon->s_end_nodeID))), - G_Node(*backbone, debug_node_in_backbone).insertion_edges.length); - } - - RSet iter_out, iter_input; - clear_RSet(&iter_out); - clear_RSet(&iter_input); - currentStartNode = &(G_Node(*DAGCon, DAGCon->s_start_nodeID)); - currentEndNode = &(G_Node(*DAGCon, DAGCon->s_end_nodeID)); - - i = 0; - - - - while(getOutputNodes(&iter_out, DAGCon, currentStartNode, &outNode)) - { - string_i = 0; - path_weight = Input_Edges((*outNode)).list[0].weight; - - while(outNode != &(G_Node(*DAGCon, DAGCon->s_end_nodeID))) - { - RSet inner; - Edge* e; - forward[string_i++] = (*outNode).base; - - clear_RSet(&inner); - while (getOutputEdges(&inner, DAGCon, outNode, &e)) - { - if(e->weight != path_weight) - { - fprintf(stderr, "error1\n"); - } - } - - - clear_RSet(&inner); - while (getInputEdges(&inner, DAGCon, outNode, &e)) - { - if(e->weight != path_weight) - { - fprintf(stderr, "error1\n"); - } - } - - clear_RSet(&inner); - while(getOutputNodes(&inner, DAGCon, outNode, &outNode)) - { - ; - } - } - forward[string_i] = '\0'; - - - string_i = 0; - if(!getInputNodes(&iter_input, DAGCon, currentEndNode, &inputNode)) - { - fprintf(stderr, "sbsbsb\n"); - } - - while(inputNode != &(G_Node(*DAGCon,DAGCon->s_start_nodeID))) - { - reverse[string_i++] = (*inputNode).base; - if(Input_Edges((*inputNode)).list[0].weight != path_weight) - { - fprintf(stderr, "error2\n"); - } - RSet inner; - Edge* e; - clear_RSet(&inner); - while (getOutputEdges(&inner, DAGCon, inputNode, &e)) - { - if(e->weight != path_weight) - { - fprintf(stderr, "error1\n"); - } - } - clear_RSet(&inner); - while (getInputEdges(&inner, DAGCon, inputNode, &e)) - { - if(e->weight != path_weight) - { - fprintf(stderr, "error1\n"); - } - } - - clear_RSet(&inner); - while(getInputNodes(&inner, DAGCon, inputNode, &inputNode)) - { - ; - } - - ///inputNode = &(G_Node(*DAGCon, Input_Edges((*inputNode)).list[0].in_node)); - } - reverse[string_i] = '\0'; - - for(j = 0; j < string_i/2; j ++) - { - char k = reverse[j]; - reverse[j] = reverse[string_i - j - 1]; - reverse[string_i - j - 1] = k; - } - - if(strcmp(forward, reverse)!=0) - { - fprintf(stderr, "f: %s, r: %s\n\n", forward, reverse); - } - - - - - - if(G_Node(*backbone, debug_node_in_backbone).insertion_edges.list[i].weight != path_weight) - { - fprintf(stderr, "error3\n"); - } - - if(G_Node(*backbone, debug_node_in_backbone).insertion_edges.list[i].length != string_i) - { - fprintf(stderr, "error4\n"); - } - - /** - step = G_Node(*backbone, debug_node_in_backbone).insertion_edges.list[i].length; - if(step != 0) - { - string_i = 0; - preNode = G_Node(*backbone, debug_node_in_backbone).insertion_edges.list[i].out_node; - - for (j = 0; j < step; j++) - { - pre[string_i++] = G_Node(*backbone, preNode).base; - preNode = G_Node(*backbone, preNode).insertion_edges.list[0].out_node; - } - } - - pre[string_i] = '\0'; - **/ - - extract_path(backbone, debug_node_in_backbone, i, pre); - - - if(strcmp(forward, pre)!=0) - { - fprintf(stderr, "f: %s, r: %s, p: %s\n\n", forward, reverse, pre); - } - - - i++; - } - - int pre_weight = 0; - for (i = 0; i < G_Node(*backbone, debug_node_in_backbone).insertion_edges.length; i++) - { - if(i == 0) - { - extract_path(backbone, debug_node_in_backbone, i, pre); - pre_weight = G_Node(*backbone, debug_node_in_backbone).insertion_edges.list[i].weight; - } - else if(i > 0) - { - extract_path(backbone, debug_node_in_backbone, i, forward); - - if(strcmp(forward, pre)==0) - { - fprintf(stderr, "f: %s, f_weight: %d, p: %s, p_weight: %d\n\n", - forward, G_Node(*backbone, debug_node_in_backbone).insertion_edges.list[i].weight, - pre, pre_weight); - } - - memcpy(pre, forward, strlen(forward) + 1); - - } - } - - - - for (i = 0; i < DAGCon->g_nodes.length; i++) - { - currentStartNode = &(G_Node(*DAGCon, i)); - - if(If_Node_Exist(*currentStartNode)) - { - clear_RSet(&iter_out); - Edge* e; - Edge* e_self; - Edge* e_reverse; - while(getOutputEdges(&iter_out, DAGCon, currentStartNode, &e_self)) - { - ///e_reverse = &(Input_Edges(G_Node(*DAGCon, e_self->out_node)).list[e_self->reverse_edge_ID]); - e = e_self; - - get_bi_direction_edges(DAGCon, e_self, &e_self, &e_reverse); - - if(e != e_self) - { - fprintf(stderr, "error0\n"); - } - - if(e_self->in_node != e_reverse->in_node || e_self->out_node != e_reverse->out_node || - e_self->weight != e_reverse->weight) - { - fprintf(stderr, "error1\n"); - } - - - if(e_self != &(Output_Edges(G_Node(*DAGCon, e_self->in_node)).list[e_self->self_edge_ID])) - { - fprintf(stderr, "error2\n"); - } - - if(Visit(*e_self) == 1 || Visit(*e_reverse) == 1) - { - fprintf(stderr, "error visit flag\n"); - } - - - - - - - - get_bi_direction_edges(DAGCon, e_reverse, &e_self, &e_reverse); - - if(e != e_self) - { - fprintf(stderr, "error0\n"); - } - - if(e_self->in_node != e_reverse->in_node || e_self->out_node != e_reverse->out_node || - e_self->weight != e_reverse->weight) - { - fprintf(stderr, "error1\n"); - } - - - if(e_self != &(Output_Edges(G_Node(*DAGCon, e_self->in_node)).list[e_self->self_edge_ID])) - { - fprintf(stderr, "error2\n"); - } - } - - - - clear_RSet(&iter_input); - while(getInputEdges(&iter_input, DAGCon, currentStartNode, &e_self)) - { - - e = e_self; - - get_bi_direction_edges(DAGCon, e_self, &e_self, &e_reverse); - - if(e != e_reverse) - { - fprintf(stderr, "error0\n"); - } - - if(e_self->in_node != e_reverse->in_node || e_self->out_node != e_reverse->out_node || - e_self->weight != e_reverse->weight) - { - fprintf(stderr, "error1\n"); - } - - - if(e_self != &(Output_Edges(G_Node(*DAGCon, e_self->in_node)).list[e_self->self_edge_ID])) - { - fprintf(stderr, "error2\n"); - } - - - - - - - - get_bi_direction_edges(DAGCon, e_reverse, &e_self, &e_reverse); - - if(e != e_reverse) - { - fprintf(stderr, "error0\n"); - } - - if(e_self->in_node != e_reverse->in_node || e_self->out_node != e_reverse->out_node || - e_self->weight != e_reverse->weight) - { - fprintf(stderr, "error1\n"); - } - - - if(e_self != &(Output_Edges(G_Node(*DAGCon, e_self->in_node)).list[e_self->self_edge_ID])) - { - fprintf(stderr, "error2\n"); - } - } - } - // else - // { - // fprintf(stderr, "node does not exist\n"); - // } - - } -} - - -void debug_Queue(Graph* DAGCon) -{ - long long* input; - long long* output; - - srand((unsigned)time(0)); - long long array_length = rand() % 100; - input = (long long*)malloc(sizeof(long long) * (array_length + 1)); - output = (long long*)malloc(sizeof(long long) * (array_length + 1)); - long long i; - - for (i = 0; i < array_length; i++) - { - input[i] = rand() % 1000000; - push_to_Queue(&(DAGCon->node_q), input[i]); - } - - i = 0; - while (pop_from_Queue(&(DAGCon->node_q), &output[i])) - { - i++; - } - - if(i != array_length) - { - fprintf(stderr, "array_length: %d\n", array_length); - } - else - { - for (i = 0; i < array_length; i++) - { - if(input[i] != output[i]) - { - fprintf(stderr, "input[%d]:%d, output[%d]: %d\n", - i, input[i], i, output[i]); - } - - } - } - - - - - - - - - - srand((unsigned)time(0)); - if(array_length != 0) - { - array_length = rand() % array_length; - } - - - for (i = 0; i < array_length; i++) - { - input[i] = rand() % 1000000; - push_to_Queue(&(DAGCon->node_q), input[i]); - } - - i = 0; - while (pop_from_Queue(&(DAGCon->node_q), &output[i])) - { - i++; - } - - if(i != array_length) - { - fprintf(stderr, "array_length: %d\n", array_length); - } - else - { - for (i = 0; i < array_length; i++) - { - if(input[i] != output[i]) - { - fprintf(stderr, "input[%d]:%d, output[%d]: %d\n", - i, input[i], i, output[i]); - } - - } - } - - - free(input); - free(output); -} - - -///return the in-edge ID of outNode -long long get_In_Edge_ID(Graph* DAGCon, long long inNode, long long outNode) -{ - long long i; - - for (i = 0; i < Input_Edges(G_Node(*DAGCon, outNode)).length; i++) - { - if (Input_Edges(G_Node(*DAGCon, outNode)).list[i].in_node == inNode) - { - return i; - } - } - return -1; } -///return the out-edge ID of inNode -long long get_Out_Edge_ID(Graph* DAGCon, long long inNode, long long outNode) -{ - long long i; - for (i = 0; i < Output_Edges(G_Node(*DAGCon, inNode)).length; i++) - { - if(Output_Edges(G_Node(*DAGCon, inNode)).list[i].out_node == outNode) - { - return i; - } - } - - return -1; -} - - - - void Merge_Out_Nodes(Graph* DAGCon, Node* currentNode) { ///if this node does not have any output, directly return @@ -5959,13 +3226,13 @@ void Merge_Out_Nodes(Graph* DAGCon, Node* currentNode) RSet buf, out_buf; char Bases[4] = {'A', 'C', 'G', 'T'}; char base; - long long base_i, i, weight; + long long base_i, weight; int flag = 0; - Node* get_node_1; - Node* out_node_of_get_node_1; - Node* consensus_node_1; - Edge* e_forward_1; - Edge* e_backward_1; + Node* get_node_1 = NULL; + Node* out_node_of_get_node_1 = NULL; + Node* consensus_node_1 = NULL; + Edge* e_forward_1 = NULL; + Edge* e_backward_1 = NULL; ///merge all base for each base for (base_i = 0; base_i < 4; base_i++) @@ -6054,10 +3321,6 @@ void Merge_Out_Nodes(Graph* DAGCon, Node* currentNode) } - - - - void Merge_In_Nodes(Graph* DAGCon, Node* currentNode) { ///if this node does not have any input, directly return @@ -6069,13 +3332,13 @@ void Merge_In_Nodes(Graph* DAGCon, Node* currentNode) RSet buf, in_buf; char Bases[4] = {'A', 'C', 'G', 'T'}; char base; - long long base_i, i, weight; + long long base_i, weight; int flag = 0; - Node* get_node; - Node* in_node_of_get_node; - Node* consensus_node; - Edge* e_forward; - Edge* e_backward; + Node* get_node = NULL; + Node* in_node_of_get_node = NULL; + Node* consensus_node = NULL; + Edge* e_forward = NULL; + Edge* e_backward = NULL; ///merge all base for each base for (base_i = 0; base_i < 4; base_i++) @@ -6163,28 +3426,24 @@ void Merge_In_Nodes(Graph* DAGCon, Node* currentNode) } } - - - - void print_graph(Graph* DAGCon) { - long long i; - for (long long i = 0; i < DAGCon->g_nodes.length; i++) + uint64_t i; + for (i = 0; i < DAGCon->g_nodes.length; i++) { Node* currentStartNode = &(G_Node(*DAGCon, i)); RSet iter_out; if(If_Node_Exist(*currentStartNode)) { - fprintf(stderr, "ID: %d (%c) (w: %d)\n", (*currentStartNode).ID, (*currentStartNode).base, (*currentStartNode).weight); + fprintf(stderr, "ID: %lu (%c) (w: %lu)\n", (*currentStartNode).ID, (*currentStartNode).base, (*currentStartNode).weight); clear_RSet(&iter_out); Edge* e; fprintf(stderr, "****Out-node: "); while(getOutputEdges(&iter_out, DAGCon, currentStartNode, &e)) { //fprintf(stderr, "%d[%c], ", G_Node(*DAGCon, e->out_node).ID, G_Node(*DAGCon, e->out_node).base); - fprintf(stderr, "%d(w: %d), ", G_Node(*DAGCon, e->out_node).ID, e->weight); + fprintf(stderr, "%lu(w: %lu), ", G_Node(*DAGCon, e->out_node).ID, e->weight); } fprintf(stderr, "\n"); @@ -6202,12 +3461,10 @@ void print_graph(Graph* DAGCon) } - - - void debug_DAGCon(Graph* DAGCon) { - for (long long i = 0; i < DAGCon->g_nodes.length; i++) + uint64_t i = 0; + for (i = 0; i < DAGCon->g_nodes.length; i++) { Node* currentStartNode = &(G_Node(*DAGCon, i)); RSet iter_out; @@ -6215,7 +3472,6 @@ void debug_DAGCon(Graph* DAGCon) if(If_Node_Exist(*currentStartNode)) { clear_RSet(&iter_out); - Edge* e; Edge* e_self; Edge* e_reverse; while(getOutputEdges(&iter_out, DAGCon, currentStartNode, &e_self)) @@ -6226,14 +3482,14 @@ void debug_DAGCon(Graph* DAGCon) if(Visit(*e_self) == 0) { - fprintf(stderr, "Visit(*e_self): %d, error visit flag: in_node: %d, out_node: %d\n", + fprintf(stderr, "Visit(*e_self): %lu, error visit flag: in_node: %lu, out_node: %lu\n", Visit(*e_self), (*e_self).in_node, (*e_self).out_node); } if(Visit(*e_reverse) == 0) { - fprintf(stderr, "Visit(*e_reverse): %d, error visit flag: in_node: %d, out_node: %d\n", + fprintf(stderr, "Visit(*e_reverse): %lu, error visit flag: in_node: %lu, out_node: %lu\n", Visit(*e_reverse), (*e_reverse).in_node, (*e_reverse).out_node); } @@ -6261,14 +3517,14 @@ void debug_DAGCon(Graph* DAGCon) if(Visit(*e_self) == 0) { - fprintf(stderr, "Visit(*e_self): %d, error visit flag: in_node: %d, out_node: %d\n", + fprintf(stderr, "Visit(*e_self): %lu, error visit flag: in_node: %lu, out_node: %lu\n", Visit(*e_self), (*e_self).in_node, (*e_self).out_node); } if(Visit(*e_reverse) == 0) { - fprintf(stderr, "Visit(*e_reverse): %d, error visit flag: in_node: %d, out_node: %d\n", + fprintf(stderr, "Visit(*e_reverse): %lu, error visit flag: in_node: %lu, out_node: %lu\n", Visit(*e_reverse), (*e_reverse).in_node, (*e_reverse).out_node); } @@ -6376,9 +3632,9 @@ inline void generate_seq_from_path(Graph* DAGCon, Node* node, int direction) { clear_Queue(&(DAGCon->node_q)); RSet iter; - Edge* e; - long long max; - Node* max_node; + Edge* e = NULL; + uint64_t max; + Node* max_node = NULL; if(direction == 0) @@ -6433,11 +3689,11 @@ inline void generate_seq_from_path(Graph* DAGCon, Node* node, int direction) long long generate_best_seq_from_edges(Graph* DAGCon) { - long long max_start, max_end, max_start_edge, max_end_edge; + long long max_start = 0, max_end = 0, max_start_edge = 0, max_end_edge = 0; RSet iter; - Edge* e; - Node* newNode; - long long max_count; + Edge* e = NULL; + Node* newNode = NULL; + long long max_count = 0; ///check the out-edges of start node @@ -6447,7 +3703,7 @@ long long generate_best_seq_from_edges(Graph* DAGCon) clear_RSet(&iter); while(getOutputEdges(&iter, DAGCon, newNode, &e)) { - if(e->weight > max_start) + if(e->weight > (uint64_t)max_start) { max_start = e->weight; max_start_edge = iter.index - 1; @@ -6461,7 +3717,7 @@ long long generate_best_seq_from_edges(Graph* DAGCon) clear_RSet(&iter); while(getInputEdges(&iter, DAGCon, newNode, &e)) { - if(e->weight > max_end) + if(e->weight > (uint64_t)max_end) { max_end = e->weight; max_end_edge = iter.index - 1; @@ -6481,43 +3737,7 @@ long long generate_best_seq_from_edges(Graph* DAGCon) &G_Node(*DAGCon, Input_Edges(G_Node(*DAGCon, DAGCon->s_end_nodeID)).list[max_end_edge].in_node), 1); } - - - ///if((*direction) == 1) - ///if(max_start < max_end && DAGCon->g_nodes.length > 5) - ///if(DAGCon->g_nodes.length > 5) - // if(max_start < max_end && DAGCon->node_q.end - DAGCon->node_q.beg > 1) - // { - // print_graph(DAGCon); - // long long str; - // while (pop_from_Queue(&(DAGCon->node_q), &str)) - // { - // fprintf(stderr, "%c", (char)str); - // } - // fprintf(stderr, "\n"); - // fprintf(stderr, "###################(*max_count): %d, (*max_edge): %d, (*direction): %d###################\n\n", - // (*max_count), max_start >= max_end? max_start_edge:max_end_edge, max_start >= max_end? 0:1); - // } - // if(max_start >= max_end) - // { - // newNode = &(G_Node(*DAGCon, DAGCon->s_start_nodeID)); - // if(Output_Edges(*newNode).list[max_start_edge].weight != (*max_count)) - // { - // fprintf(stderr, "ERROR\n"); - // } - // } - // else - // { - // newNode = &(G_Node(*DAGCon, DAGCon->s_end_nodeID)); - // if(Input_Edges(*newNode).list[max_end_edge].weight != (*max_count)) - // { - // fprintf(stderr, "ERROR\n"); - // } - // } - return max_count; - - } @@ -6525,9 +3745,9 @@ inline void generate_seq_from_node(Graph* DAGCon, Node* node, int direction) { clear_Queue(&(DAGCon->node_q)); RSet iter; - long long max; - Node* max_node; - Node* getNodes; + uint64_t max; + Node* max_node = NULL; + Node* getNodes = NULL; if(direction == 0) @@ -6582,14 +3802,15 @@ inline void generate_seq_from_node(Graph* DAGCon, Node* node, int direction) long long generate_best_seq_from_nodes(Graph* DAGCon) { - long long max_start, max_end; + long long max_start = 0, max_end = 0; RSet iter; - Edge* e; - Node* newNode; - Node* getNode; - Node* max_start_node; - Node* max_end_node; - long long max_count, i; + Edge* e = NULL; + Node* newNode = NULL; + Node* getNode = NULL; + Node* max_start_node = NULL; + Node* max_end_node = NULL; + long long max_count = 0; + uint64_t i; for (i = 0; i < DAGCon->g_nodes.length; i++) { @@ -6632,7 +3853,7 @@ long long generate_best_seq_from_nodes(Graph* DAGCon) clear_RSet(&iter); while(getOutputNodes(&iter, DAGCon, newNode, &getNode)) { - if(getNode->weight > max_start) + if(getNode->weight > (uint64_t)max_start) { max_start = getNode->weight; max_start_node = getNode; @@ -6646,7 +3867,7 @@ long long generate_best_seq_from_nodes(Graph* DAGCon) clear_RSet(&iter); while(getInputNodes(&iter, DAGCon, newNode, &getNode)) { - if(getNode->weight > max_end) + if(getNode->weight > (uint64_t)max_end) { max_end = getNode->weight; max_end_node = getNode; @@ -6664,43 +3885,7 @@ long long generate_best_seq_from_nodes(Graph* DAGCon) generate_seq_from_node(DAGCon, max_end_node, 1); } - - /** - if(DAGCon->g_nodes.length > 5) - ///if(max_start < max_end && DAGCon->node_q.end - DAGCon->node_q.beg > 1) - ///if(max_start < max_end) - { - print_graph(DAGCon); - long long str; - while (pop_from_Queue(&(DAGCon->node_q), &str)) - { - fprintf(stderr, "%c", (char)str); - } - fprintf(stderr, "\n"); - fprintf(stderr, "###################(*max_count): %d, (*max_node): %d, (*direction): %d###################\n\n", - max_count, max_start >= max_end? max_start_node->ID:max_end_node->ID, max_start >= max_end? 0:1); - } - if(max_start >= max_end) - { - newNode = &(G_Node(*DAGCon, DAGCon->s_start_nodeID)); - if(max_start_node->weight != max_count) - { - fprintf(stderr, "ERROR\n"); - } - } - else - { - newNode = &(G_Node(*DAGCon, DAGCon->s_end_nodeID)); - if(max_end_node->weight != max_count) - { - fprintf(stderr, "ERROR\n"); - } - } - **/ - return max_count; - - } @@ -6720,69 +3905,12 @@ void build_DAGCon(Graph* DAGCon, Graph* backbone, long long currentNodeID, long DAGCon->s_end_nodeID = newNode->ID; - for (i = 0; i < G_Node(*backbone, currentNodeID).insertion_edges.length; i++) + for (i = 0; i < (long long)G_Node(*backbone, currentNodeID).insertion_edges.length; i++) { path_weight = G_Node(*backbone, currentNodeID).insertion_edges.list[i].weight; lastNode = &(G_Node(*DAGCon, DAGCon->s_start_nodeID)); - - /*****************************debug***************************************/ - // newNode = add_Node_DAGCon(DAGCon, 'F'); - // add_bi_direction_edge(DAGCon, lastNode, newNode, path_weight, 0); - // add_bi_direction_edge(DAGCon, newNode, &(G_Node(*DAGCon, DAGCon->s_end_nodeID)), path_weight, 0); - // delete_Node_DAGCon(DAGCon, newNode); - // if(remove_and_check_bi_direction_edge_from_nodes(DAGCon, &(G_Node(*DAGCon, DAGCon->s_start_nodeID)), newNode)) - // { - // fprintf(stderr, "step: %d, j: %d\n", step, j); - // } - // if(remove_and_check_bi_direction_edge_from_nodes(DAGCon, newNode, &(G_Node(*DAGCon, DAGCon->s_end_nodeID)))) - // { - // fprintf(stderr, "step: %d, j: %d\n", step, j); - // } - - - // Node* node0; - // Node* node1; - // Node* node2; - // newNode = add_Node_DAGCon(DAGCon, 'T'); - // add_bi_direction_edge(DAGCon, lastNode, newNode, path_weight, 0); - // node0 = newNode; - // lastNode = newNode; - // newNode = add_Node_DAGCon(DAGCon, 'T'); - // add_bi_direction_edge(DAGCon, lastNode, newNode, path_weight, 0); - // node1 = newNode; - // lastNode = newNode; - // newNode = add_Node_DAGCon(DAGCon, 'T'); - // add_bi_direction_edge(DAGCon, lastNode, newNode, path_weight, 0); - // node2 = newNode; - - - // lastNode = &(G_Node(*DAGCon, DAGCon->s_start_nodeID)); - // delete_Node_DAGCon(DAGCon, node1); - - // Edge* e_forward; - // Edge* e_backward; - // if(get_bi_Edge(DAGCon, node2, &(G_Node(*DAGCon, DAGCon->s_end_nodeID)), &e_forward, &e_backward)) - // { - // remove_and_check_bi_direction_edge_from_edge(DAGCon, e_forward); - // } - - // if(get_bi_Edge(DAGCon, node2, &(G_Node(*DAGCon, DAGCon->s_end_nodeID)), &e_forward, &e_backward)) - // { - // fprintf(stderr, "edge remove error\n"); - // } - - // remove_and_check_bi_direction_edge_from_nodes(DAGCon, lastNode, node0); - // remove_and_check_bi_direction_edge_from_nodes(DAGCon, node0, node1); - // remove_and_check_bi_direction_edge_from_nodes(DAGCon, node1, node2); - // if(remove_and_check_bi_direction_edge_from_nodes(DAGCon, node2, &(G_Node(*DAGCon, DAGCon->s_end_nodeID)))) - // { - // fprintf(stderr, "edge remove error\n"); - // } - /*****************************debug***************************************/ - - step = G_Node(*backbone, currentNodeID).insertion_edges.list[i].length; if(step != 0) { @@ -6794,27 +3922,6 @@ void build_DAGCon(Graph* DAGCon, Graph* backbone, long long currentNodeID, long newNode = add_Node_DAGCon(DAGCon, base); add_bi_direction_edge(DAGCon, lastNode, newNode, path_weight, 0); - /*****************************debug***************************************/ - // if(!add_and_check_bi_direction_edge(DAGCon, lastNode, newNode, path_weight, 0)) - // { - // fprintf(stderr, "haha\n"); - // } - - // if(add_and_check_bi_direction_edge(DAGCon, lastNode, newNode, path_weight, 0)) - // { - // fprintf(stderr, "haha\n"); - // } - // if(j != 0) - // { - // add_bi_direction_edge(DAGCon, &(G_Node(*DAGCon, DAGCon->s_start_nodeID)), newNode, path_weight, 0); - // if(!remove_and_check_bi_direction_edge_from_nodes(DAGCon, &(G_Node(*DAGCon, DAGCon->s_start_nodeID)), - // newNode)) - // { - // fprintf(stderr, "step: %d, j: %d\n", step, j); - // } - // } - /*****************************debug***************************************/ - nodeID = G_Node(*backbone, nodeID).insertion_edges.list[0].out_node; lastNode = newNode; @@ -6824,24 +3931,6 @@ void build_DAGCon(Graph* DAGCon, Graph* backbone, long long currentNodeID, long { add_bi_direction_edge(DAGCon, lastNode, &(G_Node(*DAGCon, DAGCon->s_end_nodeID)), path_weight, 0); - /*****************************debug***************************************/ - // if(!add_and_check_bi_direction_edge(DAGCon, lastNode, &(G_Node(*DAGCon, DAGCon->s_end_nodeID)), path_weight, 0)) - // { - // fprintf(stderr, "haha\n"); - // } - - // if(add_and_check_bi_direction_edge(DAGCon, lastNode, &(G_Node(*DAGCon, DAGCon->s_end_nodeID)), path_weight, 0)) - // { - // fprintf(stderr, "haha\n"); - // } - // add_bi_direction_edge(DAGCon, &(G_Node(*DAGCon, DAGCon->s_start_nodeID)), - // &(G_Node(*DAGCon, DAGCon->s_end_nodeID)), path_weight, 0); - // if(!remove_and_check_bi_direction_edge_from_nodes(DAGCon, &(G_Node(*DAGCon, DAGCon->s_start_nodeID)), - // &(G_Node(*DAGCon, DAGCon->s_end_nodeID)))) - // { - // fprintf(stderr, "step: %d, j: %d\n", step, j); - // } - /*****************************debug***************************************/ } } @@ -6849,31 +3938,11 @@ void build_DAGCon(Graph* DAGCon, Graph* backbone, long long currentNodeID, long } - ///test_single_path_new(DAGCon, backbone, currentNodeID); - Merge_DAGCon(DAGCon); ///(*max_count) = generate_best_seq_from_edges(DAGCon); (*max_count) = generate_best_seq_from_nodes(DAGCon); - - // long long k = 0; - // for (i = 0; i < DAGCon->g_nodes.length; i++) - // { - // if(i != DAGCon->s_start_nodeID && i != DAGCon->s_end_nodeID) - // { - // if(If_Node_Exist(G_Node(*DAGCon, i)) && G_Node(*DAGCon, i).weight > k) - // { - // k = G_Node(*DAGCon, i).weight; - // } - // } - // } - - // if(k != (*max_count)) - // { - // fprintf(stderr, "k: %d, (*max_count): %d\n", k, (*max_count)); - // } - ///very important backbone->g_nodes.list[currentNodeID].num_insertions = 0; @@ -6883,23 +3952,23 @@ void build_DAGCon(Graph* DAGCon, Graph* backbone, long long currentNodeID, long void debug_whole_graph(Graph* g) { long long i, j, k; - for (i = 0; i < g->g_nodes.length; i++) + for (i = 0; i < (long long)g->g_nodes.length; i++) { if(g->g_nodes.list[i].deletion_edges.length!= 0 && g->g_nodes.list[i].deletion_edges.length!= 1) { - fprintf(stderr, "g->g_nodes.list[i].deletion_edges.length: %d\n", + fprintf(stderr, "g->g_nodes.list[i].deletion_edges.length: %lu\n", g->g_nodes.list[i].deletion_edges.length); } } - for (i = g->s_start_nodeID; i < g->s_end_nodeID; i++) + for (i = g->s_start_nodeID; i < (long long)g->s_end_nodeID; i++) { if(g->g_nodes.list[i].mismatch_edges.length > 4 || g->g_nodes.list[i].mismatch_edges.length < 1) { - fprintf(stderr, "g->s_end_nodeID: %d, g->g_nodes.list[%d].mismatch_edges.length: %d\n", + fprintf(stderr, "g->s_end_nodeID: %lu, g->g_nodes.list[%lld].mismatch_edges.length: %lu\n", g->s_end_nodeID, i, g->g_nodes.list[i].mismatch_edges.length); } } @@ -6907,17 +3976,17 @@ void debug_whole_graph(Graph* g) char current[1000]; char compare[1000]; long long total_weight = 0; - for (i = g->s_start_nodeID; i < g->s_end_nodeID; i++) + for (i = g->s_start_nodeID; i < (long long)g->s_end_nodeID; i++) { total_weight = 0; - for (j = 0; j < G_Node(*g, i).insertion_edges.length; j++) + for (j = 0; j < (long long)G_Node(*g, i).insertion_edges.length; j++) { total_weight = total_weight + G_Node(*g, i).insertion_edges.list[j].weight; extract_path(g, i, j, current); - for (k = j + 1; k < G_Node(*g, i).insertion_edges.length; k++) + for (k = j + 1; k < (long long)G_Node(*g, i).insertion_edges.length; k++) { extract_path(g, i, k, compare); if(strcmp(current, compare)==0) @@ -6927,7 +3996,7 @@ void debug_whole_graph(Graph* g) } } - if(total_weight != G_Node(*g, i).num_insertions) + if(total_weight != (long long)G_Node(*g, i).num_insertions) { fprintf(stderr,"error\n"); } @@ -6941,41 +4010,22 @@ void get_seq_from_Graph(Graph* backbone, Graph* DAGCon, Correct_dumy* dumy, Ciga char* r_string, long long r_string_length, long long r_string_site) { ///debug_whole_graph(backbone); - - // double threshold; - // if(roundID > 0) - // { - // threshold = CORRECT_THRESHOLD_SECOND; - // } - // else - // { - // threshold = CORRECT_THRESHOLD; - // } - - - - - long long new_seq_length = 0; long long currentNodeID; long long i; - // 总共有以下几种情况: + // There are several cases: // 1. match 2. mismatch (A, C, G, T, N) 3. deletion 4. insertion (A, C, G, T) - // 其实就是 1. 自己本身的weight 2. alignToNode的weight 3. insertion节点的weight + // in fact, 1. the weight of node itself 2. weight of alignToNode 3. weight of insertion node long long max_count; int max_type; long long max_edge; long long total_count; - long long nodeID; - char current_base; long long current_weight; long long max_insertion_count; currentNodeID = backbone->s_start_nodeID; - ///fprintf(stderr, "currentNodeID: %d\n", currentNodeID); - - while (currentNodeID != backbone->s_end_nodeID) + while (currentNodeID != (long long)backbone->s_end_nodeID) { total_count = 0; max_count = -1; @@ -6983,18 +4033,12 @@ char* r_string, long long r_string_length, long long r_string_site) max_edge = -1; - ///假如这是个backbone节点 - ///有三类出边 + ///if it is a backbone node, there are three types od out-edges ///1. mismatch_edges 2. insertion_edges 3. deletion_edges - if (currentNodeID >= backbone->s_start_nodeID && currentNodeID <= backbone->s_end_nodeID) + if (currentNodeID >= (long long)backbone->s_start_nodeID && currentNodeID <= (long long)backbone->s_end_nodeID) { - - - - - ///mismatch_edges - for (i = 0; i < backbone->g_nodes.list[currentNodeID].mismatch_edges.length; i++) + for (i = 0; i < (long long)backbone->g_nodes.list[currentNodeID].mismatch_edges.length; i++) { if (backbone->g_nodes.list[currentNodeID].num_insertions != 0) { @@ -7009,10 +4053,9 @@ char* r_string, long long r_string_length, long long r_string_site) total_count = total_count + current_weight; - ///match - ///match要处理插入的情况 - ///如果这里有insertion, 这个节点会过两遍 - ///第一遍num_insertions > 0, 第二遍num_insertions=0 + ///for match, it needs to deal with both match and insertion + ///if there is a insertion, we need to check this node two times + ///1. num_insertions > 0, 2. num_insertions=0 if (current_weight > max_count) { max_count = current_weight; @@ -7035,29 +4078,14 @@ char* r_string, long long r_string_length, long long r_string_site) max_count = max_insertion_count; max_type = INSERTION; } - // for (i = 0; i < backbone->g_nodes.list[currentNodeID].insertion_edges.length; i++) - // { - // total_count = total_count + backbone->g_nodes.list[currentNodeID].insertion_edges.list[i].weight; - - // if (backbone->g_nodes.list[currentNodeID].insertion_edges.list[i].weight > max_count) - // { - // max_count = backbone->g_nodes.list[currentNodeID].insertion_edges.list[i].weight; - // max_edge = i; - // max_type = INSERTION; - // } - // } } - - - - ///deletion_edges - for (i = 0; i < backbone->g_nodes.list[currentNodeID].deletion_edges.length; i++) + for (i = 0; i < (long long)backbone->g_nodes.list[currentNodeID].deletion_edges.length; i++) { total_count = total_count + backbone->g_nodes.list[currentNodeID].deletion_edges.list[i].weight; - if (backbone->g_nodes.list[currentNodeID].deletion_edges.list[i].weight > max_count) + if ((long long)backbone->g_nodes.list[currentNodeID].deletion_edges.list[i].weight > max_count) { max_count = backbone->g_nodes.list[currentNodeID].deletion_edges.list[i].weight; max_edge = i; @@ -7065,27 +4093,22 @@ char* r_string, long long r_string_length, long long r_string_site) } } - - - - - ///这种情况下矫正 + ///do correction if(max_count >= total_count*(CORRECT_THRESHOLD)) - ///if(max_count >= total_count * threshold) { currentNodeID = add_path_to_correct_read_new(backbone, DAGCon, dumy, currentNodeID, max_type, max_edge, current_cigar, self_string); } else { - ///NOTE: currentNodeID = 0 is a tmp node without any sense + ///NOTE: currentNodeID = 0 is a tmp node without any sense if(currentNodeID > 0 && if_is_homopolymer_strict(r_string_site + currentNodeID - 1, r_string, r_string_length) && max_count >= total_count*CORRECT_THRESHOLD_HOMOPOLYMER) { currentNodeID = add_path_to_correct_read_new(backbone, DAGCon, dumy, currentNodeID, max_type, max_edge, current_cigar, self_string); } - else///不矫正, 直接取下一个backbone节点 + else///don't do correction, directly use the base of next backbone node { currentNodeID++; add_base_to_correct_read_directly(dumy, backbone->g_nodes.list[currentNodeID].base); @@ -7094,9 +4117,8 @@ char* r_string, long long r_string_length, long long r_string_site) } } - ///fprintf(stderr, "currentNodeID: %d, max_type: %d\n", currentNodeID, max_type); } - else ///非backbone节点就会出错了 + else ///if there is a non-backbone node { fprintf(stderr, "error\n"); } @@ -7106,625 +4128,6 @@ char* r_string, long long r_string_length, long long r_string_site) } -void get_seq_from_Graph_print(Graph* backbone, Correct_dumy* dumy, Cigar_record* current_cigar, char* self_string, -char* r_string, long long r_string_length, long long r_string_site) -{ - long long new_seq_length = 0; - long long currentNodeID; - long long i; - // 总共有以下几种情况: - // 1. match 2. mismatch (A, C, G, T, N) 3. deletion 4. insertion (A, C, G, T) - // 其实就是 1. 自己本身的weight 2. alignToNode的weight 3. insertion节点的weight - long long max_count; - int max_type; - long long max_edge; - long long total_count; - long long nodeID; - char current_base; - long long current_weight; - - currentNodeID = backbone->s_start_nodeID; - - ///fprintf(stderr, "currentNodeID: %d\n", currentNodeID); - - while (currentNodeID != backbone->s_end_nodeID) - { - total_count = 0; - max_count = -1; - max_type = -1; - max_edge = -1; - - - ///假如这是个backbone节点 - ///有三类出边 - ///1. mismatch_edges 2. insertion_edges 3. deletion_edges - if (currentNodeID >= backbone->s_start_nodeID && currentNodeID <= backbone->s_end_nodeID) - { - - - - - - ///mismatch_edges - for (i = 0; i < backbone->g_nodes.list[currentNodeID].mismatch_edges.length; i++) - { - if(currentNodeID == 187) - { - fprintf(stderr, "backbone->g_nodes.list[currentNodeID].num_insertions: %d\n", - backbone->g_nodes.list[currentNodeID].num_insertions); - } - - if (backbone->g_nodes.list[currentNodeID].num_insertions != 0) - { - current_weight = backbone->g_nodes.list[currentNodeID].mismatch_edges.list[i].weight - - backbone->g_nodes.list[currentNodeID].mismatch_edges.list[i].num_insertions; - } - else - { - current_weight = backbone->g_nodes.list[currentNodeID].mismatch_edges.list[i].weight; - } - - if(currentNodeID == 187) - { - fprintf(stderr, "current_weight: %d\n", - current_weight); - } - - - total_count = total_count + current_weight; - - ///match - ///match要处理插入的情况 - ///如果这里有insertion, 这个节点会过两遍 - ///第一遍num_insertions > 0, 第二遍num_insertions=0 - if (current_weight > max_count) - { - max_count = current_weight; - max_edge = i; - max_type = MISMATCH; - } - } - - ///insertion_edges - if (backbone->g_nodes.list[currentNodeID].num_insertions != 0) - { - for (i = 0; i < backbone->g_nodes.list[currentNodeID].insertion_edges.length; i++) - { - total_count = total_count + backbone->g_nodes.list[currentNodeID].insertion_edges.list[i].weight; - - if (backbone->g_nodes.list[currentNodeID].insertion_edges.list[i].weight > max_count) - { - max_count = backbone->g_nodes.list[currentNodeID].insertion_edges.list[i].weight; - max_edge = i; - max_type = INSERTION; - } - - if(currentNodeID == 187) - { - fprintf(stderr, "backbone->g_nodes.list[currentNodeID].insertion_edges.list[i].weight: %d, length: %d\n", - backbone->g_nodes.list[currentNodeID].insertion_edges.list[i].weight, - backbone->g_nodes.list[currentNodeID].insertion_edges.list[i].length); - - nodeID = backbone->g_nodes.list[currentNodeID].insertion_edges.list[i].out_node; - fprintf(stderr, "%c\n", backbone->g_nodes.list[nodeID].base); - } - } - } - - - - ///deletion_edges - for (i = 0; i < backbone->g_nodes.list[currentNodeID].deletion_edges.length; i++) - { - total_count = total_count + backbone->g_nodes.list[currentNodeID].deletion_edges.list[i].weight; - - if (backbone->g_nodes.list[currentNodeID].deletion_edges.list[i].weight > max_count) - { - max_count = backbone->g_nodes.list[currentNodeID].deletion_edges.list[i].weight; - max_edge = i; - max_type = DELETION; - } - - if(currentNodeID == 187) - { - fprintf(stderr, "backbone->g_nodes.list[currentNodeID].deletion_edges.list[i].weight: %d\n", - backbone->g_nodes.list[currentNodeID].deletion_edges.list[i].weight); - } - } - - /** - if(currentNodeID > 0 && backbone->g_nodes.list[currentNodeID].base != r_string[r_string_site + currentNodeID - 1]) - { - fprintf(stderr, "currentNodeID: %d\n", currentNodeID); - } - **/ - - fprintf(stderr, "currentNodeID: %d, max_count: %d, max_type: %d, total_count: %d\n", - currentNodeID, max_count, max_type, total_count); - - - - if(currentNodeID == 187) - { - nodeID = backbone->g_nodes.list[currentNodeID].insertion_edges.list[max_edge].out_node; - fprintf(stderr, "%c", backbone->g_nodes.list[nodeID].base); - nodeID = backbone->g_nodes.list[nodeID].insertion_edges.list[0].out_node; - fprintf(stderr, "%c\n", backbone->g_nodes.list[nodeID].base); - } - - - - ///这种情况下矫正 - if(max_count >= total_count*CORRECT_THRESHOLD) - { - currentNodeID = add_path_to_correct_read(backbone, dumy, currentNodeID, max_type, max_edge, current_cigar, - self_string); - } - else - { - ///NOTE: currentNodeID = 0 is a tmp node without any sense - if(currentNodeID > 0 && if_is_homopolymer_strict(r_string_site + currentNodeID - 1, r_string, r_string_length) - && max_count >= total_count*CORRECT_THRESHOLD_HOMOPOLYMER/** && max_type != MISMATCH**/) - { - currentNodeID = add_path_to_correct_read(backbone, dumy, currentNodeID, max_type, max_edge, current_cigar, - self_string); - } - else///不矫正, 直接取下一个backbone节点 - { - currentNodeID++; - add_base_to_correct_read_directly(dumy, backbone->g_nodes.list[currentNodeID].base); - - add_cigar_record(&(backbone->g_nodes.list[currentNodeID].base), 1, current_cigar, 0); - } - } - - ///fprintf(stderr, "currentNodeID: %d, max_type: %d\n", currentNodeID, max_type); - } - else ///非backbone节点就会出错了 - { - fprintf(stderr, "error\n"); - } - - } - -} - - - - - - -/** -///从backbone_start遍历到backbone_end节点,生成出来的seq要接着放到dumy->corrected_read中 -void get_seq_from_Graph(Graph* backbone, long long backbone_start, long long backbone_end, Correct_dumy* dumy) -{ - long long new_seq_length = 0; - long long currentNodeID; - long long i; - // 总共有以下几种情况: - // 1. match 2. mismatch (A, C, G, T, N) 3. deletion 4. insertion (A, C, G, T) - // 其实就是 1. 自己本身的weight 2. alignToNode的weight 3. insertion节点的weight - long long max_count; - int max_type; - long long max_node; - long long total_count; - long long nodeID; - char current_base; - - currentNodeID = backbone_start; - while (currentNodeID != backbone_end) - { - total_count = 0; - max_count = -1; - ///图上能够被遍历到的有两种节点 - ///1. backbone节点 2. insertion节点 - ///backbone节点才有match/mismatch/deletion - ///insertion这些都没有,就是无脑看出边 - - ///假如这是个backbone节点 - if (currentNodeID >= backbone_start && currentNodeID <= backbone_end) - { - ///match - total_count += backbone->g_nodes.list[currentNodeID].weight; - max_count = backbone->g_nodes.list[currentNodeID].weight; - max_type = 0; - max_node = currentNodeID; - ///mismatch和deletion (A, C, G, T, N, D, 除了自己的那个字符) - for (i = 0; i < backbone->g_nodes.list[currentNodeID].alignedTo_Nodes.length; i++) - { - nodeID = backbone->g_nodes.list[currentNodeID].alignedTo_Nodes.list[i].out_node; - total_count += backbone->g_nodes.list[nodeID].weight; - - if (backbone->g_nodes.list[nodeID].weight > max_count) - { - max_count = backbone->g_nodes.list[nodeID].weight; - max_type = 1; - max_node = nodeID; - } - } - ///insertion (A, C, G, T, N) - ///注意这个出边还得避开下一个backbone节点 - for (i = 0; i < backbone->g_nodes.list[currentNodeID].outcome_edges.length; i++) - { - if (backbone->g_nodes.list[currentNodeID].outcome_edges.list[i].weight == 2) - { - - nodeID = backbone->g_nodes.list[currentNodeID].outcome_edges.list[i].out_node; - total_count += backbone->g_nodes.list[nodeID].weight; - - if (backbone->g_nodes.list[nodeID].weight > max_count) - { - max_count = backbone->g_nodes.list[nodeID].weight; - max_type = 2; - max_node = nodeID; - } - - ///拿到的nodeID应该一定不是backbone上的,如果是就错了 - if (nodeID >= backbone_start && nodeID <= backbone_end) - { - fprintf(stderr, "error\n"); - } - - } - } - } - else ///如果是insertion节点,就无脑看出边 - { - ///insertion (A, C, G, T, N) - ///注意这个出边不用避开下一个backbone节点 - for (i = 0; i < backbone->g_nodes.list[currentNodeID].outcome_edges.length; i++) - { - if (backbone->g_nodes.list[currentNodeID].outcome_edges.list[i].weight == 2) - { - - nodeID = backbone->g_nodes.list[currentNodeID].outcome_edges.list[i].out_node; - total_count += backbone->g_nodes.list[nodeID].weight; - - if (backbone->g_nodes.list[nodeID].weight > max_count) - { - max_count = backbone->g_nodes.list[nodeID].weight; - max_type = 2; - max_node = nodeID; - } - - } - else ///如果边不是2就不对了 - { - fprintf(stderr, "error\n"); - } - - } - } - - - - - - - - if(max_count >= total_count*CORRECT_THRESHOLD) - { - current_base = backbone->g_nodes.list[max_node].base; - ///说明是insertion - if (max_type == 2) - { - currentNodeID = max_node; - } - else ///其他情况依然沿着backbone向前 - { - currentNodeID++; - } - } - else - { - ///假如这是个backbone节点, 不矫正 - if (currentNodeID >= backbone_start && currentNodeID <= backbone_end) - { - current_base = backbone->g_nodes.list[currentNodeID].base; - currentNodeID++; - } - else///如果在insertion节点上不达标很麻烦...,只能选最大的了 - { - current_base = backbone->g_nodes.list[max_node].base; - ///说明是insertion - if (max_type == 2) - { - currentNodeID = max_node; - } - else ///insertion节点不可能出现这种情况 - { - fprintf(stderr, "error\n"); - } - } - } - - - if (max_count <= 0) - { - fprintf(stderr, "error\n"); - } - - - add_base_to_correct_read(dumy, current_base, max_type); - - } - - - - - ///最后还要处理backbone_end这个节点 - total_count = 0; - max_count = -1; - ///这个节点肯定是backbone上的节点啊 - ///match - total_count += backbone->g_nodes.list[currentNodeID].weight; - max_count = backbone->g_nodes.list[currentNodeID].weight; - max_type = 0; - max_node = currentNodeID; - ///mismatch和deletion (A, C, G, T, N, D, 除了自己的那个字符) - for (i = 0; i < backbone->g_nodes.list[currentNodeID].alignedTo_Nodes.length; i++) - { - nodeID = backbone->g_nodes.list[currentNodeID].alignedTo_Nodes.list[i].out_node; - total_count += backbone->g_nodes.list[nodeID].weight; - - if (backbone->g_nodes.list[nodeID].weight > max_count) - { - max_count = backbone->g_nodes.list[nodeID].weight; - max_type = 1; - max_node = nodeID; - } - } - ///这个节点不应该有任何出边了 - if(backbone->g_nodes.list[currentNodeID].outcome_edges.length) - { - fprintf(stderr, "haha\n"); - } - - - if(max_count >= total_count*CORRECT_THRESHOLD) - { - current_base = backbone->g_nodes.list[max_node].base; - } - else - { - current_base = backbone->g_nodes.list[currentNodeID].base; - } - - - if (max_count <= 0) - { - fprintf(stderr, "error\n"); - } - - add_base_to_correct_read(dumy, current_base, max_type); - -} -**/ - - - - - - - - - - -/** -///从backbone_start遍历到backbone_end节点,生成出来的seq要接着放到dumy->corrected_read中 -void get_seq_from_Graph_Len2(Graph* backbone, long long backbone_start, long long backbone_end, Correct_dumy* dumy) -{ - long long new_seq_length = 0; - long long currentNodeID; - long long i; - // 总共有以下几种情况: - // 1. match 2. mismatch (A, C, G, T, N) 3. deletion 4. insertion (A, C, G, T) - // 其实就是 1. 自己本身的weight 2. alignToNode的weight 3. insertion节点的weight - long long max_count; - int max_type; - long long max_node; - long long total_count; - long long nodeID; - char current_base; - char buffer[2]; - - currentNodeID = backbone_start; - while (currentNodeID != backbone_end) - { - total_count = 0; - max_count = -1; - ///图上能够被遍历到的有两种节点 - ///1. backbone节点 2. insertion节点 - ///backbone节点才有match/mismatch/deletion - ///insertion这些都没有,就是无脑看出边 - - ///假如这是个backbone节点 - if (currentNodeID >= backbone_start && currentNodeID <= backbone_end) - { - ///match - total_count += backbone->g_nodes.list[currentNodeID].weight; - max_count = backbone->g_nodes.list[currentNodeID].weight; - max_type = 0; - max_node = currentNodeID; - ///mismatch和deletion (A, C, G, T, N, D, 除了自己的那个字符) - for (i = 0; i < backbone->g_nodes.list[currentNodeID].alignedTo_Nodes.length; i++) - { - nodeID = backbone->g_nodes.list[currentNodeID].alignedTo_Nodes.list[i].out_node; - total_count += backbone->g_nodes.list[nodeID].weight; - - if (backbone->g_nodes.list[nodeID].weight > max_count) - { - max_count = backbone->g_nodes.list[nodeID].weight; - max_type = 1; - max_node = nodeID; - } - } - ///insertion (A, C, G, T, N) - ///注意这个出边还得避开下一个backbone节点 - for (i = 0; i < backbone->g_nodes.list[currentNodeID].outcome_edges.length; i++) - { - if (backbone->g_nodes.list[currentNodeID].outcome_edges.list[i].weight == 2) - { - ///fprintf(stderr, "error\n"); - nodeID = backbone->g_nodes.list[currentNodeID].outcome_edges.list[i].out_node; - total_count += backbone->g_nodes.list[nodeID].weight; - - if (backbone->g_nodes.list[nodeID].weight > max_count) - { - max_count = backbone->g_nodes.list[nodeID].weight; - max_type = 2; - max_node = nodeID; - } - - ///拿到的nodeID应该一定不是backbone上的,如果是就错了 - if (nodeID >= backbone_start && nodeID <= backbone_end) - { - fprintf(stderr, "error\n"); - } - - } - } - } - else ///如果是insertion节点,就无脑看出边 - { - if (backbone->g_nodes.list[currentNodeID].outcome_edges.length!=1) - { - fprintf(stderr, "error000\n"); - } - - ///insertion (A, C, G, T, N) - ///注意这个出边不用避开下一个backbone节点 - for (i = 0; i < backbone->g_nodes.list[currentNodeID].outcome_edges.length; i++) - { - if (backbone->g_nodes.list[currentNodeID].outcome_edges.list[i].weight == 2) - { - - nodeID = backbone->g_nodes.list[currentNodeID].outcome_edges.list[i].out_node; - total_count += backbone->g_nodes.list[nodeID].weight; - - if (backbone->g_nodes.list[nodeID].weight > max_count) - { - max_count = backbone->g_nodes.list[nodeID].weight; - max_type = 2; - max_node = nodeID; - } - - } - else ///如果边不是2就不对了 - { - fprintf(stderr, "error\n"); - } - - } - } - - - - - - - - if(max_count >= total_count*CORRECT_THRESHOLD) - { - current_base = backbone->g_nodes.list[max_node].base; - ///说明是insertion - if (max_type == 2) - { - currentNodeID = max_node; - } - else ///其他情况依然沿着backbone向前 - { - currentNodeID++; - } - } - else - { - ///假如这是个backbone节点, 不矫正 - if (currentNodeID >= backbone_start && currentNodeID <= backbone_end) - { - current_base = backbone->g_nodes.list[currentNodeID].base; - currentNodeID++; - } ///应该不存在这个问题 - else///如果在insertion节点上不达标很麻烦...,只能选最大的了 - { - ///因为现在每个insert节点只有一个出边,且这个出边到backbone - fprintf(stderr, "error111\n"); - } - } - - - if (max_count <= 0) - { - fprintf(stderr, "error\n"); - } - - if (current_base < 'A') - { - buffer[0] = s_H[(current_base >> 2) & ((uint8_t)3)]; - buffer[1] = s_H[current_base & ((uint8_t)3)]; - add_base_to_correct_read(dumy, buffer[0], max_type); - add_base_to_correct_read(dumy, buffer[1], max_type); - } - else - { - add_base_to_correct_read(dumy, current_base, max_type); - } - - } - - - - - ///最后还要处理backbone_end这个节点 - total_count = 0; - max_count = -1; - ///这个节点肯定是backbone上的节点啊 - ///match - total_count += backbone->g_nodes.list[currentNodeID].weight; - max_count = backbone->g_nodes.list[currentNodeID].weight; - max_type = 0; - max_node = currentNodeID; - ///mismatch和deletion (A, C, G, T, N, D, 除了自己的那个字符) - for (i = 0; i < backbone->g_nodes.list[currentNodeID].alignedTo_Nodes.length; i++) - { - nodeID = backbone->g_nodes.list[currentNodeID].alignedTo_Nodes.list[i].out_node; - total_count += backbone->g_nodes.list[nodeID].weight; - - if (backbone->g_nodes.list[nodeID].weight > max_count) - { - max_count = backbone->g_nodes.list[nodeID].weight; - max_type = 1; - max_node = nodeID; - } - } - ///这个节点不应该有任何出边了 - if(backbone->g_nodes.list[currentNodeID].outcome_edges.length) - { - fprintf(stderr, "haha\n"); - } - - - if(max_count >= total_count*CORRECT_THRESHOLD) - { - current_base = backbone->g_nodes.list[max_node].base; - } - else - { - current_base = backbone->g_nodes.list[currentNodeID].base; - } - - - if (max_count <= 0) - { - fprintf(stderr, "error\n"); - } - - add_base_to_correct_read(dumy, current_base, max_type); - -} -**/ - - void window_consensus(char* r_string, long long r_total_length, long long window_start, long long window_end, overlap_region_alloc* overlap_list, Correct_dumy* dumy, All_reads* R_INF, Graph* g, Graph* DAGCon, Cigar_record* current_cigar) { @@ -7737,14 +4140,12 @@ overlap_region_alloc* overlap_list, Correct_dumy* dumy, All_reads* R_INF, Graph* char* y_string; char* backbone; long long backbone_length; - long long i; + uint64_t i; long long y_start, y_length; long long overlapID, windowID; long long startNodeID, endNodeID, currentNodeID; - ///这个和前面算alignment还不一样 - ///那个时候x_start和x_end是当前窗口内的overlap的起始和结束位置 - ///这个window就是要做consensus啊,所以起始和结束就是window本身,固定的 + backbone = r_string + window_start; backbone_length = window_end - window_start + 1; @@ -7752,16 +4153,15 @@ overlap_region_alloc* overlap_list, Correct_dumy* dumy, All_reads* R_INF, Graph* long long correct_x_pos_s; - ///与当前window重叠的所有overlap for (i = 0; i < dumy->length; i++) { - ///这个是那个overlap的ID,而不是overlap里对应窗口的ID + ///this is the overlap ID overlapID = dumy->overlapID[i]; correct_x_pos_s = (overlap_list->list[overlapID].x_pos_s / WINDOW) * WINDOW; windowID = (window_start - correct_x_pos_s) / WINDOW; - ///如果这个window不匹配,跳过 + ///if this window is not matched if (overlap_list->list[overlapID].w_list[windowID].y_end == -1) { continue; @@ -7782,173 +4182,15 @@ overlap_region_alloc* overlap_list, Correct_dumy* dumy, All_reads* R_INF, Graph* x_string = r_string + x_start; y_string = dumy->overlap_region; - ///这个是比对上的起始base在backbone上对应的位置,也就是节点ID + ///here is the offset of the start base, also is the node ID currentNodeID = x_start - window_start; - ///这个是要用的cigar: overlap_list->list[overlapID].w_list[windowID].cigar; - - - /** - if(memcmp("m54238_180909_174539/6947324/ccs", Get_NAME((*R_INF),overlap_list->list[overlapID].x_id), - Get_NAME_LENGTH((*R_INF), overlap_list->list[overlapID].x_id)) == 0 && window_start == 12750 && window_end == 13124) - { - fprintf(stderr, "********x_start: %d, window_start: %d, window_end: %d, dumy->length: %d, y_name: %.*s\n", - x_start, window_start, window_end, dumy->length, - Get_NAME_LENGTH((*R_INF), overlap_list->list[overlapID].y_id), Get_NAME((*R_INF),overlap_list->list[overlapID].y_id)); - - - for (int ijk = 0; ijk < overlap_list->list[overlapID].w_list[windowID].cigar.length; ijk++) - { - fprintf(stderr, "###### Oper: %d, Len: %d\n", - overlap_list->list[overlapID].w_list[windowID].cigar.C_C[ijk], - overlap_list->list[overlapID].w_list[windowID].cigar.C_L[ijk]); - } - } - - - if(memcmp("m64011_190326_191011/163906371/ccs", Get_NAME((*R_INF),overlap_list->list[0].x_id), - Get_NAME_LENGTH((*R_INF), overlap_list->list[0].x_id)) == 0 && window_start == 9375 && window_end == 9749) - { - fprintf(stderr, "y_name: %.*s\n", Get_NAME_LENGTH((*R_INF), overlap_list->list[overlapID].y_id), - Get_NAME((*R_INF),overlap_list->list[overlapID].y_id)); - - reverse_complement(x_string, x_length); - reverse_complement(y_string, y_length); - - fprintf(stderr, "x: %.*s\n", x_length, x_string); - fprintf(stderr, "y: %.*s\n", y_length, y_string); - for (int ijk = 0; ijk < overlap_list->list[overlapID].w_list[windowID].cigar.length; ijk++) - { - fprintf(stderr, "###### Oper: %d, Len: %d\n", - overlap_list->list[overlapID].w_list[windowID].cigar.C_C[ijk], - overlap_list->list[overlapID].w_list[windowID].cigar.C_L[ijk]); - } - fprintf(stderr,"\n"); - - reverse_complement(x_string, x_length); - reverse_complement(y_string, y_length); - - - } - **/ + ///cigar: overlap_list->list[overlapID].w_list[windowID].cigar; addmatchedSeqToGraph(g, currentNodeID, x_string, x_length, y_string, y_length, &(overlap_list->list[overlapID].w_list[windowID].cigar), startNodeID, endNodeID); - - } - - /** - if(memcmp("m54238_180922_175520/52363405/ccs", Get_NAME((*R_INF),overlap_list->list[overlapID].x_id), - Get_NAME_LENGTH((*R_INF), overlap_list->list[overlapID].x_id)) == 0 && window_start == 6750 && window_end == 7124) - { - fprintf(stderr, "dumy->corrected_read_length: %d\n", dumy->corrected_read_length); - } - - if(memcmp("m54238_180922_175520/52363405/ccs", Get_NAME((*R_INF),overlap_list->list[overlapID].x_id), - Get_NAME_LENGTH((*R_INF), overlap_list->list[overlapID].x_id)) == 0 && window_start == 6750 && window_end == 7124) - { - get_seq_from_Graph_print(g, dumy, current_cigar, backbone, r_string, r_total_length, window_start); - } - else - { - get_seq_from_Graph(g, dumy, current_cigar, backbone, r_string, r_total_length, window_start); - } - - - - - - - - if(memcmp("m54238_180922_175520/52363405/ccs", Get_NAME((*R_INF),overlap_list->list[overlapID].x_id), - Get_NAME_LENGTH((*R_INF), overlap_list->list[overlapID].x_id)) == 0 && window_start == 6750 && window_end == 7124) - { - fprintf(stderr, "dumy->corrected_read_length: %d\n", dumy->corrected_read_length); - } - **/ - get_seq_from_Graph(g, DAGCon, dumy, current_cigar, backbone, r_string, r_total_length, window_start); - - ///get_seq_from_Graph(g, startNodeID, endNodeID, dumy); - ///get_seq_from_Graph_Len2(g, startNodeID, endNodeID, dumy); - - - - ///debug_graph(g, backbone_length); - - - - /** - for (i = 0; i < dumy->length; i++) - { - ///这个是那个overlap的ID,而不是overlap里对应窗口的ID - overlapID = dumy->overlapID[i]; - - correct_x_pos_s = (overlap_list->list[overlapID].x_pos_s / WINDOW) * WINDOW; - windowID = (window_start - correct_x_pos_s) / WINDOW; - - ///如果这个window不匹配,跳过 - if (overlap_list->list[overlapID].w_list[windowID].y_end == -1) - { - continue; - } - - - x_start = overlap_list->list[overlapID].w_list[windowID].x_start; - x_length = overlap_list->list[overlapID].w_list[windowID].x_end - - overlap_list->list[overlapID].w_list[windowID].x_start + 1; - - y_start = overlap_list->list[overlapID].w_list[windowID].y_start; - y_length = overlap_list->list[overlapID].w_list[windowID].y_end - - overlap_list->list[overlapID].w_list[windowID].y_start + 1; - - recover_UC_Read_sub_region(dumy->overlap_region, y_start, y_length, overlap_list->list[overlapID].y_pos_strand, - R_INF, overlap_list->list[overlapID].y_id); - - x_string = r_string + x_start; - y_string = dumy->overlap_region; - ///这个是比对上的起始base在backbone上对应的位置,也就是节点ID - currentNodeID = x_start - window_start; - ///这个是要用的cigar: overlap_list->list[overlapID].w_list[windowID].cigar; - - Graph_debug(g, currentNodeID, x_string, x_length, - y_string, y_length, &(overlap_list->list[overlapID].w_list[windowID].cigar), startNodeID, endNodeID); - } - - for (i = 0; i < g->g_nodes.length; i++) - { - if (i >= startNodeID && i <= endNodeID) - { - if (g->g_nodes.list[i].weight != 1) - { - fprintf(stderr, "error 1\n"); - } - } - else - { - if (g->g_nodes.list[i].weight != 0) - { - fprintf(stderr, "error 2\n"); - } - - if (g->g_nodes.list[i].alignedTo_Nodes.length != 0) - { - fprintf(stderr, "error 3\n"); - } - - ///节点入边不为0,说明这个不是alignTO节点,而是insert节点 - if (g->g_nodes.list[i].income_edges.length != 0 && g->g_nodes.list[i].outcome_edges.length != 0) - { - fprintf(stderr, "error 4\n"); - } - } - } - **/ - - - - } @@ -7982,7 +4224,7 @@ long long new_start, long long new_length) x_i = y_i = 0; char merge_base; - for (i = 0; i < new_cigar->length; i++) + for (i = 0; i < (long long)new_cigar->length; i++) { operation = Get_Cigar_Type(new_cigar->record[i]); operationLen = Get_Cigar_Length(new_cigar->record[i]); @@ -8026,8 +4268,6 @@ Round2_alignment* second_round, long long total_start_base, long long total_end_ long long total_window_start, long long total_window_end) { Cigar_record* new_cigar = &(second_round->tmp_cigar); - Correct_dumy* new_dumy = &(second_round->dumy); - if(new_cigar->length == 1 && Get_Cigar_Type(new_cigar->record[0]) == 0) { @@ -8042,21 +4282,18 @@ long long total_window_start, long long total_window_end) y_i = 0; int operation; int operationLen; - int cigar_error = 0; long long get_x_start, get_x_end, get_y_start, get_y_end; get_x_start = get_x_end = get_y_start = get_y_end = -1; int start_cigar = -1; int end_cigar = -1; - char merge_base; ///0 is match, 1 is mismatch, 2 is up, 3 is left - ///2是x缺字符(y多字符),而3是y缺字符(x多字符) ///obtained x_i may larger than start_base/end_base ///when operation == 3 ///so for operation == 3, we need deal with carefully - for (i = 0; i < new_cigar->length; i++) + for (i = 0; i < (long long)new_cigar->length; i++) { operation = Get_Cigar_Type(new_cigar->record[i]); operationLen = Get_Cigar_Length(new_cigar->record[i]); @@ -8109,11 +4346,11 @@ long long total_window_start, long long total_window_end) x_i++; y_i++; } - }///2是x缺字符(y多字符) + } else if (operation == 2) { y_i += operationLen; - }///3是y缺字符(x多字符) + } else if (operation == 3) { ///obtained x_i may larger than start_base/end_base @@ -8129,23 +4366,12 @@ long long total_window_start, long long total_window_end) return 0; } - /** - fprintf(stderr, "get_x_start: %d, get_x_end: %d\n", get_x_start, get_x_end); - fprintf(stderr, "get_y_start: %d, get_y_end: %d\n", get_y_start, get_y_end); - for (int ijk = 0; ijk < new_cigar->length; ijk++) - { - fprintf(stderr, "Oper: %d, Len: %d\n", Get_Cigar_Type(new_cigar->record[ijk]), - Get_Cigar_Length(new_cigar->record[ijk])); - } - **/ - - x_i = 0; y_i = 0; uint32_t single_record = 0; - for (i = 0; i < new_cigar->length; i++) + for (i = 0; i < (long long)new_cigar->length; i++) { operation = Get_Cigar_Type(new_cigar->record[i]); operationLen = Get_Cigar_Length(new_cigar->record[i]); @@ -8201,7 +4427,7 @@ long long total_window_start, long long total_window_end) else if (operation == 2) { y_i += operationLen; - }///3是y缺字符(x多字符) + } else if (operation == 3) { x_i += operationLen; @@ -8209,7 +4435,7 @@ long long total_window_start, long long total_window_end) } new_cigar->length = end_cigar - start_cigar + 1; - ///可以优化 + ///should be improved memmove(new_cigar->record, new_cigar->record + start_cigar, new_cigar->length*sizeof(uint32_t)); long long total_x_start = total_window_start + get_x_start; @@ -8217,10 +4443,10 @@ long long total_window_start, long long total_window_end) long long total_y_start = get_y_start; long long y_length = get_y_end -get_y_start + 1; - add_cigar_to_cigar(current_dumy, current_cigar, second_round, total_x_start, x_length, total_y_start, y_length); + return 1; } int process_boundary(overlap_region_alloc* overlap_list, All_reads* R_INF, Correct_dumy* dumy, Graph* g, Graph* DAGCon, @@ -8257,9 +4483,9 @@ Cigar_record* current_cigar, long long uncorrected_window_start, Round2_alignmen long long x_length, x_len, o_len; int threshold; long long Window_Len; - char* x_string; - char* y_string; - char* backbone; + char* x_string = NULL; + char* y_string = NULL; + char* backbone = NULL; long long backbone_length; long long i; long long y_start, y_length; @@ -8278,7 +4504,7 @@ Cigar_record* current_cigar, long long uncorrected_window_start, Round2_alignmen long long correct_x_pos_s; long long matched_coverage = 0; - for (i = 0; i < dumy->length; i++) + for (i = 0; i < (long long)dumy->length; i++) { overlapID = dumy->overlapID[i]; correct_x_pos_s = (overlap_list->list[overlapID].x_pos_s / WINDOW) * WINDOW; @@ -8333,7 +4559,7 @@ Cigar_record* current_cigar, long long uncorrected_window_start, Round2_alignmen ///y_start may less than 0 y_start = y_start - WINDOW_BOUNDARY/2; - ///其实可以不加...怕出bug + ///in fact, we don't need this line, just worry for bug if(y_start < 0) { continue; @@ -8352,7 +4578,8 @@ Cigar_record* current_cigar, long long uncorrected_window_start, Round2_alignmen y_string = dumy->overlap_region; ///both end site and real_y_start have extra_begin - ///有很多是完全匹配,可以先快速判断是不是完全匹配 + ///should be improved, since most of overlaps are exact overlaps + ///we can do it quickly end_site = Reserve_Banded_BPM_PATH(y_string, Window_Len, x_string, x_len, threshold, &error, &real_y_start, &(dumy->path_length), dumy->matrix_bit, dumy->path, -1, -1); } @@ -8381,7 +4608,7 @@ Cigar_record* current_cigar, long long uncorrected_window_start, Round2_alignmen Window_Len = x_len + (threshold << 1); y_start = overlap_list->list[overlapID].w_list[windowID].y_start - WINDOW_BOUNDARY/2; - ///其实可以不加...怕出bug + ///in fact, we don't need this line, just worry for bug if(y_start < 0) { continue; @@ -8398,7 +4625,8 @@ Cigar_record* current_cigar, long long uncorrected_window_start, Round2_alignmen y_string = dumy->overlap_region; ///both end site and real_y_start have extra_begin - ///有很多是完全匹配,可以先快速判断是不是完全匹配 + ///should be improved, since most of overlaps are exact overlaps + ///we can do it quickly end_site = Reserve_Banded_BPM_PATH(y_string, Window_Len, x_string, x_len, threshold, &error, &real_y_start, &(dumy->path_length), dumy->matrix_bit, dumy->path, -1, -1); } @@ -8433,39 +4661,6 @@ Cigar_record* current_cigar, long long uncorrected_window_start, Round2_alignmen x_string = r_string + x_start; y_string = dumy->overlap_region; - // if(memcmp("m54238_180922_175520/52363405/ccs", Get_NAME((*R_INF),overlap_list->list[overlapID].x_id), - // Get_NAME_LENGTH((*R_INF), overlap_list->list[overlapID].x_id)) == 0 && uncorrected_window_start == 6750) - // { - // fprintf(stderr, "********x_start: %d, uncorrected_window_start: %d, dumy->length: %d, y_name: %.*s\n", - // x_start, uncorrected_window_start, dumy->length, - // Get_NAME_LENGTH((*R_INF), overlap_list->list[overlapID].y_id), - // Get_NAME((*R_INF),overlap_list->list[overlapID].y_id)); - - - // fprintf(stderr, "*******error: %d****\n", error); - // for (int ijk = 0; ijk < tmp_cigar.cigar.length; ijk++) - // { - // fprintf(stderr, "Oper: %d, Len: %d\n", tmp_cigar.cigar.C_C[ijk], - // tmp_cigar.cigar.C_L[ijk]); - // } - - // } - - - // if(verify_cigar(x_string, x_length, y_string, y_length, &tmp_cigar.cigar, - // error)) - // { - // fprintf(stderr, "*******error: %d****\n", error); - // for (int ijk = 0; ijk < tmp_cigar.cigar.length; ijk++) - // { - // fprintf(stderr, "Oper: %d, Len: %d\n", tmp_cigar.cigar.C_C[ijk], - // tmp_cigar.cigar.C_L[ijk]); - // } - - // fprintf(stderr, "*******dumy->path_length: %d\n****\n", dumy->path_length); - // } - - currentNodeID = x_start - corrected_window_start; addmatchedSeqToGraph(g, currentNodeID, x_string, x_length, @@ -8479,10 +4674,6 @@ Cigar_record* current_cigar, long long uncorrected_window_start, Round2_alignmen } } - /** - fprintf(stderr, "matched_coverage: %d, dumy->length: %d\n", - matched_coverage, dumy->length); - **/ if(matched_coverage >= MIN_COVERAGE_THRESHOLD) { @@ -8491,10 +4682,7 @@ Cigar_record* current_cigar, long long uncorrected_window_start, Round2_alignmen { return 0; } - /** - fprintf(stderr, "s_start_nodeID: %d, s_end_nodeID: %d, corrected_window_start: %d, corrected_window_end: %d\n", - g->s_start_nodeID, g->s_end_nodeID, corrected_window_start, corrected_window_end); - **/ + clear_Cigar_record(&(second_round->tmp_cigar)); clear_Correct_dumy_pure(&(second_round->dumy)); @@ -8539,15 +4727,12 @@ Cigar_record* current_cigar, long long uncorrected_window_start, Round2_alignmen } - - void generate_consensus(overlap_region_alloc* overlap_list, All_reads* R_INF, UC_Read* g_read, Correct_dumy* dumy, Graph* g, Graph* DAGCon, Cigar_record* current_cigar, Round2_alignment* second_round) { clear_Cigar_record(current_cigar); - long long overlap_length; long long window_start, window_end; long long num_availiable_win = 0; @@ -8568,30 +4753,24 @@ void generate_consensus(overlap_region_alloc* overlap_list, All_reads* R_INF, dumy->length = 0; dumy->lengthNT = 0; - ///flag返回的是重叠数量 - ///dumy->length返回的是有效完全重叠的数量 - ///dumy->lengthNT返回的是有效不完全重叠的数量 - ///return overlaps that is overlaped with [window_start, window_end] + + ///return overlaps that are overlaped with [window_start, window_end] flag = get_available_interval(window_start, window_end, overlap_list, dumy); switch (flag) { - case 1: ///找到匹配 + case 1: ///match break; - case 0: ///没找到匹配 + case 0: ///unmatch break; - case -2: ///下一个window也不会存在匹配, 直接跳出 + case -2: ///unmatch, and cannot match for next window break; } - - ///这个是available overlap里所有window的数量... - ///num_availiable_win = num_availiable_win + dumy->length + dumy->lengthNT; num_availiable_win = num_availiable_win + dumy->length; - ///重叠窗口数,也就是coverage大小 + ///number of overlaps, also be the coverage if(dumy->length >= MIN_COVERAGE_THRESHOLD) { - window_consensus(g_read->seq, g_read->length, window_start, window_end, overlap_list, dumy, R_INF, g, DAGCon, current_cigar); @@ -8632,30 +4811,26 @@ inline int get_available_fully_covered_interval(long long window_start, long lon overlap_region_alloc* overlap_list, Correct_dumy* dumy, long long* real_length, long long* real_length_100) { long long i; - int flag = 0; - long long Begin, End, Len; + long long Len; long long overlap_length; - for (i = dumy->start_i; i < overlap_list->length; i++) + for (i = dumy->start_i; i < (long long)overlap_list->length; i++) { - ///只会发生在这个interval比list里所有元素都小的情况 - ///这种情况下一个interval需要从0开始 - if (window_end < overlap_list->list[i].x_pos_s) + if (window_end < (long long)overlap_list->list[i].x_pos_s) { dumy->start_i = 0; return 0; } - else ///只要window_end >= overlap_list->list[i].x_pos_s,就有可能重叠 + else { dumy->start_i = i; break; } } - ///只会发生在这个window比list里所有元素都大的情况 - ///这种情况下一个window也无需遍历了 - if (i >= overlap_list->length) + + if (i >= (long long)overlap_list->length) { dumy->start_i = overlap_list->length; return -2; @@ -8669,12 +4844,10 @@ overlap_region_alloc* overlap_list, Correct_dumy* dumy, long long* real_length, overlap_length = window_end - window_start + 1; (*real_length) = 0; - for (; i < overlap_list->length; i++) + for (; i < (long long)overlap_list->length; i++) { - ///是否重叠 - if((Len = OVERLAP(window_start, window_end, overlap_list->list[i].x_pos_s, overlap_list->list[i].x_pos_e)) > 0) + if((Len = OVERLAP(window_start, window_end, (long long)overlap_list->list[i].x_pos_s, (long long)overlap_list->list[i].x_pos_e)) > 0) { - ///重叠数量 fake_length++; if (overlap_length == Len && overlap_list->list[i].is_match == 1) @@ -8688,13 +4861,12 @@ overlap_region_alloc* overlap_list, Correct_dumy* dumy, long long* real_length, } } - if(overlap_list->list[i].x_pos_s > window_end) + if((long long)overlap_list->list[i].x_pos_s > window_end) { break; } } - ///fake_length是重叠的数量,而不是有效重叠的数量 if (fake_length == 0) { return 0; @@ -8708,8 +4880,6 @@ overlap_region_alloc* overlap_list, Correct_dumy* dumy, long long* real_length, int check_if_fully_covered(overlap_region_alloc* overlap_list, All_reads* R_INF, UC_Read* g_read, Correct_dumy* dumy, Graph* g, int* abnormal) { - - long long overlap_length; long long window_start, window_end; int return_flag = 1; (*abnormal) = 0; @@ -8718,26 +4888,24 @@ All_reads* R_INF, UC_Read* g_read, Correct_dumy* dumy, Graph* g, int* abnormal) init_Window_Pool(&w_inf, g_read->length, WINDOW, TAIL_LENGTH); int flag = 0; - long long realLen, tmpLen; + long long realLen = 0, tmpLen = 0; while(get_Window(&w_inf, &window_start, &window_end) && flag != -2) { dumy->length = 0; dumy->lengthNT = 0; - ///flag返回的是重叠数量 - ///dumy->length返回的是有效完全重叠的数量 - ///dumy->lengthNT返回的是有效不完全重叠的数量 - ///return overlaps that is overlaped with [window_start, window_end] + + ///return overlaps that are overlaped with [window_start, window_end] flag = get_available_fully_covered_interval(window_start, window_end, overlap_list, dumy, &realLen, &tmpLen); switch (flag) { - case 1: ///找到匹配 + case 1: ///match break; - case 0: ///没找到匹配 + case 0: ///unmatch break; - case -2: ///下一个window也不会存在匹配, 直接跳出 + case -2: ///unmatch, and cannot match for next window break; } @@ -8762,15 +4930,9 @@ All_reads* R_INF, UC_Read* g_read, Correct_dumy* dumy, Graph* g, int* abnormal) } return return_flag; - //return 1; } - - - - - void markSNP( long long window_offset, long long x_total_start, long long x_length, @@ -8975,17 +5137,6 @@ haplotype_evdience_alloc* hap) markSNP_detail(&(current_cigar->cigar), hap->flag + inner_offset, current_cigar_beg, current_cigar_end, 0); - - // end_mark: - // if(x_length >= 4) - // { - // markSNP_detail(&(current_cigar->cigar), hap->flag + inner_offset, 0, x_length/2, 0); - // markSNP_detail(&(current_cigar->cigar), hap->flag + inner_offset, x_length/2+1, x_length - 1, 0); - // } - // else - // { - // markSNP_detail(&(current_cigar->cigar), hap->flag + inner_offset, 0, x_length - 1, 0); - // } } @@ -9090,11 +5241,6 @@ CIGAR* cigar, haplotype_evdience_alloc* hap, int snp_threshold) } - - - - - ///mark SNPs at [xBeg, xEnd], note we need to deal with flag_offset carefully void addSNPtohaplotype_details(CIGAR* cigar_record, uint8_t* flag, char* x_string, char* y_string, long long x_total_start, long long y_total_start, @@ -9297,43 +5443,9 @@ haplotype_evdience_alloc* hap, int snp_threshold, char* x_T_string, char* y_T_st x_T_string + current_cigar->x_start, y_T_string + current_cigar->y_start, current_cigar->x_start, current_cigar->y_start, current_cigar_beg, current_cigar_end, overlapID, 0, hap, snp_threshold); - - /** - end_add: - if(x_length >= 4) - { - addSNPtohaplotype_details(&(current_cigar->cigar), hap->flag + inner_offset, - x_T_string + current_cigar->x_start, y_T_string + current_cigar->y_start, - current_cigar->x_start, current_cigar->y_start, 0, - x_length/2, overlapID, 0, hap, snp_threshold); - - addSNPtohaplotype_details(&(current_cigar->cigar), hap->flag + inner_offset, - x_T_string + current_cigar->x_start, y_T_string + current_cigar->y_start, - current_cigar->x_start, current_cigar->y_start, x_length/2 + 1, - x_length - 1, overlapID, 0, hap, snp_threshold); - } - else - { - addSNPtohaplotype_details(&(current_cigar->cigar), hap->flag + inner_offset, - x_T_string + current_cigar->x_start, y_T_string + current_cigar->y_start, - current_cigar->x_start, current_cigar->y_start, 0, - x_length - 1, overlapID, 0, hap, snp_threshold); - } - **/ } - - - - - - - - - - - void cluster(char* r_string, long long window_start, long long window_end, overlap_region_alloc* overlap_list, Correct_dumy* dumy, All_reads* R_INF, haplotype_evdience_alloc* hap) { @@ -9346,16 +5458,14 @@ overlap_region_alloc* overlap_list, Correct_dumy* dumy, All_reads* R_INF, haplot long long i; long long y_start, y_length; long long overlapID, windowID; - long long startNodeID, endNodeID, currentNodeID; long long correct_x_pos_s; - long long inner_window_offset; int snp_threshold; snp_threshold = 1; ///all overlaps related to the current window [window_start, window_end] ///first mark all snp pos - for (i = 0; i < dumy->length; i++) + for (i = 0; i < (long long)dumy->length; i++) { ///overlap id, instead of the window id or the y id overlapID = dumy->overlapID[i]; @@ -9417,7 +5527,7 @@ overlap_region_alloc* overlap_list, Correct_dumy* dumy, All_reads* R_INF, haplot ///add the information related to snp to haplotype_evdience_alloc - for (i = 0; i < dumy->length; i++) + for (i = 0; i < (long long)dumy->length; i++) { ///overlap ID, instead of the window ID overlapID = dumy->overlapID[i]; @@ -9481,17 +5591,15 @@ haplotype_evdience_alloc* hap, UC_Read* overlap_read) long long i; long long y_start, y_length; long long overlapID, windowID; - long long startNodeID, endNodeID, currentNodeID; long long correct_x_pos_s; - long long inner_window_offset; int snp_threshold; snp_threshold = 1; ///all overlaps related to the current window [window_start, window_end] ///first mark all snp pos - for (i = 0; i < dumy->length; i++) + for (i = 0; i < (long long)dumy->length; i++) { ///overlap id, instead of the window id or the y id overlapID = dumy->overlapID[i]; @@ -9524,7 +5632,7 @@ haplotype_evdience_alloc* hap, UC_Read* overlap_read) beg_cigar = &(overlap_list->list[overlapID].boundary_cigars.buffer[windowID-1]); } - if(windowID < overlap_list->list[overlapID].w_list_length - 1) + if(windowID < (long long)(overlap_list->list[overlapID].w_list_length - 1)) { end_cigar = &(overlap_list->list[overlapID].boundary_cigars.buffer[windowID]); } @@ -9565,7 +5673,7 @@ haplotype_evdience_alloc* hap, UC_Read* overlap_read) ///add the information related to snp to haplotype_evdience_alloc - for (i = 0; i < dumy->length; i++) + for (i = 0; i < (long long)dumy->length; i++) { ///overlap ID, instead of the window ID overlapID = dumy->overlapID[i]; @@ -9610,7 +5718,7 @@ haplotype_evdience_alloc* hap, UC_Read* overlap_read) { beg_cigar = &(overlap_list->list[overlapID].boundary_cigars.buffer[windowID-1]); } - if(windowID < overlap_list->list[overlapID].w_list_length - 1) + if(windowID < (long long)(overlap_list->list[overlapID].w_list_length - 1)) { end_cigar = &(overlap_list->list[overlapID].boundary_cigars.buffer[windowID]); } @@ -9687,399 +5795,6 @@ int cmp_max_DP(const void * a, const void * b) } } -void debug_hap_information(overlap_region_alloc* overlap_list, All_reads* R_INF, - UC_Read* g_read, haplotype_evdience_alloc* hap, - Correct_dumy* dumy) -{ - int i, overlapID, y_ID, y_Strand; - long long x_start; - long long x_length; - char* x_string; - char* y_string; - long long y_start; - long long y_length; - - for (i = 0; i < hap->length; i++) - { - if(hap->list[i].type < 2) - { - overlapID = hap->list[i].overlapID; - x_start = hap->list[i].site; - y_start = hap->list[i].overlapSite; - - - y_ID = overlap_list->list[overlapID].y_id; - y_Strand = overlap_list->list[overlapID].y_pos_strand; - - recover_UC_Read_sub_region(dumy->overlap_region, y_start, 1, y_Strand, R_INF, y_ID); - - x_string = g_read->seq + x_start; - y_string = dumy->overlap_region; - - if(y_string[0] != hap->list[i].misBase) - { - fprintf(stderr, "y_string[0]: %c, hap->list[i].misBase: %c\n", - y_string[0], hap->list[i].misBase); - } - - - if(hap->list[i].type == 0) - { - if(x_string[0] != y_string[0]) - { - fprintf(stderr, "x_string[0]: %c, y_string[0]: %c\n", - x_string[0], y_string[0]); - - - } - } - else if(hap->list[i].type == 1) - { - if(x_string[0] == y_string[0]) - { - fprintf(stderr, "x_string[0]: %c, y_string[0]: %c\n", - x_string[0], y_string[0]); - } - - } - } - } - - - for (i = 0; i < hap->length; i++) - { - if(i != 0 && hap->list[i].site < hap->list[i-1].site) - { - fprintf(stderr, "wrong order\n"); - } - } -} - - - - - - - - - -int debug_split_sub_list(haplotype_evdience_alloc* hap, -haplotype_evdience* sub_list, long long sub_length, long long num_haplotype) -{ - long long i = 0; - long long occ_0 = 0; - long long occ_1 = 0; - long long occ_1_array[5]; - memset(occ_1_array, 0, sizeof(long long) * 5); - long long occ_2 = 0; - - - for (i = 0; i < sub_length; i++) - { - if(sub_list[i].type == 0) - { - occ_0++; - } - else if(sub_list[i].type == 1) - { - occ_1_array[seq_nt6_table[(uint8_t)(sub_list[i].misBase)]]++; - occ_1++; - } - else if(sub_list[i].type == 2) - { - occ_2++; - } - } - - /** - 1. if occ_0 = 0, that means all overlaps are different with this read at this site - 2. it is not possible that occ_1 = 0, - 3. if occ_1 = 1, there are only one difference. It must be a sequencing error. - **/ - if(occ_0 == 0 || occ_1 <= 1) - { - return 0; - } - - ///note: if the max value except type0 is type2 - ///that means this is no snp hapolyte - long long max = occ_2; - long long max_i = -1; - - for (i = 0; i < 5; i++) - { - if(occ_1_array[i] > max) - { - max = occ_1_array[i]; - max_i = i; - } - } - - - - if(max_i == -1) - { - return 0; - } - - if(max <= 1) - { - return 0; - } - - ///if we have two max - for (i = 0; i < 5; i++) - { - if(occ_1_array[i] == max && i != max_i) - { - return 0; - } - } - - long long new_0 = occ_0 + 1; - long long new_total = sub_length + 1; - ///note: here occ_0++ since the read itself has a type0 - double available = new_0 + max; - double threshold = 0.95; - available = available/((double)(new_total)); - if(available < threshold) - { - return 0; - } - - ///if we just have one snp, we need to phase it carefully - if(num_haplotype == 1) - { - ///we must have just 1 match and 1 mismatch - ///any other types are not good - if(new_0 + max != new_total) - { - return 0; - } - - if(filter_snp(new_0, max, new_total) == 0) - { - return 0; - } - } - /** - if(filter_snp(new_0, max, new_total) == 0) - { - return 0; - } - **/ - - - - - ///for each calculated snp, find if it is at snp matrix - for (i = 0; i < hap->available_snp; i++) - { - if(hap->snp_stat[i].site == sub_list[0].site) - { - - - int j = 0; - int vectorID = hap->snp_stat[i].id; - int8_t* vector = Get_SNP_Vector((*hap), vectorID); - - if(hap->snp_stat[i].occ_0 != occ_0) - { - fprintf(stderr, "error occ0\n"); - } - - if(hap->snp_stat[i].occ_1 != occ_1_array[max_i]) - { - fprintf(stderr, "error occ1\n"); - } - - if(hap->snp_stat[i].overlap_num != sub_length) - { - fprintf(stderr, "error overlap_num\n"); - } - - if(hap->snp_stat[i].overlap_num != hap->snp_stat[i].occ_0 + - hap->snp_stat[i].occ_1 + hap->snp_stat[i].occ_2) - { - fprintf(stderr, "error overlap_num\n"); - } - - ///for each element in snp vector, find if it is in calculated dataset - for (j = 0; j < Get_SNP_Vector_Length((*hap)); j++) - { - if(vector[j] != -1) - { - int x_i = 0; - for (x_i = 0; x_i < sub_length; x_i++) - { - if(j == sub_list[x_i].overlapID) - { - break; - } - } - - if(x_i == sub_length) - { - fprintf(stderr, "error: j: %d\n",j); - } - else - { - if(vector[j] == 0 || sub_list[x_i].type == 0) - { - if(vector[j] != sub_list[x_i].type) - { - fprintf(stderr, "error: 0: %d\n",j); - } - } - - if(vector[j] == 1) - { - if(sub_list[x_i].type != 1) - { - fprintf(stderr, "-error: 1: %d\n",j); - } - - - if(sub_list[x_i].type == 1 && sub_list[x_i].misBase != s_H[max_i]) - { - fprintf(stderr, "+error: 1: %d\n",j); - } - } - - if(vector[j] == 2) - { - if(sub_list[x_i].type != 2) - { - if(sub_list[x_i].type == 1 && sub_list[x_i].misBase != s_H[max_i]) - { - ; - } - else - { - fprintf(stderr, "error: 2: %d\n",j); - } - - } - - } - - - } - - } - } - - - ///for each calculated data, find if it is in snp vector - for (j = 0; j < sub_length; j++) - { - if(vector[sub_list[j].overlapID] != sub_list[j].type) - { - - if(vector[sub_list[j].overlapID] == 2 && sub_list[j].type == 1 && sub_list[j].misBase != s_H[max_i]) - { - ; - } - else - { - fprintf(stderr, "vector[sub_list[j].site]: %d, sub_list[j].type: %d\n", - vector[sub_list[j].overlapID], sub_list[j].type); - } - } - } - - - break; - } - } - - - if(i == hap->available_snp) - { - fprintf(stderr, "error\n"); - } - - - /** - fprintf(stderr, "new_0: %d, occ_0: %d, max: %d, max_i: %d, sub_length: %d, new_total: %d, available: %lf\n", - new_0, occ_0, max, max_i, sub_length, new_total, available); - for (i = 0; i < sub_length; i++) - { - - fprintf(stderr, "i: %d, site: %d, type: %d, char: %c, ID: %d, name: %.*s\n", - i, sub_list[i].site, sub_list[i].type, sub_list[i].misBase, sub_list[i].overlapID, - Get_NAME_LENGTH((*R_INF), overlap_list->list[sub_list[i].overlapID].y_id), - Get_NAME((*R_INF),overlap_list->list[sub_list[i].overlapID].y_id)); - - } - fprintf(stderr, "\n"); - **/ - - - return 1; - - - -} - - - - - - - - - - - - - -int debug_snp_matrix(haplotype_evdience_alloc* hap) -{ - uint64_t pre_site = (uint64_t)-1; - uint64_t num_of_snps = 0; - long long pre_i = -1; - long long sub_length; - haplotype_evdience* sub_list; - long long i; - long long a_snp = 0; - - - ////split reads - for (i = 0; i < hap->length; i++) - { - if(pre_site != hap->list[i].site) - { - if(i != 0) - { - sub_list = hap->list + pre_i; - sub_length = i - pre_i; - ///debug_total_length = debug_total_length + sub_length; - a_snp += debug_split_sub_list(hap, sub_list, sub_length, hap->snp); - } - num_of_snps++; - pre_site = hap->list[i].site; - pre_i = i; - } - } - - if(pre_i != -1) - { - sub_list = hap->list + pre_i; - sub_length = i - pre_i; - ///debug_total_length = debug_total_length + sub_length; - a_snp += debug_split_sub_list(hap, sub_list, sub_length, hap->snp); - } - - - if(a_snp != hap->available_snp) - { - fprintf(stderr, "a_snp: %d, available_snp: %d\n", - a_snp, hap->available_snp); - } - - -} int split_sub_list(haplotype_evdience_alloc* hap, haplotype_evdience* sub_list, long long sub_length, long long num_haplotype, @@ -10184,11 +5899,9 @@ overlap_region_alloc* overlap_list, All_reads* R_INF, UC_Read* g_read) return 0; } - - - - InsertSNPVector(hap, sub_list, sub_length, s_H[max_i], g_read); + + return 1; } @@ -10211,10 +5924,10 @@ int calculate_distance_snp_vector(int8_t *vector1, int8_t *vector2, int Len) void print_core_snp(haplotype_evdience_alloc* hap) { - int i, j; + uint64_t i, j; for (i = 0; i < hap->core_snp; i++) { - fprintf(stderr, "core(i): %d, site: %d, occ_0: %d, occ_1: %d, occ_2: %d, score: %d\n", + fprintf(stderr, "core(i): %lu, site: %u, occ_0: %u, occ_1: %u, occ_2: %u, score: %d\n", i, hap->snp_stat[i].site, hap->snp_stat[i].occ_0, hap->snp_stat[i].occ_1, hap->snp_stat[i].occ_2, hap->snp_stat[i].score); @@ -10226,7 +5939,7 @@ void print_core_snp(haplotype_evdience_alloc* hap) { if(vector[j] == 0) { - fprintf(stderr, "type: %d, ID: %d\n", vector[j], j); + fprintf(stderr, "type: %d, ID: %lu\n", vector[j], j); } } @@ -10235,7 +5948,7 @@ void print_core_snp(haplotype_evdience_alloc* hap) { if(vector[j] == 1) { - fprintf(stderr, "type: %d, ID: %d\n", vector[j], j); + fprintf(stderr, "type: %d, ID: %lu\n", vector[j], j); } } @@ -10244,68 +5957,14 @@ void print_core_snp(haplotype_evdience_alloc* hap) { if(vector[j] == 2) { - fprintf(stderr, "type: %d, ID: %d\n", vector[j], j); + fprintf(stderr, "type: %d, ID: %lu\n", vector[j], j); } } } } -void merge_snp_vectors(haplotype_evdience_alloc* hap, int diff_vector_ID) -{ - int8_t *r_vector = Get_Result_SNP_Vector((*hap)); - int vectorLen = Get_SNP_Vector_Length((*hap)); - memset(r_vector, -1, vectorLen); - hap->result_stat.occ_0 = 0; - hap->result_stat.occ_1 = 0; - int8_t* vector; - int vectorID; - int i, j; - - for (i = 0; i < hap->core_snp; i++) - { - if(i == diff_vector_ID) - { - continue; - } - - vectorID = hap->snp_stat[i].id; - vector = Get_SNP_Vector((*hap), vectorID); - - for (j = 0; j < vectorLen; j++) - { - if(r_vector[j] == -1) - { - if(vector[j] == 0) - { - hap->result_stat.occ_0++; - r_vector[j] = vector[j]; - } - else if(vector[j] == 1) - { - hap->result_stat.occ_1++; - r_vector[j] = vector[j]; - } - } - else ///can debug here - { - if((vector[j] != -1 && vector[j] != 2 && vector[j] != r_vector[j])) - { - fprintf(stderr, "j: %d, vector[j]: %d, r_vector[j]: %d, hap->core_snp: %d, diff_vector_ID: %d\n", - j, vector[j], r_vector[j], hap->core_snp, diff_vector_ID); - - print_core_snp(hap); - } - } - - - - } - } - - hap->result_stat.overlap_num = hap->result_stat.occ_0 + hap->result_stat.occ_1; -} void add_to_result_snp_vector(haplotype_evdience_alloc* hap, int8_t *new_vector, int Len) { @@ -10357,7 +6016,6 @@ int debug_add_to_result_snp_vector(haplotype_evdience_alloc* hap, int8_t *new_ve ///if((new_vector[j] != -1 && new_vector[j] != 2 && new_vector[j] != r_vector[j])) if((new_vector[j] == 0 || new_vector[j] == 1) && new_vector[j] != r_vector[j]) { - ///fprintf(stderr, "j: %d\n", j); return j; } } @@ -10381,7 +6039,7 @@ int merge_snp_vectors_and_test(haplotype_evdience_alloc* hap, int diff_vector_ID int vectorID; int i, j; - for (i = 0; i < hap->core_snp; i++) + for (i = 0; i < (int)hap->core_snp; i++) { if(i == diff_vector_ID) { @@ -10426,9 +6084,7 @@ int generate_haplotypes(haplotype_evdience_alloc* hap) { int j; - int vectorID, vectorID2; - int diff_core_vector = 0; - int diff_vector_ID = -1; + int vectorID2; int8_t *vector, *vector2; if(hap->core_snp == 0) @@ -10440,14 +6096,7 @@ int generate_haplotypes(haplotype_evdience_alloc* hap) qsort(hap->snp_stat, hap->available_snp, sizeof(SnpStats), cmp_snp_stats); - // for (j = 0; j < hap->available_snp; j++) - // { - // fprintf(stderr, "j: %d, score: %d\n", j, hap->snp_stat[j].score); - // } - // fprintf(stderr, "\n\n"); - - ///the hap->core_snp is used to find centriod - + ///the hap->core_snp is used to find centriod ///if there are <5 vectors in core_snp, we didn't allow different vector if (hap->core_snp < 5) { @@ -10458,10 +6107,7 @@ int generate_haplotypes(haplotype_evdience_alloc* hap) } else ///for vectors in core_snp, we allow at most one different vector when there are >= 5 vectors in core_snp { - ///there are two condition: 1. vector 0 is the different one. 2. vector 0 is not the different one - - - diff_vector_ID = -1; + ///there are two condition: 1. vector 0 is the different one. 2. vector 0 is not the different one ///first try to merge all vector together if(merge_snp_vectors_and_test(hap, -1) == 0) { @@ -10469,7 +6115,6 @@ int generate_haplotypes(haplotype_evdience_alloc* hap) { if(merge_snp_vectors_and_test(hap, j) == 1) { - diff_vector_ID = j; break; } } @@ -10488,7 +6133,7 @@ int generate_haplotypes(haplotype_evdience_alloc* hap) vector = Get_Result_SNP_Vector((*hap)); ///and for each non-core snp vector, if it has no conflict with result vector /// add it to result vector - for (j = hap->core_snp; j < hap->available_snp; j++) + for (j = hap->core_snp; j < (int)hap->available_snp; j++) { vectorID2 = hap->snp_stat[j].id; vector2 = Get_SNP_Vector((*hap), vectorID2); @@ -10498,7 +6143,6 @@ int generate_haplotypes(haplotype_evdience_alloc* hap) } } - ///merge_snp_vectors(hap, diff_vector_ID); ///for read only have 1 snp, we need a more strict condition if (hap->core_snp == 1 && @@ -10513,109 +6157,6 @@ int generate_haplotypes(haplotype_evdience_alloc* hap) } - - -void print_snp_in_line(haplotype_evdience_alloc* hap) -{ - int j, i; - uint32_t* column; - fprintf(stderr, "###########hap->available_snp: %d###########\n", hap->available_snp); - for (j = 0; j < hap->available_snp; j++) - { - fprintf(stderr, "*********j: %d, site: %d, id: %d*********\n", j, hap->snp_stat[j].site, hap->snp_stat[j].id); - - fprintf(stderr, "type(0):\n"); - - int vectorID = hap->snp_stat[j].id; - int8_t* vector = Get_SNP_Vector((*hap), vectorID); - for (i = 0; i < hap->overlap; i++) - { - if(vector[i] == 0) - { - fprintf(stderr, "%3d, ", i); - } - } - fprintf(stderr, "\n"); - - fprintf(stderr, "type(1):\n"); - for (i = 0; i < hap->overlap; i++) - { - if(vector[i] == 1) - { - fprintf(stderr, "%3d, ", i); - } - } - fprintf(stderr, "\n"); - } - - fprintf(stderr, "***********************\n"); - for (i = 0; i < hap->dp.snp_num; i++) - { - fprintf(stderr, "hap->dp.max[%d]: %d, hap->dp.backtrack_length: %d\n", - i, hap->dp.max[i], hap->dp.backtrack_length[i]); - - if(hap->dp.backtrack_length[i] != 0) - { - column = Get_DP_Backtrack_Column(hap->dp, i); - for (j = 0; j < hap->dp.backtrack_length[i]; j++) - { - fprintf(stderr, "pre: %d,", column[j]); - } - fprintf(stderr, "\n"); - } - - } - - fprintf(stderr, "#########################\n\n\n"); -} - -///if j == -1, print result vector -void print_single_snp(haplotype_evdience_alloc* hap, int j) -{ - - - - int i, vectorID; - int8_t* vector; - if(j != -1) - { - fprintf(stderr, "*********site: %d, j: %d, id: %d*********\n", - hap->snp_stat[j].site, j, hap->snp_stat[j].id); - vectorID = hap->snp_stat[j].id; - vector = Get_SNP_Vector((*hap), vectorID); - } - else - { - fprintf(stderr, "*********result snp*********\n"); - vector = Get_Result_SNP_Vector((*hap)); - } - - - - fprintf(stderr, "type(0):\n"); - for (i = 0; i < hap->overlap; i++) - { - if(vector[i] == 0) - { - fprintf(stderr, "%3d, ", i); - } - } - fprintf(stderr, "\n"); - - fprintf(stderr, "type(1):\n"); - for (i = 0; i < hap->overlap; i++) - { - if(vector[i] == 1) - { - fprintf(stderr, "%3d, ", i); - } - } - fprintf(stderr, "\n"); - fprintf(stderr, "###############\n\n"); -} - - - void Preorder_Merge(uint32_t snpID, haplotype_evdience_alloc* hap, int is_merge) { int vectorID = hap->snp_stat[snpID].id; @@ -10653,7 +6194,7 @@ void Preorder_Merge(uint32_t snpID, haplotype_evdience_alloc* hap, int is_merge) if(is_merge) { int add_ID = 0; - for (j = 0; j < hap->dp.backtrack_length[snpID]; j++) + for (j = 0; j < (int)hap->dp.backtrack_length[snpID]; j++) { if(hap->snp_stat[column[j]].is_homopolymer == 0) { @@ -10661,7 +6202,7 @@ void Preorder_Merge(uint32_t snpID, haplotype_evdience_alloc* hap, int is_merge) } } - for (j = 0; j < hap->dp.backtrack_length[snpID]; j++) + for (j = 0; j < (int)hap->dp.backtrack_length[snpID]; j++) { if(j == add_ID) { @@ -10675,7 +6216,7 @@ void Preorder_Merge(uint32_t snpID, haplotype_evdience_alloc* hap, int is_merge) } else { - for (j = 0; j < hap->dp.backtrack_length[snpID]; j++) + for (j = 0; j < (int)hap->dp.backtrack_length[snpID]; j++) { Preorder_Merge(column[j], hap, 0); } @@ -10684,56 +6225,6 @@ void Preorder_Merge(uint32_t snpID, haplotype_evdience_alloc* hap, int is_merge) } -void generate_result_vector_repeat(haplotype_evdience_alloc* hap, int pathLen) -{ - if(pathLen != hap->dp.current_snp_num) - { - fprintf(stderr, "hahah\n"); - } - - - int8_t* vector = Get_Result_SNP_Vector((*hap)); - memset(vector, -1, Get_SNP_Vector_Length((*hap))); - hap->result_stat.occ_0 = 0; - hap->result_stat.occ_1 = 0; - hap->result_stat.occ_2 = 0; - hap->result_stat.score = pathLen; - hap->result_stat.homopolymer_num = 0; - hap->result_stat.non_homopolymer_num = 0; - - long long snpID1; - long long j = 0; - int flag, vectorID; - int current_score; - for (j = 0; j < pathLen; j++) - { - snpID1 = hap->dp.buffer[j]; - vectorID = hap->snp_stat[snpID1].id; - vector = Get_SNP_Vector((*hap), vectorID); - if(hap->snp_stat[snpID1].is_homopolymer) - { - hap->result_stat.homopolymer_num++; - } - else - { - hap->result_stat.non_homopolymer_num++; - } - - if((flag = debug_add_to_result_snp_vector(hap, vector, Get_SNP_Vector_Length((*hap))))!= -1) - { - fprintf(stderr, "incompatible snp vector....\n"); - exit(0); - } - } - hap->result_stat.overlap_num = hap->result_stat.occ_0 + hap->result_stat.occ_1; - - - insert_SNP_IDs_addition(&(hap->dp.SNP_IDs), hap->dp.buffer, pathLen, hap->result_stat.occ_0, hap->result_stat.occ_1, - hap->result_stat.homopolymer_num, hap->result_stat.non_homopolymer_num); -} - - - void Preorder_Merge_Advance_Repeat(uint32_t snpID, haplotype_evdience_alloc* hap, int pathLen) { hap->dp.visit[snpID] = 1; @@ -10742,7 +6233,6 @@ void Preorder_Merge_Advance_Repeat(uint32_t snpID, haplotype_evdience_alloc* hap if(hap->dp.backtrack_length[snpID] == 0) { - ///generate_result_vector_repeat(hap, pathLen); insert_SNP_IDs_addition(&(hap->dp.SNP_IDs), hap->dp.buffer, pathLen); return; } @@ -10752,20 +6242,18 @@ void Preorder_Merge_Advance_Repeat(uint32_t snpID, haplotype_evdience_alloc* hap int j; column = Get_DP_Backtrack_Column(hap->dp, snpID); - for (j = 0; j < hap->dp.backtrack_length[snpID]; j++) + for (j = 0; j < (int)hap->dp.backtrack_length[snpID]; j++) { Preorder_Merge_Advance_Repeat(column[j], hap, pathLen); } } } - - void generate_result_vector(haplotype_evdience_alloc* hap, int pathLen) { if(pathLen != hap->dp.current_snp_num) { - fprintf(stderr, "hahah\n"); + fprintf(stderr, "error\n"); } @@ -10851,7 +6339,7 @@ void Preorder_Merge_Advance(uint32_t snpID, haplotype_evdience_alloc* hap, int p int j; column = Get_DP_Backtrack_Column(hap->dp, snpID); - for (j = 0; j < hap->dp.backtrack_length[snpID]; j++) + for (j = 0; j < (int)hap->dp.backtrack_length[snpID]; j++) { Preorder_Merge_Advance(column[j], hap, pathLen); } @@ -10860,44 +6348,6 @@ void Preorder_Merge_Advance(uint32_t snpID, haplotype_evdience_alloc* hap, int p -int if_snp_vector_useful_v2(haplotype_evdience_alloc* hap, -long long occ_0, long long occ_1, uint32_t* SNPs, long long SNPsLen) -{ - - double occ_1_coverage_low = (occ_0 + occ_1) * 0.3; - - if(occ_1 == 0 || occ_0 == 0) - { - return 0; - } - - - if(occ_1 >= occ_1_coverage_low && occ_0 >= occ_1_coverage_low) - { - return 1; - } - else if(occ_1 >= 5 && occ_0 >= 5) - { - return 1; - } - else if(occ_1 >= 2 && occ_0 >= 2 && SNPsLen >= 2) - { - /** - int nearsnp; - int non_nearsnps; - count_nearby_snps(hap, SNPs, SNPsLen, &nearsnp, &non_nearsnps); - if(non_nearsnps > 0) - { - return 1; - } - **/ - return 1; - } - - return 0; -} - - int if_snp_vector_useful(haplotype_evdience_alloc* hap, long long occ_0, long long occ_1, uint32_t* SNPs, long long SNPsLen) { @@ -10948,7 +6398,6 @@ void merge_SNP_Vectors(haplotype_evdience_alloc* hap, uint32_t* SNPs, long long long long snpID1; long long j = 0; int flag, vectorID; - int current_score; for (j = 0; j < SNPLen; j++) { snpID1 = SNPs[j]; @@ -11010,98 +6459,6 @@ void remove_reads(haplotype_evdience_alloc* hap, uint32_t* SNPs, long long SNPsL } } - -void output_reads_phase(haplotype_evdience_alloc* hap, uint32_t* SNPs, long long SNPsLen, -overlap_region_alloc* overlap_list, All_reads* R_INF) -{ - long long i, j, snpID, vectorID, overlapLen; - int8_t *vector; - - for (i = 0; i < SNPsLen; i++) - { - snpID = SNPs[i]; - vectorID = hap->snp_stat[snpID].id; - vector = Get_SNP_Vector((*hap), vectorID); - - fprintf(stderr, "i: %d, site: %d, Get_SNP_Vector_Length((*hap)): %d\n", - i, hap->snp_stat[snpID].site, Get_SNP_Vector_Length((*hap))); - - fprintf(stderr, "flag 1\n"); - for (j = 0; j < Get_SNP_Vector_Length((*hap)); j++) - { - if(vector[j] == 1) - { - - fprintf(stderr, "%.*s\n", Get_NAME_LENGTH((*R_INF), overlap_list->list[j].y_id), - Get_NAME((*R_INF), overlap_list->list[j].y_id)); - - } - } - - - fprintf(stderr, "flag 0\n"); - for (j = 0; j < Get_SNP_Vector_Length((*hap)); j++) - { - - if(vector[j] == 0) - { - - fprintf(stderr, "%.*s\n", Get_NAME_LENGTH((*R_INF), overlap_list->list[j].y_id), - Get_NAME((*R_INF), overlap_list->list[j].y_id)); - - } - } - - } -} - -void remove_reads_debug(haplotype_evdience_alloc* hap, uint32_t* SNPs, long long SNPsLen, overlap_region_alloc* overlap_list) -{ - fprintf(stderr, "SNPsLen: %d\n", SNPsLen); - long long i, j, snpID, vectorID, overlapLen; - int8_t *vector; - - for (i = 0; i < SNPsLen; i++) - { - snpID = SNPs[i]; - vectorID = hap->snp_stat[snpID].id; - vector = Get_SNP_Vector((*hap), vectorID); - fprintf(stderr, "i: %d, snpID:%d, SNPsLen: %d, available_snp: %d, snp_stat[snpID].site: %d\n", - i, snpID, SNPsLen, hap->available_snp, hap->snp_stat[snpID].site); - - for (j = 0; j < Get_SNP_Vector_Length((*hap)); j++) - { - - - if(vector[j] == 1 && overlap_list->list[j].is_match == 1) - { - //overlap_list->list[j].is_match = 0; - overlap_list->list[j].is_match = 2; - overlapLen = overlap_list->list[j].x_pos_e - overlap_list->list[j].x_pos_s + 1; - ///overlap_list->mapped_overlaps--; - overlap_list->mapped_overlaps_length -= overlapLen; - } - - /****************************may have bugs********************************/ - if( hap->snp_stat[snpID].site >= overlap_list->list[j].x_pos_s - && - hap->snp_stat[snpID].site <= overlap_list->list[j].x_pos_e) - { - overlap_list->list[j].strong = 1; - } - - fprintf(stderr, "j: %d, x_pos_s: %d, x_pos_e: %d, strong: %d, is_match: %d", j, overlap_list->list[j].x_pos_s, - overlap_list->list[j].x_pos_e, overlap_list->list[j].strong, - overlap_list->list[j].is_match); - fprintf(stderr, "****************y: %.*s****************\n", - Get_NAME_LENGTH(R_INF, overlap_list->list[j].y_id), - Get_NAME(R_INF, overlap_list->list[j].y_id)); - /****************************may have bugs********************************/ - - } - } -} - void try_to_remove_reads(int8_t* vector, long long vectorLen, overlap_region_alloc* overlap_list, uint32_t* SNPs, long long SNPLen, haplotype_evdience_alloc* hap) { @@ -11127,20 +6484,6 @@ uint32_t* SNPs, long long SNPLen, haplotype_evdience_alloc* hap) for (i = 0; i < SNPLen; i++) { snpID = SNPs[i]; - - - ///if(overlap_list->list[0].x_id == 83735) - ///if(overlap_list->list[0].x_id == 83739) - // if(overlap_list->list[0].x_id == 1185538) - // { - // fprintf(stderr, "SNPLen: %d, x_id: %d, hap->snp_stat[snpID].site: %d, occ_0: %d, occ_1: %d, occ_2: %d, overlap_num: %d\n", - // SNPLen, overlap_list->list[0].x_id, hap->snp_stat[snpID].site, - // hap->snp_stat[snpID].occ_0, hap->snp_stat[snpID].occ_1, hap->snp_stat[snpID].occ_2, - // hap->snp_stat[snpID].overlap_num); - // } - - - ///check all overlaps for (j = 0; j < Get_SNP_Vector_Length((*hap)); j++) { @@ -11157,12 +6500,9 @@ uint32_t* SNPs, long long SNPLen, haplotype_evdience_alloc* hap) } -void process_repeat_snps(haplotype_evdience_alloc* hap, int coverage, overlap_region_alloc* overlap_list) +void process_repeat_snps(haplotype_evdience_alloc* hap, overlap_region_alloc* overlap_list) { - int i, snpID, vectorID, flag; - int8_t *vector; - - + int i; uint32_t* snp_ids; long long length; @@ -11188,771 +6528,8 @@ void process_repeat_snps(haplotype_evdience_alloc* hap, int coverage, overlap_re hap->dp.SNP_IDs.IDs[i].is_remove = 0; } } - - - - ///print_snp_in_line(hap); - // fprintf(stderr, "-:overlap_list->mapped_overlaps: %d\n", overlap_list->mapped_overlaps); - /** - if(overlap_list->mapped_overlaps > coverage * 1.6) - { - for (i = 0; i < hap->dp.SNP_IDs.IDs_length; i++) - { - snp_ids = hap->dp.SNP_IDs.buffer + hap->dp.SNP_IDs.IDs[i].beg; - length = hap->dp.SNP_IDs.IDs[i].end -hap->dp.SNP_IDs.IDs[i].beg + 1; - if(hap->dp.SNP_IDs.IDs[i].is_remove == 0 && - if_snp_vector_useful(hap, hap->dp.SNP_IDs.IDs[i].occ_0, hap->dp.SNP_IDs.IDs[i].occ_1, - occ_1_threshold_low, coverage, snp_ids, length, 1)) - { - //fprintf(stderr, "i: %d \n", i); - remove_reads(hap, snp_ids, length, overlap_list); - - } - } - } - **/ - - // fprintf(stderr, "-:overlap_list->mapped_overlaps: %d\n\n\n", overlap_list->mapped_overlaps); - - - - } -void process_repeat_snps_debug(haplotype_evdience_alloc* hap, int coverage, -overlap_region_alloc* overlap_list, All_reads* R_INF) -{ - int i, snpID, vectorID, flag; - int8_t *vector; - long long occ_1_threshold_low; - long long occ_1_threshold_up; - occ_1_threshold_low = 0; - - - uint32_t* snp_ids; - long long length; - - - - for (i = 0; i < hap->dp.SNP_IDs.IDs_length; i++) - { - snp_ids = hap->dp.SNP_IDs.buffer + hap->dp.SNP_IDs.IDs[i].beg; - length = hap->dp.SNP_IDs.IDs[i].end -hap->dp.SNP_IDs.IDs[i].beg + 1; - - merge_SNP_Vectors(hap, snp_ids, length); - - if(if_snp_vector_useful(hap, hap->result_stat.occ_0, hap->result_stat.occ_1, - snp_ids, length)) - { - - - - if(overlap_list->list[0].x_id == 5405) - { - fprintf(stderr, "snpid length: %d, occ_0: %d, occ_1: %d\n", - length, - hap->result_stat.occ_0, - hap->result_stat.occ_1); - int k; - for (k = 0; k < length; k++) - { - fprintf(stderr, "i: %d, site: %d\n", - i, hap->snp_stat[snp_ids[k]].site); - } - - for (k = 0; k < Get_SNP_Vector_Length((*hap)); k++) - { - fprintf(stderr, "flag 0\n"); - if(Get_Result_SNP_Vector((*hap))[k] == 0) - { - fprintf(stderr, "%.*s\n", - Get_NAME_LENGTH((*R_INF),overlap_list->list[k].y_id), - Get_NAME((*R_INF),overlap_list->list[k].y_id)); - } - - fprintf(stderr, "flag 1\n"); - if(Get_Result_SNP_Vector((*hap))[k] == 1) - { - fprintf(stderr, "%.*s\n", - Get_NAME_LENGTH((*R_INF),overlap_list->list[k].y_id), - Get_NAME((*R_INF),overlap_list->list[k].y_id)); - } - } - - - - } - - try_to_remove_reads(Get_Result_SNP_Vector((*hap)), Get_SNP_Vector_Length((*hap)), - overlap_list, snp_ids, length, hap); - - hap->dp.SNP_IDs.IDs[i].is_remove = 1; - } - else - { - hap->dp.SNP_IDs.IDs[i].is_remove = 0; - } - - } -} - - - -void debug_repeat_vector(haplotype_evdience_alloc* hap) -{ - int j, i, snpID, vectorID, flag; - int8_t *vector; - - - // if(memcmp(hap->dp.SNP_IDs.buffer + hap->dp.SNP_IDs.IDs[hap->dp.SNP_IDs.max_snp_id].beg, - // hap->dp.max_buffer, - // sizeof(uint32_t) *(hap->dp.SNP_IDs.IDs[hap->dp.SNP_IDs.max_snp_id].end - - // hap->dp.SNP_IDs.IDs[hap->dp.SNP_IDs.max_snp_id].beg + 1))) - // { - // fprintf(stderr, "error1\n"); - // } - - // if(hap->dp.SNP_IDs.IDs[hap->dp.SNP_IDs.max_snp_id].end - - // hap->dp.SNP_IDs.IDs[hap->dp.SNP_IDs.max_snp_id].beg + 1 != - // hap->dp.max_snp_num) - // { - // fprintf(stderr, "error2\n"); - // } - - - - - - uint32_t* snp_ids; - long long length; - for (i = 0; i < hap->dp.SNP_IDs.IDs_length; i++) - { - snp_ids = hap->dp.SNP_IDs.buffer + hap->dp.SNP_IDs.IDs[i].beg; - length = hap->dp.SNP_IDs.IDs[i].end -hap->dp.SNP_IDs.IDs[i].beg + 1; - - ////first clear result snp - vector = Get_Result_SNP_Vector((*hap)); - memset(vector, -1, Get_SNP_Vector_Length((*hap))); - - long long non_hom = 0; - long long hom = 0; - for (j = 0; j < length; j++) - { - ///note here is hap->dp.max_buffer instead of hap->dp.buffer - snpID = snp_ids[j]; - vectorID = hap->snp_stat[snpID].id; - vector = Get_SNP_Vector((*hap), vectorID); - - - if(hap->snp_stat[snpID].is_homopolymer) - { - hom++; - } - else - { - non_hom++; - } - - - if((flag = debug_add_to_result_snp_vector(hap, vector, Get_SNP_Vector_Length((*hap))))!= -1) - { - fprintf(stderr, "incompatible snp vector....\n"); - exit(0); - } - } - - vector = Get_Result_SNP_Vector((*hap)); - long long occ_0 = 0; - long long occ_1 = 0; - for (j = 0; j < Get_SNP_Vector_Length((*hap)); j++) - { - if(vector[j] == 0) - { - occ_0++; - } - - if(vector[j] == 1) - { - occ_1++; - } - } - - if(hom != hap->dp.SNP_IDs.IDs[i].homopolymer_num) - { - fprintf(stderr, "error\n"); - } - - if(non_hom != hap->dp.SNP_IDs.IDs[i].non_homopolymer_num) - { - fprintf(stderr, "error\n"); - } - - if(occ_0 != hap->dp.SNP_IDs.IDs[i].occ_0) - { - fprintf(stderr, "error\n"); - } - - if(occ_1 != hap->dp.SNP_IDs.IDs[i].occ_1) - { - fprintf(stderr, "error\n"); - } - - - - } -} - - -int generate_haplotypes_DP_back(haplotype_evdience_alloc* hap, overlap_region_alloc* overlap_list, All_reads* R_INF, long long rLen, -int force_repeat) -{ - int j, i; - - int vectorID, vectorID2; - int diff_core_vector = 0; - int diff_vector_ID = -1; - int8_t *vector, *vector2; - - - if(hap->available_snp == 0) - { - return 0; - } - - - - ///if hap->available_snp == 1, the following codes would have bugs - ///filter snps that are highly likly false - if(hap->available_snp > 1) - { - i = 0; - ///if a snp is very near to others, it should not be a real snp - for (j = 0; j < hap->available_snp; j++) - { - // if(hap->snp_stat[j].occ_1 == 1) - // { - // fprintf(stderr, "***\n"); - // } - if(j > 0 && j < hap->available_snp - 1) - { - if(hap->snp_stat[j].site != hap->snp_stat[j - 1].site + 1 - && - hap->snp_stat[j].site + 1 != hap->snp_stat[j + 1].site) - { - hap->snp_stat[i] = hap->snp_stat[j]; - i++; - } - - } - else if(j == 0) - { - if(hap->snp_stat[j].site + 1 != hap->snp_stat[j + 1].site) - { - hap->snp_stat[i] = hap->snp_stat[j]; - i++; - } - } - else - { - if(hap->snp_stat[j].site != hap->snp_stat[j - 1].site + 1) - { - hap->snp_stat[i] = hap->snp_stat[j]; - i++; - } - } - } - hap->available_snp = i; - } - - - - - - - int flag; - long long overlap_length, total_read, unuseful_read, last_j, last_j_ID, last_j_flag; - total_read = unuseful_read = 0; - ///check if any read may be conflict with others - for (i = 0; i < overlap_list->length; i++) - { - overlap_length = overlap_list->list[i].x_pos_e - overlap_list->list[i].x_pos_s + 1; - if (overlap_list->list[i].is_match == 1) - { - total_read++; - flag = -1; - for (j = 0; j < hap->available_snp; j++) - { - vectorID = hap->snp_stat[j].id; - vector = Get_SNP_Vector((*hap), vectorID); - - ///flag == -1 means there are no useful signals yet - if (flag == -1) - { - if((vector[i] == 0 || vector[i] == 1 )) - { - flag = 0; - } - }///flag == 0 means there is at least one useful signal yet - else if (flag == 0) - { - if(vector[i] != 0 && vector[i] != 1) - { - flag = 2; - last_j = hap->snp_stat[j].site; - last_j_ID = j; - last_j_flag = vector[i]; - } - }///flag == 0 means there is at least one useful signal first, and another unuseful signal after that - else if(flag == 2) - { - if((vector[i] == 0 || vector[i] == 1 )) - { - flag = 3; - break; - } - } - } - - - if(flag == 3) - { - unuseful_read++; - for (j = 0; j < hap->available_snp; j++) - { - vectorID = hap->snp_stat[j].id; - vector = Get_SNP_Vector((*hap), vectorID); - - - - if(vector[i] == 0) - { - hap->snp_stat[j].occ_0--; - hap->snp_stat[j].occ_2++; - } - else if(vector[i] == 1) - { - hap->snp_stat[j].occ_1--; - hap->snp_stat[j].occ_2++; - } - else if(vector[i] != 2) - { - hap->snp_stat[j].occ_2++; - } - - - vector[i] = 2; - } - // if(overlap_list->list[i].is_match == 0) - // { - // fprintf(stderr, "error\n"); - // } - ///this read may be unuseful - ///overlap_list->list[i].is_match = 0; - ///overlap_list->list[i].is_match = 2; - overlap_list->list[i].is_match = 4; - ///overlap_list->mapped_overlaps--; - overlap_list->mapped_overlaps_length -= overlap_length; - } - } - } - - - /*******************************DP********************************/ - init_DP_matrix(&(hap->dp), hap->available_snp); - - long long equal_best = 0; - uint32_t* column; - long long column_length; - - - - for (i = 0; i < hap->available_snp; i++) - { - ///vector of snp i - vectorID = hap->snp_stat[i].id; - vector = Get_SNP_Vector((*hap), vectorID); - hap->dp.visit[i] = 0; - hap->dp.max[i] = 1; - hap->dp.backtrack_length[i] = 0; - equal_best = 0; - column = Get_DP_Backtrack_Column(hap->dp, i); - column_length = Get_DP_Backtrack_Column_Length(hap->dp, i); - - for (j = 0; j < i; j++) - { - ///vector of snp j - vectorID2 = hap->snp_stat[j].id; - vector2 = Get_SNP_Vector((*hap), vectorID2); - - ///vector is compatible with vector2 - if(calculate_distance_snp_vector(vector, vector2, Get_SNP_Vector_Length((*hap))) == 0) - { - - if(hap->dp.max[i] < hap->dp.max[j] + 1) - { - hap->dp.max[i] = hap->dp.max[j] + 1; - - column[0] = j; - equal_best = 1; - } - else if(hap->dp.max[i] == hap->dp.max[j] + 1) - { - column[equal_best] = j; - equal_best++; - } - - - } - } - - hap->dp.backtrack_length[i] = equal_best; - } - - /*******************************DP********************************/ - - - - - - uint64_t tmp_mode = 0; - - for (i = 0; i < hap->available_snp; i++) - { - tmp_mode = hap->dp.max[i]; - tmp_mode = tmp_mode << 32; - tmp_mode = tmp_mode | (uint64_t)(i); - hap->dp.max_for_sort[i] = tmp_mode; - } - - qsort(hap->dp.max_for_sort, hap->available_snp, sizeof(uint64_t), cmp_max_DP); - - - int snpID; - int group_num = 0; - ///the minmum snp_num is 1 - hap->dp.max_snp_num = 0; - hap->dp.max_score = -2; - - - - - - //repeat - if(overlap_list->mapped_overlaps_length > Coverage_Threshold(coverage, rLen) || force_repeat) - { - for (i = 0; i < hap->available_snp; i++) - { - snpID = Get_Max_DP_ID(hap->dp.max_for_sort[i]); - if(hap->dp.visit[snpID] == 0) - { - hap->dp.current_snp_num = Get_Max_DP_Value(hap->dp.max_for_sort[i]); - Preorder_Merge_Advance_Repeat(snpID, hap, 0); - } - } - } - else //non-repeat - { - for (i = 0; i < hap->available_snp; i++) - { - snpID = Get_Max_DP_ID(hap->dp.max_for_sort[i]); - if(hap->dp.visit[snpID] == 0) - { - hap->dp.current_snp_num = Get_Max_DP_Value(hap->dp.max_for_sort[i]); - Preorder_Merge_Advance(snpID, hap, 0); - } - } - } - - /** - if(memcmp("m64016_190918_162737/49678749/ccs", - Get_NAME((*R_INF), overlap_list->list[0].x_id), - Get_NAME_LENGTH((*R_INF), overlap_list->list[0].x_id)) == 0) - { - fprintf(stderr, "x_id: %d, mapped_overlaps_length: %d\n", - overlap_list->list[0].x_id, - overlap_list->mapped_overlaps_length); - fprintf(stderr, "coverage: %d\n", - coverage); - fprintf(stderr, "rLen: %d\n", - rLen); - fprintf(stderr, "Coverage_Threshold(coverage, rLen): %f\n", - Coverage_Threshold(coverage, rLen)); - fprintf(stderr, "max_snp_num: %d\n", - hap->dp.max_snp_num); - } - **/ - - - - - - - if(overlap_list->mapped_overlaps_length > Coverage_Threshold(coverage, rLen) || force_repeat) - { - ///debug_repeat_vector(hap); - ///process_repeat_snps_debug(hap, coverage, overlap_list, R_INF); - process_repeat_snps(hap, coverage, overlap_list); - - return 1; - - } - else if(hap->dp.max_snp_num > 0) - { - /** - if(memcmp("m64011_190329_072846/59507330/ccs", - Get_NAME((*R_INF), overlap_list->list[0].x_id), - Get_NAME_LENGTH((*R_INF), overlap_list->list[0].x_id)) == 0) - { - - output_reads_phase(hap, hap->dp.max_buffer, hap->dp.max_snp_num, - overlap_list, R_INF); - } - **/ - - - - - remove_reads(hap, hap->dp.max_buffer, hap->dp.max_snp_num, overlap_list); - return 1; - } - else - { - return 0; - } - - - - - /** - vector = Get_Result_SNP_Vector((*hap)); - memset(vector, -1, Get_SNP_Vector_Length((*hap))); - vector = Get_Result_SNP_Vector((*hap)); - for (i = 0; i < hap->available_snp; i++) - { - int debug_i = Get_Max_DP_ID(hap->dp.max_for_sort[i]); - int round = hap->dp.max[debug_i]; - ///fprintf(stderr, "round: %d\n", round); - if(round > 1) - { - vector = Get_Result_SNP_Vector((*hap)); - memset(vector, -1, Get_SNP_Vector_Length((*hap))); - - - if(hap->dp.backtrack_length[debug_i] < 1) - { - fprintf(stderr, "error\n"); - } - - - - while (round > 0) - { - - - - vectorID2 = hap->snp_stat[debug_i].id; - vector2 = Get_SNP_Vector((*hap), vectorID2); - int flag; - if((flag = debug_add_to_result_snp_vector(hap, vector2, Get_SNP_Vector_Length((*hap))))!= -1) - { - fprintf(stderr, "flag: %d, debug_i: %d, i: %d, x_name: %.*s\n", - flag, debug_i, i, Get_NAME_LENGTH((*R_INF), overlap_list->list[0].x_id), Get_NAME((*R_INF),overlap_list->list[0].x_id)); - print_snp_in_line(hap); - } - - if(round != hap->dp.max[debug_i]) - { - fprintf(stderr, "error: %d\n", round); - } - - - if(hap->dp.backtrack_length[debug_i] != 0) - { - column = Get_DP_Backtrack_Column(hap->dp, debug_i); - debug_i = column[0]; - } - else if(round != 1) - { - fprintf(stderr, "round: %d\n", round); - } - - - - round--; - } - - } - } - vector = Get_Result_SNP_Vector((*hap)); - memset(vector, -1, Get_SNP_Vector_Length((*hap))); - - - vector = Get_Result_SNP_Vector((*hap)); - memset(vector, -1, Get_SNP_Vector_Length((*hap))); - vector = Get_Result_SNP_Vector((*hap)); - for (i = 0; i < hap->available_snp; i++) - { - if(hap->dp.max[i] > 1) - { - vector = Get_Result_SNP_Vector((*hap)); - memset(vector, -1, Get_SNP_Vector_Length((*hap))); - - if(hap->dp.backtrack_length[i] < 1) - { - fprintf(stderr, "error\n"); - } - - - int debug_i = i; - int round = hap->dp.max[i]; - while (round > 0) - { - - - - vectorID2 = hap->snp_stat[debug_i].id; - vector2 = Get_SNP_Vector((*hap), vectorID2); - int flag; - if((flag = debug_add_to_result_snp_vector(hap, vector2, Get_SNP_Vector_Length((*hap))))!= -1) - { - fprintf(stderr, "flag: %d, debug_i: %d, i: %d, x_name: %.*s\n", - flag, debug_i, i, Get_NAME_LENGTH((*R_INF), overlap_list->list[0].x_id), Get_NAME((*R_INF),overlap_list->list[0].x_id)); - print_snp_in_line(hap); - } - - if(round != hap->dp.max[debug_i]) - { - fprintf(stderr, "error: %d\n", round); - } - - - if(hap->dp.backtrack_length[debug_i] != 0) - { - column = Get_DP_Backtrack_Column(hap->dp, debug_i); - debug_i = column[0]; - } - else if(round != 1) - { - fprintf(stderr, "round: %d\n", round); - } - - - - round--; - } - - } - } - vector = Get_Result_SNP_Vector((*hap)); - memset(vector, -1, Get_SNP_Vector_Length((*hap))); - **/ - /** - if(hap->available_snp > 4) - // if(memcmp("m54334_180924_221206/48759269/ccs", Get_NAME((*R_INF),overlap_list->list[0].x_id), - // Get_NAME_LENGTH((*R_INF), overlap_list->list[0].x_id)) == 0) - // if(memcmp("m54328_180922_235017/65536381/ccs", Get_NAME((*R_INF),overlap_list->list[0].x_id), - // Get_NAME_LENGTH((*R_INF), overlap_list->list[0].x_id)) == 0) - { - fprintf(stderr, "\n\n\n###########hap->available_snp: %d###########\n", hap->available_snp); - - fprintf(stderr, "x_name: %.*s\n", - Get_NAME_LENGTH((*R_INF), overlap_list->list[0].x_id), Get_NAME((*R_INF),overlap_list->list[0].x_id)); - - for (j = 0; j < hap->available_snp; j++) - { - fprintf(stderr, "*********site: %d, j: %d, id: %d*********\n", - hap->snp_stat[j].site, j, hap->snp_stat[j].id); - - fprintf(stderr, "type(0):\n"); - - int vectorID = hap->snp_stat[j].id; - int8_t* vector = Get_SNP_Vector((*hap), vectorID); - for (i = 0; i < hap->overlap; i++) - { - if(vector[i] == 0) - { - fprintf(stderr, "%3d, ", i); - } - } - fprintf(stderr, "\n"); - - fprintf(stderr, "type(1):\n"); - for (i = 0; i < hap->overlap; i++) - { - if(vector[i] == 1) - { - fprintf(stderr, "%3d, ", i); - } - } - fprintf(stderr, "\n"); - - ///if(j == 21 || j == 23) - // { - // for (i = 0; i < hap->overlap; i++) - // { - // if(vector[i] == 1) - // { - // fprintf(stderr, "1: i: %d, %.*s\n", i, Get_NAME_LENGTH((*R_INF), overlap_list->list[i].y_id), - // Get_NAME((*R_INF),overlap_list->list[i].y_id)); - // } - // } - // } - } - - fprintf(stderr, "***********************\n"); - for (i = 0; i < hap->dp.snp_num; i++) - { - fprintf(stderr, "hap->dp.max[%d]: %d, hap->dp.backtrack_length: %d\n", i, hap->dp.max[i], hap->dp.backtrack_length[i]); - if(hap->dp.backtrack_length[i] != 0) - { - column = Get_DP_Backtrack_Column(hap->dp, i); - for (j = 0; j < hap->dp.backtrack_length[i]; j++) - { - fprintf(stderr, "pre: %d,", column[j]); - } - fprintf(stderr, "\n"); - } - - } - } - - **/ - - - - - /** - fprintf(stderr, "overlap_list->length: %u, total_read: %u, unuseful_read: %u\n", overlap_list->length, total_read, unuseful_read); - for (i = 0; i < overlap_list->length; i++) - { - overlap_length = overlap_list->list[i].x_pos_e - overlap_list->list[i].x_pos_s + 1; - if (overlap_length * OVERLAP_THRESHOLD <= overlap_list->list[i].align_length) - { - total_read++; - flag = -1; - for (j = 0; j < hap->available_snp; j++) - { - vectorID = hap->snp_stat[j].id; - vector = Get_SNP_Vector((*hap), vectorID); - - if(vector[i] != 2 && flag == 2) - { - fprintf(stderr, "hahahah\n"); - break; - } - - if(vector[i] == 2) - { - flag = 2; - } - } - } - } - **/ - - - -} void lable_large_indels(overlap_region_alloc* overlap_list, All_reads* R_INF, long long read_length, Correct_dumy* dumy) @@ -11961,14 +6538,14 @@ Correct_dumy* dumy) long long cigar_i, operation, operationLen; int is_delete = 0; CIGAR* cigar; - for (i = 0; i < overlap_list->length; i++) + for (i = 0; i < (long long)overlap_list->length; i++) { ///should has at least 3 windows for this overlap if (overlap_list->list[i].is_match == 1 && overlap_list->list[i].w_list_length >= 3) { ///here w_list_length >= 3 ///skip the first and last window - for (j = 1; j < overlap_list->list[i].w_list_length - 1; j++) + for (j = 1; j < (long long)(overlap_list->list[i].w_list_length - 1); j++) { ///this window is not matched, it seems to have large difference if(overlap_list->list[i].w_list[j].y_end == -1) @@ -12005,29 +6582,22 @@ Correct_dumy* dumy) } } - end_rem: - overlap_list->list[i].w_list_length >= 3; - - + end_rem:; } if(is_delete == 1) { - long long overlap_length; long long window_start, window_end; Window_Pool w_inf; init_Window_Pool(&w_inf, read_length, WINDOW, TAIL_LENGTH); int flag = 0; - long long realLen, realLen_100; + long long realLen = 0, realLen_100 = 0; int to_recover = 0; while(get_Window(&w_inf, &window_start, &window_end) && flag != -2) { dumy->length = 0; dumy->lengthNT = 0; - ///flag返回的是重叠数量 - ///dumy->length返回的是有效完全重叠的数量 - ///dumy->lengthNT返回的是有效不完全重叠的数量 ///return overlaps that is overlaped with [window_start, window_end] flag = get_available_fully_covered_interval(window_start, window_end, overlap_list, dumy, &realLen, &realLen_100); @@ -12035,11 +6605,11 @@ Correct_dumy* dumy) switch (flag) { - case 1: ///找到匹配 + case 1: ///match break; - case 0: ///没找到匹配 + case 0: ///unmatch break; - case -2: ///下一个window也不会存在匹配, 直接跳出 + case -2: ///unmatch, and the next window also cannot match break; } @@ -12053,7 +6623,7 @@ Correct_dumy* dumy) if(to_recover == 1) { - for (i = 0; i < overlap_list->length; i++) + for (i = 0; i < (long long)overlap_list->length; i++) { if (overlap_list->list[i].is_match == 100) { @@ -12064,7 +6634,7 @@ Correct_dumy* dumy) } - for (i = 0; i < overlap_list->length; i++) + for (i = 0; i < (long long)(overlap_list->length); i++) { if (overlap_list->list[i].is_match == 1) { @@ -12088,23 +6658,22 @@ int debug_print_snp_stat(char* name, haplotype_evdience_alloc* hap, overlap_regi { fprintf(stderr, "\n%s, available_snp: %d\n", name, hap->available_snp); int i; - for (i = 0; i < hap->available_snp; i++) + for (i = 0; i < (int)hap->available_snp; i++) { fprintf(stderr, "site: %d, occ_0: %d, occ_1: %d, occ_2: %d\n", hap->snp_stat[i].site, hap->snp_stat[i].occ_0, hap->snp_stat[i].occ_1, hap->snp_stat[i].occ_2); } - } + } + + return 1; } int generate_haplotypes_DP(haplotype_evdience_alloc* hap, overlap_region_alloc* overlap_list, All_reads* R_INF, long long rLen, int force_repeat) { int j, i; - int vectorID, vectorID2; - int diff_core_vector = 0; - int diff_vector_ID = -1; int8_t *vector, *vector2; @@ -12122,9 +6691,9 @@ int force_repeat) { i = 0; ///if a snp is very near to others, it should not be a real snp - for (j = 0; j < hap->available_snp; j++) + for (j = 0; j < (int)hap->available_snp; j++) { - if(j > 0 && j < hap->available_snp - 1) + if(j > 0 && j < (int)(hap->available_snp - 1)) { if(hap->snp_stat[j].site != hap->snp_stat[j - 1].site + 1 && @@ -12161,17 +6730,17 @@ int force_repeat) int flag; - long long overlap_length, total_read, unuseful_read, last_j, last_j_ID, last_j_flag; + long long overlap_length, total_read, unuseful_read; total_read = unuseful_read = 0; ///check if any read may be conflict with others - for (i = 0; i < overlap_list->length; i++) + for (i = 0; i < (long long)overlap_list->length; i++) { overlap_length = overlap_list->list[i].x_pos_e - overlap_list->list[i].x_pos_s + 1; if (overlap_list->list[i].is_match == 1) { total_read++; flag = -1; - for (j = 0; j < hap->available_snp; j++) + for (j = 0; j < (int)hap->available_snp; j++) { vectorID = hap->snp_stat[j].id; vector = Get_SNP_Vector((*hap), vectorID); @@ -12189,9 +6758,6 @@ int force_repeat) if(vector[i] != 0 && vector[i] != 1) { flag = 2; - last_j = hap->snp_stat[j].site; - last_j_ID = j; - last_j_flag = vector[i]; } }///flag == 0 means there is at least one useful signal first, and another unuseful signal after that else if(flag == 2) @@ -12208,7 +6774,7 @@ int force_repeat) if(flag == 3) { unuseful_read++; - for (j = 0; j < hap->available_snp; j++) + for (j = 0; j < (int)hap->available_snp; j++) { vectorID = hap->snp_stat[j].id; vector = Get_SNP_Vector((*hap), vectorID); @@ -12250,11 +6816,10 @@ int force_repeat) long long equal_best = 0; uint32_t* column; - long long column_length; - for (i = 0; i < hap->available_snp; i++) + for (i = 0; i < (int)hap->available_snp; i++) { ///vector of snp i vectorID = hap->snp_stat[i].id; @@ -12264,7 +6829,6 @@ int force_repeat) hap->dp.backtrack_length[i] = 0; equal_best = 0; column = Get_DP_Backtrack_Column(hap->dp, i); - column_length = Get_DP_Backtrack_Column_Length(hap->dp, i); for (j = 0; j < i; j++) { @@ -12298,13 +6862,9 @@ int force_repeat) /*******************************DP********************************/ - - - - uint64_t tmp_mode = 0; - for (i = 0; i < hap->available_snp; i++) + for (i = 0; i < (int)hap->available_snp; i++) { tmp_mode = hap->dp.max[i]; tmp_mode = tmp_mode << 32; @@ -12316,17 +6876,12 @@ int force_repeat) int snpID; - int group_num = 0; ///the minmum snp_num is 1 hap->dp.max_snp_num = 0; hap->dp.max_score = -2; - - - - - for (i = 0; i < hap->available_snp; i++) + for (i = 0; i < (int)hap->available_snp; i++) { snpID = Get_Max_DP_ID(hap->dp.max_for_sort[i]); if(hap->dp.visit[snpID] == 0) @@ -12342,7 +6897,7 @@ int force_repeat) //if(hap->dp.max_snp_num > 0) if(hap->available_snp > 0) { - process_repeat_snps(hap, coverage, overlap_list); + process_repeat_snps(hap, overlap_list); return 1; } else @@ -12384,71 +6939,28 @@ inline int check_informative_site(haplotype_evdience_alloc* hap, SnpStats* snp) return 0; } -inline long long snp_occ_in_one_read(haplotype_evdience_alloc* hap, overlap_region_alloc* overlap_list, -long long readID) -{ - long long i; - long long vectorID; - int8_t *vector; - - long long snp_occ = 0; - for (i = 0; i < hap->available_snp; i++) - { - vectorID = hap->snp_stat[i].id; - vector = Get_SNP_Vector((*hap), vectorID); - if(vector[readID] == 1) - { - snp_occ++; - } - } - - return snp_occ; -} - -inline void remove_read_from_snps(haplotype_evdience_alloc* hap, overlap_region_alloc* overlap_list, -long long readID) -{ - long long i; - long long vectorID; - int8_t *vector; - - for (i = 0; i < hap->available_snp; i++) - { - vectorID = hap->snp_stat[i].id; - vector = Get_SNP_Vector((*hap), vectorID); - vector[readID] = 2; - } - overlap_list->list[readID].is_match = 4; -} int generate_haplotypes_naive(haplotype_evdience_alloc* hap, overlap_region_alloc* overlap_list, All_reads* R_INF, long long rLen, int force_repeat) { int j, i; - int vectorID, vectorID2; - int diff_core_vector = 0; - int diff_vector_ID = -1; - int8_t *vector, *vector2; - if(hap->available_snp == 0) { return 0; } - - ///if hap->available_snp == 1, the following codes would have bugs ///filter snps that are highly likly false if(hap->available_snp > 1) { i = 0; ///if a snp is very close to others, it should not be a real snp - for (j = 0; j < hap->available_snp; j++) + for (j = 0; j < (int)hap->available_snp; j++) { - if(j > 0 && j < hap->available_snp - 1) + if(j > 0 && j < (int)(hap->available_snp - 1)) { if(hap->snp_stat[j].site != hap->snp_stat[j - 1].site + 1 && @@ -12480,12 +6992,12 @@ int force_repeat) } - long long m, snp_occ; + long long m; if(hap->available_snp > 0) { ///************************debug**************************/// m = 0; - for (i = 0; i < hap->available_snp; i++) + for (i = 0; i < (int)hap->available_snp; i++) { if(check_informative_site(hap, &(hap->snp_stat[i]))) { @@ -12500,7 +7012,7 @@ int force_repeat) init_DP_matrix(&(hap->dp), hap->available_snp); - for (i = 0; i < hap->available_snp; i++) + for (i = 0; i < (int)hap->available_snp; i++) { hap->dp.max_buffer[i] = i; } @@ -12518,355 +7030,13 @@ int force_repeat) } -void print_Haplotype(haplotype_evdience_alloc* hap, overlap_region_alloc* overlap_list, All_reads* R_INF) -{ - int j, i; - - - fprintf(stderr, "\nhap->snp: %d, hap->length: %d, perc: %d, x_name: %.*s\n", - hap->snp, hap->length, (hap->snp == 0? 0: hap->length/hap->snp), - Get_NAME_LENGTH((*R_INF), overlap_list->list[0].x_id), Get_NAME((*R_INF),overlap_list->list[0].x_id) - ); - fprintf(stderr, "hap->available_snp: %d, hap->core_snp:%d\n", - hap->available_snp, hap->core_snp); - - int Len_x, matched_overlap = 0; - for (j = 0; j < overlap_list->length; j++) - { - Len_x = overlap_list->list[j].x_pos_e - overlap_list->list[j].x_pos_s + 1; - - if (overlap_list->list[j].is_match == 1) - { - matched_overlap++; - } - } - - fprintf(stderr, "occ_0: %d, occ_1: %d, overlap_num: %d, matched_overlap: %d, overlap_list->length: %d\n", - hap->result_stat.occ_0, hap->result_stat.occ_1, - hap->result_stat.overlap_num, matched_overlap, overlap_list->length); - - - return; - - fprintf(stderr, "Phaseing sucessfully!\n"); - fprintf(stderr, "occ_0: %d, occ_1: %d, overlap_num: %d\n", - hap->result_stat.occ_0, hap->result_stat.occ_1, - hap->result_stat.overlap_num); - - - int8_t* vector = Get_Result_SNP_Vector((*hap)); - for (j = 0; j < hap->overlap; j++) - { - if(vector[j] == 0) - { - fprintf(stderr, "Ptype: %d, ID: %d\n", vector[j], j); - } - } - - - for (j = 0; j < hap->overlap; j++) - { - if(vector[j] == 1) - { - fprintf(stderr, "Ptype: %d, ID: %d\n", vector[j], j); - } - } - - - for (j = 0; j < hap->overlap; j++) - { - if(vector[j] == 2) - { - fprintf(stderr, "Ptype: %d, ID: %d\n", vector[j], j); - } - } - - - for (i = 0; i < hap->core_snp; i++) - { - fprintf(stderr, "core(i): %d, site: %d, occ_0: %d, occ_1: %d, occ_2: %d, score: %d\n", - i, hap->snp_stat[i].site, hap->snp_stat[i].occ_0, hap->snp_stat[i].occ_1, - hap->snp_stat[i].occ_2, - hap->snp_stat[i].score); - - int vectorID = hap->snp_stat[i].id; - int8_t* vector = Get_SNP_Vector((*hap), vectorID); - - for (j = 0; j < hap->overlap; j++) - { - if(vector[j] == 0) - { - fprintf(stderr, "type: %d, ID: %d\n", vector[j], j); - } - } - - - for (j = 0; j < hap->overlap; j++) - { - if(vector[j] == 1) - { - fprintf(stderr, "type: %d, ID: %d\n", vector[j], j); - } - } - - - for (j = 0; j < hap->overlap; j++) - { - if(vector[j] == 2) - { - fprintf(stderr, "type: %d, ID: %d\n", vector[j], j); - } - } - - } - - - - for (; i < hap->available_snp; i++) - { - fprintf(stderr, "i: %d, site: %d, occ_0: %d, occ_1: %d, occ_2: %d, score: %d\n", - i, hap->snp_stat[i].site, hap->snp_stat[i].occ_0, hap->snp_stat[i].occ_1, - hap->snp_stat[i].occ_2, - hap->snp_stat[i].score); - - int vectorID = hap->snp_stat[i].id; - int8_t* vector = Get_SNP_Vector((*hap), vectorID); - - for (j = 0; j < hap->overlap; j++) - { - if(vector[j] == 0) - { - fprintf(stderr, "type: %d, ID: %d\n", vector[j], j); - } - } - - - for (j = 0; j < hap->overlap; j++) - { - if(vector[j] == 1) - { - fprintf(stderr, "type: %d, ID: %d\n", vector[j], j); - } - } - - - for (j = 0; j < hap->overlap; j++) - { - if(vector[j] == 2) - { - fprintf(stderr, "type: %d, ID: %d\n", vector[j], j); - } - } - - } - -} - - - -void debug_near_snp(overlap_region_alloc* overlap_list, All_reads* R_INF, - UC_Read* g_read, Correct_dumy* dumy, haplotype_evdience_alloc* hap) -{ - int i, j, overlap_length, window_start, window_end, flag; - long long window_num = (g_read->length + WINDOW - 1) / WINDOW; - int total_read = 0; - int unuseful_read = 0; - for (i = 0; i < overlap_list->length; i++) - { - int flag = -1; - overlap_length = overlap_list->list[i].x_pos_e - overlap_list->list[i].x_pos_s + 1; - if (overlap_list->list[i].is_match == 1) - { - total_read++; - for (j = 0; j < hap->available_snp; j++) - { - int vectorID = hap->snp_stat[j].id; - int8_t* vector = Get_SNP_Vector((*hap), vectorID); - - if(vector[i] != 2 && flag == 2) - { - unuseful_read++; - break; - } - - if(vector[i] == 2) - { - flag = 2; - } - } - } - } - - - - - - for (j = 1; j < hap->available_snp; j++) - { - if(hap->snp_stat[j].site <= hap->snp_stat[j - 1].site) - { - fprintf(stderr, "error\n"); - } - } - - for (j = 0; j < hap->available_snp; j++) - { - fprintf(stderr, "\n\nsite: %d, score: %d\n", hap->snp_stat[j].site, hap->snp_stat[j].score ); - - // if((j>0 && hap->snp_stat[j].site == hap->snp_stat[j - 1].site + 1) - // || - // (j < hap->available_snp - 1 && hap->snp_stat[j].site + 1 == hap->snp_stat[j + 1].site)) - { - - fprintf(stderr, "x_name: %.*s\n", - Get_NAME_LENGTH((*R_INF), overlap_list->list[0].x_id), Get_NAME((*R_INF),overlap_list->list[0].x_id)); - - int vectorID = hap->snp_stat[j].id; - int8_t* vector = Get_SNP_Vector((*hap), vectorID); - for (i = 0; i < hap->overlap; i++) - { - if(vector[i] == 0) - { - fprintf(stderr, "type: %d, ID: %d\n", vector[i], i); - } - } - - for (i = 0; i < hap->overlap; i++) - { - if(vector[i] == 1) - { - fprintf(stderr, "type: %d, ID: %d, %.*s\n", - vector[i], i, - Get_NAME_LENGTH((*R_INF), overlap_list->list[i].y_id), Get_NAME((*R_INF),overlap_list->list[i].y_id)); - - } - } - - window_start = 0; - window_end = WINDOW - 1; - if (window_end >= g_read->length) - { - window_end = g_read->length - 1; - } - long long ijk; - for (ijk = 0; ijk < window_num; ijk++) - { - - - if(hap->snp_stat[j].site <= window_end && hap->snp_stat[j].site >= window_start) - { - fprintf(stderr, "winID: %d, winNum: %d, window_start: %d, window_end: %d\n", - ijk, window_num, window_start, window_end); - - dumy->length = 0; - dumy->lengthNT = 0; - ///flag返回的是重叠数量 - ///dumy->length返回的是有效完全重叠的数量 - ///dumy->lengthNT返回的是有效不完全重叠的数量 - ///return overlaps that is overlaped with [window_start, window_end] - flag = get_available_interval(window_start, window_end, overlap_list, dumy); - switch (flag) - { - case 1: ///找到匹配 - break; - case 0: ///没找到匹配 - break; - case -2: ///下一个window也不会存在匹配, 直接跳出 - i = window_num; - break; - } - - - for (i = 0; i < dumy->length; i++) - { - ///这个是那个overlap的ID,而不是overlap里对应窗口的ID - int overlapID = dumy->overlapID[i]; - - ///overlap_list->list[overlapID].x_pos_s is the begining of the whole overlap - int correct_x_pos_s = (overlap_list->list[overlapID].x_pos_s / WINDOW) * WINDOW; - ///window_start is the begining of this window in the whole x_read - int windowID = (window_start - correct_x_pos_s) / WINDOW; - - ///如果这个window不匹配,跳过 - if (overlap_list->list[overlapID].w_list[windowID].y_end == -1) - { - continue; - } - - if(vector[overlapID] == 1) - { - fprintf(stderr, "overlapID: %d, x_strat: %d, x_end: %d, y_start: %d, y_end: %d\n", - overlapID, - overlap_list->list[overlapID].w_list[windowID].x_start, - overlap_list->list[overlapID].w_list[windowID].x_end, - overlap_list->list[overlapID].w_list[windowID].y_start, - overlap_list->list[overlapID].w_list[windowID].y_end); - - recover_UC_Read_sub_region(dumy->overlap_region, overlap_list->list[overlapID].w_list[windowID].y_start, - overlap_list->list[overlapID].w_list[windowID].y_end -overlap_list->list[overlapID].w_list[windowID].y_start + 1, - overlap_list->list[overlapID].y_pos_strand, - R_INF, overlap_list->list[overlapID].y_id); - - char* x_string = g_read->seq + overlap_list->list[overlapID].w_list[windowID].x_start; - char* y_string = dumy->overlap_region; - - fprintf(stderr, "x_string: \n%.*s\n", - overlap_list->list[overlapID].w_list[windowID].x_end - overlap_list->list[overlapID].w_list[windowID].x_start + 1, - x_string); - - fprintf(stderr, "y_string: \n%.*s\n", - overlap_list->list[overlapID].w_list[windowID].y_end - overlap_list->list[overlapID].w_list[windowID].y_start + 1, - y_string); - - - int haha_i = 0; - - for (haha_i = 0; haha_i < overlap_list->list[overlapID].w_list[windowID].cigar.length; haha_i++) - { - fprintf(stderr, "oper: %d, len: %d\n", - overlap_list->list[overlapID].w_list[windowID].cigar.C_C[haha_i], - overlap_list->list[overlapID].w_list[windowID].cigar.C_L[haha_i] - ); - } - - } - - - } - - } - - - - - - window_start = window_start + WINDOW; - window_end = window_end + WINDOW; - if (window_end >= g_read->length) - { - window_end = g_read->length - 1; - } - } - - - - - } - - } - fprintf(stderr, "total_read: %d, unuseful_read: %d\n", total_read, unuseful_read); - -} - - - void partition_overlaps(overlap_region_alloc* overlap_list, All_reads* R_INF, UC_Read* g_read, Correct_dumy* dumy, haplotype_evdience_alloc* hap, int force_repeat) { ResizeInitHaplotypeEvdience(hap); - long long i, j, overlap_length; + long long i; long long window_start, window_end; long long num_availiable_win = 0; @@ -12879,30 +7049,24 @@ void partition_overlaps(overlap_region_alloc* overlap_list, All_reads* R_INF, { dumy->length = 0; dumy->lengthNT = 0; - ///flag返回的是重叠数量 - ///dumy->length返回的是有效完全重叠的数量 - ///dumy->lengthNT返回的是有效不完全重叠的数量 + ///return overlaps that is overlaped with [window_start, window_end] flag = get_available_interval(window_start, window_end, overlap_list, dumy); switch (flag) { - case 1: ///找到匹配 + case 1: ///found matched overlaps break; - case 0: ///没找到匹配 + case 0: ///do not find any matched overlaps break; - case -2: ///下一个window也不会存在匹配, 直接跳出 + case -2: ///do not find any matched overlaps, and the next window also cannot match break; } - ///这个是available overlap里所有window的数量... - ///num_availiable_win = num_availiable_win + dumy->length + dumy->lengthNT; num_availiable_win = num_availiable_win + dumy->length; cluster(g_read->seq, window_start, window_end, overlap_list, dumy, R_INF, hap); } - - ///very time-consuming qsort(hap->list, hap->length, sizeof(haplotype_evdience), cmp_haplotype_evdience); @@ -12920,8 +7084,6 @@ void partition_overlaps(overlap_region_alloc* overlap_list, All_reads* R_INF, long long sub_length; haplotype_evdience* sub_list; - ///long long debug_total_length = 0; - ////split reads for (i = 0; i < hap->length; i++) { @@ -12931,7 +7093,6 @@ void partition_overlaps(overlap_region_alloc* overlap_list, All_reads* R_INF, { sub_list = hap->list + pre_i; sub_length = i - pre_i; - ///debug_total_length = debug_total_length + sub_length; split_sub_list(hap, sub_list, sub_length, hap->snp, overlap_list, R_INF, g_read); } num_of_snps++; @@ -12944,7 +7105,6 @@ void partition_overlaps(overlap_region_alloc* overlap_list, All_reads* R_INF, { sub_list = hap->list + pre_i; sub_length = i - pre_i; - ///debug_total_length = debug_total_length + sub_length; split_sub_list(hap, sub_list, sub_length, hap->snp, overlap_list, R_INF, g_read); } @@ -12965,7 +7125,7 @@ void partition_overlaps_advance(overlap_region_alloc* overlap_list, All_reads* R { ResizeInitHaplotypeEvdience(hap); - long long i, j, overlap_length; + long long i; long long window_start, window_end; long long num_availiable_win = 0; @@ -12978,22 +7138,18 @@ void partition_overlaps_advance(overlap_region_alloc* overlap_list, All_reads* R { dumy->length = 0; dumy->lengthNT = 0; - ///flag返回的是重叠数量 - ///dumy->length返回的是有效完全重叠的数量 - ///dumy->lengthNT返回的是有效不完全重叠的数量 ///return overlaps that is overlaped with [window_start, window_end] flag = get_available_interval(window_start, window_end, overlap_list, dumy); switch (flag) { - case 1: ///找到匹配 + case 1: ///found matched overlaps break; - case 0: ///没找到匹配 + case 0: ///do not find any matched overlaps break; - case -2: ///下一个window也不会存在匹配, 直接跳出 + case -2: ///do not find any matched overlaps, and the next window also cannot match break; } - ///这个是available overlap里所有window的数量... num_availiable_win = num_availiable_win + dumy->length; ///need to deal with @@ -13005,12 +7161,6 @@ void partition_overlaps_advance(overlap_region_alloc* overlap_list, All_reads* R ///very time-consuming qsort(hap->list, hap->length, sizeof(haplotype_evdience), cmp_haplotype_evdience); - - - - - ///debug_hap_information(overlap_list, R_INF, g_read, hap, dumy); - SetSnpMatrix(hap, hap->snp, overlap_list->length); @@ -13020,25 +7170,15 @@ void partition_overlaps_advance(overlap_region_alloc* overlap_list, All_reads* R long long sub_length; haplotype_evdience* sub_list; - ///long long debug_total_length = 0; - ////split reads for (i = 0; i < hap->length; i++) { - // if(overlap_list->list[0].x_id == 109837) - // { - // fprintf(stderr, "hap->list[%d].site: %d, type: %d, misBase: %c, overlapID: %d, %.*s\n", - // i, hap->list[i].site, hap->list[i].type, hap->list[i].misBase, hap->list[i].overlapID, - // Get_NAME_LENGTH((*R_INF), overlap_list->list[hap->list[i].overlapID].y_id), - // Get_NAME((*R_INF), overlap_list->list[hap->list[i].overlapID].y_id)); - // } if(pre_site != hap->list[i].site) { if(i != 0) { sub_list = hap->list + pre_i; sub_length = i - pre_i; - ///debug_total_length = debug_total_length + sub_length; split_sub_list(hap, sub_list, sub_length, hap->snp, overlap_list, R_INF, g_read); } num_of_snps++; @@ -13051,305 +7191,23 @@ void partition_overlaps_advance(overlap_region_alloc* overlap_list, All_reads* R { sub_list = hap->list + pre_i; sub_length = i - pre_i; - ///debug_total_length = debug_total_length + sub_length; split_sub_list(hap, sub_list, sub_length, hap->snp, overlap_list, R_INF, g_read); } - ///debug_snp_matrix(hap); generate_haplotypes_DP(hap, overlap_list, R_INF, g_read->length, force_repeat); ///generate_haplotypes_naive(hap, overlap_list, R_INF, g_read->length, force_repeat); lable_large_indels(overlap_list, R_INF, g_read->length, dumy); - - - ///debug_snp_matrix(hap); } - - - - - - - - - - - - - - - - -void correct_overlap_back(overlap_region_alloc* overlap_list, All_reads* R_INF, - UC_Read* g_read, Correct_dumy* dumy, UC_Read* overlap_read, Graph* g, Graph* DAGCon, - long long* matched_overlap_0, long long* matched_overlap_1, - long long* potiental_matched_overlap_0, long long* potiental_matched_overlap_1, - Cigar_record* current_cigar, haplotype_evdience_alloc* hap, - Round2_alignment* second_round) -{ - - - reverse_complement(g_read->seq, g_read->length); - - clear_Correct_dumy(dumy, overlap_list); - - long long window_num = (g_read->length + WINDOW - 1) / WINDOW; - - long long i; - - long long window_start, window_end; - - window_start = 0; - window_end = WINDOW - 1; - if (window_end >= g_read->length) - { - window_end = g_read->length - 1; - } - - - - int flag; - - for (i = 0; i < window_num; i++) - { - - dumy->length = 0; - dumy->lengthNT = 0; - flag = get_interval(window_start, window_end, overlap_list, dumy); - - switch (flag) - { - case 1: ///找到匹配 - break; - case 0: ///没找到匹配 - break; - case -2: ///下一个window也不会存在匹配, 直接跳出 - i = window_num; - break; - } - - if(dumy->length + dumy->lengthNT>overlap_list->length) - { - fprintf(stderr, "error length\n"); - } - - - ///verify_get_interval(window_start, window_end, overlap_list, dumy); - - verify_window(window_start, window_end, overlap_list, dumy, R_INF, g_read->seq); - - window_start = window_start + WINDOW; - window_end = window_end + WINDOW; - if (window_end >= g_read->length) - { - window_end = g_read->length - 1; - } - } - - debug_stats(overlap_list, R_INF, g_read, dumy, overlap_read, potiental_matched_overlap_0, potiental_matched_overlap_1); - - recalcate_window(overlap_list, R_INF, g_read, dumy, overlap_read); - - debug_stats(overlap_list, R_INF, g_read, dumy, overlap_read, matched_overlap_0, matched_overlap_1); - - partition_overlaps(overlap_list, R_INF, g_read, dumy, hap, 0); - - generate_consensus(overlap_list, R_INF, g_read, dumy, g, DAGCon, current_cigar, second_round); -} - - -void print_overlap(char* name, long long readID, -overlap_region_alloc* overlap_list, All_reads* R_INF, int output_reads, int output_cigar) -{ - if(memcmp(name, Get_NAME((*R_INF), readID), - Get_NAME_LENGTH((*R_INF),readID)) == 0) - { - long long i, j; - fprintf(stderr, "\n\n****************ref_read: %.*s, id: %d****************\n", - Get_NAME_LENGTH((*R_INF),readID), Get_NAME((*R_INF),readID), readID); - - fprintf(stderr, "\n###flag: 1\n"); - - for (i = 0; i < overlap_list->length; i++) - { - if(overlap_list->list[i].is_match == 1) - { - fprintf(stderr, "%.*s\n", Get_NAME_LENGTH((*R_INF),overlap_list->list[i].y_id), - Get_NAME((*R_INF),overlap_list->list[i].y_id)); - - fprintf(stderr, "alignLen: %d, x_s: %d, x_e: %d, y_s: %d, y_e: %d, y_dir: %d, strong: %d\n", - overlap_list->list[i].align_length, - overlap_list->list[i].x_pos_s, - overlap_list->list[i].x_pos_e, - overlap_list->list[i].y_pos_s, - overlap_list->list[i].y_pos_e, - overlap_list->list[i].y_pos_strand, - overlap_list->list[i].strong); - } - } - - fprintf(stderr, "\n###flag: 2\n"); - - for (i = 0; i < overlap_list->length; i++) - { - if(overlap_list->list[i].is_match == 2) - { - fprintf(stderr, "%.*s\n", Get_NAME_LENGTH((*R_INF),overlap_list->list[i].y_id), - Get_NAME((*R_INF),overlap_list->list[i].y_id)); - - fprintf(stderr, "alignLen: %d, x_s: %d, x_e: %d, y_s: %d, y_e: %d, y_dir: %d, strong: %d\n", - overlap_list->list[i].align_length, - overlap_list->list[i].x_pos_s, - overlap_list->list[i].x_pos_e, - overlap_list->list[i].y_pos_s, - overlap_list->list[i].y_pos_e, - overlap_list->list[i].y_pos_strand, - overlap_list->list[i].strong); - } - } - - fprintf(stderr, "\n###flag: 4\n"); - - for (i = 0; i < overlap_list->length; i++) - { - if(overlap_list->list[i].is_match == 4) - { - fprintf(stderr, "%.*s\n", Get_NAME_LENGTH((*R_INF),overlap_list->list[i].y_id), - Get_NAME((*R_INF),overlap_list->list[i].y_id)); - fprintf(stderr, "alignLen: %d, x_s: %d, x_e: %d, y_s: %d, y_e: %d, y_dir: %d, strong: %d\n", - overlap_list->list[i].align_length, - overlap_list->list[i].x_pos_s, - overlap_list->list[i].x_pos_e, - overlap_list->list[i].y_pos_s, - overlap_list->list[i].y_pos_e, - overlap_list->list[i].y_pos_strand, - overlap_list->list[i].strong); - } - } - - if(output_reads) - { - UC_Read g_read; - init_UC_Read(&g_read); - recover_UC_Read(&g_read, R_INF, readID); - - fprintf(stderr, "\n\nOutput all related reads\n"); - fprintf(stderr, "ref_read:\n"); - fprintf(stderr, ">%.*s\n", Get_NAME_LENGTH((*R_INF),readID), Get_NAME((*R_INF),readID)); - fprintf(stderr, "%.*s\n", g_read.length, g_read.seq); - - ///fprintf(stderr, "query_read:\n"); - for (i = 0; i < overlap_list->length; i++) - { - ///fprintf(stderr, "i: %d\n", i); - recover_UC_Read(&g_read, R_INF, overlap_list->list[i].y_id); - fprintf(stderr, ">%.*s\n", - Get_NAME_LENGTH((*R_INF),overlap_list->list[i].y_id), - Get_NAME((*R_INF),overlap_list->list[i].y_id)); - fprintf(stderr, "%.*s\n", g_read.length, g_read.seq); - } - - fprintf(stderr, "Has already output all related reads\n\n"); - - destory_UC_Read(&g_read); - } - - - if(output_cigar) - { - for (i = 0; i < overlap_list->length; i++) - { - - fprintf(stderr, "\ni: %d, %.*s, x_s: %d, x_e: %d, y_s: %d, y_end: %d, w_list_length: %d, dir: %d, strong: %d, is_match: %d\n", - i, Get_NAME_LENGTH((*R_INF),overlap_list->list[i].y_id), - Get_NAME((*R_INF),overlap_list->list[i].y_id), - overlap_list->list[i].x_pos_s, - overlap_list->list[i].x_pos_e, - overlap_list->list[i].y_pos_s, - overlap_list->list[i].y_pos_e, - overlap_list->list[i].w_list_length, - overlap_list->list[i].y_pos_strand, - overlap_list->list[i].strong, - overlap_list->list[i].is_match); - - fprintf(stderr, "window cigar: \n"); - for (j = 0; j < overlap_list->list[i].w_list_length; j++) - { - fprintf(stderr, "************************\ncigar_j: %d, x_s: %d, x_e: %d, y_s: %d, y_end: %d\n", - j, overlap_list->list[i].w_list[j].x_start, - overlap_list->list[i].w_list[j].x_end, - overlap_list->list[i].w_list[j].y_start, - overlap_list->list[i].w_list[j].y_end); - if(overlap_list->list[i].w_list[j].y_end == -1) - { - fprintf(stderr, "not match\n"); - } - else - { - int cigar_i, operation, operationLen; - CIGAR* cigar = &(overlap_list->list[i].w_list[j].cigar); - fprintf(stderr, "length: %d\n", cigar->length); - for (cigar_i = 0; cigar_i < cigar->length; cigar_i++) - { - operation = cigar->C_C[cigar_i]; - operationLen = cigar->C_L[cigar_i]; - fprintf(stderr, "oper: %d, Len: %d\n", operation, operationLen); - } - } - } - - - - fprintf(stderr, "boundary cigar: \n"); - for (j = 0; j < overlap_list->list[i].boundary_cigars.length; j++) - { - fprintf(stderr, "###################\ncigar_j: %d, x_s: %d, x_e: %d, y_s: %d, y_end: %d\n", - j, overlap_list->list[i].boundary_cigars.buffer[j].x_start, - overlap_list->list[i].boundary_cigars.buffer[j].x_end, - overlap_list->list[i].boundary_cigars.buffer[j].y_start, - overlap_list->list[i].boundary_cigars.buffer[j].y_end); - - - if(overlap_list->list[i].boundary_cigars.buffer[j].y_end == -1) - { - fprintf(stderr, "not match\n"); - } - else - { - int cigar_i, operation, operationLen; - CIGAR* cigar = &(overlap_list->list[i].boundary_cigars.buffer[j].cigar); - fprintf(stderr, "length: %d\n", cigar->length); - for (cigar_i = 0; cigar_i < cigar->length; cigar_i++) - { - operation = cigar->C_C[cigar_i]; - operationLen = cigar->C_L[cigar_i]; - fprintf(stderr, "oper: %d, Len: %d\n", operation, operationLen); - } - } - } - } - } - - - fprintf(stderr, "End\n\n\n\n\n"); - - } - -} - - - void correct_overlap(overlap_region_alloc* overlap_list, All_reads* R_INF, - UC_Read* g_read, Correct_dumy* dumy, UC_Read* overlap_read, Graph* g, Graph* DAGCon, - long long* matched_overlap_0, long long* matched_overlap_1, - long long* potiental_matched_overlap_0, long long* potiental_matched_overlap_1, - Cigar_record* current_cigar, haplotype_evdience_alloc* hap, - Round2_alignment* second_round, int force_repeat, int is_consensus, - int* fully_cov, int* abnormal, uint8_t* c2n) + UC_Read* g_read, Correct_dumy* dumy, UC_Read* overlap_read, + Graph* g, Graph* DAGCon, Cigar_record* current_cigar, + haplotype_evdience_alloc* hap, Round2_alignment* second_round, + int force_repeat, int is_consensus, int* fully_cov, int* abnormal) { reverse_complement(g_read->seq, g_read->length); @@ -13379,11 +7237,6 @@ void correct_overlap(overlap_region_alloc* overlap_list, All_reads* R_INF, break; } - if(dumy->length + dumy->lengthNT>overlap_list->length) - { - fprintf(stderr, "error length\n"); - } - ///dumy->lengthNT represent how many overlaps that the length of them is not equal to WINDOW; may larger or less than WINDOW ///dumy->length represent how many overlaps that the length of them is WINDOW /****************************may improve**************************/ @@ -13391,49 +7244,11 @@ void correct_overlap(overlap_region_alloc* overlap_list, All_reads* R_INF, verify_window(window_start, window_end, overlap_list, dumy, R_INF, g_read->seq); } - /** - recalcate_window(overlap_list, R_INF, g_read, dumy, overlap_read); - partition_overlaps(overlap_list, R_INF, g_read, dumy, hap, force_repeat); - **/ - - recalcate_window_advance(overlap_list, R_INF, g_read, dumy, overlap_read); - ///partition_overlaps(overlap_list, R_INF, g_read, dumy, hap, force_repeat); - partition_overlaps_advance(overlap_list, R_INF, g_read, overlap_read, dumy, hap, force_repeat); - - - - - - - // print_overlap("m64013_190410_223304/61081096/ccs", - // overlap_list->list[0].x_id, overlap_list, R_INF, 0, 1); - - // print_overlap("m64013_190412_043951/83756835/ccs", - // overlap_list->list[0].x_id, overlap_list, R_INF, 0, 1); - - // print_overlap("m64013_190322_203854/152832751/ccs", - // overlap_list->list[0].x_id, overlap_list, R_INF, 0, 1); - - // print_overlap("m64011_190329_072846/175047040/ccs", - // overlap_list->list[0].x_id, overlap_list, R_INF, 0, 1); - - // print_overlap("m64013_190322_203854/13174300/ccs", - // overlap_list->list[0].x_id, overlap_list, R_INF, 0, 1); - - // print_overlap("m64013_190322_203854/177145456/ccs", - // overlap_list->list[0].x_id, overlap_list, R_INF, 0, 1); - - // print_overlap("m64013_190322_203854/120717695/ccs", - // overlap_list->list[0].x_id, overlap_list, R_INF, 0, 1); - - // print_overlap("m64011_190329_072846/76548728/ccs", - // overlap_list->list[0].x_id, overlap_list, R_INF, 0, 1); - - // print_overlap("m64013_190324_024932/23660629/ccs", - // overlap_list->list[0].x_id, overlap_list, R_INF, 0, 1); - - + // recalcate_window(overlap_list, R_INF, g_read, dumy, overlap_read); + // partition_overlaps(overlap_list, R_INF, g_read, dumy, hap, force_repeat); + recalcate_window_advance(overlap_list, R_INF, g_read, dumy, overlap_read); + partition_overlaps_advance(overlap_list, R_INF, g_read, overlap_read, dumy, hap, force_repeat); if(is_consensus) { @@ -13446,7 +7261,6 @@ void correct_overlap(overlap_region_alloc* overlap_list, All_reads* R_INF, } - void init_Cigar_record(Cigar_record* dummy) { dummy->length = 0; @@ -13527,7 +7341,6 @@ void clear_Correct_dumy(Correct_dumy* list, overlap_region_alloc* overlap_list) } - void clear_Correct_dumy_pure(Correct_dumy* list) { list->length = 0; @@ -13539,359 +7352,6 @@ void clear_Correct_dumy_pure(Correct_dumy* list) } -void pre_filter_by_nearby_single(k_mer_pos* new_n_list, k_mer_pos* old_n_list, uint64_t n_length, uint64_t n_end_pos, UC_Read* g_read, -All_reads* R_INF, Correct_dumy* dumy, uint64_t* new_n_length) -{ - (*new_n_length) = 0; - ///这种就是0,new_n_list也不需要有数据 - if (n_length == 0) - { - return; - } - - char* x_string = NULL; - char* y_string = NULL; - - - long long x_offset = (long long)(n_end_pos) - WINDOW + 1; - if (x_offset < 0) - { - x_offset = 0; - } - - ///x_length是x上待验证区间的实际长度 - ///x_offset是x上待验证区间的起始位置 - ///如果是向前取待验证区间,那么x_length至少大于等于40 (compressed k-mer长度,不是实际长度) - long long x_length = n_end_pos - x_offset + 1; - ///这种也不要过滤了,直接把old_n_list全部赋值过来就好了 - if (x_length < (WINDOW/2)) - { - (*new_n_length) = n_length; - memcpy(new_n_list, old_n_list, sizeof(k_mer_pos)*n_length); - return; - } - long long Window_Len = x_length + (THRESHOLD << 1); - x_string = g_read->seq + x_offset; - - - long long y_offset; - long long y_length; - - long long i = 0; - - long long y_read_length; - int end_site; - unsigned int error; - - for (i = 0; i < n_length; i++) - { - ////old_n_list[i].offset是y上k-mer的结束位置 - ///n_end_pos是x上k-mer的结束位置 - ///x_length是x上区间长度 - y_offset = (long long)(old_n_list[i].offset) - x_length; - ///这种情况下弃疗 - if (y_offset < 0) - { - new_n_list[(*new_n_length)].readID = old_n_list[i].readID; - new_n_list[(*new_n_length)].offset = old_n_list[i].offset; - (*new_n_length)++; - continue; - } - - y_offset = y_offset - THRESHOLD; - ///还能抢救 - if (y_offset < 0) - { - y_offset = 0; - } - - ///y的read的总长度 - y_read_length = Get_READ_LENGTH((*R_INF), old_n_list[i].readID); - y_length = MIN(Window_Len, y_read_length - y_offset); - - ///如果y的长度比x长度还小,那就直接弃疗了 - if (y_length < x_length) - { - new_n_list[(*new_n_length)].readID = old_n_list[i].readID; - new_n_list[(*new_n_length)].offset = old_n_list[i].offset; - (*new_n_length)++; - continue; - } - - ///y的方向都是0,因为索引里都是0 - recover_UC_Read_sub_region(dumy->overlap_region, y_offset, y_length, 0, R_INF, old_n_list[i].readID); - y_string = dumy->overlap_region; - - memset (y_string + y_length, 0, Window_Len - y_length); - end_site = Reserve_Banded_BPM(y_string, y_length, x_string, x_length, THRESHOLD, &error); - - if (error!=(unsigned int)-1) - { - new_n_list[(*new_n_length)].readID = old_n_list[i].readID; - new_n_list[(*new_n_length)].offset = old_n_list[i].offset; - (*new_n_length)++; - } - } -} - - - -void pre_filter_by_nearby(k_mer_pos* new_n_list, k_mer_pos* old_n_list, uint64_t n_length, uint64_t n_end_pos, UC_Read* g_read, -All_reads* R_INF, Correct_dumy* dumy, uint64_t* new_n_length) -{ - (*new_n_length) = 0; - ///这种就是0,new_n_list也不需要有数据 - if (n_length == 0) - { - return; - } - - char* x_string = NULL; - char* y_string = NULL; - - - long long x_offset = (long long)(n_end_pos) - WINDOW + 1; - if (x_offset < 0) - { - x_offset = 0; - } - - ///x_length是x上待验证区间的实际长度 - ///x_offset是x上待验证区间的起始位置 - ///如果是向前取待验证区间,那么x_length至少大于等于40 (compressed k-mer长度,不是实际长度) - long long x_length = n_end_pos - x_offset + 1; - ///这种也不要过滤了,直接把old_n_list全部赋值过来就好了 - if (x_length < (WINDOW/2)) - { - (*new_n_length) = n_length; - memcpy(new_n_list, old_n_list, sizeof(k_mer_pos)*n_length); - return; - } - long long Window_Len = x_length + (THRESHOLD << 1); - x_string = g_read->seq + x_offset; - - - long long y_offset; - long long y_length; - - long long i = 0; - - long long y_read_length; - int end_site; - unsigned int error; - - int groupLen = 0; - int return_sites[GROUP_SIZE]; - unsigned int return_sites_error[GROUP_SIZE]; - uint64_t readID[GROUP_SIZE]; - uint64_t offset[GROUP_SIZE]; - - for (i = 0; i < n_length; i++) - { - ////old_n_list[i].offset是y上k-mer的结束位置 - ///n_end_pos是x上k-mer的结束位置 - ///x_length是x上区间长度 - y_offset = (long long)(old_n_list[i].offset) - x_length; - ///这种情况下弃疗 - if (y_offset < 0) - { - new_n_list[(*new_n_length)].readID = old_n_list[i].readID; - new_n_list[(*new_n_length)].offset = old_n_list[i].offset; - (*new_n_length)++; - continue; - } - - y_offset = y_offset - THRESHOLD; - ///还能抢救 - if (y_offset < 0) - { - y_offset = 0; - } - - ///y的read的总长度 - y_read_length = Get_READ_LENGTH((*R_INF), old_n_list[i].readID); - y_length = MIN(Window_Len, y_read_length - y_offset); - - ///如果y的长度比x长度还小,那就直接弃疗了 - if (y_length < x_length) - { - new_n_list[(*new_n_length)].readID = old_n_list[i].readID; - new_n_list[(*new_n_length)].offset = old_n_list[i].offset; - (*new_n_length)++; - continue; - } - - if(y_length == Window_Len) - { - ///y的方向都是0,因为索引里都是0 - recover_UC_Read_sub_region(dumy->overlap_region_group[groupLen], y_offset, y_length, 0, - R_INF, old_n_list[i].readID); - readID[groupLen] = old_n_list[i].readID; - offset[groupLen] = old_n_list[i].offset; - - - groupLen++; - if (groupLen == GROUP_SIZE) - { - Reserve_Banded_BPM_4_SSE_only(dumy->overlap_region_group[0], dumy->overlap_region_group[1], - dumy->overlap_region_group[2], dumy->overlap_region_group[3], y_length, x_string, x_length, - return_sites, return_sites_error, THRESHOLD, dumy->Peq_SSE); - groupLen = 0; - - if (return_sites_error[0]!=(unsigned int)-1) - { - new_n_list[(*new_n_length)].readID = readID[0]; - new_n_list[(*new_n_length)].offset = offset[0]; - (*new_n_length)++; - } - - if (return_sites_error[1]!=(unsigned int)-1) - { - new_n_list[(*new_n_length)].readID = readID[1]; - new_n_list[(*new_n_length)].offset = offset[1]; - (*new_n_length)++; - } - - if (return_sites_error[2]!=(unsigned int)-1) - { - new_n_list[(*new_n_length)].readID = readID[2]; - new_n_list[(*new_n_length)].offset = offset[2]; - (*new_n_length)++; - } - - if (return_sites_error[3]!=(unsigned int)-1) - { - new_n_list[(*new_n_length)].readID = readID[3]; - new_n_list[(*new_n_length)].offset = offset[3]; - (*new_n_length)++; - } - - } - - } - else - { - ///y的方向都是0,因为索引里都是0 - recover_UC_Read_sub_region(dumy->overlap_region, y_offset, y_length, 0, R_INF, old_n_list[i].readID); - y_string = dumy->overlap_region; - - memset (y_string + y_length, 0, Window_Len - y_length); - end_site = Reserve_Banded_BPM(y_string, y_length, x_string, x_length, THRESHOLD, &error); - - if (error!=(unsigned int)-1) - { - new_n_list[(*new_n_length)].readID = old_n_list[i].readID; - new_n_list[(*new_n_length)].offset = old_n_list[i].offset; - (*new_n_length)++; - } - } - - - - } - - - - if (groupLen == 1) - { - end_site = Reserve_Banded_BPM(dumy->overlap_region_group[0], Window_Len, x_string, x_length, THRESHOLD, &error); - - if (error!=(unsigned int)-1) - { - new_n_list[(*new_n_length)].readID = readID[0]; - new_n_list[(*new_n_length)].offset = offset[0]; - (*new_n_length)++; - } - - } - else - { - Reserve_Banded_BPM_4_SSE_only(dumy->overlap_region_group[0], dumy->overlap_region_group[1], - dumy->overlap_region_group[2], dumy->overlap_region_group[3], Window_Len, x_string, x_length, - return_sites, return_sites_error, THRESHOLD, dumy->Peq_SSE); - - - for (i = 0; i < groupLen; i++) - { - if (return_sites_error[i]!=(unsigned int)-1) - { - new_n_list[(*new_n_length)].readID = readID[i]; - new_n_list[(*new_n_length)].offset = offset[i]; - (*new_n_length)++; - } - } - - groupLen = 0; - - - } - - ///要排序.... - if ((*new_n_length)>1) - { - qsort(new_n_list, (*new_n_length), sizeof(k_mer_pos), cmp_k_mer_pos); - } -} - - - -/**********************for prefilter************************ */ - -void destory_k_mer_pos_list_alloc_prefilter(k_mer_pos_list_alloc* list) -{ - long long i = 0; - for (i = 0; i < list->size; i++) - { - if (list->list[i].size != 0) - { - free(list->list[i].list); - } - } - - free(list->list); -} - - -void append_k_mer_pos_list_alloc_prefilter(k_mer_pos_list_alloc* list, k_mer_pos* n_list, uint64_t n_length, -uint64_t n_end_pos, uint8_t n_direction, UC_Read* g_read, All_reads* R_INF, Correct_dumy* dumy) -{ - - if (list->length + 1 > list->size) - { - list->size = list->size * 2; - list->list = (k_mer_pos_list*)realloc(list->list, sizeof(k_mer_pos_list)*list->size); - ///新分配空间要初始化 - memset(list->list + (list->size/2), 0, sizeof(k_mer_pos_list)*(list->size/2)); - } - - if (list->list[list->length].size < n_length) - { - list->list[list->length].size = n_length; - list->list[list->length].list = (k_mer_pos*)realloc(list->list[list->length].list, - sizeof(k_mer_pos)*list->list[list->length].size); - } - - - ///list->list[list->length].list = n_list; - ///memcpy(list->list[list->length].list, n_list, sizeof(k_mer_pos)*n_length); - pre_filter_by_nearby(list->list[list->length].list, n_list, n_length, n_end_pos, - g_read, R_INF, dumy, &n_length); - - - if(n_length > 0) - { - list->list[list->length].length = n_length; - list->list[list->length].direction = n_direction; - list->list[list->length].end_pos = n_end_pos; - - list->length++; - } -} - - -/**********************for prefilter************************ */ - - void init_Cigar_record_alloc(Cigar_record_alloc* x) { x->length = 0; @@ -13936,3 +7396,133 @@ void destory_Cigar_record_alloc(Cigar_record_alloc* x) } free(x->buffer); } + + +void add_new_cell_to_cigar_record(Cigar_record* dummy, uint32_t len, uint32_t type) +{ + uint32_t tmp; + tmp = len; + tmp = tmp << 2; + tmp = tmp | type; + + dummy->length++; + + + if(dummy->length > dummy->size) + { + dummy->size = dummy->size * 2; + dummy->record = (uint32_t*)realloc(dummy->record, dummy->size*sizeof(uint32_t)); + } + + dummy->record[dummy->length - 1] = tmp; +} + +void add_existing_cell_to_cigar_record(Cigar_record* dummy, uint32_t len, uint32_t type) +{ + uint32_t tmp; + + tmp = dummy->record[dummy->length - 1] >> 2; + tmp = tmp + len; + tmp = tmp << 2; + tmp = tmp | type; + dummy->record[dummy->length - 1] = tmp; +} + + +void add_new_cell_to_cigar_record_with_different_base(Cigar_record* dummy, uint32_t len, uint32_t type, char* seq) +{ + uint32_t tmp; + tmp = len; + tmp = tmp << 2; + tmp = tmp | type; + + + dummy->length++; + + if(dummy->length > dummy->size) + { + dummy->size = dummy->size * 2; + dummy->record = (uint32_t*)realloc(dummy->record, dummy->size*sizeof(uint32_t)); + } + + dummy->record[dummy->length - 1] = tmp; + + + + if (dummy->lost_base_length + len> dummy->lost_base_size) + { + dummy->lost_base_size = (dummy->lost_base_length + len) * 2; + dummy->lost_base = (char*)realloc(dummy->lost_base, dummy->lost_base_size*sizeof(char)); + } + + uint32_t i = 0; + for (i = 0; i < len; i++, dummy->lost_base_length++) + { + dummy->lost_base[dummy->lost_base_length] = seq[i]; + } +} + +void add_existing_cell_to_cigar_record_with_different_base(Cigar_record* dummy, uint32_t len, uint32_t type, char* seq) +{ + uint32_t tmp; + + tmp = dummy->record[dummy->length - 1] >> 2; + tmp = tmp + len; + tmp = tmp << 2; + tmp = tmp | type; + dummy->record[dummy->length - 1] = tmp; + + if (dummy->lost_base_length + len> dummy->lost_base_size) + { + dummy->lost_base_size = (dummy->lost_base_length + len) * 2; + dummy->lost_base = (char*)realloc(dummy->lost_base, dummy->lost_base_size*sizeof(char)); + } + + uint32_t i = 0; + for (i = 0; i < len; i++, dummy->lost_base_length++) + { + dummy->lost_base[dummy->lost_base_length] = seq[i]; + } +} + + + +void afine_gap_alignment(const char *tseq, uint8_t* tnum, const int tl, +const char *qseq, uint8_t* qnum, const int ql, const uint8_t *c2n, const int strand, +int sc_mch, int sc_mis, int gapo, int gape, int bandLen, int zdrop, int end_bonus, +long long* max_t_pos, long long* max_q_pos, long long* score, long long* droped) +{ + /**************for ksw2**************/ + // (*max_t_pos) = (*max_q_pos) = -1; + // int i, a = sc_mch, b = sc_mis < 0? sc_mis : -sc_mis; // a>0 and b<0 + + // int8_t mat[25] = {(int8_t)a,(int8_t)b,(int8_t)b,(int8_t)b,0, + // (int8_t)b,(int8_t)a,(int8_t)b,(int8_t)b,0, (int8_t)b,(int8_t)b,(int8_t)a,(int8_t)b,0, + // (int8_t)b,(int8_t)b,(int8_t)b,(int8_t)a,0, 0,0,0,0,0}; + // ksw_extz_t ez; + // memset(&ez, 0, sizeof(ksw_extz_t)); + + // if(strand == FORWARD_KSW) + // { + // for (i = 0; i < tl; ++i) tnum[i] = c2n[(uint8_t)tseq[i]]; // encode to 0/1/2/3 + // for (i = 0; i < ql; ++i) qnum[i] = c2n[(uint8_t)qseq[i]]; + // } + // else if(strand == BACKWARD_KSW) + // { + // for (i = 0; i < tl; ++i) tnum[i] = c2n[(uint8_t)tseq[tl - i - 1]]; // encode to 0/1/2/3 + // for (i = 0; i < ql; ++i) qnum[i] = c2n[(uint8_t)qseq[ql - i - 1]]; + // } + // ksw_extz2_sse(0, ql, qnum, tl, tnum, 5, mat, gapo, gape, bandLen, zdrop, end_bonus, 0, &ez); + // (*score) = ez.max; + // (*max_t_pos) = ez.max_t; + // (*max_q_pos) = ez.max_q; + // (*droped) = ez.zdropped; + // free(ez.cigar); + + /** + for (i = 0; i < ez.n_cigar; ++i) // print CIGAR + printf("%d%c", ez.cigar[i]>>4, "MID"[ez.cigar[i]&0xf]); + putchar('\n'); + **/ + /**************for ksw2**************/ +} \ No newline at end of file diff --git a/Correct.h b/Correct.h index 8aff96e..791106a 100644 --- a/Correct.h +++ b/Correct.h @@ -16,12 +16,11 @@ #define INSERTION 2 #define DELETION 3 -#define MIN(x,y) ((x)<=(y)?(x):(y)) ///#define FLAG_THRE 0 -#define MAX(x, y) ((x >= y)?x:y) -#define MIN(x, y) ((x <= y)?x:y) +#define MAX(x, y) ((x >= y)?(x):(y)) +#define MIN(x, y) ((x <= y)?(x):(y)) #define DIFF(x, y) ((MAX((x), (y))) - (MIN((x), (y)))) #define OVERLAP(x_start, x_end, y_start, y_end) (MIN(x_end, y_end) - MAX(x_start, y_start) + 1) ///#define OVERLAP(x_start, x_end, y_start, y_end) MIN(x_end, y_end) - MAX(x_start, y_start) + 1 @@ -37,8 +36,6 @@ #define Adjust_Threshold(threshold, x_len) ((threshold == 0 && x_len >= 4)? 1: threshold) - - typedef struct { long long read_length; @@ -67,7 +64,6 @@ inline void init_Window_Pool(Window_Pool* dumy, long long read_length, long long dumy->window_num = (dumy->read_length + dumy->window_length - 1) / dumy->window_length; } - inline int get_Window(Window_Pool* dumy, long long* w_beg, long long* w_end) { (*w_beg) = dumy->window_start; @@ -92,28 +88,10 @@ inline int get_Window(Window_Pool* dumy, long long* w_beg, long long* w_end) { dumy->window_end = dumy->read_length - 1; } - // else if (dumy->read_length - dumy->window_end - 1 <= dumy->tail_length) - // { - // dumy->window_end = dumy->read_length - 1; - // } - - - /** - if((*w_end) - (*w_beg) + 1 < 375 && (*w_end) + 1 != dumy->read_length) - { - fprintf(stderr, "(*w_beg):%d, (*w_end): %d, dumy->read_length: %d\n", - (*w_beg), (*w_end), dumy->read_length); - } - **/ - - return 1; } - - - typedef struct { /**[0-1] bits are type:**/ @@ -326,45 +304,6 @@ inline int filter_one_snp(int occ_0, int occ_1, int total) return 1; } - -inline int filter_one_snp_advance_back(int occ_0, int occ_1, int total, int group_size, -long long homopolymer_num, long long non_homopolymer_num) -{ - - - double available; - - if(occ_0 <= occ_1) - { - available = occ_0; - } - else - { - available = occ_1; - } - int min = available; - - double threshold1 = 0.35; - double threshold2 = 0.24; - available = available/((double)(total)); - - - //if(non_homopolymer_num > 0 && min >= 5 && group_size > 1) - if(non_homopolymer_num > 0 && min >= 5) - { - if(available < threshold2 || total < 10) - { - return 0; - } - } - else if(available < threshold1 || occ_0 < MIN_COVERAGE_THRESHOLD + 1 || total < 10) - { - return 0; - } - return 1; -} - - inline void count_nearby_snps(haplotype_evdience_alloc* hap, uint32_t* SNPs, int SNPsLen, int* nearsnp, int* non_nearsnps) { long long i, current_id, large_id, small_id; @@ -502,96 +441,6 @@ uint32_t* SNPs, int SNPsLen) return 1; } -/** -inline int if_is_homopolymer(long long site, char* read, long long read_length) -{ - long long beg, end, i; - - beg = site - 10; - if(beg < 0) - { - beg = 0; - } - - end = site + 10; - - if(end >= read_length) - { - end = read_length - 1; - } - - char f_homopolymer_ch = 0; - long long f_homopolymer_len = 0; - - for (i = site + 1; i <= end; i++) - { - if(f_homopolymer_ch == 0) - { - f_homopolymer_ch = read[i]; - f_homopolymer_len = 1; - } - else - { - if(read[i] != f_homopolymer_ch) - { - break; - } - else - { - f_homopolymer_len++; - } - } - } - - char b_homopolymer_ch = 0; - long long b_homopolymer_len = 0; - - for (i = site - 1; i >= beg; i--) - { - if(b_homopolymer_ch == 0) - { - b_homopolymer_ch = read[i]; - b_homopolymer_len = 1; - } - else - { - if(read[i] != b_homopolymer_ch) - { - break; - } - else - { - b_homopolymer_len++; - } - } - } - - if(f_homopolymer_ch == read[site]) - { - f_homopolymer_len++; - } - else if(b_homopolymer_ch == read[site]) - { - b_homopolymer_len++; - } - - - if(f_homopolymer_len >= 5 || b_homopolymer_len >= 5) - { - return 1; - } - - if (b_homopolymer_ch == f_homopolymer_ch - && - (f_homopolymer_len + b_homopolymer_len >= 5)) - { - return 1; - } - - - return 0; -} -**/ inline int if_is_homopolymer_strict(long long site, char* read, long long read_length) { @@ -666,22 +515,6 @@ inline int if_is_homopolymer_strict(long long site, char* read, long long read_l b_homopolymer_len++; } - /** - fprintf(stderr, "site: %d, beg: %d, end: %d\n", site, beg, end); - for (i = beg; i <= end; i++) - { - if (i == site) - { - fprintf(stderr, "|%c|", read[i]); - } - else - { - fprintf(stderr, "%c", read[i]); - } - } - fprintf(stderr, "\n"); - **/ - if(f_homopolymer_len >= threshold || b_homopolymer_len >= threshold) { return 1; @@ -774,22 +607,6 @@ inline int if_is_homopolymer_repeat(long long site, char* read, long long read_l b_homopolymer_len++; } - /** - fprintf(stderr, "site: %d, beg: %d, end: %d\n", site, beg, end); - for (i = beg; i <= end; i++) - { - if (i == site) - { - fprintf(stderr, "|%c|", read[i]); - } - else - { - fprintf(stderr, "%c", read[i]); - } - } - fprintf(stderr, "\n"); - **/ - if(f_homopolymer_len >= threshold || b_homopolymer_len >= threshold) { return 1; @@ -825,8 +642,6 @@ UC_Read* g_read) h->snp_stat[h->available_snp].is_homopolymer = if_is_homopolymer_strict(h->snp_stat[h->available_snp].site, g_read->seq, g_read->length); - ///fprintf(stderr, "is_homopolymer: %d\n", h->snp_stat[h->available_snp].is_homopolymer); - int8_t* vector = Get_SNP_Vector((*h), h->available_snp); for (i = 0; i < sub_length; i++) { @@ -1053,7 +868,7 @@ inline void insert_SNP_IDs_addition(Snp_ID_Vector_Alloc* SNP_IDs, uint32_t* IDs_ } -inline void init_DP_matrix(DP_matrix* dp, int32_t snp_num) +inline void init_DP_matrix(DP_matrix* dp, uint32_t snp_num) { if(snp_num > dp->snp_size) @@ -1147,9 +962,6 @@ inline void destoryHaplotypeEvdience(haplotype_evdience_alloc* h) free(h->dp.buffer); free(h->dp.max_buffer); destory_SNP_IDs(&(h->dp.SNP_IDs)); - - - } inline void ResizeInitHaplotypeEvdience(haplotype_evdience_alloc* h) @@ -1237,115 +1049,22 @@ void clear_Round2_alignment(Round2_alignment* h); void correct_overlap(overlap_region_alloc* overlap_list, All_reads* R_INF, UC_Read* g_read, Correct_dumy* dumy, UC_Read* overlap_read, Graph* g, Graph* DAGCon, - long long* matched_overlap_0, long long* matched_overlap_1, - long long* potiental_matched_overlap_0, long long* potiental_matched_overlap_1, Cigar_record* current_cigar, haplotype_evdience_alloc* hap, Round2_alignment* second_round, int force_repeat, int is_consensus, - int* fully_cov, int* abnormal, uint8_t* c2n); + int* fully_cov, int* abnormal); void init_Correct_dumy(Correct_dumy* list); void destory_Correct_dumy(Correct_dumy* list); void clear_Correct_dumy(Correct_dumy* list, overlap_region_alloc* overlap_list); void clear_Correct_dumy_pure(Correct_dumy* list); -void pre_filter_by_nearby(k_mer_pos* new_n_list, k_mer_pos* old_n_list, uint64_t n_length, uint64_t n_end_pos, UC_Read* g_read, -All_reads* R_INF, Correct_dumy* dumy, uint64_t* new_n_length); -void pre_filter_by_nearby_single(k_mer_pos* new_n_list, k_mer_pos* old_n_list, uint64_t n_length, uint64_t n_end_pos, UC_Read* g_read, -All_reads* R_INF, Correct_dumy* dumy, uint64_t* new_n_length); void get_seq_from_Graph(Graph* backbone, Graph* DAGCon, Correct_dumy* dumy, Cigar_record* current_cigar, char* self_string, char* r_string, long long r_string_length, long long r_string_site); - void init_Cigar_record(Cigar_record* dummy); void destory_Cigar_record(Cigar_record* dummy); void clear_Cigar_record(Cigar_record* dummy); - - - - -inline void add_new_cell_to_cigar_record(Cigar_record* dummy, uint32_t len, uint32_t type) -{ - uint32_t tmp; - tmp = len; - tmp = tmp << 2; - tmp = tmp | type; - - dummy->length++; - - - if(dummy->length > dummy->size) - { - dummy->size = dummy->size * 2; - dummy->record = (uint32_t*)realloc(dummy->record, dummy->size*sizeof(uint32_t)); - } - - dummy->record[dummy->length - 1] = tmp; -} - -inline void add_existing_cell_to_cigar_record(Cigar_record* dummy, uint32_t len, uint32_t type) -{ - uint32_t tmp; - - tmp = dummy->record[dummy->length - 1] >> 2; - tmp = tmp + len; - tmp = tmp << 2; - tmp = tmp | type; - dummy->record[dummy->length - 1] = tmp; -} - - -inline void add_new_cell_to_cigar_record_with_different_base(Cigar_record* dummy, uint32_t len, uint32_t type, char* seq) -{ - uint32_t tmp; - tmp = len; - tmp = tmp << 2; - tmp = tmp | type; - - - dummy->length++; - - if(dummy->length > dummy->size) - { - dummy->size = dummy->size * 2; - dummy->record = (uint32_t*)realloc(dummy->record, dummy->size*sizeof(uint32_t)); - } - - dummy->record[dummy->length - 1] = tmp; - - - - if (dummy->lost_base_length + len> dummy->lost_base_size) - { - dummy->lost_base_size = (dummy->lost_base_length + len) * 2; - dummy->lost_base = (char*)realloc(dummy->lost_base, dummy->lost_base_size*sizeof(char)); - } - - int i = 0; - for (i = 0; i < len; i++, dummy->lost_base_length++) - { - dummy->lost_base[dummy->lost_base_length] = seq[i]; - } -} - -inline void add_existing_cell_to_cigar_record_with_different_base(Cigar_record* dummy, uint32_t len, uint32_t type, char* seq) -{ - uint32_t tmp; - - tmp = dummy->record[dummy->length - 1] >> 2; - tmp = tmp + len; - tmp = tmp << 2; - tmp = tmp | type; - dummy->record[dummy->length - 1] = tmp; - - if (dummy->lost_base_length + len> dummy->lost_base_size) - { - dummy->lost_base_size = (dummy->lost_base_length + len) * 2; - dummy->lost_base = (char*)realloc(dummy->lost_base, dummy->lost_base_size*sizeof(char)); - } - - int i = 0; - for (i = 0; i < len; i++, dummy->lost_base_length++) - { - dummy->lost_base[dummy->lost_base_length] = seq[i]; - } -} +void add_new_cell_to_cigar_record(Cigar_record* dummy, uint32_t len, uint32_t type); +void add_existing_cell_to_cigar_record(Cigar_record* dummy, uint32_t len, uint32_t type); +void add_new_cell_to_cigar_record_with_different_base(Cigar_record* dummy, uint32_t len, uint32_t type, char* seq); +void add_existing_cell_to_cigar_record_with_different_base(Cigar_record* dummy, uint32_t len, uint32_t type, char* seq); /*** @@ -1356,15 +1075,11 @@ inline void add_existing_cell_to_cigar_record_with_different_base(Cigar_record* 3. deletion ***/ inline void add_cigar_record(char* seq, uint32_t len, Cigar_record* dummy, uint32_t type) -{ - - - uint32_t tmp; - +{ if(type == 0)///match { ///add to existing cell, just increase length - if(dummy->current_operation == type) + if((uint32_t)dummy->current_operation == type) { add_existing_cell_to_cigar_record(dummy, len, type); } @@ -1379,7 +1094,7 @@ inline void add_cigar_record(char* seq, uint32_t len, Cigar_record* dummy, uint3 { ///add to existing cell, just increase length ///and add different bases - if(dummy->current_operation == type) + if((uint32_t)dummy->current_operation == type) { add_existing_cell_to_cigar_record_with_different_base(dummy, len, type, seq); } @@ -1394,7 +1109,7 @@ inline void add_cigar_record(char* seq, uint32_t len, Cigar_record* dummy, uint3 { ///add to existing cell, just increase length ///and add different bases - if(dummy->current_operation == type) + if((uint32_t)dummy->current_operation == type) { add_existing_cell_to_cigar_record_with_different_base(dummy, len, type, seq); } @@ -1418,7 +1133,7 @@ inline void add_cigar_record(char* seq, uint32_t len, Cigar_record* dummy, uint3 **/ ///add to existing cell, just increase length ///and add different bases - if(dummy->current_operation == type) + if((uint32_t)dummy->current_operation == type) { add_existing_cell_to_cigar_record_with_different_base(dummy, len, type, seq); } @@ -1431,21 +1146,10 @@ inline void add_cigar_record(char* seq, uint32_t len, Cigar_record* dummy, uint3 } dummy->current_operation = type; - - - - - - } int verify_cigar_2(char* x, int x_len, char* y, int y_len, Cigar_record* cigar, int error); -/**********************for prefilter************************ */ -void destory_k_mer_pos_list_alloc_prefilter(k_mer_pos_list_alloc* list); -void append_k_mer_pos_list_alloc_prefilter(k_mer_pos_list_alloc* list, k_mer_pos* n_list, uint64_t n_length, -uint64_t n_end_pos, uint8_t n_direction, UC_Read* g_read, All_reads* R_INF, Correct_dumy* dumy); -/**********************for prefilter************************ */ int verify_single_window(long long x_start, long long x_end, long long overlap_x_s, long long overlap_y_s, int x_id, diff --git a/Hash_Table.cpp b/Hash_Table.cpp index 8026afa..eb0c87a 100644 --- a/Hash_Table.cpp +++ b/Hash_Table.cpp @@ -39,128 +39,6 @@ void clear_Heap(HeapSq* HBT) -inline int cmp_ElemType_back_back(ElemType* x, ElemType* y) -{ - long long r_pos_x, r_pos_y; - if (x->node.strand < y->node.strand) - { - return 1; - } - else if (x->node.strand > y->node.strand) - { - return 2; - } - else - { - if (x->node.readID < y->node.readID) - { - return 1; - } - else if (x->node.readID > y->node.readID) - { - return 2; - } - else - { - - r_pos_x = x->node.offset - x->node.self_offset; - r_pos_y = y->node.offset - y->node.self_offset; - - ///if (x->node.offset < y->node.offset) - if (r_pos_x < r_pos_y) - { - return 1; - } - ///else if (x->node.offset > y->node.offset) - else if (r_pos_x > r_pos_y) - { - return 2; - } - else - { - if (x->node.self_offset < y->node.self_offset) - { - return 1; - } - else if (x->node.self_offset > y->node.self_offset) - { - return 2; - } - else ///如果r_pos_x和self_offset都相等,那么offset肯定也相等,也没必要再比了 - { - return 0; - } - - } - } - } - - - - -} - - -inline int cmp_ElemType_back(ElemType* x, ElemType* y) -{ - long long r_pos_x, r_pos_y; - - if (x->node.readID < y->node.readID) - { - return 1; - } - else if (x->node.readID > y->node.readID) - { - return 2; - } - else - { if (x->node.strand < y->node.strand) - { - return 1; - } - else if (x->node.strand > y->node.strand) - { - return 2; - } - else - { - - r_pos_x = x->node.offset - x->node.self_offset; - r_pos_y = y->node.offset - y->node.self_offset; - - ///if (x->node.offset < y->node.offset) - if (r_pos_x < r_pos_y) - { - return 1; - } - ///else if (x->node.offset > y->node.offset) - else if (r_pos_x > r_pos_y) - { - return 2; - } - else - { - if (x->node.self_offset < y->node.self_offset) - { - return 1; - } - else if (x->node.self_offset > y->node.self_offset) - { - return 2; - } - else ///如果r_pos_x和self_offset都相等,那么offset肯定也相等,也没必要再比了 - { - return 0; - } - - } - } - } -} - - - - inline int cmp_ElemType(ElemType* x, ElemType* y) { @@ -202,7 +80,7 @@ inline int cmp_ElemType(ElemType* x, ElemType* y) { return 2; } - else ///如果r_pos_x和self_offset都相等,那么offset肯定也相等,也没必要再比了 + else ///if both r_pos_x and self_offset are equal, offset must be equal { return 0; } @@ -216,29 +94,28 @@ inline int cmp_ElemType(ElemType* x, ElemType* y) inline void Insert_Heap(HeapSq* HBT, ElemType* x) { long long i, j; - if (HBT->len == HBT->MaxSize) //若堆满,将数组空间扩展为原来的2倍 + if (HBT->len == HBT->MaxSize) { HBT->MaxSize = 2*HBT->MaxSize; HBT->heap = (ElemType*)realloc(HBT->heap, HBT->MaxSize*sizeof(ElemType)); HBT->index_i = (uint64_t*)realloc(HBT->index_i,HBT->MaxSize*sizeof(uint64_t)); } - HBT->heap[HBT->len] = *x; //向堆尾添加新元素 - HBT->len++; //堆长度加1 - i = HBT->len - 1; //i指向待调整元素的位置,即其数组下标,初始指向新元素所在的堆尾位置 + HBT->heap[HBT->len] = *x; //add element to tail + HBT->len++; + i = HBT->len - 1; while (i != 0) { - j = (i - 1) / 2; //j指向下标为i的元素的双亲 - ///if (x >= HBT->heap[j]) //若新元素大于待调整元素的双亲,则比较调整结束,退出循环 + j = (i - 1) / 2; + ///if (x >= HBT->heap[j]) ///1: xheap[j])!=1) break; - HBT->heap[i] = HBT->heap[j]; //将双亲元素下移到待调整元素的位置 - i = j; //使待调整位置变为其双亲位置,进行下一次循环 + HBT->heap[i] = HBT->heap[j]; + i = j; } - HBT->heap[i] = *x;//把新元素调整到最终位置 + HBT->heap[i] = *x; } - inline int DeleteHeap(HeapSq* HBT, ElemType* get) { ElemType temp, x; @@ -247,47 +124,42 @@ inline int DeleteHeap(HeapSq* HBT, ElemType* get) { return 0; } - temp = HBT->heap[0]; //暂存堆顶元素 + temp = HBT->heap[0]; HBT->len--; - if (HBT->len == 0) //若删除操作后堆为空则返回 + if (HBT->len == 0) { *get = temp; return 2; } - x = HBT->heap[HBT->len]; //将待调整的原堆尾元素暂存x中,以便放入最终位置 - i = 0; //用i指向待调整元素的位置,初始指向堆顶位置 - j = 2 * i + 1;//用j指向i的左孩子位置,初始指向下标为1的位置 - while (j <= HBT->len - 1)//寻找待调整元素的最终位置,每次使孩子元素上移一层,调整到孩子为空时止 + x = HBT->heap[HBT->len]; + i = 0; + j = 2 * i + 1; + while (j <= HBT->len - 1) { - ///if (j < HBT->len - 1 && HBT->heap[j] > HBT->heap[j+1])//若存在右孩子且较小,使j指向右孩子 + ///if (j < HBT->len - 1 && HBT->heap[j] > HBT->heap[j+1]) if (j < HBT->len - 1 && cmp_ElemType(&HBT->heap[j], &HBT->heap[j + 1]) == 2) j++; - ///if (x <= HBT->heap[j]) //若x比其较小的孩子还小,则调整结束,退出循环 + ///if (x <= HBT->heap[j]) if (cmp_ElemType(&x, &HBT->heap[j])!=2) break; - HBT->heap[i] = HBT->heap[j];//否则,将孩子元素移到双亲位置 - i = j; //将待调整位置变为其较小的孩子位置 - j = 2 * i + 1;//将j变为新的待调整位置的左孩子位置,继续下一次循环 + HBT->heap[i] = HBT->heap[j]; + i = j; + j = 2 * i + 1; } - HBT->heap[i] = x; //把x放到最终位置 + HBT->heap[i] = x; - //返回原堆顶元素 *get = temp; return 1; } - - - - void init_overlap_region_alloc(overlap_region_alloc* list) { list->size = 1000; list->length = 0; ///list->list = (overlap_region*)malloc(sizeof(overlap_region)*list->size); list->list = (overlap_region*)calloc(list->size, sizeof(overlap_region)); - long long i; + uint64_t i; for (i = 0; i < list->size; i++) { init_fake_cigar(&(list->list[i].f_cigar)); @@ -298,7 +170,7 @@ void clear_overlap_region_alloc(overlap_region_alloc* list) { list->length = 0; list->mapped_overlaps_length = 0; - int i = 0; + uint64_t i = 0; for (i = 0; i < list->size; i++) { list->list[i].w_list_length = 0; @@ -309,7 +181,7 @@ void clear_overlap_region_alloc(overlap_region_alloc* list) void destory_overlap_region_alloc(overlap_region_alloc* list) { - int i = 0; + uint64_t i = 0; for (i = 0; i < list->size; i++) { if (list->list[i].w_list_size != 0) @@ -349,511 +221,6 @@ int get_fake_gap_shift(Fake_Cigar* x, int index) - -///r->length = Get_READ_LENGTH((*R_INF), ID); -void append_overlap_region_alloc(overlap_region_alloc* list, overlap_region* tmp, All_reads* R_INF) -{ - - if (list->length + 1 > list->size) - { - list->size = list->size * 2; - list->list = (overlap_region*)realloc(list->list, sizeof(overlap_region)*list->size); - ///新分配空间要初始化 - memset(list->list + (list->size/2), 0, sizeof(overlap_region)*(list->size/2)); - } - - if (list->length!=0 && - list->list[list->length - 1].y_id==tmp->y_id - ) - { - if(list->list[list->length - 1].shared_seed >= tmp->shared_seed) - { - return; - } - else - { - list->length--; - } - - - } - - - - - - - - - if(tmp->x_pos_s <= tmp->y_pos_s) - { - tmp->y_pos_s = tmp->y_pos_s - tmp->x_pos_s; - tmp->x_pos_s = 0; - } - else - { - tmp->x_pos_s = tmp->x_pos_s - tmp->y_pos_s; - tmp->y_pos_s = 0; - } - - tmp->x_pos_e = Get_READ_LENGTH((*R_INF), tmp->x_id) - tmp->x_pos_s - 1; - tmp->y_pos_e = Get_READ_LENGTH((*R_INF), tmp->y_id) - tmp->y_pos_s - 1; - - if(tmp->x_pos_e <= tmp->y_pos_e) - { - tmp->y_pos_e = tmp->y_pos_s + tmp->x_pos_e; - tmp->x_pos_e = tmp->x_pos_s + tmp->x_pos_e; - } - else - { - tmp->x_pos_e = tmp->x_pos_s + tmp->y_pos_e; - tmp->y_pos_e = tmp->y_pos_s + tmp->y_pos_e; - } - - - - - - ///y的区间方向永远是0 - ///x有可能是1 - ///如果x是1,那我们把它逆过来 - if (tmp->x_pos_strand == 1) - { - list->list[list->length].x_id = tmp->x_id; - list->list[list->length].x_pos_e = Get_READ_LENGTH((*R_INF), tmp->x_id) - tmp->x_pos_s - 1; - list->list[list->length].x_pos_s = Get_READ_LENGTH((*R_INF), tmp->x_id) - tmp->x_pos_e - 1; - list->list[list->length].x_pos_strand = 0; - - list->list[list->length].y_id = tmp->y_id; - list->list[list->length].y_pos_e = Get_READ_LENGTH((*R_INF), tmp->y_id) - tmp->y_pos_s - 1; - list->list[list->length].y_pos_s = Get_READ_LENGTH((*R_INF), tmp->y_id) - tmp->y_pos_e - 1; - list->list[list->length].y_pos_strand = 1; - - - - - // resize_fake_cigar(&(list->list[list->length].f_cigar), (tmp->f_cigar.length + 2)); - // add_fake_cigar(&(list->list[list->length].f_cigar), list->list[list->length].x_pos_s, 0); - // long long distance_gap; - // long long pre_distance_gap = 0; - // long long i = 0; - // for (i = 0; i < tmp->f_cigar.length; i++) - // { - // distance_gap = get_fake_gap_shift(&(tmp->f_cigar), i); - // if(distance_gap != pre_distance_gap) - // { - // pre_distance_gap = distance_gap; - // add_fake_cigar(&(list->list[list->length].f_cigar), - // Get_READ_LENGTH((*R_INF), tmp->x_id) - get_fake_gap_pos(&(tmp->f_cigar), i) - 1, - // pre_distance_gap); - // } - // } - - // if(get_fake_gap_pos(&(list->list[list->length].f_cigar), - // list->list[list->length].f_cigar.length - 1) != list->list[list->length].x_pos_e) - // { - // add_fake_cigar(&(list->list[list->length].f_cigar), - // list->list[list->length].x_pos_e, - // get_fake_gap_shift(&(list->list[list->length].f_cigar), - // list->list[list->length].f_cigar.length - 1)); - // } - } - else - { - list->list[list->length].x_id = tmp->x_id; - list->list[list->length].x_pos_e = tmp->x_pos_e; - list->list[list->length].x_pos_s = tmp->x_pos_s; - list->list[list->length].x_pos_strand = tmp->x_pos_strand; - - list->list[list->length].y_id = tmp->y_id; - list->list[list->length].y_pos_e = tmp->y_pos_e; - list->list[list->length].y_pos_s = tmp->y_pos_s; - list->list[list->length].y_pos_strand = tmp->y_pos_strand; - - - - - // resize_fake_cigar(&(list->list[list->length].f_cigar), (tmp->f_cigar.length + 2)); - // add_fake_cigar(&(list->list[list->length].f_cigar), list->list[list->length].x_pos_s, 0); - // long long distance_self_pos = tmp->x_pos_e - tmp->x_pos_s; - // long long distance_pos = tmp->y_pos_e - tmp->y_pos_s; - // long long init_distance_gap = distance_pos - distance_self_pos; - // long long pre_distance_gap = init_distance_gap; - // long long distance_gap; - // long long i = 0; - // for (i = tmp->f_cigar.length - 1; i >= 0; i--) - // { - // distance_gap = get_fake_gap_shift(&(tmp->f_cigar), i); - // if(distance_gap != pre_distance_gap) - // { - // pre_distance_gap = distance_gap; - - // add_fake_cigar(&(list->list[list->length].f_cigar), - // get_fake_gap_pos(&(tmp->f_cigar), i), init_distance_gap - pre_distance_gap); - // } - // } - - // if(get_fake_gap_pos(&(list->list[list->length].f_cigar), - // list->list[list->length].f_cigar.length - 1) != list->list[list->length].x_pos_e) - // { - // add_fake_cigar(&(list->list[list->length].f_cigar), - // list->list[list->length].x_pos_e, - // get_fake_gap_shift(&(list->list[list->length].f_cigar), - // list->list[list->length].f_cigar.length - 1)); - // } - - - - /******************************for debug********************************/ - // distance_self_pos = tmp->x_pos_e - tmp->x_pos_s; - // distance_pos = tmp->y_pos_e - tmp->y_pos_s; - // distance_gap = distance_pos - distance_self_pos; - // fprintf(stderr, "\nx_s: %d, x_e: %d, y_s: %d, y_e: %d, distance_gap: %d\n", - // list->list[list->length].x_pos_s, list->list[list->length].x_pos_e, - // list->list[list->length].y_pos_s, list->list[list->length].y_pos_e, distance_gap); - // for (i = 0; i < list->list[list->length].f_cigar.length; i++) - // { - // fprintf(stderr, "##i: %d, gap_pos_in_x: %d, gap_shift: %d\n", - // i, get_fake_gap_pos(&(list->list[list->length].f_cigar), i), - // get_fake_gap_shift(&(list->list[list->length].f_cigar), i)); - // } - // for (i = 0; i < tmp->f_cigar.length; i++) - // { - // fprintf(stderr, "**i: %d, gap_pos_in_x: %d, gap_shift: %d\n", - // i, get_fake_gap_pos(&(tmp->f_cigar), i), - // get_fake_gap_shift(&(tmp->f_cigar), i)); - // } - /******************************for debug********************************/ - - - - } - - - - list->list[list->length].shared_seed = tmp->shared_seed; - list->list[list->length].align_length = 0; - list->list[list->length].is_match = 0; - list->list[list->length].non_homopolymer_errors = 0; - - list->length++; -} - -void append_overlap_region_alloc_from_existing(overlap_region_alloc* list, overlap_region* tmp, All_reads* R_INF) -{ - - if (list->length + 1 > list->size) - { - list->size = list->size * 2; - list->list = (overlap_region*)realloc(list->list, sizeof(overlap_region)*list->size); - ///新分配空间要初始化 - memset(list->list + (list->size/2), 0, sizeof(overlap_region)*(list->size/2)); - } - - if (list->length!=0 && - list->list[list->length - 1].y_id==tmp->y_id - ) - { - if(list->list[list->length - 1].shared_seed >= tmp->shared_seed) - { - return; - } - else - { - list->length--; - } - - - } - - - - if(tmp->x_pos_s <= tmp->y_pos_s) - { - tmp->y_pos_s = tmp->y_pos_s - tmp->x_pos_s; - tmp->x_pos_s = 0; - } - else - { - tmp->x_pos_s = tmp->x_pos_s - tmp->y_pos_s; - tmp->y_pos_s = 0; - } - - - - tmp->x_pos_e = Get_READ_LENGTH((*R_INF), tmp->x_id) - tmp->x_pos_s - 1; - tmp->y_pos_e = Get_READ_LENGTH((*R_INF), tmp->y_id) - tmp->y_pos_s - 1; - - - if(tmp->x_pos_e <= tmp->y_pos_e) - { - tmp->y_pos_e = tmp->y_pos_s + tmp->x_pos_e; - tmp->x_pos_e = tmp->x_pos_s + tmp->x_pos_e; - } - else - { - tmp->x_pos_e = tmp->x_pos_s + tmp->y_pos_e; - tmp->y_pos_e = tmp->y_pos_s + tmp->y_pos_e; - } - - - - - - // if(tmp->x_pos_strand != 0) - // { - // fprintf(stderr, "error\n"); - // } - - list->list[list->length].x_id = tmp->x_id; - list->list[list->length].x_pos_e = tmp->x_pos_e; - list->list[list->length].x_pos_s = tmp->x_pos_s; - list->list[list->length].x_pos_strand = tmp->x_pos_strand; - - list->list[list->length].y_id = tmp->y_id; - list->list[list->length].y_pos_e = tmp->y_pos_e; - list->list[list->length].y_pos_s = tmp->y_pos_s; - list->list[list->length].y_pos_strand = tmp->y_pos_strand; - - - - list->list[list->length].shared_seed = tmp->shared_seed; - list->list[list->length].align_length = 0; - list->list[list->length].is_match = 0; - list->list[list->length].non_homopolymer_errors = 0; - - list->length++; - - - // fprintf(stderr, "shared_seed: %d, x_pos_s: %d, x_pos_e: %d, y_pos_s: %d, y_pos_e: %d, list->length: %d\n", - // tmp->shared_seed, tmp->x_pos_s, tmp->x_pos_e, tmp->y_pos_s, tmp->y_pos_e, list->length); - - // fprintf(stderr, "*****x_pos_s: %d, x_pos_e: %d, y_pos_s: %d, y_pos_e: %d, list->length: %d\n", - // list->list[list->length - 1].x_pos_s, list->list[list->length - 1].x_pos_e, - // list->list[list->length - 1].y_pos_s, list->list[list->length - 1].y_pos_e, - // list->length); -} - - -int append_inexact_overlap_region_alloc_back(overlap_region_alloc* list, overlap_region* tmp, All_reads* R_INF) -{ - - if (list->length + 1 > list->size) - { - list->size = list->size * 2; - list->list = (overlap_region*)realloc(list->list, sizeof(overlap_region)*list->size); - ///新分配空间要初始化 - memset(list->list + (list->size/2), 0, sizeof(overlap_region)*(list->size/2)); - } - - if (list->length!=0 && - list->list[list->length - 1].y_id==tmp->y_id - ) - { - if(list->list[list->length - 1].shared_seed >= tmp->shared_seed) - { - return 0; - } - else - { - list->length--; - } - } - - if(tmp->x_pos_s <= tmp->y_pos_s) - { - tmp->y_pos_s = tmp->y_pos_s - tmp->x_pos_s; - tmp->x_pos_s = 0; - } - else - { - tmp->x_pos_s = tmp->x_pos_s - tmp->y_pos_s; - tmp->y_pos_s = 0; - } - - - long long x_right_length = Get_READ_LENGTH((*R_INF), tmp->x_id) - tmp->x_pos_e - 1; - long long y_right_length = Get_READ_LENGTH((*R_INF), tmp->y_id) - tmp->y_pos_e - 1; - - - - if(x_right_length <= y_right_length) - { - tmp->x_pos_e = Get_READ_LENGTH((*R_INF), tmp->x_id) - 1; - tmp->y_pos_e = tmp->y_pos_e + x_right_length; - } - else - { - tmp->x_pos_e = tmp->x_pos_e + y_right_length; - tmp->y_pos_e = Get_READ_LENGTH((*R_INF), tmp->y_id) - 1; - } - - - if (tmp->x_pos_strand == 1) - { - list->list[list->length].x_id = tmp->x_id; - list->list[list->length].x_pos_e = Get_READ_LENGTH((*R_INF), tmp->x_id) - tmp->x_pos_s - 1; - list->list[list->length].x_pos_s = Get_READ_LENGTH((*R_INF), tmp->x_id) - tmp->x_pos_e - 1; - list->list[list->length].x_pos_strand = 0; - - list->list[list->length].y_id = tmp->y_id; - list->list[list->length].y_pos_e = Get_READ_LENGTH((*R_INF), tmp->y_id) - tmp->y_pos_s - 1; - list->list[list->length].y_pos_s = Get_READ_LENGTH((*R_INF), tmp->y_id) - tmp->y_pos_e - 1; - list->list[list->length].y_pos_strand = 1; - - - - - resize_fake_cigar(&(list->list[list->length].f_cigar), (tmp->f_cigar.length + 2)); - add_fake_cigar(&(list->list[list->length].f_cigar), list->list[list->length].x_pos_s, 0); - long long distance_gap; - long long pre_distance_gap = 0; - long long i = 0; - for (i = 0; i < tmp->f_cigar.length; i++) - { - distance_gap = get_fake_gap_shift(&(tmp->f_cigar), i); - if(distance_gap != pre_distance_gap) - { - pre_distance_gap = distance_gap; - add_fake_cigar(&(list->list[list->length].f_cigar), - Get_READ_LENGTH((*R_INF), tmp->x_id) - get_fake_gap_pos(&(tmp->f_cigar), i) - 1, - pre_distance_gap); - } - } - - if(get_fake_gap_pos(&(list->list[list->length].f_cigar), - list->list[list->length].f_cigar.length - 1) != list->list[list->length].x_pos_e) - { - add_fake_cigar(&(list->list[list->length].f_cigar), - list->list[list->length].x_pos_e, - get_fake_gap_shift(&(list->list[list->length].f_cigar), - list->list[list->length].f_cigar.length - 1)); - } - - - /******************************for debug********************************/ - // long long distance_self_pos = tmp->x_pos_e - tmp->x_pos_s; - // long long distance_pos = tmp->y_pos_e - tmp->y_pos_s; - // distance_gap = distance_pos - distance_self_pos; - - // if(distance_gap != - // get_fake_gap_shift(&(list->list[list->length].f_cigar), - // list->list[list->length].f_cigar.length - 1)) - // { - // fprintf(stderr, "error\n"); - // } - // long long distance_self_pos = tmp->x_pos_e - tmp->x_pos_s; - // long long distance_pos = tmp->y_pos_e - tmp->y_pos_s; - // distance_gap = distance_pos - distance_self_pos; - // fprintf(stderr, "\nx_s: %d, x_e: %d, y_s: %d, y_e: %d, distance_gap: %d, xLen: %d\n", - // list->list[list->length].x_pos_s, list->list[list->length].x_pos_e, - // list->list[list->length].y_pos_s, list->list[list->length].y_pos_e, distance_gap, - // Get_READ_LENGTH((*R_INF), tmp->x_id)); - // for (i = 0; i < list->list[list->length].f_cigar.length; i++) - // { - // fprintf(stderr, "##i: %d, gap_pos_in_x: %d, gap_shift: %d\n", - // i, get_fake_gap_pos(&(list->list[list->length].f_cigar), i), - // get_fake_gap_shift(&(list->list[list->length].f_cigar), i)); - // } - // for (i = 0; i < tmp->f_cigar.length; i++) - // { - // fprintf(stderr, "**i: %d, gap_pos_in_x: %d, gap_shift: %d\n", - // i, get_fake_gap_pos(&(tmp->f_cigar), i), - // get_fake_gap_shift(&(tmp->f_cigar), i)); - // } - /******************************for debug********************************/ - } - else - { - list->list[list->length].x_id = tmp->x_id; - list->list[list->length].x_pos_e = tmp->x_pos_e; - list->list[list->length].x_pos_s = tmp->x_pos_s; - list->list[list->length].x_pos_strand = tmp->x_pos_strand; - - list->list[list->length].y_id = tmp->y_id; - list->list[list->length].y_pos_e = tmp->y_pos_e; - list->list[list->length].y_pos_s = tmp->y_pos_s; - list->list[list->length].y_pos_strand = tmp->y_pos_strand; - - - - resize_fake_cigar(&(list->list[list->length].f_cigar), (tmp->f_cigar.length + 2)); - add_fake_cigar(&(list->list[list->length].f_cigar), list->list[list->length].x_pos_s, 0); - long long distance_self_pos = tmp->x_pos_e - tmp->x_pos_s; - long long distance_pos = tmp->y_pos_e - tmp->y_pos_s; - long long init_distance_gap = distance_pos - distance_self_pos; - long long pre_distance_gap = init_distance_gap; - long long distance_gap; - long long i = 0; - for (i = tmp->f_cigar.length - 1; i >= 0; i--) - { - distance_gap = get_fake_gap_shift(&(tmp->f_cigar), i); - if(distance_gap != pre_distance_gap) - { - pre_distance_gap = distance_gap; - - add_fake_cigar(&(list->list[list->length].f_cigar), - get_fake_gap_pos(&(tmp->f_cigar), i), init_distance_gap - pre_distance_gap); - } - } - - if(get_fake_gap_pos(&(list->list[list->length].f_cigar), - list->list[list->length].f_cigar.length - 1) != list->list[list->length].x_pos_e) - { - add_fake_cigar(&(list->list[list->length].f_cigar), - list->list[list->length].x_pos_e, - get_fake_gap_shift(&(list->list[list->length].f_cigar), - list->list[list->length].f_cigar.length - 1)); - } - - - - - /******************************for debug********************************/ - // distance_self_pos = tmp->x_pos_e - tmp->x_pos_s; - // distance_pos = tmp->y_pos_e - tmp->y_pos_s; - // distance_gap = distance_pos - distance_self_pos; - - // if(distance_gap != - // get_fake_gap_shift(&(list->list[list->length].f_cigar), - // list->list[list->length].f_cigar.length - 1)) - // { - // fprintf(stderr, "error\n"); - // } - // distance_self_pos = tmp->x_pos_e - tmp->x_pos_s; - // distance_pos = tmp->y_pos_e - tmp->y_pos_s; - // distance_gap = distance_pos - distance_self_pos; - // fprintf(stderr, "\nx_s: %d, x_e: %d, y_s: %d, y_e: %d, distance_gap: %d\n", - // list->list[list->length].x_pos_s, list->list[list->length].x_pos_e, - // list->list[list->length].y_pos_s, list->list[list->length].y_pos_e, distance_gap); - // for (i = 0; i < list->list[list->length].f_cigar.length; i++) - // { - // fprintf(stderr, "##i: %d, gap_pos_in_x: %d, gap_shift: %d\n", - // i, get_fake_gap_pos(&(list->list[list->length].f_cigar), i), - // get_fake_gap_shift(&(list->list[list->length].f_cigar), i)); - // } - // for (i = 0; i < tmp->f_cigar.length; i++) - // { - // fprintf(stderr, "**i: %d, gap_pos_in_x: %d, gap_shift: %d\n", - // i, get_fake_gap_pos(&(tmp->f_cigar), i), - // get_fake_gap_shift(&(tmp->f_cigar), i)); - // } - /******************************for debug********************************/ - } - - - list->list[list->length].shared_seed = tmp->shared_seed; - list->list[list->length].align_length = 0; - list->list[list->length].is_match = 0; - list->list[list->length].non_homopolymer_errors = 0; - list->list[list->length].strong = 0; - - list->length++; - - return 1; -} - - int append_inexact_overlap_region_alloc(overlap_region_alloc* list, overlap_region* tmp, All_reads* R_INF, int add_beg_end) { @@ -862,7 +229,7 @@ All_reads* R_INF, int add_beg_end) { list->size = list->size * 2; list->list = (overlap_region*)realloc(list->list, sizeof(overlap_region)*list->size); - ///新分配空间要初始化 + /// need to set new space to be 0 memset(list->list + (list->size/2), 0, sizeof(overlap_region)*(list->size/2)); } @@ -938,7 +305,7 @@ All_reads* R_INF, int add_beg_end) long long pre_distance_gap = 0xfffffffffffffff; /****************************may have bugs********************************/ long long i = 0; - for (i = 0; i < tmp->f_cigar.length; i++) + for (i = 0; i < (long long)tmp->f_cigar.length; i++) { distance_gap = get_fake_gap_shift(&(tmp->f_cigar), i); if(distance_gap != pre_distance_gap) @@ -951,46 +318,13 @@ All_reads* R_INF, int add_beg_end) } if(add_beg_end == 1 && get_fake_gap_pos(&(list->list[list->length].f_cigar), - list->list[list->length].f_cigar.length - 1) != list->list[list->length].x_pos_e) + list->list[list->length].f_cigar.length - 1) != (long long)list->list[list->length].x_pos_e) { add_fake_cigar(&(list->list[list->length].f_cigar), list->list[list->length].x_pos_e, get_fake_gap_shift(&(list->list[list->length].f_cigar), list->list[list->length].f_cigar.length - 1)); } - - - /******************************for debug********************************/ - // long long distance_self_pos = tmp->x_pos_e - tmp->x_pos_s; - // long long distance_pos = tmp->y_pos_e - tmp->y_pos_s; - // distance_gap = distance_pos - distance_self_pos; - - // if(distance_gap != - // get_fake_gap_shift(&(list->list[list->length].f_cigar), - // list->list[list->length].f_cigar.length - 1)) - // { - // fprintf(stderr, "error\n"); - // } - // long long distance_self_pos = tmp->x_pos_e - tmp->x_pos_s; - // long long distance_pos = tmp->y_pos_e - tmp->y_pos_s; - // distance_gap = distance_pos - distance_self_pos; - // fprintf(stderr, "\nx_s: %d, x_e: %d, y_s: %d, y_e: %d, distance_gap: %d, xLen: %d\n", - // list->list[list->length].x_pos_s, list->list[list->length].x_pos_e, - // list->list[list->length].y_pos_s, list->list[list->length].y_pos_e, distance_gap, - // Get_READ_LENGTH((*R_INF), tmp->x_id)); - // for (i = 0; i < list->list[list->length].f_cigar.length; i++) - // { - // fprintf(stderr, "##i: %d, gap_pos_in_x: %d, gap_shift: %d\n", - // i, get_fake_gap_pos(&(list->list[list->length].f_cigar), i), - // get_fake_gap_shift(&(list->list[list->length].f_cigar), i)); - // } - // for (i = 0; i < tmp->f_cigar.length; i++) - // { - // fprintf(stderr, "**i: %d, gap_pos_in_x: %d, gap_shift: %d\n", - // i, get_fake_gap_pos(&(tmp->f_cigar), i), - // get_fake_gap_shift(&(tmp->f_cigar), i)); - // } - /******************************for debug********************************/ } else { @@ -1034,63 +368,15 @@ All_reads* R_INF, int add_beg_end) } if(add_beg_end == 1 && get_fake_gap_pos(&(list->list[list->length].f_cigar), - list->list[list->length].f_cigar.length - 1) != list->list[list->length].x_pos_e) + list->list[list->length].f_cigar.length - 1) != (long long)list->list[list->length].x_pos_e) { add_fake_cigar(&(list->list[list->length].f_cigar), list->list[list->length].x_pos_e, get_fake_gap_shift(&(list->list[list->length].f_cigar), list->list[list->length].f_cigar.length - 1)); } - - - - - /******************************for debug********************************/ - // distance_self_pos = tmp->x_pos_e - tmp->x_pos_s; - // distance_pos = tmp->y_pos_e - tmp->y_pos_s; - // distance_gap = distance_pos - distance_self_pos; - - // if(distance_gap != - // get_fake_gap_shift(&(list->list[list->length].f_cigar), - // list->list[list->length].f_cigar.length - 1)) - // { - // fprintf(stderr, "error\n"); - // } - // distance_self_pos = tmp->x_pos_e - tmp->x_pos_s; - // distance_pos = tmp->y_pos_e - tmp->y_pos_s; - // distance_gap = distance_pos - distance_self_pos; - // fprintf(stderr, "\nx_s: %d, x_e: %d, y_s: %d, y_e: %d, distance_gap: %d\n", - // list->list[list->length].x_pos_s, list->list[list->length].x_pos_e, - // list->list[list->length].y_pos_s, list->list[list->length].y_pos_e, distance_gap); - // for (i = 0; i < list->list[list->length].f_cigar.length; i++) - // { - // fprintf(stderr, "##i: %d, gap_pos_in_x: %d, gap_shift: %d\n", - // i, get_fake_gap_pos(&(list->list[list->length].f_cigar), i), - // get_fake_gap_shift(&(list->list[list->length].f_cigar), i)); - // } - // for (i = 0; i < tmp->f_cigar.length; i++) - // { - // fprintf(stderr, "**i: %d, gap_pos_in_x: %d, gap_shift: %d\n", - // i, get_fake_gap_pos(&(tmp->f_cigar), i), - // get_fake_gap_shift(&(tmp->f_cigar), i)); - // } - /******************************for debug********************************/ } - // if(list->list[list->length].f_cigar.length < 3 && tmp->f_cigar.length != 1) - // { - // fprintf(stderr, "\n original cigar:\n"); - // print_fake_gap(&tmp->f_cigar); - // fprintf(stderr, "new cigar:\n"); - // print_fake_gap(&list->list[list->length].f_cigar); - // fprintf(stderr, "xs: %d, xe: %d, strand: %d, xLen: %d\n", - // list->list[list->length].x_pos_s, - // list->list[list->length].x_pos_e, - // tmp->x_pos_strand, - // Get_READ_LENGTH((*R_INF), tmp->x_id)); - // } - - list->list[list->length].shared_seed = tmp->shared_seed; list->list[list->length].align_length = 0; list->list[list->length].is_match = 0; @@ -1103,8 +389,6 @@ All_reads* R_INF, int add_beg_end) } - - void append_overlap_region_alloc_debug(overlap_region_alloc* list, overlap_region* tmp) { @@ -1131,142 +415,6 @@ void append_overlap_region_alloc_debug(overlap_region_alloc* list, overlap_regio } -void debug_overlap_region(Candidates_list* candidates, overlap_region_alloc* overlap_list, uint64_t readID) -{ - uint64_t i = 0; - uint64_t total_length = 0; - uint64_t pre_total_length = 0; - uint64_t current_ID; - uint64_t current_stand; - long long current_pos_diff; - long long current_self_pos; - long long tmp_pos_distance; - long long tmp_self_pos_distance; - long long constant_distance = 5; - double error_rate = 0.05; - - - - for ( i = 0; i < overlap_list->length; i++) - { - pre_total_length = total_length; - total_length = total_length + overlap_list->list[i].shared_seed; - - uint64_t j = 0; - current_ID = candidates->list[pre_total_length].readID; - current_stand = candidates->list[pre_total_length].strand; - current_pos_diff = candidates->list[pre_total_length].offset - candidates->list[pre_total_length].self_offset; - current_self_pos = candidates->list[pre_total_length].self_offset; - for (j = pre_total_length; j < total_length; j++) - { - if(current_ID != candidates->list[j].readID || - current_stand != candidates->list[j].strand) - { - fprintf(stderr, "ERROR Overlap 1!\n"); - } - - - ///这个一定是正值 - tmp_pos_distance = candidates->list[j].offset - candidates->list[j].self_offset - current_pos_diff; - ///这个不一定是正值 - tmp_self_pos_distance = candidates->list[j].self_offset - current_self_pos; - if (tmp_self_pos_distance < 0) - { - fprintf(stderr, "ERROR Overlap 3!\n"); - } - - if (tmp_self_pos_distance >= 0) - { - tmp_self_pos_distance = tmp_self_pos_distance * error_rate + constant_distance; - if (tmp_pos_distance >= tmp_self_pos_distance) - { - fprintf(stderr, "ERROR Overlap 4!\n"); - } - } - - - } - - if (total_length < candidates->length) - { - j = total_length; - - if (current_ID == candidates->list[j].readID && - current_stand == candidates->list[j].strand) - { - ///这个一定是正值 - tmp_pos_distance = candidates->list[j].offset - candidates->list[j].self_offset - current_pos_diff; - ///这个不一定是正值 - tmp_self_pos_distance = candidates->list[j].self_offset - current_self_pos; - if (tmp_self_pos_distance >= 0) - { - tmp_self_pos_distance = tmp_self_pos_distance * error_rate + constant_distance; - if (tmp_pos_distance < tmp_self_pos_distance) - { - fprintf(stderr, "ERROR Overlap 5!\n"); - } - } - } - } - - - } - - if (total_length != candidates->length) - { - fprintf(stderr, "ERROR Overlap 2!\n"); - fprintf(stderr, "total_length: %llu\n", total_length); - fprintf(stderr, "candidates->length: %llu\n", candidates->length); - } -} - - - -void print_overlap_region(Candidates_list* candidates, overlap_region_alloc* overlap_list, All_reads* R_INF) -{ - - pthread_mutex_lock(&output_mutex); - - - uint64_t i = 0; - for ( i = 0; i < overlap_list->length; i++) - { - fprintf(stderr, "************i: %llu***********\n", i); - fprintf(stderr, "x: %llu, %llu, %llu, %llu, %llu\n", - overlap_list->list[i].x_id,overlap_list->list[i].x_pos_s, - overlap_list->list[i].x_pos_e, overlap_list->list[i].x_pos_strand, - Get_READ_LENGTH((*R_INF), overlap_list->list[i].x_id) - ); - - fprintf(stderr, "y: %llu, %llu, %llu, %llu, %llu\n", - overlap_list->list[i].y_id,overlap_list->list[i].y_pos_s, - overlap_list->list[i].y_pos_e, overlap_list->list[i].y_pos_strand, - Get_READ_LENGTH((*R_INF), overlap_list->list[i].y_id) - ); - - } - - - - - fprintf(stderr, "#######################\nLength: %llu\n", candidates->length); - - - for (i = 0; i < candidates->length; i++) - { - fprintf(stderr, "\ni: %llu\n", i); - fprintf(stderr, "strand: %llu\n", candidates->list[i].strand); - fprintf(stderr, "readID: %llu\n", candidates->list[i].readID); - fprintf(stderr, "diff: %lld\n", candidates->list[i].offset - candidates->list[i].self_offset); - fprintf(stderr, "offset: %lld\n", candidates->list[i].offset); - fprintf(stderr, "self_offset: %lld\n", candidates->list[i].self_offset); - - } - - pthread_mutex_unlock(&output_mutex); -} - - int cmp_by_x_pos_s(const void * a, const void * b) { if ((*(overlap_region*)a).x_pos_s > (*(overlap_region*)b).x_pos_s) @@ -1326,134 +474,6 @@ int cmp_by_x_pos_e(const void * a, const void * b) } } - ///r->length = Get_READ_LENGTH((*R_INF), ID); - -void calculate_overlap_region(Candidates_list* candidates, overlap_region_alloc* overlap_list, -uint64_t readID, uint64_t readLength, All_reads* R_INF) -{ - overlap_region tmp_region; - uint64_t i = 0; - long long current_pos_diff; - long long current_self_pos; - uint64_t current_ID; - uint64_t current_stand; - long long tmp_pos_distance; - long long tmp_self_pos_distance; - long long constant_distance = 5; - double error_rate = 0.05; - - - if (candidates->length == 0) - { - return; - } - - - - i = 0; - while (i < candidates->length) - { - current_pos_diff = candidates->list[i].offset - candidates->list[i].self_offset; - current_self_pos = candidates->list[i].self_offset; - current_ID = candidates->list[i].readID; - current_stand = candidates->list[i].strand; - - tmp_region.shared_seed = 1; - ///这个是查询read的信息 - tmp_region.x_id = readID; - tmp_region.x_pos_s = candidates->list[i].self_offset; - tmp_region.x_pos_e = candidates->list[i].self_offset; - tmp_region.x_pos_strand = current_stand; - - ///这个是被查询的read的信息 - tmp_region.y_id = current_ID; - tmp_region.y_pos_s = candidates->list[i].offset; - tmp_region.y_pos_e = candidates->list[i].offset; - tmp_region.y_pos_strand = 0; ///永远是0 - - - - i++; - while (i < candidates->length) - { - if (current_ID == candidates->list[i].readID && - current_stand == candidates->list[i].strand) - { - ///这个一定是正值 - tmp_pos_distance = candidates->list[i].offset - candidates->list[i].self_offset - current_pos_diff; - ///这个不一定是正值 - tmp_self_pos_distance = candidates->list[i].self_offset - current_self_pos; - - if(tmp_self_pos_distance < 0) - { - ///fprintf(stderr, "tmp_self_pos_distance: %d\n", tmp_self_pos_distance); - tmp_self_pos_distance = tmp_self_pos_distance * -1; - ///fprintf(stderr, "tmp_self_pos_distance: %d\n", tmp_self_pos_distance); - } - - ///if (tmp_self_pos_distance >= 0) - { - tmp_self_pos_distance = tmp_self_pos_distance * error_rate + constant_distance; - if (tmp_pos_distance < tmp_self_pos_distance) - { - /****************************may have bugs********************************/ - ///i++; - // tmp_region.x_pos_e = candidates->list[i].self_offset; - // tmp_region.y_pos_e = candidates->list[i].offset; - /****************************may have bugs********************************/ - tmp_region.shared_seed++; - - if(candidates->list[i].self_offset < tmp_region.x_pos_s) - { - tmp_region.x_pos_s = candidates->list[i].self_offset; - } - - if(candidates->list[i].self_offset > tmp_region.x_pos_e) - { - tmp_region.x_pos_e = candidates->list[i].self_offset; - } - - if(candidates->list[i].offset < tmp_region.y_pos_s) - { - tmp_region.y_pos_s = candidates->list[i].offset; - } - - - if(candidates->list[i].offset > tmp_region.y_pos_e) - { - tmp_region.y_pos_e = candidates->list[i].offset; - } - - - /****************************may have bugs********************************/ - i++; - /****************************may have bugs********************************/ - continue; - } - } - } - - break; - } - - ///自己和自己重叠的要排除 - ///if (tmp_region.x_id != tmp_region.y_id && tmp_region.shared_seed > 1) - if (tmp_region.x_id != tmp_region.y_id) - { - append_overlap_region_alloc(overlap_list, &tmp_region, R_INF); - } - - - } - - ///以x_pos_e,即结束位置为主元排序 - ///qsort(overlap_list->list, overlap_list->length, sizeof(overlap_region), cmp_by_x_pos_e); - qsort(overlap_list->list, overlap_list->length, sizeof(overlap_region), cmp_by_x_pos_s); - - ///debug_overlap_region(candidates, overlap_list, readID); - ///print_overlap_region(candidates, overlap_list, R_INF); -} - void debug_chain(k_mer_hit* a, long long a_n, Chain_Data* dp) { @@ -1483,13 +503,13 @@ void debug_chain(k_mer_hit* a, long long a_n, Chain_Data* dp) if(indels != dp->indels[i]) { - fprintf(stderr, "indels: %d, dp->indels[i]: %d\n", + fprintf(stderr, "indels: %lld, dp->indels[i]: %lld\n", indels, dp->indels[i]); } if(selfLen != dp->self_length[i]) { - fprintf(stderr, "selfLen: %d, dp->self_length[i]: %d\n", + fprintf(stderr, "selfLen: %lld, dp->self_length[i]: %lld\n", selfLen, dp->self_length[i]); } @@ -1528,163 +548,6 @@ long long y_beg, long long y_end, long long yLen) return x_end - x_beg + 1; } -void chain_DP_back(k_mer_hit* a, long long a_n, Chain_Data* dp, overlap_region* result, -double band_width_threshold) -{ - long long i, j; - long long self_pos, pos, max_j, max_i, max_score, score, n_skip; - long long distance_pos, distance_self_pos, distance_gap, log_distance_gap, distance_min; - ///double band_width_threshold = 0.05; - double band_width_penalty = 1 / band_width_threshold; - long long min_score = k_mer_length; - long long max_indels, max_self_length; - double gap_rate; - long long total_indels, total_self_length; - - resize_Chain_Data(dp, a_n); - // fill the score and backtrack arrays - for (i = 0; i < a_n; ++i) - { - pos = a[i].offset; - self_pos = a[i].self_offset; - max_j = -1; - max_score = min_score; - n_skip = 0; - max_indels = 0; - max_self_length = 0; - - - ///may have a pre-cut condition for j - for (j = i - 1; j >= 0; --j) - { - distance_pos = pos - a[j].offset; - distance_self_pos = self_pos - a[j].self_offset; - ///a has been sorted by a[].offset - ///note for a, we do not have any two elements that have both equal offsets and self_offsets - ///but there maybe two elements that have equal offsets or equal self_offsets - if(distance_pos == 0 || distance_self_pos <= 0) - { - continue; - } - - distance_gap = distance_pos > distance_self_pos? distance_pos - distance_self_pos : distance_self_pos - distance_pos; - - total_indels = dp->indels[j] + distance_gap; - total_self_length = dp->self_length[j] + distance_self_pos; - if(total_indels > band_width_threshold * total_self_length) - { - continue; - } - - distance_min = distance_pos < distance_self_pos? distance_pos:distance_self_pos; - score = distance_min < min_score? distance_min : min_score; - - /** - log_distance_gap = distance_gap? ilog2_32(distance_gap) : 0; - score -= (long long)(distance_gap * 0.01 * min_score) + (log_distance_gap/2); - **/ - gap_rate = (double)((double)(total_indels)/(double)(total_self_length)); - ///if the gap rate > 0.05, score will be negative - score -= (long long)(gap_rate * score * band_width_penalty); - - score += dp->score[j]; - - if(score > max_score) - { - max_score = score; - max_j = j; - max_indels = total_indels; - max_self_length = total_self_length; - if (n_skip > 0) - { - n_skip--; - } - } - } - - dp->score[i] = max_score; - dp->pre[i] = max_j; - dp->indels[i] = max_indels; - dp->self_length[i] = max_self_length; - } - - - ///debug_chain(a, a_n, dp); - - - - max_score = -1; - max_i = -1; - for (i = 0; i < a_n; ++i) - { - if(dp->score[i] > max_score) - { - max_score = dp->score[i]; - max_i = i; - } - } - - - clear_fake_cigar(&(result->f_cigar)); - - i = max_i; - result->x_pos_e = a[i].self_offset; - result->y_pos_e = a[i].offset; - result->shared_seed = max_score; - - distance_self_pos = result->x_pos_e - a[i].self_offset; - distance_pos = result->y_pos_e - a[i].offset; - long long pre_distance_gap = distance_pos - distance_self_pos; - ///record first site - ///the length of f_cigar should be at least 1 - add_fake_cigar(&(result->f_cigar), a[i].self_offset, pre_distance_gap); - long long chainLen = 0; - if(result->x_pos_strand == 1) - { - while (i >= 0) - { - distance_self_pos = result->x_pos_e - a[i].self_offset; - distance_pos = result->y_pos_e - a[i].offset; - distance_gap = distance_pos - distance_self_pos; - if(distance_gap != pre_distance_gap) - { - pre_distance_gap = distance_gap; - ///record this site - add_fake_cigar(&(result->f_cigar), a[i].self_offset, pre_distance_gap); - } - - chainLen++; - result->x_pos_s = a[i].self_offset; - result->y_pos_s = a[i].offset; - i = dp->pre[i]; - } - } - else - { - - while (i >= 0) - { - distance_self_pos = result->x_pos_e - a[i].self_offset; - distance_pos = result->y_pos_e - a[i].offset; - distance_gap = distance_pos - distance_self_pos; - if(distance_gap == pre_distance_gap) - { - result->f_cigar.length--; - add_fake_cigar(&(result->f_cigar), a[i].self_offset, pre_distance_gap); - } - else - { - pre_distance_gap = distance_gap; - add_fake_cigar(&(result->f_cigar), a[i].self_offset, pre_distance_gap); - } - - chainLen++; - result->x_pos_s = a[i].self_offset; - result->y_pos_s = a[i].offset; - i = dp->pre[i]; - } - } -} ///double band_width_threshold = 0.05; void chain_DP(k_mer_hit* a, long long a_n, Chain_Data* dp, overlap_region* result, @@ -1692,10 +555,10 @@ double band_width_threshold, int max_skip, int x_readLen, int y_readLen) { long long i, j; long long self_pos, pos, max_j, max_i, max_score, score, n_skip; - long long distance_pos, distance_self_pos, distance_gap, log_distance_gap, distance_min; + long long distance_pos, distance_self_pos, distance_gap, distance_min; ///double band_width_threshold = 0.05; double band_width_penalty = 1 / band_width_threshold; - long long min_score = k_mer_length; + long long min_score = asm_opt.k_mer_length; long long max_indels, max_self_length; double gap_rate; long long total_indels, total_self_length; @@ -1871,117 +734,15 @@ double band_width_threshold, int max_skip, int x_readLen, int y_readLen) } } -void debug_seed_offset(k_mer_hit* a, long long a_n, Chain_Data* dp, overlap_region* result, -All_reads* R_INF) -{ - long long i; - long long max_score = -1; - long long max_i = -1; - for (i = 0; i < a_n; i++) - { - if(max_score < dp->score[i]) - { - max_score = dp->score[i]; - max_i = i; - } - } - - if(max_score != result->shared_seed) - { - fprintf(stderr, "max_score: %d, result->shared_seed: %d, a_n: %d, dp->length: %d\n", - max_score, result->shared_seed, a_n, dp->length); - } - - long long chainLen = 0; - i = max_i; - while (i >= 0) - { - chainLen++; - i = dp->pre[i]; - } - - - long long* chain_x_pos = (long long*)malloc(sizeof(long long)*chainLen); - long long* chain_y_pos = (long long*)malloc(sizeof(long long)*chainLen); - long long chain_i; - i = max_i; - if(result->y_pos_strand == 0) - { - chain_i = chainLen - 1; - while (i >= 0) - { - chain_x_pos[chain_i] = a[i].self_offset; - chain_y_pos[chain_i] = a[i].offset; - chain_i--; - i = dp->pre[i]; - } - } - else - { - chain_i = 0; - while (i >= 0) - { - chain_x_pos[chain_i] = - Get_READ_LENGTH((*R_INF), result->x_id) - a[i].self_offset - 1; - - chain_y_pos[chain_i] = - Get_READ_LENGTH((*R_INF), result->y_id) - a[i].offset - 1; - chain_i++; - i = dp->pre[i]; - } - } - - - chain_i = 0; - - long long distance_self_pos; - long long distance_pos; - long long distance_gap; - - - for (i = 0; i < chainLen; i++) - { - distance_self_pos = chain_x_pos[i] - result->x_pos_s; - distance_pos = chain_y_pos[i] - result->y_pos_s; - distance_gap = distance_pos - distance_self_pos; - - if(distance_gap != y_start_offset(chain_x_pos[i], &(result->f_cigar))) - { - fprintf(stderr, "distance_gap: %d, y_offset: %d\n", distance_gap, - y_start_offset(chain_x_pos[i], &(result->f_cigar))); - } - } - - - - - - - - - - - - - free(chain_x_pos); - free(chain_y_pos); - -} void calculate_overlap_region_by_chaining(Candidates_list* candidates, overlap_region_alloc* overlap_list, uint64_t readID, uint64_t readLength, All_reads* R_INF, double band_width_threshold, int add_beg_end) { overlap_region tmp_region; - uint64_t i = 0; - long long current_pos_diff; - long long current_self_pos; + long long i = 0; uint64_t current_ID; uint64_t current_stand; - long long tmp_pos_distance; - long long tmp_self_pos_distance; - long long constant_distance = 5; - if (candidates->length == 0) { @@ -2052,154 +813,6 @@ uint64_t readID, uint64_t readLength, All_reads* R_INF, double band_width_thresh - -void calculate_inexact_overlap_region(Candidates_list* candidates, overlap_region_alloc* overlap_list, -uint64_t readID, uint64_t readLength, All_reads* R_INF) -{ - overlap_region tmp_region; - uint64_t i = 0; - long long current_pos_diff; - long long current_self_pos; - uint64_t current_ID; - uint64_t current_stand; - long long tmp_pos_distance; - long long tmp_self_pos_distance; - long long constant_distance = 5; - double error_rate = 0.05; - - - if (candidates->length == 0) - { - return; - } - - - - i = 0; - while (i < candidates->length) - { - current_pos_diff = candidates->list[i].offset - candidates->list[i].self_offset; - current_self_pos = candidates->list[i].self_offset; - current_ID = candidates->list[i].readID; - current_stand = candidates->list[i].strand; - - tmp_region.shared_seed = 1; - ///这个是查询read的信息 - tmp_region.x_id = readID; - tmp_region.x_pos_s = candidates->list[i].self_offset; - tmp_region.x_pos_e = candidates->list[i].self_offset; - tmp_region.x_pos_strand = current_stand; - - ///这个是被查询的read的信息 - tmp_region.y_id = current_ID; - tmp_region.y_pos_s = candidates->list[i].offset; - tmp_region.y_pos_e = candidates->list[i].offset; - tmp_region.y_pos_strand = 0; ///永远是0 - - // if(memcmp("m64013_190412_043951/159056664/ccs", Get_NAME((*R_INF), readID), - // Get_NAME_LENGTH((*R_INF), readID)) == 0 - // && - // memcmp("m64013_190322_203854/42402522/ccs", Get_NAME((*R_INF), current_ID), - // Get_NAME_LENGTH((*R_INF), current_ID)) == 0) - // { - // fprintf(stderr, "***********i: %d, x_pos_s: %d, x_pos_e: %d, y_pos_s: %d, y_pos_e: %d\n", - // i, tmp_region.x_pos_s, tmp_region.x_pos_e, tmp_region.y_pos_s, tmp_region.y_pos_e); - // fprintf(stderr, "***********i: %d, self_offset: %d, offset: %d\n", - // i, candidates->list[i].self_offset, candidates->list[i].offset); - // } - - - - i++; - while (i < candidates->length) - { - if (current_ID == candidates->list[i].readID && - current_stand == candidates->list[i].strand) - { - ///这个一定是正值 - tmp_pos_distance = candidates->list[i].offset - candidates->list[i].self_offset - current_pos_diff; - ///这个不一定是正值 - tmp_self_pos_distance = candidates->list[i].self_offset - current_self_pos; - - if(tmp_self_pos_distance < 0) - { - ///fprintf(stderr, "tmp_self_pos_distance: %d\n", tmp_self_pos_distance); - tmp_self_pos_distance = tmp_self_pos_distance * -1; - ///fprintf(stderr, "tmp_self_pos_distance: %d\n", tmp_self_pos_distance); - } - - ///if (tmp_self_pos_distance >= 0) - { - tmp_self_pos_distance = tmp_self_pos_distance * error_rate + constant_distance; - if (tmp_pos_distance < tmp_self_pos_distance) - { - /****************************may have bugs********************************/ - ///i++; - // tmp_region.x_pos_e = candidates->list[i].self_offset; - // tmp_region.y_pos_e = candidates->list[i].offset; - /****************************may have bugs********************************/ - tmp_region.shared_seed++; - - if(candidates->list[i].self_offset < tmp_region.x_pos_s) - { - tmp_region.x_pos_s = candidates->list[i].self_offset; - } - - if(candidates->list[i].self_offset > tmp_region.x_pos_e) - { - tmp_region.x_pos_e = candidates->list[i].self_offset; - } - - if(candidates->list[i].offset < tmp_region.y_pos_s) - { - tmp_region.y_pos_s = candidates->list[i].offset; - } - - - if(candidates->list[i].offset > tmp_region.y_pos_e) - { - tmp_region.y_pos_e = candidates->list[i].offset; - } - - // if(memcmp("m64013_190412_043951/159056664/ccs", Get_NAME((*R_INF), readID), - // Get_NAME_LENGTH((*R_INF), readID)) == 0 - // && - // memcmp("m64013_190322_203854/42402522/ccs", Get_NAME((*R_INF), current_ID), - // Get_NAME_LENGTH((*R_INF), current_ID)) == 0) - // { - // fprintf(stderr, "i: %d, x_pos_s: %d, x_pos_e: %d, y_pos_s: %d, y_pos_e: %d\n", - // i, tmp_region.x_pos_s, tmp_region.x_pos_e, tmp_region.y_pos_s, tmp_region.y_pos_e); - - // fprintf(stderr, "i: %d, self_offset: %d, offset: %d, tmp_pos_distance: %d, tmp_self_pos_distance: %d\n", - // i, candidates->list[i].self_offset, candidates->list[i].offset, tmp_pos_distance, - // tmp_self_pos_distance); - // } - - - /****************************may have bugs********************************/ - i++; - /****************************may have bugs********************************/ - continue; - } - } - } - - break; - } - - ///自己和自己重叠的要排除 - ///if (tmp_region.x_id != tmp_region.y_id && tmp_region.shared_seed > 1) - if (tmp_region.x_id != tmp_region.y_id) - { - append_inexact_overlap_region_alloc(overlap_list, &tmp_region, R_INF, 1); - } - - - } -} - - - void append_window_list(overlap_region* region, uint64_t x_start, uint64_t x_end, int y_start, int y_end, int error, int extra_begin, int extra_end, int error_threshold) { @@ -2207,10 +820,10 @@ int extra_begin, int extra_end, int error_threshold) long long length = region->x_pos_e - region->x_pos_s + 1; ///the length of window may large or small than WINDOW /****************************may have bugs********************************/ - long long num_windows = length / WINDOW + 4; + uint64_t num_windows = length / WINDOW + 4; /****************************may have bugs********************************/ - ///w_list_length会在clear_overlap_region_alloc中定时清空 + ///w_list_length has alredy set to be 0 at clear_overlap_region_alloc if (num_windows > region->w_list_size) { region->w_list_size = num_windows; @@ -2232,20 +845,6 @@ int extra_begin, int extra_end, int error_threshold) - - - - - - - - - - - - - - void init_k_mer_pos_list_alloc(k_mer_pos_list_alloc* list) { list->size = 1000; @@ -2314,20 +913,18 @@ inline void append_pos_to_Candidates_list(Candidates_list* candidates, ElemType* void test_single_list(Candidates_list* candidates, k_mer_pos* n_list, uint64_t n_lengh, uint64_t end_pos, uint64_t strand) { uint64_t i; - - int j = 0; - + long long j = 0; for (i = 0; i < n_lengh; i++) { for (; j < candidates->length; j++) { if ( - n_list[i].offset == candidates->list[j].offset + n_list[i].offset == (uint64_t)candidates->list[j].offset && n_list[i].readID == candidates->list[j].readID && - end_pos == candidates->list[j].self_offset + end_pos == (uint64_t)candidates->list[j].self_offset && strand == candidates->list[j].strand ) @@ -2342,157 +939,6 @@ void test_single_list(Candidates_list* candidates, k_mer_pos* n_list, uint64_t n } } -} - -void verify_merge_result(k_mer_pos_list_alloc* list, Candidates_list* candidates) -{ - uint64_t total_length = 0; - uint64_t i = 0; - long long r_dis_x, r_dis_y; - for (i = 0; i < list->length; i++) - { - total_length = total_length + list->list[i].length; - } - - if (total_length!=candidates->length) - { - fprintf(stderr, "ERROR length & size.\n"); - } - - for (i = 1; i < candidates->length; i++) - { - if (candidates->list[i].strand < candidates->list[i-1].strand) - { - fprintf(stderr, "ERROR -1\n"); - } - else if (candidates->list[i].strand == candidates->list[i-1].strand) - { - if (candidates->list[i].readID < candidates->list[i-1].readID) - { - fprintf(stderr, "ERROR 0\n"); - } - else if (candidates->list[i].readID == candidates->list[i-1].readID) - { - r_dis_x = candidates->list[i].offset - candidates->list[i].self_offset; - r_dis_y = candidates->list[i-1].offset - candidates->list[i-1].self_offset; - - ///if (candidates->list[i].offset < candidates->list[i-1].offset) - if (r_dis_x < r_dis_y) - { - fprintf(stderr, "ERROR 1\n"); - } - ///else if (candidates->list[i].offset == candidates->list[i-1].offset) - else if (r_dis_x == r_dis_y) - { - if (candidates->list[i].self_offset < candidates->list[i-1].self_offset) - { - fprintf(stderr, "ERROR 2\n"); - } - } - } - } - } - - - uint64_t j = 0; - for (i = 0; i < list->length; i++) - { - test_single_list(candidates, list->list[i].list, list->list[i].length, list->list[i].end_pos, list->list[i].direction); - } - - - -} - - -void output_to_stderr(Candidates_list* candidates) -{ - uint64_t i = 0; - - pthread_mutex_lock(&output_mutex); - - - - fprintf(stderr, "************************\nLength: %llu\n", candidates->length); - - - for (i = 0; i < candidates->length; i++) - { - fprintf(stderr, "\ni: %llu\n", i); - fprintf(stderr, "strand: %llu\n", candidates->list[i].strand); - fprintf(stderr, "readID: %llu\n", candidates->list[i].readID); - fprintf(stderr, "diff: %lld\n", candidates->list[i].offset - candidates->list[i].self_offset); - fprintf(stderr, "offset: %lld\n", candidates->list[i].offset); - fprintf(stderr, "self_offset: %lld\n", candidates->list[i].self_offset); - - } - - pthread_mutex_unlock(&output_mutex); - - -} - - -void merge_k_mer_pos_list_alloc_heap_sort_back(k_mer_pos_list_alloc* list, Candidates_list* candidates, HeapSq* HBT) -{ - clear_Heap(HBT); - - uint64_t total_length = 0; - uint64_t i; - ElemType x, y; - ///所有list的长度都不是0 - ///把各个表第一个元素加入到堆中 - for (i = 0; i < list->length; i++) - { - x.ID = i; - x.node.offset = list->list[i].list[0].offset; - x.node.readID = list->list[i].list[0].readID; - x.node.self_offset = list->list[i].end_pos; - x.node.strand = list->list[i].direction; - - Insert_Heap(HBT, &x); - - HBT->index_i[i] = 1; - - total_length = total_length + list->list[i].length; - } - - - candidates->length = 0; - if(total_length > candidates->size) - { - candidates->size = total_length; - candidates->list = (k_mer_hit*)realloc(candidates->list, sizeof(k_mer_hit)*candidates->size); - candidates->tmp = (k_mer_hit*)realloc(candidates->tmp, sizeof(k_mer_hit)*candidates->size); - } - - uint64_t ID; - while (DeleteHeap(HBT, &x)) - { - append_pos_to_Candidates_list(candidates, &x); - ///x.ID说明是从第x.ID个列表中的这个节点已经从堆里出来了 - ///HBT->index_i[x.ID]是第x.ID个列表的当前元素的下标 - i = HBT->index_i[x.ID]; - ID = x.ID; - ///fprintf(stderr, "x.ID: %llu, i: %llu, length: %llu\n", x.ID, i, list->list[x.ID].length); - - - if (i < list->list[x.ID].length) - { - y.ID = ID; - y.node.offset = list->list[x.ID].list[i].offset; - y.node.readID = list->list[x.ID].list[i].readID; - y.node.self_offset = list->list[x.ID].end_pos; - y.node.strand = list->list[x.ID].direction; - Insert_Heap(HBT, &y); - HBT->index_i[ID]++; - } - } - - ///verify_merge_result(list, candidates); - - - } @@ -2503,11 +949,9 @@ void merge_k_mer_pos_list_alloc_heap_sort(k_mer_pos_list_alloc* list, Candidates uint64_t total_length = 0; uint64_t i; ElemType x, y; - ///所有list的长度都不是0 - ///把各个表第一个元素加入到堆中 + //add the first element of each list to stack for (i = 0; i < list->length; i++) { - x.ID = i; x.node.offset = list->list[i].list[0].offset; x.node.readID = list->list[i].list[0].readID; @@ -2518,13 +962,12 @@ void merge_k_mer_pos_list_alloc_heap_sort(k_mer_pos_list_alloc* list, Candidates HBT->index_i[i] = 1; - total_length = total_length + list->list[i].length; } candidates->length = 0; - if(total_length > candidates->size) + if(total_length > (uint64_t)candidates->size) { candidates->size = total_length; candidates->list = (k_mer_hit*)realloc(candidates->list, sizeof(k_mer_hit)*candidates->size); @@ -2533,11 +976,11 @@ void merge_k_mer_pos_list_alloc_heap_sort(k_mer_pos_list_alloc* list, Candidates uint64_t ID; int flag; - while (flag = DeleteHeap(HBT, &x)) + ///while (flag = DeleteHeap(HBT, &x)) + while ((flag = DeleteHeap(HBT, &x))) { append_pos_to_Candidates_list(candidates, &x); - ///x.ID说明是从第x.ID个列表中的这个节点已经从堆里出来了 - ///HBT->index_i[x.ID]是第x.ID个列表的当前元素的下标 + i = HBT->index_i[x.ID]; ID = x.ID; @@ -2566,211 +1009,10 @@ void merge_k_mer_pos_list_alloc_heap_sort(k_mer_pos_list_alloc* list, Candidates Insert_Heap(HBT, &y); HBT->index_i[ID]++; } - } - - - ///verify_merge_result(list, candidates); - ///output_to_stderr(candidates); - + } } -void insert_kv_list_to_candidates(k_v* list, long long occ, long long y_id, long long y_offset, long long y_strand, -Candidates_list* candidates) -{ - if(candidates->length + occ > candidates->size) - { - candidates->size = (candidates->length + occ) * 2; - candidates->list = (k_mer_hit*)realloc(candidates->list, sizeof(k_mer_hit)*candidates->size); - candidates->tmp = (k_mer_hit*)realloc(candidates->tmp, sizeof(k_mer_hit)*candidates->size); - } - - long long i; - ElemType x; - for (i = 0; i < occ; i++) - { - x.node.offset = y_offset; - x.node.readID = y_id; - x.node.self_offset = list[i].value; - x.node.strand = y_strand; - append_pos_to_Candidates_list(candidates, &x); - } -} - - -///有bug,找时间排一下 -void merge_k_mer_pos_list_alloc_heap_sort_advance(k_mer_pos_list_alloc* list, Candidates_list* candidates, HeapSq* HBT) -{ - - uint64_t total_length = 0; - uint64_t i; - ElemType x, y; - - /************************************forward*************************************************/ - clear_Heap(HBT); - - ///所有list的长度都不是0 - ///把各个表第一个元素加入到堆中 - for (i = 0; i < list->length; i++) - { - if (list->list[i].direction == 0) - { - x.ID = i; - x.node.offset = list->list[i].list[0].offset; - x.node.readID = list->list[i].list[0].readID; - x.node.self_offset = list->list[i].end_pos; - x.node.strand = list->list[i].direction; - - Insert_Heap(HBT, &x); - - HBT->index_i[i] = 1; - - total_length = total_length + list->list[i].length; - } - } - - - candidates->length = 0; - if(total_length > candidates->size) - { - candidates->size = total_length; - candidates->list = (k_mer_hit*)realloc(candidates->list, sizeof(k_mer_hit)*candidates->size); - candidates->tmp = (k_mer_hit*)realloc(candidates->tmp, sizeof(k_mer_hit)*candidates->size); - } - - - uint64_t ID; - int flag; - while (flag = DeleteHeap(HBT, &x)) - { - append_pos_to_Candidates_list(candidates, &x); - ///x.ID说明是从第x.ID个列表中的这个节点已经从堆里出来了 - ///HBT->index_i[x.ID]是第x.ID个列表的当前元素的下标 - i = HBT->index_i[x.ID]; - ID = x.ID; - ///fprintf(stderr, "x.ID: %llu, i: %llu, length: %llu\n", x.ID, i, list->list[x.ID].length); - /** - if (flag == 2) - { - for (; i < list->list[x.ID].length; i++) - { - y.ID = ID; - y.node.offset = list->list[x.ID].list[i].offset; - y.node.readID = list->list[x.ID].list[i].readID; - y.node.self_offset = list->list[x.ID].end_pos; - y.node.strand = list->list[x.ID].direction; - append_pos_to_Candidates_list(candidates, &y); - } - - break; - } - **/ - - - if (i < list->list[x.ID].length) - { - y.ID = ID; - y.node.offset = list->list[x.ID].list[i].offset; - y.node.readID = list->list[x.ID].list[i].readID; - y.node.self_offset = list->list[x.ID].end_pos; - y.node.strand = list->list[x.ID].direction; - Insert_Heap(HBT, &y); - HBT->index_i[ID]++; - } - } - - - - /************************************reverse complement*************************************************/ - - clear_Heap(HBT); - ///所有list的长度都不是0 - ///把各个表第一个元素加入到堆中 - for (i = 0; i < list->length; i++) - { - if (list->list[i].direction == 1) - { - x.ID = i; - x.node.offset = list->list[i].list[0].offset; - x.node.readID = list->list[i].list[0].readID; - x.node.self_offset = list->list[i].end_pos; - x.node.strand = list->list[i].direction; - - Insert_Heap(HBT, &x); - - HBT->index_i[i] = 1; - - total_length = total_length + list->list[i].length; - } - } - - if(total_length > candidates->size) - { - candidates->size = total_length; - candidates->list = (k_mer_hit*)realloc(candidates->list, sizeof(k_mer_hit)*candidates->size); - candidates->tmp = (k_mer_hit*)realloc(candidates->tmp, sizeof(k_mer_hit)*candidates->size); - } - - while (flag = DeleteHeap(HBT, &x)) - { - append_pos_to_Candidates_list(candidates, &x); - ///x.ID说明是从第x.ID个列表中的这个节点已经从堆里出来了 - ///HBT->index_i[x.ID]是第x.ID个列表的当前元素的下标 - i = HBT->index_i[x.ID]; - ID = x.ID; - ///fprintf(stderr, "x.ID: %llu, i: %llu, length: %llu\n", x.ID, i, list->list[x.ID].length); - /** - if (flag == 2) - { - for (; i < list->list[x.ID].length; i++) - { - y.ID = ID; - y.node.offset = list->list[x.ID].list[i].offset; - y.node.readID = list->list[x.ID].list[i].readID; - y.node.self_offset = list->list[x.ID].end_pos; - y.node.strand = list->list[x.ID].direction; - append_pos_to_Candidates_list(candidates, &y); - } - - break; - } - **/ - - - if (i < list->list[x.ID].length) - { - y.ID = ID; - y.node.offset = list->list[x.ID].list[i].offset; - y.node.readID = list->list[x.ID].list[i].readID; - y.node.self_offset = list->list[x.ID].end_pos; - y.node.strand = list->list[x.ID].direction; - Insert_Heap(HBT, &y); - HBT->index_i[ID]++; - } - } - -} - - -void merge_k_mer_pos_list_alloc(k_mer_pos_list_alloc* list, Candidates_list* candidates) -{ - qsort(list->list, list->length, sizeof(k_mer_pos_list), cmp_k_mer_pos_list); - - uint64_t i; - for (i = 0; i < list->length; i++) - { - /** - if (i > 0 && (list->list[i].length < list->list[i-1].length || list->list[i].length == 0 || list->list[i-1].length == 0)) - { - fprintf(stderr, "ERROR\n"); - } - **/ - - merge_Candidates_list(candidates, list->list[i].list, list->list[i].length, - list->list[i].end_pos, list->list[i].direction); - } - -} void init_Count_Table(Count_Table** table) { @@ -2786,8 +1028,8 @@ void init_Total_Count_Table(int k, Total_Count_Table* TCB) { if(k>64) { - fprintf(stdout, "k-mer is too long. The length of k-mer must <= 64."); - fflush(stdout); + fprintf(stderr, "k-mer is too long. The length of k-mer must <= 64."); + fflush(stderr); exit(0); } @@ -2800,7 +1042,7 @@ void init_Total_Count_Table(int k, Total_Count_Table* TCB) TCB->prefix_bits = total_bits - TCB->suffix_bits; } ///TCB->suffix_mode = (1ULL<suffix_bits) - 1; - ///这个右移是安全的,因为TCB->suffix_bits不可能是0 + ///right shift is safe, since TCB->suffix_bits cannot be 0 TCB->suffix_mode = ALL >> (64 - TCB->suffix_bits); ///number of small hash table @@ -2873,13 +1115,13 @@ void destory_Total_Pos_Table(Total_Pos_Table* TCB) void write_Total_Pos_Table(Total_Pos_Table* TCB, char* read_file_name) { - fprintf(stdout, "Writing index to disk ...... \n"); + fprintf(stderr, "Writing index to disk... \n"); char* index_name = (char*)malloc(strlen(read_file_name)+5); sprintf(index_name, "%s.idx", read_file_name); FILE* fp = fopen(index_name, "w"); - fwrite(&adapterLen, sizeof(adapterLen), 1, fp); - fwrite(&k_mer_min_freq, sizeof(k_mer_min_freq), 1, fp); - fwrite(&k_mer_max_freq, sizeof(k_mer_max_freq), 1, fp); + fwrite(&asm_opt.adapterLen, sizeof(asm_opt.adapterLen), 1, fp); + fwrite(&asm_opt.k_mer_min_freq, sizeof(asm_opt.k_mer_min_freq), 1, fp); + fwrite(&asm_opt.k_mer_max_freq, sizeof(asm_opt.k_mer_max_freq), 1, fp); fwrite(&TCB->prefix_bits, sizeof(TCB->prefix_bits), 1, fp); fwrite(&TCB->suffix_bits, sizeof(TCB->suffix_bits), 1, fp); fwrite(&TCB->suffix_mode, sizeof(TCB->suffix_mode), 1, fp); @@ -2898,13 +1140,13 @@ void write_Total_Pos_Table(Total_Pos_Table* TCB, char* read_file_name) free(index_name); fclose(fp); - fprintf(stdout, "Index has been written.\n"); + fprintf(stderr, "Index has been written.\n"); } int load_Total_Pos_Table(Total_Pos_Table* TCB, char* read_file_name) { - fprintf(stdout, "Loading index to disk ...... \n"); + fprintf(stderr, "Loading index from disk... \n"); char* index_name = (char*)malloc(strlen(read_file_name)+5); sprintf(index_name, "%s.idx", read_file_name); FILE* fp = fopen(index_name, "r"); @@ -2912,27 +1154,28 @@ int load_Total_Pos_Table(Total_Pos_Table* TCB, char* read_file_name) { return 0; } + int f_flag; int local_adapterLen; - fread(&local_adapterLen, sizeof(local_adapterLen), 1, fp); - if(local_adapterLen != adapterLen) + f_flag = fread(&local_adapterLen, sizeof(local_adapterLen), 1, fp); + if(local_adapterLen != asm_opt.adapterLen) { - fprintf(stdout, "the adapterLen of index is: %d, but the adapterLen set by user is: %d\n", - local_adapterLen, adapterLen); + fprintf(stderr, "the adapterLen of index is: %d, but the adapterLen set by user is: %d\n", + local_adapterLen, asm_opt.adapterLen); exit(1); } - fread(&k_mer_min_freq, sizeof(k_mer_min_freq), 1, fp); - fread(&k_mer_max_freq, sizeof(k_mer_max_freq), 1, fp); - fread(&TCB->prefix_bits, sizeof(TCB->prefix_bits), 1, fp); - fread(&TCB->suffix_bits, sizeof(TCB->suffix_bits), 1, fp); - fread(&TCB->suffix_mode, sizeof(TCB->suffix_mode), 1, fp); - fread(&TCB->size, sizeof(TCB->size), 1, fp); - fread(&TCB->useful_k_mer, sizeof(TCB->useful_k_mer), 1, fp); - fread(&TCB->total_occ, sizeof(TCB->total_occ), 1, fp); + f_flag += fread(&asm_opt.k_mer_min_freq, sizeof(asm_opt.k_mer_min_freq), 1, fp); + f_flag += fread(&asm_opt.k_mer_max_freq, sizeof(asm_opt.k_mer_max_freq), 1, fp); + f_flag += fread(&TCB->prefix_bits, sizeof(TCB->prefix_bits), 1, fp); + f_flag += fread(&TCB->suffix_bits, sizeof(TCB->suffix_bits), 1, fp); + f_flag += fread(&TCB->suffix_mode, sizeof(TCB->suffix_mode), 1, fp); + f_flag += fread(&TCB->size, sizeof(TCB->size), 1, fp); + f_flag += fread(&TCB->useful_k_mer, sizeof(TCB->useful_k_mer), 1, fp); + f_flag += fread(&TCB->total_occ, sizeof(TCB->total_occ), 1, fp); if (TCB->useful_k_mer+1) { TCB->k_mer_index = (uint64_t*)malloc(sizeof(uint64_t)*(TCB->useful_k_mer+1)); - fread(TCB->k_mer_index, sizeof(uint64_t), TCB->useful_k_mer+1, fp); + f_flag += fread(TCB->k_mer_index, sizeof(uint64_t), TCB->useful_k_mer+1, fp); } else { @@ -2943,7 +1186,7 @@ int load_Total_Pos_Table(Total_Pos_Table* TCB, char* read_file_name) if (TCB->total_occ) { TCB->pos = (k_mer_pos*)malloc(sizeof(k_mer_pos)*TCB->total_occ); - fread(TCB->pos, sizeof(k_mer_pos), TCB->total_occ, fp); + f_flag += fread(TCB->pos, sizeof(k_mer_pos), TCB->total_occ, fp); } else { @@ -2965,7 +1208,7 @@ int load_Total_Pos_Table(Total_Pos_Table* TCB, char* read_file_name) free(index_name); fclose(fp); - fprintf(stdout, "Index has been loaded.\n"); + fprintf(stderr, "Index has been loaded.\n"); return 1; } @@ -2978,7 +1221,7 @@ typedef struct void insert_H_peaks(H_peaks* h, long long index, long long value) { - if(h->length <= index) + if(h->length <= (uint64_t)index) { long long newLen = index + 1; h->list = (long long*)realloc(h->list, newLen*sizeof(long long)); @@ -3033,28 +1276,28 @@ void get_peak_debug(Total_Count_Table* TCB, long long* min, long long* max) sub_key = kh_key(h, k); recover_hash_code(sub_ID, sub_key, &code, TCB->suffix_mode, - TCB->suffix_bits, k_mer_length); - RC_Hash_code(&code, &rc_code, k_mer_length); - RC_Hash_code(&rc_code, &debug_code, k_mer_length); + TCB->suffix_bits, asm_opt.k_mer_length); + RC_Hash_code(&code, &rc_code, asm_opt.k_mer_length); + RC_Hash_code(&rc_code, &debug_code, asm_opt.k_mer_length); if(code.x[0] != debug_code.x[0] || code.x[1] != debug_code.x[1]) { - fprintf(stderr, "sbsbsb\n"); + fprintf(stderr, "error\n"); } - Hashcode_to_string(&code, str, k_mer_length); - Hashcode_to_string(&rc_code, rc_str, k_mer_length); - reverse_complement(str, k_mer_length); - if(memcmp(str, rc_str, k_mer_length) != 0) + Hashcode_to_string(&code, str, asm_opt.k_mer_length); + Hashcode_to_string(&rc_code, rc_str, asm_opt.k_mer_length); + reverse_complement(str, asm_opt.k_mer_length); + if(memcmp(str, rc_str, asm_opt.k_mer_length) != 0) { - fprintf(stderr, "hehehehe\n"); + fprintf(stderr, "error\n"); int j; - for (j = 0; j < k_mer_length; j++) + for (j = 0; j < asm_opt.k_mer_length; j++) { fprintf(stderr, "%c",str[j]); } fprintf(stderr, "\n"); - for (j = 0; j < k_mer_length; j++) + for (j = 0; j < asm_opt.k_mer_length; j++) { fprintf(stderr, "%c",rc_str[j]); } @@ -3067,9 +1310,9 @@ void get_peak_debug(Total_Count_Table* TCB, long long* min, long long* max) c_count = kh_value(h, k); - if(get_Total_Count_Table(TCB, &code, k_mer_length) != c_count) + if(get_Total_Count_Table(TCB, &code, asm_opt.k_mer_length) != c_count) { - fprintf(stderr, "sbsbsb\n"); + fprintf(stderr, "error\n"); } insert_H_peaks(&LH, c_count, c_count); @@ -3080,7 +1323,7 @@ void get_peak_debug(Total_Count_Table* TCB, long long* min, long long* max) (*max) = -1; (*min) = -1; long long max_value = -1; - for (i = 0; i < LH.length; i++) + for (i = 0; i < (long long)LH.length; i++) { if(LH.list[i] >= max_value) { @@ -3090,7 +1333,7 @@ void get_peak_debug(Total_Count_Table* TCB, long long* min, long long* max) } long long min_value = max_value; - for (i = 0; i < LH.length; i++) + for (i = 0; i < (long long)LH.length; i++) { if(LH.list[i] < min_value && LH.list[i] != 0) { @@ -3099,10 +1342,10 @@ void get_peak_debug(Total_Count_Table* TCB, long long* min, long long* max) } } - for (i = 0; i < LH.length; i++) + for (i = 0; i < (long long)LH.length; i++) { ///fprintf(stderr, "%d, %d\n", i, LH.list[i]); - fprintf(stderr, "%d\n", LH.list[i]); + fprintf(stderr, "%lld\n", LH.list[i]); } @@ -3140,11 +1383,11 @@ int get_total_freq(Total_Count_Table* TCB, uint64_t sub_ID, uint64_t sub_key, lo long long count, rc_count; recover_hash_code(sub_ID, sub_key, &code, TCB->suffix_mode, - TCB->suffix_bits, k_mer_length); - RC_Hash_code(&code, &rc_code, k_mer_length); + TCB->suffix_bits, asm_opt.k_mer_length); + RC_Hash_code(&code, &rc_code, asm_opt.k_mer_length); - count = get_Total_Count_Table(TCB, &code, k_mer_length); - rc_count = get_Total_Count_Table(TCB, &rc_code, k_mer_length); + count = get_Total_Count_Table(TCB, &code, asm_opt.k_mer_length); + rc_count = get_Total_Count_Table(TCB, &rc_code, asm_opt.k_mer_length); (*T_count) = count + rc_count; if(count == 0) @@ -3212,7 +1455,7 @@ void get_peak(Total_Count_Table* TCB, long long* min, long long* max, long long* (*min) = -1; long long max_value = -1; //// seed with freq 1 is useless - for (i = 2; i < LH.length; i++) + for (i = 2; i < (long long)LH.length; i++) { if(LH.list[i] >= max_value) { @@ -3223,7 +1466,7 @@ void get_peak(Total_Count_Table* TCB, long long* min, long long* max, long long* long long opt = 4; (*up_boundary) = -1; - for (i = (*max) + opt; i < LH.length; i++) + for (i = (*max) + opt; i < (long long)LH.length; i++) { if(LH.list[i] > LH.list[i-opt]) { @@ -3253,7 +1496,7 @@ void get_peak(Total_Count_Table* TCB, long long* min, long long* max, long long* long long min_value = max_value; //// seed with freq 1 is useless - for (i = 2; i < LH.length && i < (*max); i++) + for (i = 2; i < (long long)LH.length && i < (*max); i++) { if(LH.list[i] < min_value && LH.list[i] != 0) { @@ -3262,109 +1505,10 @@ void get_peak(Total_Count_Table* TCB, long long* min, long long* max, long long* } } - // for (i = 0; i < LH.length; i++) - // { - // ///fprintf(stderr, "%d, %d\n", i, LH.list[i]); - // fprintf(stderr, "%d\n", LH.list[i]); - // } - // fflush(stderr); - - free(LH.list); } -void Traverse_Counting_Table_back(Total_Count_Table* TCB, Total_Pos_Table* PCB, int k_mer_min_freq, int k_mer_max_freq) -{ - int i; - Count_Table* h; - khint_t k; - uint64_t sub_key; - uint64_t sub_ID; - PCB->useful_k_mer = 0; - PCB->total_occ = 0; - long long freq_min, freq_max, freq_up; - ///get_peak_debug(TCB, &freq_min, &freq_max); - get_peak(TCB, &freq_min, &freq_max, &freq_up); - fprintf(stderr, "freq_min: %d, freq_max: %d, freq_up: %d\n", - freq_min, freq_max, freq_up); - - ///init_Total_Pos_Table(PCB, TCB); - - khint_t t; ///这就是个迭代器 - int absent; - - /******************************************** - hash_table(key) ----> PCB->k_mer_index ------> PCB->pos - ********************************************/ - for (i = 0; i < TCB->size; i++) - { - h = TCB->sub_h[i]; - for (k = kh_begin(h); k != kh_end(h); ++k) - { - if (kh_exist(h, k)) // test if a bucket contains data - { - ///只有符合频率范围要求的k-mer,才会被加入到pos table中 - if (kh_value(h, k)>=k_mer_min_freq && kh_value(h, k)<=k_mer_max_freq) - { - - sub_ID = i; - sub_key = kh_key(h, k); - - t = kh_put(POS64, PCB->sub_h[sub_ID], sub_key, &absent); - - if (absent) - { - ///kh_value(PCB->sub_h[sub_ID], t) = useful_k_mer + total_occ; - kh_value(PCB->sub_h[sub_ID], t) = PCB->useful_k_mer; - } - else ///哈希表中已有的元素 - { - ///kh_value(PCB->sub_h[sub_ID], t)++; - fprintf(stderr, "ERROR\n"); - } - - - PCB->useful_k_mer++; - PCB->total_occ = PCB->total_occ + kh_value(h, k); - } - } - } - } - - - fprintf(stdout, "useful_k_mer: %lld\n",PCB->useful_k_mer); - fprintf(stdout, "total_occ: %lld\n",PCB->total_occ); - - PCB->k_mer_index = (uint64_t*)malloc(sizeof(uint64_t)*(PCB->useful_k_mer+1)); - - PCB->k_mer_index[0] = 0; - - PCB->total_occ = 0; - PCB->useful_k_mer = 0; - - for (i = 0; i < TCB->size; i++) - { - h = TCB->sub_h[i]; - for (k = kh_begin(h); k != kh_end(h); ++k) - { - if (kh_exist(h, k)) // test if a bucket contains data - { - if (kh_value(h, k)>=k_mer_min_freq && kh_value(h, k)<=k_mer_max_freq) - { - PCB->useful_k_mer++; - PCB->total_occ = PCB->total_occ + kh_value(h, k); - PCB->k_mer_index[PCB->useful_k_mer] = PCB->total_occ; - } - } - } - } - - PCB->pos = (k_mer_pos*)malloc(sizeof(k_mer_pos)*PCB->total_occ); - memset(PCB->pos, 0, sizeof(k_mer_pos)*PCB->total_occ); - - -} void Traverse_Counting_Table(Total_Count_Table* TCB, Total_Pos_Table* PCB, int k_mer_min_freq, int k_mer_max_freq) { @@ -3379,8 +1523,8 @@ void Traverse_Counting_Table(Total_Count_Table* TCB, Total_Pos_Table* PCB, int k long long freq_min, max, freq_up; ///get_peak_debug(TCB, &freq_min, &freq_max); get_peak(TCB, &freq_min, &max, &freq_up); - fprintf(stdout, "freq_min: %d, freq_max: %d, freq_up:%d\n", - freq_min, max, freq_up); + // fprintf(stdout, "freq_min: %d, freq_max: %d, freq_up:%d\n", + // freq_min, max, freq_up); if(freq_min < k_mer_min_freq) { k_mer_min_freq = freq_min; @@ -3390,11 +1534,11 @@ void Traverse_Counting_Table(Total_Count_Table* TCB, Total_Pos_Table* PCB, int k k_mer_max_freq = freq_up; } - fprintf(stdout, "k_mer_min_freq: %d, k_mer_max_freq: %d\n", - k_mer_min_freq, k_mer_max_freq); + // fprintf(stdout, "k_mer_min_freq: %d, k_mer_max_freq: %d\n", + // k_mer_min_freq, k_mer_max_freq); - khint_t t; ///这就是个迭代器 + khint_t t; int absent; long long count; @@ -3412,7 +1556,6 @@ void Traverse_Counting_Table(Total_Count_Table* TCB, Total_Pos_Table* PCB, int k sub_key = kh_key(h, k); get_total_freq(TCB, sub_ID, sub_key, &count); - ///只有符合频率范围要求的k-mer,才会被加入到pos table中 if (count>=k_mer_min_freq && count<=k_mer_max_freq) { t = kh_put(POS64, PCB->sub_h[sub_ID], sub_key, &absent); @@ -3422,7 +1565,7 @@ void Traverse_Counting_Table(Total_Count_Table* TCB, Total_Pos_Table* PCB, int k ///kh_value(PCB->sub_h[sub_ID], t) = useful_k_mer + total_occ; kh_value(PCB->sub_h[sub_ID], t) = PCB->useful_k_mer; } - else ///哈希表中已有的元素 + else { ///kh_value(PCB->sub_h[sub_ID], t)++; fprintf(stderr, "ERROR\n"); @@ -3437,8 +1580,8 @@ void Traverse_Counting_Table(Total_Count_Table* TCB, Total_Pos_Table* PCB, int k } - fprintf(stdout, "useful_k_mer: %lld\n",PCB->useful_k_mer); - fprintf(stdout, "total_occ: %lld\n",PCB->total_occ); + // fprintf(stdout, "useful_k_mer: %lld\n",PCB->useful_k_mer); + // fprintf(stdout, "total_occ: %lld\n",PCB->total_occ); PCB->k_mer_index = (uint64_t*)malloc(sizeof(uint64_t)*(PCB->useful_k_mer+1)); @@ -3562,439 +1705,6 @@ void destory_Candidates_list(Candidates_list* l) destory_Chain_Data(&(l->chainDP)); } -///1是x小,2是y小,0是相等 -inline int cmp_pos(k_mer_hit* x, k_mer_pos* y, uint64_t y_pos, uint8_t strand) -{ - if (x->strand < strand) - { - return 1; - } - else if (x->strand > strand) - { - return 2; - } - else - { - if (x->readID < y->readID) - { - return 1; - } - else if (x->readID > y->readID) - { - return 2; - } - else - { - if (x->offset < y->offset) - { - return 1; - } - else if (x->offset > y->offset) - { - return 2; - } - else - { - if (x->self_offset < y_pos) - { - return 1; - } - else if (x->self_offset > y_pos) - { - return 2; - } - else - { - return 0; - } - - } - } - } - -} - -void debug_merge_Candidates_list(Candidates_list* l, k_mer_pos* n_list, uint64_t n_lengh, uint64_t end_pos, uint64_t i, -uint64_t i_1, uint64_t i_2, uint64_t len1, uint64_t len2, uint64_t strand) -{ - if (i != l->length) - { - fprintf(stderr, "ERROR\n"); - fprintf(stderr, "i: %llu, l->length: %llu\n", i , l->length); - fprintf(stderr, "i_1: %llu, len1: %llu\n", i_1 , len1); - fprintf(stderr, "i_2: %llu, len2: %llu\n", i_2 , len2); - fprintf(stderr, "strand: %llu\n", strand); - - } - - for (i = 1; i < l->length; i++) - { - if (l->list[i].strand < l->list[i-1].strand) - { - fprintf(stderr, "ERROR -1\n"); - } - else if (l->list[i].strand == l->list[i-1].strand) - { - if (l->list[i].readID < l->list[i-1].readID) - { - fprintf(stderr, "ERROR 0\n"); - } - else if (l->list[i].readID == l->list[i-1].readID) - { - if (l->list[i].offset < l->list[i-1].offset) - { - fprintf(stderr, "ERROR 1\n"); - } - else if (l->list[i].offset == l->list[i-1].offset) - { - if (l->list[i].self_offset < l->list[i-1].self_offset) - { - fprintf(stderr, "ERROR 2\n"); - } - } - } - } - } - - for (i = 0; i < n_lengh; i++) - { - int j = 0; - for (j = 0; j < l->length; j++) - { - if ( - n_list[i].offset == l->list[j].offset - && - n_list[i].readID == l->list[j].readID - && - end_pos == l->list[j].self_offset - && - strand == l->list[j].strand - ) - { - break; - } - } - - if (j == l->length) - { - fprintf(stderr, "ERROR 4\n"); - } - } - - - for (i = 0; i < l->length - n_lengh; i++) - { - int j = 0; - for (j = 0; j < l->length; j++) - { - if ( - l->tmp[i].offset == l->list[j].offset - && - l->tmp[i].readID == l->list[j].readID - && - l->tmp[i].self_offset == l->list[j].self_offset - && - l->tmp[i].strand == l->list[j].strand - ) - { - break; - } - } - - if (j == l->length) - { - fprintf(stderr, "ERROR 5\n"); - } - } -} - -void merge_Candidates_list(Candidates_list* l, k_mer_pos* n_list, uint64_t n_lengh, uint64_t end_pos, int strand) -{ - if (n_lengh == 0) ///不加这个就会巨慢 - { - return; - } - - - if(l->length + n_lengh > l->size) - { - l->size = l->length + n_lengh; - l->list = (k_mer_hit*)realloc(l->list, sizeof(k_mer_hit)*l->size); - l->tmp = (k_mer_hit*)realloc(l->tmp, sizeof(k_mer_hit)*l->size); - } - - int flag; - uint64_t len1, len2, i_1, i_2, i; - k_mer_hit *l_1; - k_mer_pos *l_2; - - l_1 = l->list; - if (strand == 0) - { - ///由于标号全是0,则标号为1的位置不受影响(数组后半部分),可以直接复制过去 - ///需要从l_1 + l->rc_pos开始,复制l->length-l->rc_pos个元素,到l->tmp + l->rc_pos + n_lengh处 - memcpy(l->tmp + l->rc_pos + n_lengh, l_1 + l->rc_pos, sizeof(k_mer_hit)*(l->length-l->rc_pos)); - ///l_1只需要从0扫描到l->rc_pos就好了 - len1 = l->rc_pos; - i_1 = 0; - ///此时目标数组没数据, - i = 0; - ///更新l->rc_pos - l->rc_pos = l->rc_pos + n_lengh; - } - else - { - ///如果strand==1,则标号为0的位置不受影响(数组前半部分),可以直接复制过去 - ///从l_1开始,复制l->rc_pos个元素,到l->tmp - memcpy(l->tmp, l_1, sizeof(k_mer_hit)*l->rc_pos); - ///l_1只需要从l->rc_pos扫描到末尾就好了 - i_1 = l->rc_pos; - len1 = l->length; - ///此时目标数组已经有了l->rc_pos - i = i_1; - ///l->rc_pos不用更新 - } - - - - - l_2 = n_list; - len2 = n_lengh; - i_2 = 0; - - - while (i_1tmp[i].readID = l_1[i_1].readID; - l->tmp[i].offset = l_1[i_1].offset; - l->tmp[i].self_offset = l_1[i_1].self_offset; - l->tmp[i].strand = l_1[i_1].strand; - i_1++; - i++; - } - else ///l_2小或者l_1 == l_2 - { - l->tmp[i].readID = l_2[i_2].readID; - l->tmp[i].offset = l_2[i_2].offset; - l->tmp[i].self_offset = end_pos; - l->tmp[i].strand = strand; - i_2++; - i++; - } - } - - - while (i_1tmp + i, l_1 + i_1, sizeof(k_mer_hit)*(len1-i_1)); - i = i + len1-i_1; - i_1 = len1; - } - - while (i_2tmp[i].readID = l_2[i_2].readID; - l->tmp[i].offset = l_2[i_2].offset; - l->tmp[i].self_offset = end_pos; - l->tmp[i].strand = strand; - i_2++; - i++; - } - - - - k_mer_hit* k; - k = l->list; - l->list = l->tmp; - l->tmp = k; - l->length = l->length + n_lengh; - - - /** - if (strand == 0) - { - i = l->length; - len1 = l->length - n_lengh; - } - debug_merge_Candidates_list(l, n_list, n_lengh, end_pos, i, i_1, i_2, len1, len2, strand); - **/ - -} - - - - - - - -/********************************for debug***************************************/ -void merge_Candidates_list_version(Candidates_list* l, k_mer_pos* n_list, uint64_t n_lengh, uint64_t end_pos, int strand) -{ - if (n_lengh == 0) ///不加这个就会巨慢 - { - return; - } - - - if(l->length + n_lengh > l->size) - { - l->size = l->length + n_lengh; - l->list = (k_mer_hit*)realloc(l->list, sizeof(k_mer_hit)*l->size); - l->tmp = (k_mer_hit*)realloc(l->tmp, sizeof(k_mer_hit)*l->size); - } - - int flag; - uint64_t len1, len2, i_1, i_2, i; - k_mer_hit *l_1; - k_mer_pos *l_2; - - l_1 = l->list; - len1 = l->length; - i_1 = 0; - - l_2 = n_list; - len2 = n_lengh; - i_2 = 0; - - i = 0; - - while (i_1tmp[i].readID = l_1[i_1].readID; - l->tmp[i].offset = l_1[i_1].offset; - l->tmp[i].self_offset = l_1[i_1].self_offset; - l->tmp[i].strand = l_1[i_1].strand; - i_1++; - i++; - } - else ///l_2小或者l_1 == l_2 - { - l->tmp[i].readID = l_2[i_2].readID; - l->tmp[i].offset = l_2[i_2].offset; - l->tmp[i].self_offset = end_pos; - l->tmp[i].strand = strand; - i_2++; - i++; - } - } - - - while (i_1tmp[i].readID = l_1[i_1].readID; - l->tmp[i].offset = l_1[i_1].offset; - l->tmp[i].self_offset = l_1[i_1].self_offset; - l->tmp[i].strand = l_1[i_1].strand; - i_1++; - i++; - } - - while (i_2tmp[i].readID = l_2[i_2].readID; - l->tmp[i].offset = l_2[i_2].offset; - l->tmp[i].self_offset = end_pos; - l->tmp[i].strand = strand; - i_2++; - i++; - } - - k_mer_hit* k; - k = l->list; - l->list = l->tmp; - l->tmp = k; - l->length = l->length + n_lengh; - - ///debug_merge_Candidates_list(l, n_list, n_lengh, end_pos, i, i_1, i_2, len1, len2, strand); -} - - - - - - - -/********************************for debug***************************************/ -void debug_mode(uint64_t d, uint64_t thread_ID, uint64_t thread_num) -{ - fprintf(stderr, "#%llu: Start debug_mode...\n", thread_ID); - - uint64_t i; - - for (i = thread_ID; i < 4,294,967,296; i = i + thread_num) - { - if(i%d != mod_d(0, i, d)) - { - fprintf(stderr, "ERROR mode, i: %llu\n", i); - fprintf(stderr, "i mod d: %llu\n", i%d); - fprintf(stderr, "mod_d(0, i, d): %llu\n", mod_d(0, i, d)); - } - - } - - fprintf(stderr, "#%llu: Finish debug_mode\n", thread_ID); - -} - - -/********************************for debug***************************************/ -void test_COUNT64() -{ - - uint64_t sb[7] = {5000000000, 6000000000, 7000000000, 8000000000, 9000000000, 10000000000 ,5000000000}; - Count_Table* h; - ///构建哈希表 - init_Count_Table(&h); - - int i; - khint_t k; ///这就是个迭代器 - int absent; - - for (i = 0; i < 7; i++) - { - ///将5作为key插入到哈希表COUNT64中 - ///absent为0代表哈希表里面已经有这个key了 - k = kh_put(COUNT64, h, sb[i], &absent); - ///代表插入了一个哈希表中没有的元素 - ///则直接插入 - if (absent) - { - kh_value(h, k) = i; - } - else ///哈希表中已有的元素 - { - kh_value(h, k) = i; - } - } - - for (i = 0; i < 7; i++) - { - ///查询哈希表,key为k - k = kh_get(COUNT64, h, sb[i]); - - if (k != kh_end(h)) - { - fprintf(stderr, "value: %d\n", kh_value(h, k)); - } - else - { - fprintf(stderr, "not found!\n"); - } - } - - - kh_destroy(COUNT64, h); - - -} int cmp_candidates_list(const void * a, const void * b) @@ -4039,159 +1749,6 @@ int cmp_candidates_list(const void * a, const void * b) } -void sort_candidates(Candidates_list* candidates, long long readID, -overlap_region_alloc* overlap_list, All_reads* R_INF) -{ - qsort(candidates->list, candidates->length, sizeof(k_mer_hit), cmp_candidates_list); - - - overlap_region tmp_region; - uint64_t i = 0; - long long current_pos_diff; - long long current_self_pos; - uint64_t current_ID; - uint64_t current_stand; - long long tmp_pos_distance; - long long tmp_self_pos_distance; - long long constant_distance = 5; - double error_rate = 0.05; - - if (candidates->length == 0) - { - return; - } - - - // i = 0; - // for (i = 0; i < candidates->length; i++) - // { - // fprintf(stderr, "i: %d, ada_offset: %d, offset: %d, self_offset: %d, strand: %d\n", - // i, candidates->list[i].offset - candidates->list[i].self_offset, - // candidates->list[i].offset, candidates->list[i].self_offset, - // candidates->list[i].strand); - // } - - - - - i = 0; - while (i < candidates->length) - { - - current_pos_diff = candidates->list[i].offset - candidates->list[i].self_offset; - current_self_pos = candidates->list[i].self_offset; - current_ID = candidates->list[i].readID; - current_stand = candidates->list[i].strand; - - tmp_region.shared_seed = 1; - ///这个是查询read的信息 - tmp_region.x_id = readID; - tmp_region.x_pos_s = candidates->list[i].self_offset; - tmp_region.x_pos_e = candidates->list[i].self_offset; - tmp_region.x_pos_strand = 0; ///永远是0 - - ///这个是被查询的read的信息 - tmp_region.y_id = current_ID; - tmp_region.y_pos_s = candidates->list[i].offset; - tmp_region.y_pos_e = candidates->list[i].offset; - tmp_region.y_pos_strand = candidates->list[i].strand; - - i++; - - - - while (i < candidates->length) - { - if (current_ID == candidates->list[i].readID && - current_stand == candidates->list[i].strand) - { - ///这个一定是正值 - tmp_pos_distance = candidates->list[i].offset - candidates->list[i].self_offset - current_pos_diff; - ///这个不一定是正值 - tmp_self_pos_distance = candidates->list[i].self_offset - current_self_pos; - - if(tmp_self_pos_distance < 0) - { - ///fprintf(stderr, "tmp_self_pos_distance: %d\n", tmp_self_pos_distance); - tmp_self_pos_distance = tmp_self_pos_distance * -1; - ///fprintf(stderr, "tmp_self_pos_distance: %d\n", tmp_self_pos_distance); - } - - ///if (tmp_self_pos_distance >= 0) - { - tmp_self_pos_distance = tmp_self_pos_distance * error_rate + constant_distance; - if (tmp_pos_distance < tmp_self_pos_distance) - { - /****************************may have bugs********************************/ - ///i++; - // tmp_region.x_pos_e = candidates->list[i].self_offset; - // tmp_region.y_pos_e = candidates->list[i].offset; - /****************************may have bugs********************************/ - tmp_region.shared_seed++; - - if(candidates->list[i].self_offset < tmp_region.x_pos_s) - { - tmp_region.x_pos_s = candidates->list[i].self_offset; - } - - if(candidates->list[i].self_offset > tmp_region.x_pos_e) - { - tmp_region.x_pos_e = candidates->list[i].self_offset; - } - - if(candidates->list[i].offset < tmp_region.y_pos_s) - { - tmp_region.y_pos_s = candidates->list[i].offset; - } - - - if(candidates->list[i].offset > tmp_region.y_pos_e) - { - tmp_region.y_pos_e = candidates->list[i].offset; - } - - - /****************************may have bugs********************************/ - i++; - /****************************may have bugs********************************/ - continue; - } - } - } - - break; - } - - - - - - - - - - ///自己和自己重叠的要排除 - ///if (tmp_region.x_id != tmp_region.y_id && tmp_region.shared_seed > 1) - if (tmp_region.x_id != tmp_region.y_id) - { - // fprintf(stderr, "i: %lld, candidates->length: %lld, tmp_region->shared_seed: %lld, x_pos_s: %lld, x_pos_e: %lld, y_pos_s: %lld, y_pos_e: %lld\n", - // i, candidates->length, tmp_region.shared_seed, tmp_region.x_pos_s, tmp_region.x_pos_e, tmp_region.y_pos_s, tmp_region.y_pos_e); - // fprintf(stderr, "\n"); - - // fprintf(stderr, "shared_seed: %d, x_pos_s: %d, x_pos_e: %d, y_pos_s: %d, y_pos_e: %d\n", - // tmp_region.shared_seed, tmp_region.x_pos_s, tmp_region.x_pos_e, tmp_region.y_pos_s, tmp_region.y_pos_e); - ///append_overlap_region_alloc(overlap_list, &tmp_region, R_INF); - append_overlap_region_alloc_from_existing(overlap_list, &tmp_region, R_INF); - - } - } - - ///以x_pos_e,即结束位置为主元排序 - ///qsort(overlap_list->list, overlap_list->length, sizeof(overlap_region), cmp_by_x_pos_e); - ///qsort(overlap_list->list, overlap_list->length, sizeof(overlap_region), cmp_by_x_pos_s); - - clear_Candidates_list(candidates); -} void init_fake_cigar(Fake_Cigar* x) @@ -4244,7 +1801,7 @@ void add_fake_cigar(Fake_Cigar* x, uint32_t gap_site, int32_t gap_shift) } -void resize_fake_cigar(Fake_Cigar* x, long long size) +void resize_fake_cigar(Fake_Cigar* x, uint64_t size) { if(size > x->size) { diff --git a/Hash_Table.h b/Hash_Table.h index dff6500..f1b997f 100644 --- a/Hash_Table.h +++ b/Hash_Table.h @@ -31,7 +31,7 @@ typedef khash_t(POS64) Pos_Table; #define FINAL_OVERLAP_ERROR_RATE 0.03 #define GROUP_SIZE 4 -///最长是10M10D10M10D10M这种 +///the max cigar likes 10M10D10M10D10M ///#define CIGAR_MAX_LENGTH THRESHOLD*2+2 #define CIGAR_MAX_LENGTH 31*2+4 @@ -93,8 +93,6 @@ typedef struct int extra_begin; int extra_end; int error_threshold; - ///int y_pre_start; - ///error小于等于0都要重新算 int error; CIGAR cigar; } window_list; @@ -218,20 +216,7 @@ typedef struct } Total_Pos_Table; -/********************************for debug***************************************/ -inline void print_64bit(uint64_t x) -{ - int i; - for(i = 63; i >= 0; i--) - { - if(x & ((1ULL<x[0] | (code->x[1] << k); - low_key = code->x[0] | (code->x[1] << SAFE_SHIFT(k)); - //k不可能为0, 所以这个右移不会有问题 - h_key = code->x[1] >> (64 - k); - if(mod_d(h_key, low_key, MODE_VALUE) > 3) - { - return 0; - } - - return 1; -} ////suffix_bits = 64 in default inline int recover_hash_code(uint64_t sub_ID, uint64_t sub_key, Hash_code* code, @@ -278,6 +248,8 @@ uint64_t suffix_mode, int suffix_bits, int k) code->x[1] = h_key << (64 - k); code->x[1] = code->x[1] | (low_key >> SAFE_SHIFT(k)); + + return 1; } ///inline int get_sub_table(uint64_t* get_sub_ID, uint64_t* get_sub_key, Total_Count_Table* TCB, Hash_code* code, int k) @@ -285,10 +257,10 @@ inline int get_sub_table(uint64_t* get_sub_ID, uint64_t* get_sub_key, uint64_t s Hash_code* code, int k) { uint64_t h_key, low_key; - ///k有可能是64,所以可能会有问题 + ///k might be 64,so it is unsafe ///low_key = code->x[0] | (code->x[1] << k); low_key = code->x[0] | (code->x[1] << SAFE_SHIFT(k)); - //k不可能为0, 所以这个右移不会有问题 + //k cannot be 0, so this shift is safe h_key = code->x[1] >> (64 - k); if(mod_d(h_key, low_key, MODE_VALUE) > 3) @@ -296,25 +268,13 @@ Hash_code* code, int k) return 0; } - - ///注意suffix_bits最大就是64 - ///前一个右移不安全,因为TCB->suffix_bits有可能为64 - ///后一个左移安全,因为TCB->suffix_bits不可能为0 - //uint64_t sub_ID = (low_key >> TCB->suffix_bits) | (h_key << (64 - TCB->suffix_bits)); uint64_t sub_ID = (low_key >> SAFE_SHIFT(suffix_bits)) | (h_key << (64 - suffix_bits)); uint64_t sub_key = (low_key & suffix_mode); *get_sub_ID = sub_ID; *get_sub_key = sub_key; - - // Hash_code de_code; - // recover_hash_code(sub_ID, sub_key, &de_code, suffix_mode, suffix_bits, k); - ///if(de_code.x[0] != (*code).x[0] || de_code.x[1] != (*code).x[1]) fprintf(stderr, "hehe\n"); - ///if(de_code.x[0] == (*code).x[0] || de_code.x[1] == (*code).x[1]) fprintf(stderr, "hehe\n"); - return 1; - } @@ -326,7 +286,7 @@ inline int insert_Total_Count_Table(Total_Count_Table* TCB, Hash_code* code, int return 0; } - khint_t t; ///这就是个迭代器 + khint_t t; int absent; @@ -340,7 +300,7 @@ inline int insert_Total_Count_Table(Total_Count_Table* TCB, Hash_code* code, int { kh_value(TCB->sub_h[sub_ID], t) = 1; } - else ///哈希表中已有的元素 + else { //kh_value(TCB->sub_h[sub_ID], t) = kh_value(TCB->sub_h[sub_ID], t) + 1; kh_value(TCB->sub_h[sub_ID], t)++; @@ -360,10 +320,9 @@ inline int get_Total_Count_Table(Total_Count_Table* TCB, Hash_code* code, int k) return 0; } - khint_t t; ///这就是个迭代器 - int absent; + khint_t t; - ///查询哈希表,key为k + ///query hash table,key is k t = kh_get(COUNT64, TCB->sub_h[sub_ID], sub_key); if (t != kh_end(TCB->sub_h[sub_ID])) @@ -389,10 +348,9 @@ inline uint64_t get_Total_Pos_Table(Total_Pos_Table* PCB, Hash_code* code, int k return (uint64_t)-1; } - khint_t t; ///这就是个迭代器 - int absent; + khint_t t; - ///查询哈希表,key为k + ///query hash table,key is k t = kh_get(POS64, PCB->sub_h[sub_ID], sub_key); if (t != kh_end(PCB->sub_h[sub_ID])) @@ -441,7 +399,6 @@ inline uint64_t locate_Total_Pos_Table(Total_Pos_Table* PCB, Hash_code* code, k_ int cmp_k_mer_pos(const void * a, const void * b); -//inline uint64_t insert_Total_Pos_Table(Total_Pos_Table* PCB, Hash_code* code, int k, uint64_t readID, uint64_t pos, uint64_t direction) inline uint64_t insert_Total_Pos_Table(Total_Pos_Table* PCB, Hash_code* code, int k, uint64_t readID, uint64_t pos) { k_mer_pos* list; @@ -474,8 +431,7 @@ inline uint64_t insert_Total_Pos_Table(Total_Pos_Table* PCB, Hash_code* code, in __sync_lock_release(&PCB->sub_h_lock[sub_ID].lock); - ///当所有位置都存好后,不会再有其他线程修改该list - ///所以可以在临界区外排序 + //if all pos has been saved, it is safe to sort if (flag && occ>1) { qsort(list, occ, sizeof(k_mer_pos), cmp_k_mer_pos); @@ -510,7 +466,6 @@ void Traverse_Counting_Table(Total_Count_Table* TCB, Total_Pos_Table* PCB, int k void init_Candidates_list(Candidates_list* l); void clear_Candidates_list(Candidates_list* l); void destory_Candidates_list(Candidates_list* l); -void merge_Candidates_list(Candidates_list* l, k_mer_pos* n_list, uint64_t n_lengh, uint64_t end_pos, int strand); void init_k_mer_pos_list_alloc(k_mer_pos_list_alloc* list); @@ -521,9 +476,7 @@ uint64_t n_end_pos, uint8_t n_direction); -void merge_k_mer_pos_list_alloc(k_mer_pos_list_alloc* list, Candidates_list* candidates); void merge_k_mer_pos_list_alloc_heap_sort(k_mer_pos_list_alloc* list, Candidates_list* candidates, HeapSq* HBT); -void merge_k_mer_pos_list_alloc_heap_sort_advance(k_mer_pos_list_alloc* list, Candidates_list* candidates, HeapSq* HBT); void Init_Heap(HeapSq* HBT); void destory_Heap(HeapSq* HBT); @@ -532,48 +485,24 @@ void clear_Heap(HeapSq* HBT); void init_overlap_region_alloc(overlap_region_alloc* list); void clear_overlap_region_alloc(overlap_region_alloc* list); void destory_overlap_region_alloc(overlap_region_alloc* list); -void append_overlap_region_alloc(overlap_region_alloc* list, overlap_region* tmp, All_reads* R_INF); -void calculate_overlap_region(Candidates_list* candidates, overlap_region_alloc* overlap_list, -uint64_t readID, uint64_t readLength, All_reads* R_INF); void append_window_list(overlap_region* region, uint64_t x_start, uint64_t x_end, int y_start, int y_end, int error, int extra_begin, int extra_end, int error_threshold); -void insert_kv_list_to_candidates(k_v* list, long long occ, long long y_id, long long y_offset, long long y_strand, -Candidates_list* candidates); void overlap_region_sort_y_id(overlap_region *a, long long n); -void calculate_inexact_overlap_region(Candidates_list* candidates, overlap_region_alloc* overlap_list, -uint64_t readID, uint64_t readLength, All_reads* R_INF); void calculate_overlap_region_by_chaining(Candidates_list* candidates, overlap_region_alloc* overlap_list, uint64_t readID, uint64_t readLength, All_reads* R_INF, double band_width_threshold, int add_beg_end); - - - -static const char LogTable256[256] = { -#define LT(n) n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n - -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, - LT(4), LT(5), LT(5), LT(6), LT(6), LT(6), LT(6), - LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7) -}; - -static inline int ilog2_32(uint32_t v) -{ - uint32_t t, tt; - if ((tt = v>>16)) return (t = tt>>8) ? 24 + LogTable256[t] : 16 + LogTable256[tt]; - return (t = v>>8) ? 8 + LogTable256[t] : LogTable256[v]; -} - void init_fake_cigar(Fake_Cigar* x); void destory_fake_cigar(Fake_Cigar* x); void clear_fake_cigar(Fake_Cigar* x); void add_fake_cigar(Fake_Cigar* x, uint32_t gap_site, int32_t gap_shift); -void resize_fake_cigar(Fake_Cigar* x, long long size); +void resize_fake_cigar(Fake_Cigar* x, uint64_t size); int get_fake_gap_pos(Fake_Cigar* x, int index); int get_fake_gap_shift(Fake_Cigar* x, int index); inline long long y_start_offset(long long x_start, Fake_Cigar* o) @@ -585,7 +514,7 @@ inline long long y_start_offset(long long x_start, Fake_Cigar* o) long long i; - for (i = 0; i < o->length; i++) + for (i = 0; i < (long long)o->length; i++) { if(x_start < get_fake_gap_pos(o, i)) { @@ -593,7 +522,7 @@ inline long long y_start_offset(long long x_start, Fake_Cigar* o) } } - if(i == 0 || i == o->length) + if(i == 0 || i == (long long)o->length) { fprintf(stderr, "ERROR\n"); exit(0); @@ -606,139 +535,16 @@ inline long long y_start_offset(long long x_start, Fake_Cigar* o) inline void print_fake_gap(Fake_Cigar* o) { long long i; - for (i = 0; i < o->length; i++) + for (i = 0; i < (long long)o->length; i++) { - fprintf(stderr, "**i: %d, gap_pos_in_x: %d, gap_shift: %d\n", + fprintf(stderr, "**i: %lld, gap_pos_in_x: %d, gap_shift: %d\n", i, get_fake_gap_pos(o, i), get_fake_gap_shift(o, i)); } } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -/********************************for debug***************************************/ -inline int verify_Total_Count_Table(Total_Count_Table* TCB, Hash_code* code, int k) -{ - uint64_t sub_ID, sub_key; - if(!get_sub_table(&sub_ID, &sub_key, TCB->suffix_mode, TCB->suffix_bits, code, k)) - { - return 0; - } - - khint_t t; ///这就是个迭代器 - int absent; - - ///查询哈希表,key为k - t = kh_get(COUNT64, TCB->sub_h[sub_ID], sub_key); - - if (t != kh_end(TCB->sub_h[sub_ID])) - { - kh_value(TCB->sub_h[sub_ID], t)--; - if (kh_value(TCB->sub_h[sub_ID], t)<0) - { - return -1; - } - else - { - return 1; - } - } - else - { - return -1; - } -} - - - - - - -/********************************for debug***************************************/ -inline int Traverse_Total_Count_Table(Total_Count_Table* TCB) -{ - int i; - Count_Table* h; - khint_t k; - - long long non_empty_k_mer = 0; - - for (i = 0; i < TCB->size; i++) - { - h = TCB->sub_h[i]; - for (k = kh_begin(h); k != kh_end(h); ++k) - { - if (kh_exist(h, k)) // test if a bucket contains data - { - non_empty_k_mer++; - - if (kh_value(h, k)!= 0) - { - fprintf(stderr, "ERROR when Traversing!\n"); - } - } - } - } - - fprintf(stdout, "non_empty_k_mer: %lld\n", non_empty_k_mer); -} - - -/********************************for debug***************************************/ -void test_COUNT64(); - -/********************************for debug***************************************/ -void debug_mode(uint64_t d, uint64_t thread_ID, uint64_t thread_num); - - - -/********************************for debug***************************************/ -void merge_Candidates_list_version(Candidates_list* l, k_mer_pos* n_list, uint64_t n_lengh, uint64_t end_pos, int strand); - -void sort_candidates(Candidates_list* candidates, long long readID, -overlap_region_alloc* overlap_list, All_reads* R_INF); -void append_overlap_region_alloc_from_existing(overlap_region_alloc* list, overlap_region* tmp, All_reads* R_INF); -int cmp_by_x_pos_s(const void * a, const void * b); void resize_Chain_Data(Chain_Data* x, long long size); - - - void init_window_list_alloc(window_list_alloc* x); void clear_window_list_alloc(window_list_alloc* x); void destory_window_list_alloc(window_list_alloc* x); diff --git a/Levenshtein_distance.cpp b/Levenshtein_distance.cpp index 6468c31..4622c9d 100644 --- a/Levenshtein_distance.cpp +++ b/Levenshtein_distance.cpp @@ -1,806 +1 @@ #include "Levenshtein_distance.h" - -void output_bit_myers(Word x, int length) -{ - int i = 0; - while (i < length) - { - fprintf(stderr, "%u", (x >> i) & ((Word)1)); - i++; - } - fprintf(stderr, "\n"); -} - -void prase_vertical(Word VP, Word VN, int length, int matrix[1000][1000], int i) -{ - - i++; - int k = 0; - int j = i; - int diff; - while (k < length) - { - - int x_p = (VP >> k) & ((Word)1); - int x_n = (VN >> k) & ((Word)1); - if (x_p == 1 && x_n == 1) - { - fprintf(stderr, "error\n"); - } - - - - if (x_p == 1) - { - diff = 1; - ///fprintf(stderr, "[+1]"); - } - - if (x_n == 1) - { - diff = -1; - ///fprintf(stderr, "[-1]"); - } - - if (x_p == 0 && x_n == 0) - { - diff = 0; - ///fprintf(stderr, "[+0]"); - } - j++; - if (matrix[i][j] - matrix[i][j - 1] != diff) - { - fprintf(stderr, "*************V(k): %u\n", k); - ///return; - } - k++; - } - ///fprintf(stderr, "\n"); -} - -void prase_D0(Word D0, int length, int matrix[1000][1000], int i) -{ - - i++; - int k = 0; - int j = i; - int diff; - while (k < length) - { - - int diff = (D0 >> k) & ((Word)1); - - if(diff==matrix[i][j] - matrix[i-1][j-1]) - { - fprintf(stderr, "*************D(k): %u\n", k); - fprintf(stderr, "diff: %u, matrix[i][j]: %u, matrix[i-1][j-1]: %u\n", diff, matrix[i][j], matrix[i-1][j-1]); - ///return; - } - j++; - k++; - } - ///fprintf(stderr, "\n"); -} - -void prase_H(Word HP, Word HN, int length, int matrix[1000][1000], int i) -{ - - i++; - int k = 0; - int j = i; - int diff; - while (k < length) - { - - int x_p = (HP >> k) & ((Word)1); - int x_n = (HN >> k) & ((Word)1); - if (x_p == 1 && x_n == 1) - { - fprintf(stderr, "error\n"); - } - if (x_p == 1) - { - diff = 1; - ///fprintf(stderr, "[+1]"); - } - if (x_n == 1) - { - diff = -1; - ///fprintf(stderr, "[-1]"); - } - if (x_p == 0 && x_n == 0) - { - diff = 0; - ///fprintf(stderr, "[+0]"); - } - - if(diff!=matrix[i][j] - matrix[i-1][j]) - { - fprintf(stderr, "*************H(k): %u\n", k); - ///return; - } - - j++; - k++; - } - ///fprintf(stderr, "\n"); -} - -/** - pattern是长的那个,是y - p_length是长的那个的长度, p_length实际没用 - text是短的那个,是x - t_length是短的那个的长度 - errthold是阈值 - return_err是编辑距离 - 返回值是结束位置 - **/ -int Reserve_Banded_BPM_debug -(char *pattern, int p_length, char *text, int t_length, unsigned short errthold, unsigned int* return_err, int matrix[1000][1000]) -{ - (*return_err) = (unsigned int)-1; - - Word Peq[256]; - - int band_length = (errthold << 1) + 1; - int i = 0; - Word tmp_Peq_1 = (Word)1; - - Peq['A'] = (Word)0; - Peq['T'] = (Word)0; - Peq['G'] = (Word)0; - Peq['C'] = (Word)0; - - - Word Peq_A; - Word Peq_T; - Word Peq_C; - Word Peq_G; - - ///band_length = 2k + 1 - for (i = 0; i= 6) - if (i >= 0) - { - /** - fprintf(stderr, "VP:\n"); - output_bit_myers(VP, band_length); - - fprintf(stderr, "VN:\n"); - output_bit_myers(VN, band_length); - - fprintf(stderr, "HP:\n"); - output_bit_myers(HP, band_length); - - fprintf(stderr, "HN:\n"); - output_bit_myers(HN, band_length); - - fprintf(stderr, "D0:\n"); - output_bit_myers(D0, band_length); - - fprintf(stderr, "text[i]: %c\n", text[i]); - - fprintf(stderr, "Peq[text[i]]:\n"); - output_bit_myers(Peq[text[i]], band_length); - - for (size_t j = i; j < i + band_length; j++) - { - fprintf(stderr, "%c", pattern[j]); - } - fprintf(stderr, "\n"); - - fprintf(stderr, "Previous begin.\n"); - prase_vertical(VP, VN, band_length, matrix, i-1); - prase_D0(D0, band_length, matrix, i-1); - prase_H(HP, HN, band_length, matrix, i-1); - fprintf(stderr, "Previous test done.\n"); - **/ - - X = Peq[text[i]] | VN; - /** - fprintf(stderr, "#X:\n"); - output_bit_myers(X, band_length); - **/ - - D0 = ((VP + (X&VP)) ^ VP) | X; - /** - fprintf(stderr, "#(X&VP):\n"); - output_bit_myers((X&VP), band_length); - - fprintf(stderr, "#(VP + (X&VP)):\n"); - output_bit_myers((VP + (X&VP)), band_length); - - fprintf(stderr, "#((VP + (X&VP)) ^ VP):\n"); - output_bit_myers(((VP + (X&VP)) ^ VP), band_length); - - fprintf(stderr, "#D0:\n"); - output_bit_myers(D0, band_length); - **/ - HN = VP&D0; - HP = VN | ~(VP | D0); - - X = D0 >> 1; - VN = X&HP; - VP = HN | ~(X | HP); - } - else - { - ///pattern[0]ÔÚPeq[2k], ¶øpattern[2k]ÔÚPeq[0] - X = Peq[text[i]] | VN; - - D0 = ((VP + (X&VP)) ^ VP) | X; - - HN = VP&D0; - HP = VN | ~(VP | D0); - - X = D0 >> 1; - VN = X&HP; - VP = HN | ~(X | HP); - } - - - - - - - - /** - for (size_t j = i + 1; j <= i + band_length + 1; j++) - { - fprintf(stderr, "[%u]", matrix[i + 1][j]); - } - fprintf(stderr, "\n"); - **/ - - prase_vertical(VP, VN, band_length, matrix, i); - prase_D0(D0, band_length, matrix, i); - prase_H(HP, HN, band_length, matrix, i); - /** - fprintf(stderr, "VP:\n"); - output_bit_myers(VP, band_length); - - fprintf(stderr, "VN:\n"); - output_bit_myers(VN, band_length); - **/ - - - if (!(D0&err_mask)) - { - ++err; - - ///¼´Ê¹È«²¿µÝ¼õ£¬Ò²¾Í¼õ2k - if ((err - last_high)>errthold) - { - ///fprintf(stderr, "0 ######, i: %u\n", i); - return -1; - } - - } - - - Peq['A'] = Peq['A'] >> 1; - Peq['C'] = Peq['C'] >> 1; - Peq['G'] = Peq['G'] >> 1; - Peq['T'] = Peq['T'] >> 1; - - - ++i; - ++i_bd; - Peq[pattern[i_bd]] = Peq[pattern[i_bd]] | Mask; - - - ///Peq['T'] = Peq['T'] | Peq['C']; - } - - - - - - X = Peq[text[i]] | VN; - D0 = ((VP + (X&VP)) ^ VP) | X; - HN = VP&D0; - HP = VN | ~(VP | D0); - X = D0 >> 1; - VN = X&HP; - VP = HN | ~(X | HP); - if (!(D0&err_mask)) - { - ++err; - if ((err - last_high)>errthold) - return -1; - } - - prase_vertical(VP, VN, band_length, matrix, i); - prase_D0(D0, band_length, matrix, i); - prase_H(HP, HN, band_length, matrix, i); - - - fprintf(stderr, "err: %d, matrix[][]: %d\n", err, matrix[i+1][i+1]); - fprintf(stderr, "VP:\n"); - output_bit_myers(VP, band_length); - - fprintf(stderr, "VN:\n"); - output_bit_myers(VN, band_length); - - ////fprintf(stderr, "sucess(2)\n"); - - /// last_high = 2k - /// site = (SEQ_LENGTH + 2k) - 2k -1 - /// site = SEQ_LENGTH - 1 - ///int site = p_length - last_high - 1; - int site = t_length - 1; - int return_site = -1; - if ((err <= errthold) && (err<=*return_err)) - { - *return_err = err; - return_site = site; - } - int i_last = i; - i = 0; - - - - - while (i> i)&(Word)1); - err = err - ((VN >> i)&(Word)1); - ++i; - - fprintf(stderr, "*i: %u, err: %d\n", i, err); - - if ((err <= errthold) && (err <= *return_err)) - { - *return_err = err; - return_site = site + i; - } - } - - - unsigned int ungap_err; - ungap_err = err; - - - while (i> i)&(Word)1); - err = err - ((VN >> i)&(Word)1); - ++i; - - fprintf(stderr, "*i: %u, err: %d\n", i, err); - - if ((err <= errthold) && (err<=*return_err)) - { - *return_err = err; - return_site = site + i; - } - - - - - } - - - if ((ungap_err <= errthold) && (ungap_err == *return_err)) - { - return_site = site + errthold; - } - - return return_site; - -} - -int BS_Reserve_Banded_BPM -(char *pattern, int p_length, char *text, int t_length, unsigned short errthold, unsigned int* return_err) -{ - (*return_err) = (unsigned int)-1; - - ///Õâ¸öÊÇÄǸöÐèÒªÔ¤´¦ÀíµÄÏòÁ¿ - Word Peq[256]; - - int band_length = (errthold << 1) + 1; - int i = 0; - Word tmp_Peq_1 = (Word)1; - - Peq['A'] = (Word)0; - Peq['T'] = (Word)0; - Peq['G'] = (Word)0; - Peq['C'] = (Word)0; - - - Word Peq_A; - Word Peq_T; - Word Peq_C; - Word Peq_G; - - ///band_length = 2k + 1 - ///ÕâÊǰÑpatternµÄǰ2k + 1¸ö×Ö·ûÔ¤´¦Àí - ///pattern[0]¶ÔÓ¦Peq[0] - ///pattern[2k]¶ÔÓ¦Peq[2k] - for (i = 0; i> 1; - VN = X&HP; - VP = HN | ~(X | HP); - ///Èç¹ûб¶Ô½ÇÏß·½ÏòÆ¥ÅäÔòD0ÊÇ1 - ///Èç¹û²»Æ¥ÅäÔòD0ÊÇ0 - ///Õâ¸öÒâ˼ÊÇÈç¹û×îÉÏÃæÄÇÌõ¶Ô½ÇÏßÉϵÄб¶Ô½ÇÏß·½Ïò·¢ÉúÎóÅä,ÔòÖ´ÐÐÄÚ²¿³ÌÐò - /// - if (!(D0&err_mask)) - { - ++err; - - ///¼´Ê¹È«²¿µÝ¼õ£¬Ò²¾Í¼õ2k - if ((err - last_high)>errthold) - return -1; - } - - ///pattern[0]ÔÚPeq[2k], ¶øpattern[2k]ÔÚPeq[0] - //ÓÒÒÆÊµ¼ÊÉÏÊǰÑpattern[0]ÒÆµôÁË - Peq['A'] = Peq['A'] >> 1; - Peq['C'] = Peq['C'] >> 1; - Peq['G'] = Peq['G'] >> 1; - Peq['T'] = Peq['T'] >> 1; - - - ++i; - ++i_bd; - ///ÕâÊǰÑеÄpattern[2k]¼Ó½øÀ´, ÕâÃ²ËÆÊǼӵ½Peq[2k]ÉÏÁË - Peq[pattern[i_bd]] = Peq[pattern[i_bd]] | Mask; - - - Peq['T'] = Peq['T'] | Peq['C']; - } - - - - - ///fprintf(stderr, "sucess(1)\n"); - - - ///Õâ¸öÑ­»·ÄóöÀ´ÊÇΪÁË·ÀÖ¹ÄÚ´æÐ¹Â¶ - ///ÆäʵҲ¾ÍÊÇÑ­»·ÀïµÄ×îºóÒ»ÐÐÓï¾ä°É - ///ÍêÈ«¿ÉÒÔ°ÑpatternÔö´óһλ - ///²»¹ýÕâÑùÒ²ºÃ£¬¿ÉÒÔ¼õÉÙ¼ÆË㿪Ïú - X = Peq[text[i]] | VN; - D0 = ((VP + (X&VP)) ^ VP) | X; - HN = VP&D0; - HP = VN | ~(VP | D0); - X = D0 >> 1; - VN = X&HP; - VP = HN | ~(X | HP); - if (!(D0&err_mask)) - { - ++err; - if ((err - last_high)>errthold) - return -1; - } - - - - - - ////fprintf(stderr, "sucess(2)\n"); - - /// last_high = 2k - /// site = (SEQ_LENGTH + 2k) - 2k -1 - /// site = SEQ_LENGTH - 1 - ///´ËʱÕâ¸ösiteÃ²ËÆÊÇ×îÉÏÃæÄÇÌõ¶Ô½ÇÏßµÄλÖà - ///int site = p_length - last_high - 1; - int site = t_length - 1; - int return_site = -1; - if ((err <= errthold) && (err<=*return_err)) - { - *return_err = err; - return_site = site; - } - int i_last = i; - i = 0; - - - - - while (i> i)&(Word)1); - err = err - ((VN >> i)&(Word)1); - ++i; - - if ((err <= errthold) && (err <= *return_err)) - { - *return_err = err; - return_site = site + i; - } - } - - - unsigned int ungap_err; - ungap_err = err; - - - while (i> i)&(Word)1); - err = err - ((VN >> i)&(Word)1); - ++i; - - if ((err <= errthold) && (err<=*return_err)) - { - *return_err = err; - return_site = site + i; - } - - - - - } - - - if ((ungap_err <= errthold) && (ungap_err == *return_err)) - { - return_site = site + errthold; - } - - return return_site; - -} - -int Reserve_Banded_BPM_new(char *pattern,int p_length,char *text,int t_length, - unsigned short errthold,unsigned short band_down,unsigned short band_below,unsigned short band_length,int* return_err, int thread_id) -{ - - Word Peq[128]; - char Peq_index[4]= {'A','C','G','T'}; - int symbol = 0; - int r; - Word tmp_Peq_1=(Word)1; - - - Peq['A']=(Word)0; - Peq['T']=(Word)0; - Peq['G']=(Word)0; - Peq['C']=(Word)0; - Word Peq_A; - Word Peq_T; - Word Peq_C; - Word Peq_G; - - for (r =0; r>1; - VN=X&HP; - VP=HN|~(X|HP); - if(!(D0&err_mask)) - { - ++err; - if((err-last_high)>errthold) - return -1; - } - - Peq['A']=Peq['A']>>1; - Peq['C']=Peq['C']>>1; - Peq['G']=Peq['G']>>1; - Peq['T']=Peq['T']>>1; - - - ++i; - ++i_bd; - Peq[pattern[i_bd]]=Peq[pattern[i_bd]]|Mask; - } - - - ///这个循环拿出来是为了防止内存泄露 - - X=Peq[text[i]]|VN; - D0=((VP+(X&VP))^VP)|X; - HN=VP&D0; - HP=VN|~(VP|D0); - X=D0>>1; - VN=X&HP; - VP=HN|~(X|HP); - if(!(D0&err_mask)) - { - ++err; - if((err-last_high)>errthold) - return -1; - } - - - int site=p_length-last_high-1; - int return_site=-1; - if((err<=errthold)&&(err<*return_err)) - { - *return_err=err; - return_site=site; - } - int i_last=i; - i=0; - while(i>i)&(Word)1); - err=err-((VN>>i)&(Word)1); - ++i; - - if((err<=errthold)&&(err<*return_err)) - { - *return_err=err; - return_site=site+i; - } - - - } - return return_site; - -} diff --git a/Levenshtein_distance.h b/Levenshtein_distance.h index b783b53..681d034 100644 --- a/Levenshtein_distance.h +++ b/Levenshtein_distance.h @@ -12,152 +12,18 @@ typedef uint64_t Word; typedef uint32_t Word_32; - -typedef struct -{ - /** - * vec[num_words-1], vec[num_words-2], ..., vec[1], vec[0] - * num_bits-1, num_bits-2, num_bits-3, ..., 8, 7, 6, 5, 4, 3, 2, 1, 0 - **/ - Word* vec; - Word num_words; - Word num_bits; - ///for vec[num_words-1] - Word last_bit_mode; - Word last_bit_shift; -} bit_vectors; - -inline void init_bit_vector(bit_vectors* b, Word num_bits) -{ - b->num_bits = num_bits; - b->num_words = ((b->num_bits)>>6); - b->last_bit_mode = (Word)-1; - b->last_bit_shift = b->num_bits&63; - if(b->last_bit_shift != 0) - { - b->num_words++; - b->last_bit_mode = b->last_bit_mode >> (64 - b->last_bit_shift); - } - b->vec = (Word*)calloc(b->num_words, sizeof(Word)); -} - -inline void destory_bit_vector(bit_vectors* b, Word num_bits) -{ - free(b->vec); -} - -///& -inline int AND_bit_vector(bit_vectors* x, bit_vectors* y, bit_vectors* dest) -{ - if(x->num_bits != y->num_bits || x->num_bits != dest->num_bits) return 0; - - Word i; - for (i = 0; i < x->num_words; i++) - { - dest->vec[i] = x->vec[i] & y->vec[i]; - } - - if(dest->num_words > 0) - { - dest->vec[dest->num_words - 1] = dest->vec[dest->num_words - 1] & dest->last_bit_mode; - } - - return 1; -} - -///| -inline int OR_bit_vector(bit_vectors* x, bit_vectors* y, bit_vectors* dest) -{ - if(x->num_bits != y->num_bits || x->num_bits != dest->num_bits) return 0; - - Word i; - for (i = 0; i < x->num_words; i++) - { - dest->vec[i] = x->vec[i] | y->vec[i]; - } - - if(dest->num_words > 0) - { - dest->vec[dest->num_words - 1] = dest->vec[dest->num_words - 1] & dest->last_bit_mode; - } - - return 1; -} - - -///^ -inline int XOR_bit_vector(bit_vectors* x, bit_vectors* y, bit_vectors* dest) -{ - if(x->num_bits != y->num_bits || x->num_bits != dest->num_bits) return 0; - - Word i; - for (i = 0; i < x->num_words; i++) - { - dest->vec[i] = x->vec[i] ^ y->vec[i]; - } - - if(dest->num_words > 0) - { - dest->vec[dest->num_words - 1] = dest->vec[dest->num_words - 1] & dest->last_bit_mode; - } - - return 1; -} - -///~ -inline int NOT_bit_vector(bit_vectors* source, bit_vectors* dest) -{ - if(source->num_bits != dest->num_bits) return 0; - - Word i; - for (i = 0; i < source->num_words; i++) - { - dest->vec[i] = ~(source->vec[i]); - } - - if(dest->num_words > 0) - { - dest->vec[dest->num_words - 1] = dest->vec[dest->num_words - 1] & dest->last_bit_mode; - } - - return 1; -} - -///<< 1 -inline int L_shift_1_bit_vector(bit_vectors* source, bit_vectors* dest) -{ - /** - * vec[num_words-1], vec[num_words-2], ..., vec[1], vec[0] - * num_bits-1, num_bits-2, num_bits-3, ..., 8, 7, 6, 5, 4, 3, 2, 1, 0 - **/ - if(source->num_bits != dest->num_bits || source->num_words < 1) return 0; - Word i; - for (i = source->num_words - 1; i >= 1; i--) - { - dest->vec[i] = (source->vec[i])<<1; - dest->vec[i] = dest->vec[i] | ((source->vec[i-1])>>63); - } - - dest->vec[0] = (source->vec[0])<<1; - dest->vec[dest->num_words - 1] = dest->vec[dest->num_words - 1] & dest->last_bit_mode; - return 1; -} - - inline void get_error(int t_length, int errthold, int init_err, Word VP, Word VN, unsigned int* return_err, int* back_site) { (*return_err) = (unsigned int)-1; int site = t_length - 1; int return_site = -1; - ///p_length大部分情况下应该是t_length + 2 * errthold,这是i要小于last_high = 2 * errthold - ///也就是p_length - t_length - ///那么当p_length < t_length + 2 * errthold, available_i也应该是这个值 + ///in most cases, p_length should be t_length + 2 * errthold ///int available_i = p_length - t_length; int available_i = 2 * errthold; - if ((init_err <= errthold) && (init_err <= (*return_err))) + if ((init_err <= errthold) && ((unsigned int)init_err <= (*return_err))) { (*return_err) = init_err; return_site = site; @@ -173,7 +39,7 @@ unsigned int* return_err, int* back_site) init_err = init_err - ((VN >> i)&(Word)1); ++i; - if ((init_err <= errthold) && (init_err <= *return_err)) + if ((init_err <= errthold) && ((unsigned int)init_err <= *return_err)) { *return_err = init_err; return_site = site + i; @@ -188,7 +54,7 @@ unsigned int* return_err, int* back_site) } /****************************may have bugs********************************/ - if((ungap_error<=errthold) && (ungap_error == (*return_err))) + if((ungap_error<=(unsigned int)errthold) && (ungap_error == (*return_err))) { return_site = site + errthold; } @@ -227,7 +93,7 @@ unsigned int* return_err, int* return_p_end, int* return_t_end) ///band_length = 2k + 1 for (i = 0; ierrthold) + if ((err - last_high)>(int)errthold) { - ///fprintf(stderr, "0 ######, i: %u\n", i); return -1; } @@ -527,17 +384,14 @@ inline int Reserve_Banded_BPM ++i; ++i_bd; - Peq[pattern[i_bd]] = Peq[pattern[i_bd]] | Mask; - - - ///Peq['T'] = Peq['T'] | Peq['C']; + Peq[(uint8_t)pattern[i_bd]] = Peq[(uint8_t)pattern[i_bd]] | Mask; } - X = Peq[text[i]] | VN; + X = Peq[(uint8_t)text[i]] | VN; D0 = ((VP + (X&VP)) ^ VP) | X; HN = VP&D0; HP = VN | ~(VP | D0); @@ -560,16 +414,13 @@ inline int Reserve_Banded_BPM ///int site = p_length - last_high - 1; int site = t_length - 1; int return_site = -1; - ///p_length大部分情况下应该是t_length + 2 * errthold,这是i要小于last_high = 2 * errthold - ///也就是p_length - t_length - ///那么当p_length < t_length + 2 * errthold, available_i也应该是这个值 + ///in most cases, p_lengthshould be t_length + 2 * errthold int available_i = p_length - t_length; - if ((err <= errthold) && (err<=*return_err)) + if ((err <= errthold) && ((unsigned int)err<=*return_err)) { *return_err = err; return_site = site; } - int i_last = i; i = 0; /****************************may have bugs********************************/ @@ -582,14 +433,14 @@ inline int Reserve_Banded_BPM err = err - ((VN >> i)&(Word)1); ++i; - if ((err <= errthold) && (err <= *return_err)) + if ((err <= (int)errthold) && ((unsigned int)err <= *return_err)) { *return_err = err; return_site = site + i; } /****************************may have bugs********************************/ - if(i == errthold) + if(i == (int)errthold) { ungap_error = err; } @@ -617,7 +468,7 @@ inline int try_cigar(char *pattern, int p_length, { int i = 0; int tmp_err = 0; - ///y上的起始位置 + ///start pos of y int start_site = end_site - t_length + 1; if (start_site >= 0) @@ -626,7 +477,7 @@ inline int try_cigar(char *pattern, int p_length, for (i = 0; i < t_length; i++) { ///path[i] = 0; - ///path倒着存 + ///path is saved backwards path[t_length - i - 1] = 0; if (text[i] != pattern[i + start_site]) { @@ -655,8 +506,8 @@ inline int try_cigar(char *pattern, int p_length, } -///p_length有可能不够,但是t_length总是够的 -///就是p_length有可能小于t_length + 2 * errthold + +///p_length might be samller than t_length + 2 * errthold inline int Reserve_Banded_BPM_PATH (char *pattern, int p_length, char *text, int t_length, unsigned short errthold, unsigned int* return_err, int* return_start_site, int* return_path_length, Word* matrix_bit, char* path, @@ -666,7 +517,6 @@ inline int Reserve_Banded_BPM_PATH { if (old_error == 0) { - ///fprintf(stderr, "0 error\n"); (*return_err) = old_error; (*return_start_site) = old_end_site - t_length + 1; return old_end_site; @@ -675,7 +525,6 @@ inline int Reserve_Banded_BPM_PATH if (try_cigar(pattern, p_length, text, t_length, old_end_site, path, old_error, return_start_site, return_path_length)) { - ///fprintf(stderr, "no gap error\n"); (*return_err) = old_error; return old_end_site; } @@ -704,7 +553,7 @@ inline int Reserve_Banded_BPM_PATH ///band_length = 2k + 1 for (i = 0; ierrthold) + if ((err - last_high)>(int)errthold) { return -1; } @@ -793,7 +642,7 @@ inline int Reserve_Banded_BPM_PATH ++i; ++i_bd; - Peq[pattern[i_bd]] = Peq[pattern[i_bd]] | Mask; + Peq[(uint8_t)pattern[i_bd]] = Peq[(uint8_t)pattern[i_bd]] | Mask; ///Peq['T'] = Peq['T'] | Peq['C']; @@ -810,7 +659,7 @@ inline int Reserve_Banded_BPM_PATH - X = Peq[text[i]] | VN; + X = Peq[(uint8_t)text[i]] | VN; D0 = ((VP + (X&VP)) ^ VP) | X; HN = VP&D0; HP = VN | ~(VP | D0); @@ -820,7 +669,7 @@ inline int Reserve_Banded_BPM_PATH if (!(D0&err_mask)) { ++err; - if ((err - last_high)>errthold) + if ((err - last_high)>(int)errthold) return -1; } @@ -846,16 +695,13 @@ inline int Reserve_Banded_BPM_PATH unsigned int ungap_error = (unsigned int)-1; /****************************may have bugs********************************/ - ///p_length大部分情况下应该是t_length + 2 * errthold,这是i要小于last_high = 2 * errthold - ///也就是p_length - t_length - ///那么当p_length < t_length + 2 * errthold, available_i也应该是这个值 + ///in most cases, p_length should be t_length + 2 * errthold int available_i = p_length - t_length; - if ((err <= errthold) && (err<=*return_err)) + if ((err <= (int)errthold) && ((unsigned int)err<=*return_err)) { *return_err = err; return_site = site; } - int i_last = i; i = 0; while (i < available_i) @@ -864,14 +710,14 @@ inline int Reserve_Banded_BPM_PATH err = err - ((VN >> i)&(Word)1); ++i; - if ((err <= errthold) && (err <= *return_err)) + if ((err <= (int)errthold) && ((unsigned int)err <= *return_err)) { *return_err = err; return_site = site + i; } /****************************may have bugs********************************/ - if(i == errthold) + if(i == (int)errthold) { ungap_error = err; } @@ -894,24 +740,19 @@ inline int Reserve_Banded_BPM_PATH } /****************************may have bugs********************************/ - ////注意,这里p_length要矫正啊啊 - ///不矫正会出错 - ///因为p_length有可能不够 + + ///need to correct p_length here, since p_length might be smaller than t_length + 2* err_threashlod p_length = t_length + 2 * errthold; - ///end_site是正确的 + ///end_site is always correct int end_site = return_site; int start_site = end_site; - ///这个是各个bit-vector里面,end_site对应bit所在的位置 int back_track_site = band_length - (p_length - end_site); Word v_value, h_value, delta_value, min_value, current_value; - Word direction, is_mismatch; ///0 is match, 1 is mismatch, 2 is up, 3 is left - - ///代表pattern到哪了,就是短的那个到哪了 + ///Word direction; ///0 is match, 1 is mismatch, 2 is up, 3 is left + Word direction = 0; ///0 is match, 1 is mismatch, 2 is up, 3 is left i = t_length; int path_length = 0; - - ///到0就结束了,后面的路径可以直接match current_value = *return_err; @@ -1044,15 +885,11 @@ inline int Reserve_Banded_BPM_PATH (*return_start_site) = start_site; (*return_path_length) = path_length; - - - return return_site; - } -////这个p_length四个是一样的 +////four patterns have the same p_length inline int Reserve_Banded_BPM_4_SSE_only(char *pattern1, char *pattern2, char *pattern3, char *pattern4, int p_length, char *text, int t_length, int* return_sites, unsigned int* return_sites_error, unsigned short errthold, __m128i* Peq_SSE) @@ -1076,10 +913,10 @@ inline int Reserve_Banded_BPM_4_SSE_only(char *pattern1, char *pattern2, char *p for (i = 0; ibuffer[(uint32_t)(source_index[i])]); - /** - ele.rev = tmp->rev; - ele.qns = Get_tn((*tmp)); - ele.qns = ele.qns << 32; - ele.qns = ele.qns | (uint64_t)(Get_ts((*tmp))); - ele.qe = Get_te((*tmp)); - - ele.tn = Get_qn((*tmp)); - ele.ts = Get_qs((*tmp)); - ele.te = Get_qe((*tmp)); - - ele.bl = R_INF.read_length[ele.tn]; - ele.ml = tmp->ml; - ele.el = tmp->el; - ele.no_l_indel = tmp->no_l_indel; - - add_ma_hit_t_alloc(dest_paf, &ele); - **/ add_ma_hit_t_alloc(dest_paf, tmp); } } @@ -92,8 +73,6 @@ void add_overlaps(ma_hit_t_alloc* source_paf, ma_hit_t_alloc* dest_paf, uint64_t void remove_overlaps(ma_hit_t_alloc* source_paf, uint64_t* source_index, long long listLen) { long long i, m; - ma_hit_t ele; - ma_hit_t* tmp; for (i = 0; i < listLen; i++) { source_paf->buffer[(uint32_t)(source_index[i])].qns = (uint64_t)(-1); @@ -146,16 +125,15 @@ uint64_t* source_index, long long listLen) void print_revise_edges(ma_hit_t_alloc* source_paf, uint64_t* source_index, long long listLen) { - long long i, m; - ma_hit_t ele; + long long i; ma_hit_t* tmp; for (i = 0; i < listLen; i++) { tmp = &(source_paf->buffer[(uint32_t)(source_index[i])]); fprintf(stderr, "%.*s(%d) ---(+)--> %.*s(%d), Len: %d\n", - Get_NAME_LENGTH(R_INF, Get_qn((*tmp))), Get_NAME(R_INF, Get_qn((*tmp))), Get_qn((*tmp)), - Get_NAME_LENGTH(R_INF, Get_tn((*tmp))), Get_NAME(R_INF, Get_tn((*tmp))), Get_tn((*tmp)), + (int)Get_NAME_LENGTH(R_INF, Get_qn((*tmp))), Get_NAME(R_INF, Get_qn((*tmp))), Get_qn((*tmp)), + (int)Get_NAME_LENGTH(R_INF, Get_tn((*tmp))), Get_NAME(R_INF, Get_tn((*tmp))), Get_tn((*tmp)), Get_qe((*tmp)) - Get_qs((*tmp))); } } @@ -202,7 +180,7 @@ void asg_arc_index(asg_t *g) void asg_seq_set(asg_t *g, int sid, int len, int del) { ///just malloc size - if (sid >= g->m_seq) { + if (sid >= (int)g->m_seq) { g->m_seq = sid + 1; kv_roundup32(g->m_seq); g->seq = (asg_seq_t*)realloc(g->seq, g->m_seq * sizeof(asg_seq_t)); @@ -213,10 +191,6 @@ void asg_seq_set(asg_t *g, int sid, int len, int del) g->seq[sid].del = !!del; g->seq[sid].len = len; - // if(g->seq[sid].del) - // { - // g->seq[sid].len = 0; - // } } @@ -285,7 +259,12 @@ int asg_arc_del_multi(asg_t *g) } free(cnt); if (n_multi) asg_cleanup(g); - fprintf(stderr, "[M::%s] removed %d multi-arcs\n", __func__, n_multi); + + if(VERBOSE >= 1) + { + fprintf(stderr, "[M::%s] removed %d multi-arcs\n", __func__, n_multi); + } + return n_multi; } @@ -303,7 +282,10 @@ int asg_arc_del_asymm(asg_t *g) if (i == nv) g->arc[e].del = 1, ++n_asymm; } if (n_asymm) asg_cleanup(g); - fprintf(stderr, "[M::%s] removed %d asymmetric arcs\n", __func__, n_asymm); + if(VERBOSE >= 1) + { + fprintf(stderr, "[M::%s] removed %d asymmetric arcs\n", __func__, n_asymm); + } return n_asymm; } @@ -356,16 +338,6 @@ void add_ma_hit_t_alloc(ma_hit_t_alloc* x, ma_hit_t* element) -void init_Assembly_Graph(Assembly_Graph* x) -{ - init_ma_hit_t_alloc(&(x->overlaps)); -} - -void destory_Assembly_Graph(Assembly_Graph* x) -{ - destory_ma_hit_t_alloc(&(x->overlaps)); -} - long long get_specific_overlap(ma_hit_t_alloc* x, uint32_t qn, uint32_t tn) { long long i; @@ -384,57 +356,6 @@ long long get_specific_overlap(ma_hit_t_alloc* x, uint32_t qn, uint32_t tn) -void collect_ma_hit_t(ma_hit_t_alloc* dest, ma_hit_t_alloc* sources, long long num_sources) -{ - long long bi_overlaps = 0; - long long si_overlaps = 0; - long long i, j, index; - uint32_t qn, tn; - ma_hit_t new_element; - for (i = 0; i < num_sources; i++) - { - resize_ma_hit_t_alloc(dest, dest->length + sources[i].length); - for (j = 0; j < sources[i].length; j++) - { - qn = sources[i].buffer[j].qns>>32; - tn = sources[i].buffer[j].tn; - - index = get_specific_overlap(&(sources[tn]), tn, qn); - - - if(index != -1) - { - - // fprintf(stderr, "\n+qn: %d, tn: %d, qs: %d, qe: %d, ts: %d, te: %d\n", qn, tn, - // (uint32_t)(sources[i].buffer[j].qns), sources[i].buffer[j].qe, - // sources[i].buffer[j].ts, sources[i].buffer[j].te); - - // fprintf(stderr, "-qn: %d, tn: %d, qs: %d, qe: %d, ts: %d, te: %d\n\n", - // sources[tn].buffer[index].qns>>32, sources[tn].buffer[index].tn, - // (uint32_t)(sources[tn].buffer[index].qns), sources[tn].buffer[index].qe, - // sources[tn].buffer[index].ts, sources[tn].buffer[index].te); - - if(qn <= tn) - { - - } - - - bi_overlaps++; - } - else - { - si_overlaps++; - } - } - } - - - fprintf(stderr, "bi_overlaps: %d, si_overlaps: %d\n", bi_overlaps, si_overlaps); - -} - - inline void set_reverse_overlap(ma_hit_t* dest, ma_hit_t* source) { dest->qns = Get_tn(*source); @@ -531,15 +452,7 @@ void normalize_ma_hit_t(ma_hit_t_alloc* sources, long long num_sources) si_overlaps++; } } - } - - - /** - for (i = 0; i < num_sources; i++) - { - ma_hit_sort_qns(sources[i].buffer, sources[i].length); - } - **/ + } } @@ -548,10 +461,8 @@ void normalize_ma_hit_t_single_side(ma_hit_t_alloc* sources, long long num_sourc double startTime = Get_T(); long long bi_overlaps = 0; - long long si_overlaps = 0; long long i, j, index; uint32_t qn, tn; - ma_hit_t new_element; long long qLen_0, qLen_1, m; for (i = 0; i < num_sources; i++) { @@ -594,8 +505,10 @@ void normalize_ma_hit_t_single_side(ma_hit_t_alloc* sources, long long num_sourc sources[i].length = m; } - - fprintf(stderr, "[M::%s] takes %0.2fs\n\n", __func__, Get_T()-startTime); + if(VERBOSE >= 1) + { + fprintf(stderr, "[M::%s] takes %0.2fs\n\n", __func__, Get_T()-startTime); + } } @@ -605,11 +518,11 @@ void ma_hit_contained(ma_hit_t_alloc* sources, long long n_read, ma_sub_t *cover { double startTime = Get_T(); int32_t r; - size_t i, j, m; + long long i, j, m; asg_arc_t t; for (i = 0; i < n_read; ++i) { - for (j = 0; j < sources[i].length; j++) + for (j = 0; j < (long long)sources[i].length; j++) { ma_hit_t *h = &(sources[i].buffer[j]); //check the corresponding two reads @@ -633,7 +546,7 @@ void ma_hit_contained(ma_hit_t_alloc* sources, long long n_read, ma_sub_t *cover for (i = 0; i < n_read; ++i) { m = 0; - for (j = 0; j < sources[i].length; j++) + for (j = 0; j < (long long)sources[i].length; j++) { ma_hit_t *h = &(sources[i].buffer[j]); ///both the qn and tn have not been deleted @@ -651,7 +564,10 @@ void ma_hit_contained(ma_hit_t_alloc* sources, long long n_read, ma_sub_t *cover coverage_cut[i].del = 1; } } - fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + if(VERBOSE >= 1) + { + fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + } } @@ -660,14 +576,13 @@ void ma_hit_contained(ma_hit_t_alloc* sources, long long n_read, ma_sub_t *cover void ma_hit_flt(ma_hit_t_alloc* sources, long long n_read, ma_sub_t *coverage_cut, int max_hang, int min_ovlp) { double startTime = Get_T(); - size_t i, j, m; + long long i, j, m; asg_arc_t t; - uint64_t tot_dp = 0, tot_len = 0; for (i = 0; i < n_read; ++i) { m = 0; - for (j = 0; j < sources[i].length; j++) + for (j = 0; j < (long long)sources[i].length; j++) { ma_hit_t *h = &(sources[i].buffer[j]); //check the corresponding two reads @@ -707,180 +622,17 @@ void ma_hit_flt(ma_hit_t_alloc* sources, long long n_read, ma_sub_t *coverage_cu (coverage_cut)[i].del = 1; } } - fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); -} - - -///a is the overlap vector, n is the length of overlap vector -///min_dp is used for coverage droping -///select reads with coverage >= min_dp -void ma_hit_sub_back(int min_dp, ma_hit_t_alloc* sources, long long n_read, uint64_t* readLen, -long long mini_overlap_length, ma_sub_t** coverage_cut) -{ - (*coverage_cut) = (ma_sub_t*)malloc(sizeof(ma_sub_t)*n_read); - - size_t i, j, n_remained = 0; - kvec_t(uint32_t) b = {0,0,0}; - - ///all overlaps in vector a has been sorted by qns - ///so for overlaps of one reads, it must be contiguous - for (i = 0; i < n_read; ++i) + if(VERBOSE >= 1) { - // fprintf(stderr, "i: %d, n_read: %d\n", i, n_read); - - kv_resize(uint32_t, b, sources[i].length); - b.n = 0; - for (j = 0; j < sources[i].length; j++) - { - uint32_t qs, qe; - qs = Get_qs(sources[i].buffer[j]); - qe = Get_qe(sources[i].buffer[j]); - kv_push(uint32_t, b, qs<<1); - kv_push(uint32_t, b, qe<<1|1); - } - - ///we can identify the qs and qe by the 0-th bit - ks_introsort_uint32_t(b.n, b.a); - ma_sub_t max, max2; - max.s = max.e = max.del = max2.s = max2.e = max2.del = 0; - int dp, start; - ///max is the longest subregion, max2 is the second longest subregion - for (j = 0, dp = 0; j < b.n; ++j) - { - int old_dp = dp; - ///if a[j] is qe - if (b.a[j]&1) - { - --dp; - } - else - { - ++dp; - } - - ///min_dp is the coverage drop threshold - ///there are two cases: 1. old_dp = dp + 1 (b.a[j] is qe); 2. old_dp = dp - 1 (b.a[j] is qs); - ///if one read has multiple separate sub-regions with coverage > 3, does miniasm only select the longest one? - if (old_dp < min_dp && dp >= min_dp) ///old_dp < dp, b.a[j] is qs - { ///case 2, a[j] is qs - start = b.a[j]>>1; - } - else if (old_dp >= min_dp && dp < min_dp) ///old_dp > min_dp, b.a[j] is qe - { - int len = (b.a[j]>>1) - start; - if (len > max.e - max.s) - { - max2 = max; - max.s = start; - max.e = b.a[j]>>1; - } - else if (len > max2.e - max2.s) - { - max2.s = start; - max2.e = b.a[j]>>1; - } - } - } - - - - if (max.e - max.s > 0) - { - (*coverage_cut)[i].s = max.s; - (*coverage_cut)[i].e = max.e; - (*coverage_cut)[i].del = 0; - ++n_remained; - } - else - { - (*coverage_cut)[i].del = 1; - } - } - - - - - ma_hit_t* p; - ma_sub_t* rq; - ma_sub_t* rt; - long long m = 0; - for (i = 0; i < n_read; ++i) - { - m = 0; - for (j = 0; j < sources[i].length; j++) - { - ///this is a overlap - p = &(sources[i].buffer[j]); - - rq = &((*coverage_cut)[Get_qn(*p)]); - rt = &((*coverage_cut)[Get_tn(*p)]); - ///if any of target read and the query read has no enough coverage - if (rq->del || rt->del) continue; - int qs, qe, ts, te; - - - - - ///target and query in different strand - if (p->rev) - { - qs = p->te < rt->e? Get_qs(*p): Get_qs(*p) + (p->te - rt->e); - qe = p->ts > rt->s? p->qe : p->qe - (rt->s - p->ts); - ts = p->qe < rq->e? p->ts : p->ts + (p->qe - rq->e); - te = Get_qs(*p) > rq->s? p->te : p->te - (rq->s - Get_qs(*p)); - } - else ///target and query in same strand - { - ///note: ts is the targe start in this overlap, - ///while rt->s is the high coverage start in the whole target (not only in this overlap) - ///so this line is to normalize the qs in quey to high coverage region - qs = p->ts > rt->s? Get_qs(*p): Get_qs(*p) + (rt->s - p->ts); //(rt->s - p->ts) is the offset - qe = p->te < rt->e? p->qe : p->qe - (p->te - rt->e);//(p->te - rt->e) is the offset - ts = Get_qs(*p) > rq->s? p->ts : p->ts + (rq->s - Get_qs(*p));//(rq->s - Get_qs(*p) is the offset - te = p->qe < rq->e? p->te : p->te - (p->qe - rq->e);//(p->qe - rq->e) is the offset - } - - - - //cut by self coverage - qs = (qs > rq->s? qs : rq->s) - rq->s; - qe = (qe < rq->e? qe : rq->e) - rq->s; - ts = (ts > rt->s? ts : rt->s) - rt->s; - te = (te < rt->e? te : rt->e) - rt->s; - - if (qe - qs >= mini_overlap_length && te - ts >= mini_overlap_length) - { - ///p->qns = p->qns>>32<<32 | qs; - p->qns = p->qns>>32; - p->qns = p->qns << 32; - p->qns = p->qns | qs; - - p->qe = qe; - p->ts = ts; - p->te = te; - sources[i].buffer[m] = *p; - m++; - } - } - sources[i].length = m; + fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); } - - - - - - - - - - - - free(b.a); - ///free((*coverage_cut)); } + + + ///a is the overlap vector, n is the length of overlap vector ///min_dp is used for coverage droping ///select reads with coverage >= min_dp @@ -891,12 +643,12 @@ long long mini_overlap_length, ma_sub_t** coverage_cut) (*coverage_cut) = (ma_sub_t*)malloc(sizeof(ma_sub_t)*n_read); - size_t i, j, n_remained = 0; + uint64_t i, j, n_remained = 0; kvec_t(uint32_t) b = {0,0,0}; ///all overlaps in vector a has been sorted by qns ///so for overlaps of one reads, it must be contiguous - for (i = 0; i < n_read; ++i) + for (i = 0; i < (uint64_t)n_read; ++i) { if(min_dp <= 1) { @@ -923,7 +675,7 @@ long long mini_overlap_length, ma_sub_t** coverage_cut) ks_introsort_uint32_t(b.n, b.a); ma_sub_t max, max2; max.s = max.e = max.del = max2.s = max2.e = max2.del = 0; - int dp, start; + int dp, start = 0; ///max is the longest subregion, max2 is the second longest subregion for (j = 0, dp = 0; j < b.n; ++j) { @@ -953,13 +705,13 @@ long long mini_overlap_length, ma_sub_t** coverage_cut) else if (old_dp >= min_dp && dp < min_dp) ///old_dp > min_dp, b.a[j] is qe { int len = (b.a[j]>>1) - start; - if (len > max.e - max.s) + if (len > (int)(max.e - max.s)) { max2 = max; max.s = start; max.e = b.a[j]>>1; } - else if (len > max2.e - max2.s) + else if (len > int(max2.e - max2.s)) { max2.s = start; max2.e = b.a[j]>>1; @@ -985,189 +737,32 @@ long long mini_overlap_length, ma_sub_t** coverage_cut) } free(b.a); - - fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); -} - - - - -void ma_hit_chimeric(int min_dp, ma_hit_t_alloc* sources, ma_hit_t_alloc* reverse_sources, -long long n_read, uint64_t* readLen, ma_sub_t* coverage_cut) -{ - double startTime = Get_T(); - - int i, j, k, n_remove = 0; - kvec_t(uint32_t) b = {0,0,0}; - - for (i = 0; i < n_read; ++i) - { - - kv_resize(uint32_t, b, readLen[i]); - memset(b.a, 0, sizeof(uint32_t)*readLen[i]); - - - - for (j = 0; j < sources[i].length; j++) - { - uint32_t qs, qe; - qs = Get_qs(sources[i].buffer[j]); - qe = Get_qe(sources[i].buffer[j]); - - - ///if(qe - qs < 1000) continue; - for (k = qs; k < qe; k++) - { - b.a[k]++; - } - } - - - // for (j = 0; j < reverse_sources[i].length; j++) - // { - // uint32_t qs, qe; - // qs = Get_qs(reverse_sources[i].buffer[j]); - // qe = Get_qe(reverse_sources[i].buffer[j]); - - // for (k = qs; k < qe; k++) - // { - // b.a[k]++; - // } - // } - - - int left, right; - left = -1; - right = readLen[i]; - for (k = 0; k < readLen[i]; k++) - { - if(b.a[k] < min_dp) - { - left = k; - break; - } - } - - for (k = readLen[i] - 1; k >= 0; k--) - { - if(b.a[k] < min_dp) - { - right = k; - break; - } - } - - // if(i == 4616942 || i == 4024299 || i == 6135193) - // { - // fprintf(stderr, "i: %d, left: %d, right: %d, rLen: %d, min_dp: %d\n", - // i, left, right, readLen[i], min_dp); - // for (k = 0; k < readLen[i]; k++) - // { - // fprintf(stderr, "a[%d]: %d\n", k, b.a[k]); - // } - // } - - if( (left <= right) && (left > 0) && (right < readLen[i] - 1)) - { - coverage_cut[i].c = 1; - n_remove++; - /****************************may have bugs********************************/ - coverage_cut[i].del = 1; - sources[i].length = 0; - /****************************may have bugs********************************/ - } - else - { - // if(coverage_cut[i].c == 2) - // { - // fprintf(stderr, "\ni: %d, readLen: %d\n", i, readLen[i]); - // for (j = 0; j < sources[i].length; j++) - // { - // fprintf(stderr, "j: %d, qs: %d, qe: %d, ts: %d, te: %d\n", - // j, Get_qs(sources[i].buffer[j]), Get_qe(sources[i].buffer[j]), - // Get_ts(sources[i].buffer[j]), Get_te(sources[i].buffer[j])); - // } - // } - coverage_cut[i].c = 0; - } - } - - free(b.a); - - fprintf(stderr, "[M::%s] takes %0.2f s, n_remove: %d\n\n", __func__, Get_T()-startTime, n_remove); -} - - - -int boundary_verify_back(uint32_t interval_s, uint32_t interval_e, ma_hit_t* map, -char* x_buffer, char* y_buffer, All_reads* R_INF) -{ - uint32_t xs, xe, ys, ye, dir, x_id, y_id, xLen; - dir = (*map).rev; - xs = Get_qs((*map)); - xe = Get_qe((*map)) - 1; - x_id = Get_qn((*map)); - y_id = Get_tn((*map)); - long long yLen = Get_READ_LENGTH((*R_INF), y_id); - - if(dir == 1) - { - ys = yLen - (Get_te((*map)) - 1) - 1; - ye = yLen - Get_ts((*map)) - 1; - - } - else - { - ys = Get_ts((*map)); - ye = Get_te((*map)) - 1; - } - - xLen = interval_e - interval_s; - - if(xLen <= WINDOW) - { - return verify_single_window(interval_s, interval_e-1, xs, ys, x_id, y_id, - dir, x_buffer, y_buffer, R_INF); - } - else - { - if(verify_single_window(interval_s, interval_s + WINDOW - 1, - xs, ys, x_id, y_id, dir, x_buffer, y_buffer, R_INF) == 0) - { - return 0; - } - - if(verify_single_window(interval_e - WINDOW, interval_e-1, - xs, ys, x_id, y_id, dir, x_buffer, y_buffer, R_INF) == 0) - { - return 0; - } - - return 1; + if(VERBOSE >= 1) + { + fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); } } + + + int boundary_verify(uint32_t x_interval_s, uint32_t x_interval_e, ma_hit_t* map, char* x_buffer, char* y_buffer, All_reads* R_INF) { - uint32_t xs, xe, ys, ye, dir, x_id, y_id, x_interval_Len, y_interval_Len, y_interval_s, y_interval_e; + uint32_t xs, ys, dir, x_id, y_id, x_interval_Len, y_interval_Len, y_interval_s, y_interval_e; dir = (*map).rev; xs = Get_qs((*map)); - xe = Get_qe((*map)) - 1; x_id = Get_qn((*map)); y_id = Get_tn((*map)); long long yLen = Get_READ_LENGTH((*R_INF), y_id); if(dir == 1) { - ys = yLen - (Get_te((*map)) - 1) - 1; - ye = yLen - Get_ts((*map)) - 1; - + ys = yLen - (Get_te((*map)) - 1) - 1; } else { ys = Get_ts((*map)); - ye = Get_te((*map)) - 1; } ///[x_interval_s, x_interval_e) x_interval_Len = x_interval_e - x_interval_s; @@ -1368,13 +963,14 @@ void print_overlaps(ma_hit_t_alloc* paf, long long rLen, long long interval_s, l { if(Get_qs(paf->buffer[j]) == 0) { - fprintf(stderr, "?????? interval_s: %d, interval_e: %d, qn: %d, tn: %d, j: %d, qs: %d, qe: %d, ts: %d, te: %d, dir: %d\n", + fprintf(stderr, "?????? interval_s: %lld, interval_e: %lld, qn: %u, tn: %u, j: %lld, qs: %u, qe: %u, ts: %u, te: %u, dir: %u\n", interval_s, interval_e, Get_qn(paf->buffer[j]), Get_tn(paf->buffer[j]), j, Get_qs(paf->buffer[j]), Get_qe(paf->buffer[j]), Get_ts(paf->buffer[j]), Get_te(paf->buffer[j]), paf->buffer[j].rev); - fprintf(stderr, "%.*s\n", Get_NAME_LENGTH(R_INF, Get_tn(paf->buffer[j])), + + fprintf(stderr, "%.*s\n", (int)Get_NAME_LENGTH(R_INF, Get_tn(paf->buffer[j])), Get_NAME(R_INF, Get_tn(paf->buffer[j]))); } } @@ -1384,13 +980,14 @@ void print_overlaps(ma_hit_t_alloc* paf, long long rLen, long long interval_s, l { if(Get_qe(paf->buffer[j]) == rLen) { - fprintf(stderr, "?????? interval_s: %d, interval_e: %d, qn: %d, tn: %d, j: %d, qs: %d, qe: %d, ts: %d, te: %d, dir: %d\n", + fprintf(stderr, "?????? interval_s: %lld, interval_e: %lld, qn: %u, tn: %u, j: %lld, qs: %u, qe: %u, ts: %u, te: %u, dir: %u\n", interval_s, interval_e, Get_qn(paf->buffer[j]), Get_tn(paf->buffer[j]), j, Get_qs(paf->buffer[j]), Get_qe(paf->buffer[j]), Get_ts(paf->buffer[j]), Get_te(paf->buffer[j]), paf->buffer[j].rev); - fprintf(stderr, "%.*s\n", Get_NAME_LENGTH(R_INF, Get_tn(paf->buffer[j])), + + fprintf(stderr, "%.*s\n", (int)Get_NAME_LENGTH(R_INF, Get_tn(paf->buffer[j])), Get_NAME(R_INF, Get_tn(paf->buffer[j]))); } } @@ -1401,13 +998,14 @@ void print_overlaps(ma_hit_t_alloc* paf, long long rLen, long long interval_s, l { if(Get_qs(paf->buffer[j]) != 0 && Get_qe(paf->buffer[j]) != rLen) { - fprintf(stderr, "?????? interval_s: %d, interval_e: %d, qn: %d, tn: %d, j: %d, qs: %d, qe: %d, ts: %d, te: %d, dir: %d\n", + fprintf(stderr, "?????? interval_s: %lld, interval_e: %lld, qn: %u, tn: %u, j: %lld, qs: %u, qe: %u, ts: %u, te: %u, dir: %u\n", interval_s, interval_e, Get_qn(paf->buffer[j]), Get_tn(paf->buffer[j]), j, Get_qs(paf->buffer[j]), Get_qe(paf->buffer[j]), Get_ts(paf->buffer[j]), Get_te(paf->buffer[j]), paf->buffer[j].rev); - fprintf(stderr, "%.*s\n", Get_NAME_LENGTH(R_INF, Get_tn(paf->buffer[j])), + + fprintf(stderr, "%.*s\n", (int)Get_NAME_LENGTH(R_INF, Get_tn(paf->buffer[j])), Get_NAME(R_INF, Get_tn(paf->buffer[j]))); } } @@ -1421,7 +1019,6 @@ long long n_read, uint64_t* readLen, ma_sub_t* coverage_cut, float shift_rate) double startTime = Get_T(); init_aux_table(); long long i, rLen, /**cov,**/ n_simple_remove = 0, n_complex_remove = 0, n_complex_remove_real = 0; - uint32_t qs, qe; uint32_t interval_s, interval_e; ma_sub_t max_left, max_right; kvec_t(char) b_q = {0,0,0}; @@ -1533,181 +1130,17 @@ long long n_read, uint64_t* readLen, ma_sub_t* coverage_cut, float shift_rate) free(b_q.a); free(b_t.a); - fprintf(stderr, "[M::%s] takes %0.2f s, n_simple_remove: %d, n_complex_remove: %d/%d\n\n", - __func__, Get_T()-startTime, n_simple_remove, n_complex_remove_real, n_complex_remove); -} - - -void detect_chimeric_reads_back(ma_hit_t_alloc* paf, ma_hit_t_alloc* rev_paf, -long long n_read, uint64_t* readLen, ma_sub_t* coverage_cut, float shift_rate) -{ - double startTime = Get_T(); - long long i, j, cov, rLen, n_simple_remove = 0, n_complex_remove = 0, n_complex_remove_real = 0; - uint32_t qs, qe; - uint32_t interval_s, interval_e; - ma_sub_t max_left, max_right; - kvec_t(char) b_q = {0,0,0}; - kvec_t(char) b_t = {0,0,0}; - for (i = 0; i < n_read; ++i) + if(VERBOSE >= 1) { - coverage_cut[i].c = 0; - rLen = readLen[i]; - - - max_left.s = max_right.s = rLen; - max_left.e = max_right.e = 0; - - for (j = 0; j < paf[i].length; j++) - { - qs = Get_qs(paf[i].buffer[j]); - qe = Get_qe(paf[i].buffer[j]); - - - ///overlaps from left side - if(qs == 0) - { - if(qs < max_left.s) max_left.s = qs; - if(qe > max_left.e) max_left.e = qe; - } - - ///overlaps from right side - if(qe == rLen) - { - if(qs < max_right.s) max_right.s = qs; - if(qe > max_right.e) max_right.e = qe; - } - - ///note: if (qs == 0 && qe == rLen) - ///this overlap would be added to both b_left and b_right - ///that is what we want - } - - - ///that means this read is an end node - ///if(max_left.e == 0 || max_right.e == 0) - if(max_left.s == rLen || max_right.s == rLen) - { - continue; - } - - - for (j = 0; j < paf[i].length; j++) - { - qs = Get_qs(paf[i].buffer[j]); - qe = Get_qe(paf[i].buffer[j]); - ///check contained overlaps - if(qs != 0 && qe != rLen) - { - ///[qs, qe), [max_left.s, max_left.e) - if(qs < max_left.e && qe > max_left.e) - { - if(qe > max_left.e) max_left.e = qe; - } - - ///[qs, qe), [max_right.s, max_right.e) - if(qs < max_right.s && qe > max_right.s) - { - if(qs < max_right.s) max_right.s = qs; - } - } - } - - ////shift_rate should be (FINAL_OVERLAP_ERROR_RATE*2) - ///this read is a normal read - if(max_left.e > max_right.s && - (max_left.e - max_right.s >= rLen * shift_rate)) - { - ///coverage_cut[i].c = 0; - continue; - } - - ///simple chimeric reads - if(max_left.e <= max_right.s) - { - ///coverage_cut[i].c = 2; - coverage_cut[i].c = 1; - coverage_cut[i].del = 1; - paf[i].length = 0; - n_simple_remove++; - continue; - } - - ///now max_left.e > max_right.s && max_left.e - max_right.s is small enough - //[interval_s, interval_e) - interval_s = max_right.s; - interval_e = max_left.e; - - kv_resize(char, b_q, rLen+10); - kv_resize(char, b_t, (rLen*2+10)); - - cov = 0; - for (j = 0; j < paf[i].length; j++) - { - qs = Get_qs(paf[i].buffer[j]); - qe = Get_qe(paf[i].buffer[j]); - ///[interval_s, interval_e) must be at least contained at one of the [qs, qe) - if(qs<=interval_s && qe>=interval_e) - { - // if(i == 439960) - // { - // fprintf(stderr, "?????? interval_s: %d, interval_e: %d, qn: %d, tn: %d, j: %d, qs: %d, qe: %d, ts: %d, te: %d, dir: %d\n", - // interval_s, interval_e, - // Get_qn(paf[i].buffer[j]), Get_tn(paf[i].buffer[j]), - // j, Get_qs(paf[i].buffer[j]), Get_qe(paf[i].buffer[j]), - // Get_ts(paf[i].buffer[j]), Get_te(paf[i].buffer[j]), - // paf[i].buffer[j].rev); - - // fprintf(stderr, "%.*s\n", Get_NAME_LENGTH(R_INF, Get_tn(paf[i].buffer[j])), - // Get_NAME(R_INF, Get_tn(paf[i].buffer[j]))); - // } - ///num++; - cov++; - if(boundary_verify(interval_s, interval_e, &(paf[i].buffer[j]), - b_q.a, b_t.a, &R_INF) == 0) - { - coverage_cut[i].c = 1; - coverage_cut[i].del = 1; - paf[i].length = 0; - n_complex_remove_real++; - break; - } - } - } - - if(coverage_cut[i].c == 0 && interval_e - interval_s < WINDOW && cov <= 2) - { - coverage_cut[i].c = 1; - coverage_cut[i].del = 1; - paf[i].length = 0; - n_complex_remove_real++; - } - - - - - - ///if all of them are wrong - // if(j == paf[i].length) - // { - // coverage_cut[i].c = 1; - // coverage_cut[i].del = 1; - // paf[i].length = 0; - // n_complex_remove_real++; - // } - - - n_complex_remove++; + fprintf(stderr, "[M::%s] takes %0.2f s, n_simple_remove: %lld, n_complex_remove: %lld/%lld\n\n", + __func__, Get_T()-startTime, n_simple_remove, n_complex_remove_real, n_complex_remove); } - - free(b_q.a); - free(b_t.a); - - fprintf(stderr, "[M::%s] takes %0.2f s, n_simple_remove: %d, n_complex_remove: %d/%d\n\n", - __func__, Get_T()-startTime, n_simple_remove, n_complex_remove_real, n_complex_remove); } + + void ma_hit_cut(int min_dp, ma_hit_t_alloc* sources, long long n_read, uint64_t* readLen, long long mini_overlap_length, ma_sub_t** coverage_cut) { @@ -1717,7 +1150,7 @@ long long mini_overlap_length, ma_sub_t** coverage_cut) ma_sub_t* rq; ma_sub_t* rt; long long m = 0; - for (i = 0; i < n_read; ++i) + for (i = 0; i < (uint64_t)n_read; ++i) { m = 0; for (j = 0; j < sources[i].length; j++) @@ -1772,10 +1205,10 @@ long long mini_overlap_length, ma_sub_t** coverage_cut) //cut by self coverage //and normalize the qs, qe, ts, te by rq->s and rt->e - qs = (qs > rq->s? qs : rq->s) - rq->s; - qe = (qe < rq->e? qe : rq->e) - rq->s; - ts = (ts > rt->s? ts : rt->s) - rt->s; - te = (te < rt->e? te : rt->e) - rt->s; + qs = ((uint32_t)qs > rq->s? qs : rq->s) - rq->s; + qe = ((uint32_t)qe < rq->e? qe : rq->e) - rq->s; + ts = ((uint32_t)ts > rt->s? ts : rt->s) - rt->s; + te = ((uint32_t)te < rt->e? te : rt->e) - rt->s; if (qe - qs >= mini_overlap_length && te - ts >= mini_overlap_length) { @@ -1799,281 +1232,15 @@ long long mini_overlap_length, ma_sub_t** coverage_cut) } } - fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + if(VERBOSE >= 1) + { + fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + } + } -void debug_normalize_ma_hit_t(ma_hit_t_alloc* sources, long long num_sources) -{ - long long i, j, index; - uint32_t qn, tn; - long long total_overlaps = 0; - long long total_reads = 0; - - for (i = 0; i < num_sources; i++) - { - for (j = 0; j < sources[i].length; j++) - { - if(Get_qn(sources[i].buffer[j]) != i) - { - fprintf(stderr, "1 error 2\n"); - } - total_overlaps += sources[i].length; - - } - - if(sources[i].length != 0) - { - total_reads++; - } - } - - for (i = 0; i < num_sources; i++) - { - for (j = 0; j < sources[i].length; j++) - { - qn = sources[i].buffer[j].qns>>32; - tn = sources[i].buffer[j].tn; - - long long k; - for (k = 0; k < sources[i].length; k++) - { - //here can be improved, since ma_hit_t_alloc has been sorted by tn - if(sources[i].buffer[k].tn == tn - && - ((uint32_t)(sources[i].buffer[k].qns>>32)) == qn) - { - if(k != j) - { - fprintf(stderr, "2 ERROR\n"); - } - } - } - } - } - - - - - - - - - - - for (i = 0; i < num_sources; i++) - { - for (j = 0; j < sources[i].length; j++) - { - - - - if(sources[i].buffer[j].bl != Get_qe(sources[i].buffer[j]) - Get_qs(sources[i].buffer[j])) - { - fprintf(stderr, "3 error2, bl: %d, qs: %d, qe: %d\n", - sources[i].buffer[j].bl, Get_qs(sources[i].buffer[j]), - Get_qe(sources[i].buffer[j])); - } - - - qn = sources[i].buffer[j].qns>>32; - tn = sources[i].buffer[j].tn; - - index = get_specific_overlap(&(sources[tn]), tn, qn); - - - if(index == -1) - { - fprintf(stderr, "4 error3\n"); - } - else - { - - if(sources[i].buffer[j].rev != sources[tn].buffer[index].rev) - { - fprintf(stderr, "5 hahaha\n"); - } - - if(sources[i].buffer[j].el != sources[tn].buffer[index].el) - { - fprintf(stderr, "el hahaha\n"); - } - - if(Get_qn(sources[i].buffer[j]) != Get_tn(sources[tn].buffer[index])) - { - fprintf(stderr, "6 error4\n"); - } - - if(Get_tn(sources[i].buffer[j]) != Get_qn(sources[tn].buffer[index])) - { - fprintf(stderr, "7 error5\n"); - } - - if(Get_ts(sources[i].buffer[j]) != Get_qs(sources[tn].buffer[index])) - { - fprintf(stderr, "error6, Get_ts(%d, %d)=%d, Get_qs(%d, %d)=%d, rev: %d\n", - i, j, Get_ts(sources[i].buffer[j]), - tn, index, Get_qs(sources[tn].buffer[index]), sources[i].buffer[j].rev); - } - - if(Get_te(sources[i].buffer[j]) != Get_qe(sources[tn].buffer[index])) - { - fprintf(stderr, "error7, Get_te(%d, %d)=%d, Get_qe(%d, %d)=%d, rev: %d\n", - i, j, Get_te(sources[i].buffer[j]), - tn, index, Get_qe(sources[tn].buffer[index]), - sources[i].buffer[j].rev); - } - - - - if(sources[i].buffer[j].ml != sources[tn].buffer[index].ml) - { - fprintf(stderr, "9 hahaha\n"); - } - - } - - } - } - - fprintf(stderr, "total_reads:%d, total_overlaps: %d\n", total_reads, total_overlaps); -} - - - -void debug_cut_ma_hit_t(ma_hit_t_alloc* sources, long long num_sources, ma_sub_t* coverage_cut) -{ - long long i, j, index; - uint32_t qn, tn; - long long total_overlaps = 0; - long long total_reads = 0; - - for (i = 0; i < num_sources; i++) - { - for (j = 0; j < sources[i].length; j++) - { - if(Get_qn(sources[i].buffer[j]) != i) - { - fprintf(stderr, "error 2\n"); - } - total_overlaps += sources[i].length; - - } - - if(sources[i].length != 0) - { - total_reads++; - } - - if((sources[i].length == 0 && coverage_cut[i].del == 1) - || - (sources[i].length != 0 && coverage_cut[i].del == 0)) - { - ; - } - else - { - fprintf(stderr, "i: %d, sources[i].length: %d, coverage_cut[i].del: %d\n", i, sources[i].length, coverage_cut[i].del); - } - - } - - for (i = 0; i < num_sources; i++) - { - for (j = 0; j < sources[i].length; j++) - { - qn = sources[i].buffer[j].qns>>32; - tn = sources[i].buffer[j].tn; - - long long k; - for (k = 0; k < sources[i].length; k++) - { - //here can be improved, since ma_hit_t_alloc has been sorted by tn - if(sources[i].buffer[k].tn == tn - && - ((uint32_t)(sources[i].buffer[k].qns>>32)) == qn) - { - if(k != j) - { - fprintf(stderr, "ERROR\n"); - } - } - } - } - } - - - - - - - - - - - for (i = 0; i < num_sources; i++) - { - for (j = 0; j < sources[i].length; j++) - { - /** - if(sources[i].buffer[j].bl != sources[i].buffer[j].ml) - { - fprintf(stderr, "error1\n"); - } - **/ - - qn = sources[i].buffer[j].qns>>32; - tn = sources[i].buffer[j].tn; - - index = get_specific_overlap(&(sources[tn]), tn, qn); - - - if(index == -1) - { - fprintf(stderr, "error3\n"); - } - else - { - if(sources[i].buffer[j].rev != sources[tn].buffer[index].rev) - { - fprintf(stderr, "hahaha\n"); - } - - if(Get_qn(sources[i].buffer[j]) != Get_tn(sources[tn].buffer[index])) - { - fprintf(stderr, "error4\n"); - } - - if(Get_tn(sources[i].buffer[j]) != Get_qn(sources[tn].buffer[index])) - { - fprintf(stderr, "error5\n"); - } - - if(Get_ts(sources[i].buffer[j]) != Get_qs(sources[tn].buffer[index])) - { - fprintf(stderr, "\nerror6, Get_ts(%d, %d)=%d, Get_qs(%d, %d)=%d, rev: %d\n", - i, j, Get_ts(sources[i].buffer[j]), - tn, index, Get_qs(sources[tn].buffer[index]), - sources[i].buffer[j].rev); - } - - if(Get_te(sources[i].buffer[j]) != Get_qe(sources[tn].buffer[index])) - { - fprintf(stderr, "\nerror7, Get_te(%d, %d)=%d, Get_qe(%d, %d)=%d, rev: %d\n", - i, j, Get_te(sources[i].buffer[j]), - tn, index, Get_qe(sources[tn].buffer[index]), - sources[i].buffer[j].rev); - } - - } - - } - } - - fprintf(stderr, "total_reads:%d, total_overlaps: %d\n", total_reads, total_overlaps); -} - /********************************** * Filter short potential unitigs * **********************************/ @@ -2118,7 +1285,7 @@ static inline int asg_is_utg_end(const asg_t *g, uint32_t v, uint64_t *lw) asg_arc_t *aw, *av = asg_arc_a(g, v^1); ///if this arc has not been deleted - for (i = nv = 0; i < nv0; ++i) + for (i = nv = 0; i < (int)nv0; ++i) if (!av[i].del) i0 = i, ++nv; ///see the example below @@ -2171,7 +1338,7 @@ static inline int asg_is_utg_end(const asg_t *g, uint32_t v, uint64_t *lw) w = av[i0].v ^ 1; nw0 = asg_arc_n(g, w); aw = asg_arc_a(g, w); - for (i = nw = 0; i < nw0; ++i) + for (i = nw = 0; i < (int)nw0; ++i) if (!aw[i].del) ++nw; @@ -2270,17 +1437,16 @@ static inline int asg_is_single_edge(const asg_t *g, uint32_t v, uint32_t start_ **/ ///v^1 is the another direction of v - uint32_t w, nv, nw, nw0, nv0 = asg_arc_n(g, v^1); - int i, i0 = -1; + uint32_t nv, nv0 = asg_arc_n(g, v^1); + int i; asg_arc_t *av = asg_arc_a(g, v^1); int flag = 0; ///if this arc has not been deleted - for (i = nv = 0; i < nv0; ++i) + for (i = nv = 0; i < (int)nv0; ++i) { ///if (!av[i].del) { - i0 = i; ++nv; if(av[i].v>>1 == start_node) { @@ -2308,7 +1474,7 @@ int max_hang, int min_ovlp) g = asg_init(); ///add seq to graph, seq just save the length of each read - for (i = 0; i < n_read; ++i) + for (i = 0; i < (uint64_t)n_read; ++i) { ///if a read has been deleted, should we still add them? asg_seq_set(g, i, coverage_cut[i].e - coverage_cut[i].s, coverage_cut[i].del); @@ -2317,7 +1483,7 @@ int max_hang, int min_ovlp) g->seq_vis = (uint8_t*)calloc(g->n_seq*2, sizeof(uint8_t)); - for (i = 0; i < n_read; ++i) + for (i = 0; i < (uint64_t)n_read; ++i) { for (j = 0; j < sources[i].length; j++) { @@ -2352,7 +1518,11 @@ int max_hang, int min_ovlp) } asg_cleanup(g); - fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + + if(VERBOSE >= 1) + { + fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + } return g; } @@ -2360,7 +1530,7 @@ int max_hang, int min_ovlp) // pop bubbles from vertex v0; the graph MJUST BE symmetric: if u->v present, v'->u' must be present as well //note!!!!!!!! here we don't exculde the deleted edges -static uint64_t asg_bub_finder_with_del(asg_t *g, uint32_t v0, int max_dist, buf_t *b, +uint64_t asg_bub_finder_with_del(asg_t *g, uint32_t v0, int max_dist, buf_t *b, uint32_t cut_in_node) { uint32_t i, n_pending = 0; @@ -2416,7 +1586,7 @@ uint32_t cut_in_node) kv_push(uint32_t, b->e, (g->idx[v]>>32) + i); ///find a too far path? directly terminate the whole bubble poping - if (d + l > max_dist) + if (d + l > (uint32_t)max_dist) { //fprintf(stderr, "n_pop error2\n"); break; // too far @@ -2477,116 +1647,6 @@ pop_reset: } -int asg_arc_del_triangular(asg_t *g, long long max_dist) -{ - ///the reason is that each read has two direction (query->target, target->query) - uint32_t v, w, n_vtx = g->n_seq * 2, n_reduced = 0; - buf_t b; - if (!g->is_symm) asg_symm(g); - memset(&b, 0, sizeof(buf_t)); - ///set information for each node - b.a = (binfo_t*)calloc(n_vtx, sizeof(binfo_t)); - int flag0, flag1, node; - for (v = 0; v < n_vtx; ++v) - { - uint32_t nv = asg_arc_n(g, v); - asg_arc_t *av = asg_arc_a(g, v); - if (g->seq[v>>1].del) - { - continue; - } - - if(nv != 2) - { - continue; - } - - /**********************test first node************************/ - flag0 = asg_is_single_edge(g, av[0].v, v>>1); - flag1 = asg_is_single_edge(g, av[1].v, v>>1); - if(flag0 == flag1) - { - continue; - } - if(flag0 < 1 || flag0 > 2) - { - continue; - } - if(flag1 < 1 || flag1 > 2) - { - continue; - } - if(flag0 == 2) - { - node = 0; - } - else if(flag1 == 2) - { - node = 1; - } - /**********************test first node************************/ - - - - /**********************test second node************************/ - w = av[node].v^1; - asg_arc_t *aw = asg_arc_a(g, w); - uint32_t nw = asg_arc_n(g, w); - flag0 = asg_is_single_edge(g, aw[0].v, w>>1); - flag1 = asg_is_single_edge(g, aw[1].v, w>>1); - if(flag0 == flag1) - { - continue; - } - if(flag0 < 1 || flag0 > 2) - { - continue; - } - if(flag1 < 1 || flag1 > 2) - { - continue; - } - - - if(flag0 == 2 && (v>>1) != (aw[0].v>>1)) - { - fprintf(stderr, "error 0\n"); - } - - - if(flag1 == 2 && (v>>1) != (aw[1].v>>1)) - { - fprintf(stderr, "error 1\n"); - } - - - - /**********************test second node************************/ - - ///if not a bubble - if(asg_bub_finder_with_del(g, v, max_dist, &b, (w>>1)) == 0) - { - continue; - } - - - av[node].del = 1; - ///remove the reverse direction - asg_arc_del(g, av[node].v^1, av[node].ul>>32^1, 1); - n_reduced++; - - } - - free(b.a); free(b.S.a); free(b.T.a); free(b.b.a); free(b.e.a); - - if (n_reduced) { - asg_cleanup(g); - asg_symm(g); - } - - fprintf(stderr, "[M::%s] removed %d short overlaps\n", __func__, n_reduced); -} - // pop bubbles from vertex v0; the graph MJUST BE symmetric: if u->v present, v'->u' must be present as well @@ -2646,7 +1706,7 @@ static uint64_t asg_bub_finder_with_del_advance(asg_t *g, uint32_t v0, int max_d kv_push(uint32_t, b->e, (g->idx[v]>>32) + i); ///find a too far path? directly terminate the whole bubble poping - if (d + l > max_dist) + if (d + l > (uint32_t)max_dist) { //fprintf(stderr, "n_pop error2\n"); break; // too far @@ -2764,7 +1824,7 @@ buf_t *b) kv_push(uint32_t, b->e, (g->idx[v]>>32) + i); ///find a too far path? directly terminate the whole bubble poping - if (d + l > max_dist) + if (d + l > (uint32_t)max_dist) { //fprintf(stderr, "n_pop error2\n"); break; // too far @@ -2882,7 +1942,7 @@ int max_dist, buf_t *b, uint32_t exculde_init, uint32_t exclude_node, uint32_t* kv_push(uint32_t, b->e, (g->idx[v]>>32) + i); ///find a too far path? directly terminate the whole bubble poping - if (d + l > max_dist) + if (d + l > (uint32_t)max_dist) { break; // too far } @@ -2939,128 +1999,6 @@ pop_reset: return n_pop; } -// pop bubbles from vertex v0; the graph MJUST BE symmetric: if u->v present, v'->u' must be present as well -//note!!!!!!!! here we don't exculde the deleted edges -static uint64_t asg_bub_end_finder_with_del_advance_debug(asg_t *g, uint32_t* v_Ns, uint32_t occ, -int max_dist, buf_t *b, uint32_t exculde_init, uint32_t exclude_node) -{ - uint32_t i, j, n_pending = 0; - uint64_t n_pop = 0; - ///S saves nodes with all incoming edges visited - b->S.n = b->T.n = b->b.n = b->e.n = 0; - for (j = 0; j < occ; j++) - { - ///if this node has been deleted - if (g->seq[v_Ns[j]>>1].del) return 0; // already deleted - ///for each node, b->a saves all related information - b->a[v_Ns[j]].c = b->a[v_Ns[j]].d = 0; - ///b->S is the nodes with all incoming edges visited - kv_push(uint32_t, b->S, (v_Ns[j]<<1)|exculde_init); - - fprintf(stderr, "init: %.*s\n", Get_NAME_LENGTH(R_INF, v_Ns[j]>>1), - Get_NAME(R_INF, v_Ns[j]>>1)); - } - - - - do { - ///v is a node that all incoming edges have been visited - ///d is the distance from v0 to v - uint32_t v = kv_pop(b->S), f = v & (uint32_t)1; - v = v >> 1; - uint32_t d = b->a[v].d, c = b->a[v].c; - - - uint32_t nv = asg_arc_n(g, v); - asg_arc_t *av = asg_arc_a(g, v); - - fprintf(stderr, "v: %.*s, nv: %d, f: %d\n", - Get_NAME_LENGTH(R_INF, v>>1), Get_NAME(R_INF, v>>1), nv, f); - - ///all out-edges of v - for (i = 0; i < nv; ++i) { // loop through v's neighbors - /** - p->ul: |____________31__________|__________1___________|______________32_____________| - qn direction of overlap length of this node (not overlap length) - (in the view of query) - p->v : |___________31___________|__________1___________| - tn reverse direction of overlap - (in the view of target) - p->ol: overlap length - **/ - uint32_t w = av[i].v, l = (uint32_t)av[i].ul; // v->w with length l - binfo_t *t = &b->a[w]; - - fprintf(stderr, "w: %.*s\n", Get_NAME_LENGTH(R_INF, w>>1), - Get_NAME(R_INF, w>>1)); - - if(f && (exclude_node) == (w)) - { - fprintf(stderr, "exclude_node: %.*s\n", Get_NAME_LENGTH(R_INF, exclude_node>>1), - Get_NAME(R_INF, exclude_node>>1)); - continue; - } - - ///if (av[i].del) continue; - - ///push the edge - kv_push(uint32_t, b->e, (g->idx[v]>>32) + i); - - ///find a too far path? directly terminate the whole bubble poping - if (d + l > max_dist) - { - break; // too far - } - - - - if (t->s == 0) { // this vertex has never been visited - kv_push(uint32_t, b->b, w); // save it for revert - ///t->p means the in-node of w is v - ///t->s = 1 means w has been visited - ///d is len(v0->v), l is len(v->w), so t->d is len(v0->w) - t->p = v, t->s = 1, t->d = d + l; - ///incoming edges of w - t->r = count_out_with_del(g, w^1); - ///t->r = count_out_without_del(g, w^1); - ++n_pending; - } else { // visited before - ///c seems the max weight of node - if (c + 1 > t->c || (c + 1 == t->c && d + l > t->d)) t->p = v; - if (c + 1 > t->c) t->c = c + 1; - ///update len(v0->w) - if (d + l < t->d) t->d = d + l; // update dist - } - /****************************may have bugs********************************/ - ///assert(t->r > 0); - /****************************may have bugs********************************/ - //if all incoming edges of w have visited - //push it to b->S - if (--(t->r) == 0) { - uint32_t x = asg_arc_n(g, w); - //if (x) kv_push(uint32_t, b->S, w); - if (x) kv_push(uint32_t, b->S, w<<1); - else kv_push(uint32_t, b->T, w); // a tip - --n_pending; - } - } - ///if i < nv, that means (d + l > max_dist) - if (i < nv || b->S.n == 0) - { - goto pop_reset; - } - - } while (b->S.n > 1 || n_pending); - ///asg_bub_backtrack(g, v0, b); - ///n_pop = 1 | (uint64_t)b->T.n<<32; - n_pop = 1; -pop_reset: - for (i = 0; i < b->b.n; ++i) { // clear the states of visited vertices - binfo_t *t = &b->a[b->b.a[i]]; - t->s = t->c = t->d = 0; - } - return n_pop; -} @@ -3079,101 +2017,7 @@ int if_node_exist(uint32_t* nodes, uint32_t length, uint32_t query) } -int test_triangular(asg_t *g, uint32_t* nodes, uint32_t length, -uint32_t startNode, uint32_t endNode) -{ - - uint32_t i, v, w; - int flag0, flag1, node; - int n_reduced = 0; - for (i = 0; i < length; ++i) - { - v = nodes[i]; - if((v>>1) == (startNode>>1) || (v>>1) == (endNode>>1)) - { - continue; - } - - uint32_t nv = asg_arc_n(g, v); - asg_arc_t *av = asg_arc_a(g, v); - if(nv != 2) - { - continue; - } - /**********************test first node************************/ - flag0 = asg_is_single_edge(g, av[0].v, v>>1); - flag1 = asg_is_single_edge(g, av[1].v, v>>1); - if(flag0 == flag1) - { - continue; - } - if(flag0 < 1 || flag0 > 2) - { - continue; - } - if(flag1 < 1 || flag1 > 2) - { - continue; - } - if(flag0 == 2) - { - node = 0; - } - else if(flag1 == 2) - { - node = 1; - } - /**********************test first node************************/ - - - - /**********************test second node************************/ - w = av[node].v^1; - asg_arc_t *aw = asg_arc_a(g, w); - uint32_t nw = asg_arc_n(g, w); - flag0 = asg_is_single_edge(g, aw[0].v, w>>1); - flag1 = asg_is_single_edge(g, aw[1].v, w>>1); - if(flag0 == flag1) - { - continue; - } - if(flag0 < 1 || flag0 > 2) - { - continue; - } - if(flag1 < 1 || flag1 > 2) - { - continue; - } - - - if(flag0 == 2 && (v>>1) != (aw[0].v>>1)) - { - fprintf(stderr, "error 0\n"); - } - - - if(flag1 == 2 && (v>>1) != (aw[1].v>>1)) - { - fprintf(stderr, "error 1\n"); - } - /**********************test second node************************/ - - if(if_node_exist(nodes, length, (w>>1)) && ((w>>1) != (endNode>>1))) - { - av[node].del = 1; - ///remove the reverse direction - asg_arc_del(g, av[node].v^1, av[node].ul>>32^1, 1); - n_reduced++; - - ////fprintf(stderr, "v>>1: %u, w>>1: %u\n", v>>1, w>>1); - } - - } - - return n_reduced; -} long long single_edge_length(asg_t *g, uint32_t begNode, uint32_t endNode, long long edgeLen) { @@ -3308,13 +2152,7 @@ int detect_simple_bubble(asg_t *g, uint32_t begNode, uint32_t* endNode, long lon if(b) kv_push(uint32_t, b->b, begNode>>1); - // if(begNode == 17939188) - // { - // fprintf(stderr, "#begNode: %d, nv: %d\n", begNode, asg_arc_n(g, begNode)); - // fprintf(stderr, "#asg_arc_a(g, begNode)[0].v: %d\n", asg_arc_a(g, begNode)[0].v); - // fprintf(stderr, "#asg_arc_a(g, begNode)[1].v: %d\n", asg_arc_a(g, begNode)[1].v); - // fflush(stderr); - // } + if(detect_single_path(g, asg_arc_a(g, begNode)[0].v, &e1, &l1, b) == TWO_INPUT @@ -3342,21 +2180,9 @@ long long* Len, buf_t* b, uint32_t max_ext) uint32_t nv, rnv; asg_arc_t *av; long long bLen; - long long pre_b_n; + long long pre_b_n = 0; (*Len) = 0; - - // if(begNode == 18046652) - // { - // fprintf(stderr, "inner begNode: %d, vn: %d\n", begNode, asg_arc_n(g, begNode)); - // fflush(stderr); - // } - - // if(begNode == 18014006) - // { - // fprintf(stderr, "inner begNode: %d, vn: %d\n", begNode, asg_arc_n(g, begNode)); - // fflush(stderr); - // } while (1) @@ -3366,17 +2192,6 @@ long long* Len, buf_t* b, uint32_t max_ext) (*endNode) = v; (*Len)++; - // if(begNode == 18046652) - // { - // fprintf(stderr, "(*Len): %d, v: %d, begNode: %d\n", (*Len), v, begNode); - // fflush(stderr); - // } - - // if(begNode == 18014006) - // { - // fprintf(stderr, "(*Len): %d, v: %d, begNode: %d\n", (*Len), v, begNode); - // fflush(stderr); - // } if((*Len) > max_ext) { @@ -3385,12 +2200,7 @@ long long* Len, buf_t* b, uint32_t max_ext) if(b) kv_push(uint32_t, b->b, v>>1); - /** - if(b && b->b.n > 1000000) - { - fprintf(stderr, "begNode>>1: %u, v>>1: %u, b->b.n: %u\n", begNode>>1, v>>1, b->b.n); - } - **/ + if(nv == 0) @@ -3401,11 +2211,6 @@ long long* Len, buf_t* b, uint32_t max_ext) if(nv == 2) { - // if(begNode == 18014006) - // { - // fprintf(stderr, "bubble (*Len): %d, v: %d, begNode: %d\n", (*Len), v, begNode); - // fflush(stderr); - // } if(b) pre_b_n = b->b.n; if(!detect_simple_bubble(g, v, &v, &bLen, b)) @@ -3414,15 +2219,6 @@ long long* Len, buf_t* b, uint32_t max_ext) return TWO_OUTPUT; } - - // if(begNode == 18014006) - // { - // fprintf(stderr, "bubble (*Len): %d, v: %d, begNode: %d\n", (*Len), v, begNode); - // fflush(stderr); - // } - - - (*Len) = (*Len) + bLen - 2; continue; } @@ -3646,18 +2442,9 @@ uint32_t startNode, uint32_t endNode, int max_dist, buf_t* bub) if(if_node_exist(nodes, length, (w>>1)) && ((w>>1) != (endNode>>1))) { - - //if(av[NodeLen_first[1]].el == 1 && av[NodeLen_first[2]].el == 0 && - //aw[NodeLen_second[1]].el == 1 && aw[NodeLen_second[2]].el == 0) - ///if(av[NodeLen_first[2]].el == 0 && aw[NodeLen_second[2]].el == 0) - // if( - // (av[NodeLen_first[1]].strong == 1 && av[NodeLen_first[2]].strong == 0 && - // aw[NodeLen_second[1]].strong == 1 && aw[NodeLen_second[2]].strong == 0) - // || - // (av[NodeLen_first[1]].el == 1 && av[NodeLen_first[2]].el == 0 && - // aw[NodeLen_second[1]].el == 1 && aw[NodeLen_second[2]].el == 0)) - uint32_t convex1, convex2, f1, f2; - long long l1, l2; + + uint32_t convex1 = 0, convex2 = 0, f1, f2; + long long l1 = 0, l2 = 0; todel = 0; f1 = detect_bubble_end_with_bubbles(g, av[0].v, av[1].v, &convex1, &l1, NULL); f2 = detect_bubble_end_with_bubbles(g, aw[0].v, aw[1].v, &convex2, &l2, NULL); @@ -3769,7 +2556,6 @@ uint32_t startNode, uint32_t endNode, int max_dist, buf_t* bub) ///remove the reverse direction asg_arc_del(g, av[NodeLen_first[2]].v^1, av[NodeLen_first[2]].ul>>32^1, 1); n_reduced++; - ///fprintf(stderr, "***rm v>>1: %u, w>>1: %u\n", v>>1, w>>1); } } @@ -3786,13 +2572,10 @@ uint32_t startNode, uint32_t endNode, int max_dist, buf_t* bub) int find_single_link(asg_t *g, uint32_t link_beg, int linkLen, uint32_t* link_end) { uint32_t v, w; - int i; - i = 0; v = link_beg^1; uint32_t nv, nw; asg_arc_t *av; int edgeLen = 0; - int flag = -1; nv = asg_arc_n(g, v); av = asg_arc_a(g, v); @@ -3993,67 +2776,17 @@ uint32_t startNode, uint32_t endNode, int max_dist, buf_t* bub) uint32_t i, j, v, w; ///int flag0, flag1, node; int n_reduced = 0, todel; - uint32_t Nodes1[2]; - uint32_t Nodes2[2]; + uint32_t Nodes1[2]={0}; + uint32_t Nodes2[2]={0}; - uint32_t Ns_first[2]; - uint32_t Ns_second[2]; - - /** - if(startNode == 203) - { - uint32_t nv; - asg_arc_t *av; - - fprintf(stderr, "start: %d, end:%d, length: %d\n", startNode>>1, endNode>>1, length); - - v = startNode; - nv = asg_arc_n(g, v); - av = asg_arc_a(g, v); - fprintf(stderr, "******start: %d, nv: %d, dir: %d******\n", v>>1, nv, v&1); - for (j = 0; j < nv; j++) - { - fprintf(stderr, "j: %d, w: %d, dir: %d\n", j, av[j].v>>1, av[j].v&1); - } - fprintf(stderr, "*********************************************\n"); - - for (i = 0; i < length; ++i) - { - v = nodes[i]; - - nv = asg_arc_n(g, v); - av = asg_arc_a(g, v); - fprintf(stderr, "******v: %d, nv: %d, dir: %d******\n", v>>1, nv, v&1); - for (j = 0; j < nv; j++) - { - fprintf(stderr, "j: %d, w: %d, dir: %d\n", j, av[j].v>>1, av[j].v&1); - } - fprintf(stderr, "*********************************************\n"); - } + uint32_t Ns_first[2]={0}; + uint32_t Ns_second[2]={0}; - v = endNode; - nv = asg_arc_n(g, v); - av = asg_arc_a(g, v); - fprintf(stderr, "******end: %d, nv: %d, dir: %d******\n", v>>1, nv, v&1); - for (j = 0; j < nv; j++) - { - fprintf(stderr, "j: %d, w: %d, dir: %d\n", j, av[j].v>>1, av[j].v&1); - } - fprintf(stderr, "*********************************************\n"); - } - **/ - for (i = 0; i < length; ++i) { v = nodes[i]; - /** - if(startNode == 203) - { - fprintf(stderr, "0 v: %d, i: %d\n", v, i); - } - **/ if((v>>1) == (startNode>>1) || (v>>1) == (endNode>>1)) { @@ -4128,25 +2861,15 @@ uint32_t startNode, uint32_t endNode, int max_dist, buf_t* bub) continue; } - - - /** - if(startNode == 203) - { - fprintf(stderr, "1 v>>1: %d, i: %d\n", v>>1, i); - } - **/ - - uint32_t convex1, convex2, f1, f2; long long l1, l2; todel = 0; - if(Nodes1[0]^1 == startNode^1 || Nodes1[0]^1 == endNode) + if((Nodes1[0]^1) == (startNode^1) || (Nodes1[0]^1) == endNode) { continue; } - if(Nodes2[1]^1 == startNode^1 || Nodes2[1]^1 == endNode) + if((Nodes2[1]^1) == (startNode^1) || (Nodes2[1]^1) == endNode) { continue; } @@ -4154,20 +2877,11 @@ uint32_t startNode, uint32_t endNode, int max_dist, buf_t* bub) f1 = detect_bubble_end_with_bubbles(g, Nodes1[0]^1, Nodes2[1]^1, &convex1, &l1, NULL); - - - /** - if(startNode == 203) - { - fprintf(stderr, "2 v: %d, i: %d\n", v, i); - } - **/ - - if(Nodes2[0]^1 == startNode^1 || Nodes2[0]^1 == endNode) + if((Nodes2[0]^1) == (startNode^1) || (Nodes2[0]^1) == endNode) { continue; } - if(Nodes1[1]^1 == startNode^1 || Nodes1[1]^1 == endNode) + if((Nodes1[1]^1) == (startNode^1) || (Nodes1[1]^1) == endNode) { continue; } @@ -4175,15 +2889,6 @@ uint32_t startNode, uint32_t endNode, int max_dist, buf_t* bub) f2 = detect_bubble_end_with_bubbles(g, Nodes2[0]^1, Nodes1[1]^1, &convex2, &l2, NULL); - - - /** - if(startNode == 203) - { - fprintf(stderr, "3 v: %d, i: %d\n", v, i); - } - **/ - if(f1 && f2) { if(((convex1>>1) == (startNode>>1) || (convex1>>1) == (endNode>>1)) && @@ -4222,14 +2927,6 @@ uint32_t startNode, uint32_t endNode, int max_dist, buf_t* bub) } } - /** - if(startNode == 203) - { - fprintf(stderr, "4 v: %d, i: %d\n", v, i); - } - **/ - - if(todel == 0) { if(!f1) @@ -4288,29 +2985,6 @@ uint32_t startNode, uint32_t endNode, int max_dist, buf_t* bub) } - - - - - - - - - - - - - - - - - - - - - - - if(todel) { @@ -4334,7 +3008,7 @@ int asg_arc_del_triangular_advance(asg_t *g, long long max_dist) { double startTime = Get_T(); ///the reason is that each read has two direction (query->target, target->query) - uint32_t v, w, n_vtx = g->n_seq * 2, n_reduced = 0, n_reduced_a = 0; + uint32_t v, n_vtx = g->n_seq * 2, n_reduced = 0, n_reduced_a = 0; if (!g->is_symm) asg_symm(g); @@ -4349,13 +3023,9 @@ int asg_arc_del_triangular_advance(asg_t *g, long long max_dist) memset(&bub, 0, sizeof(buf_t)); bub.a = (binfo_t*)calloc(n_vtx, sizeof(binfo_t)); - - - int flag0, flag1, node; for (v = 0; v < n_vtx; ++v) { uint32_t nv = asg_arc_n(g, v); - asg_arc_t *av = asg_arc_a(g, v); if (g->seq[v>>1].del) { continue; @@ -4366,34 +3036,14 @@ int asg_arc_del_triangular_advance(asg_t *g, long long max_dist) continue; } - // if(v == 17929187) - // { - // fprintf(stderr, "*********v: %d\n", v); - // fflush(stderr); - // } - - ///if this is a bubble if(asg_bub_finder_with_del_advance(g, v, max_dist, &b) == 1) { n_reduced += test_triangular_exact(g, b.b.a, b.b.n, v, b.S.a[0], max_dist, &bub); - - // if(v == 17929187) - // { - // fprintf(stderr, "??????v: %d, b.S.a[0]: %d\n", v, b.S.a[0]); - // fflush(stderr); - // } - n_reduced_a += test_triangular_addition_exact(g, b.b.a, b.b.n, v, b.S.a[0],max_dist, &bub); } - // if(v == 17929187) - // { - // fprintf(stderr, "##########v: %d\n", v); - // fflush(stderr); - // } - } free(b.a); free(b.S.a); free(b.T.a); free(b.b.a); free(b.e.a); @@ -4404,119 +3054,24 @@ int asg_arc_del_triangular_advance(asg_t *g, long long max_dist) asg_symm(g); } - fprintf(stderr, "[M::%s] removed %d/%d triangular/triangular_a overlaps\n", - __func__, n_reduced, n_reduced_a); - fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); - - return n_reduced + n_reduced_a; -} - - - - -int asg_arc_del_triangular_advance_debug(asg_t *g, long long max_dist) -{ - double startTime = Get_T(); - ///the reason is that each read has two direction (query->target, target->query) - uint32_t v, w, n_vtx = g->n_seq * 2, n_reduced = 0, n_reduced_a = 0; - - - if (!g->is_symm) asg_symm(g); - - - buf_t b; - memset(&b, 0, sizeof(buf_t)); - b.a = (binfo_t*)calloc(n_vtx, sizeof(binfo_t)); - - - buf_t bub; - memset(&bub, 0, sizeof(buf_t)); - bub.a = (binfo_t*)calloc(n_vtx, sizeof(binfo_t)); - - fprintf(stderr, "n_vtx: %d\n", n_vtx); - - int flag0, flag1, node; - for (v = 0; v < n_vtx; ++v) + if(VERBOSE >= 1) { - - fprintf(stderr, "0 v: %d\n", v); - fflush(stderr); - - uint32_t nv = asg_arc_n(g, v); - asg_arc_t *av = asg_arc_a(g, v); - if (g->seq[v>>1].del) - { - continue; - } - - if(nv < 2) - { - continue; - } - - // if(v == 17929187) - // { - // fprintf(stderr, "*********v: %d\n", v); - // fflush(stderr); - // } - - fprintf(stderr, "1 v: %d\n", v); - fflush(stderr); - - ///if this is a bubble - if(asg_bub_finder_with_del_advance(g, v, max_dist, &b) == 1) - { - fprintf(stderr, "2 v: %d\n", v); - fflush(stderr); - - n_reduced += test_triangular_exact(g, b.b.a, b.b.n, v, b.S.a[0], max_dist, &bub); - - fprintf(stderr, "3 v: %d\n", v); - fflush(stderr); - - if(v == 203) - { - fprintf(stderr, "??????v: %d, b.S.a[0]: %d, b.b.n: %d\n", v, b.S.a[0], b.b.n); - fflush(stderr); - } - - n_reduced_a += test_triangular_addition_exact(g, b.b.a, b.b.n, v, b.S.a[0],max_dist, &bub); - - fprintf(stderr, "5 v: %d\n", v); - fflush(stderr); - - } - - fprintf(stderr, "5 v: %d\n", v); - fflush(stderr); - - // if(v == 17929187) - // { - // fprintf(stderr, "##########v: %d\n", v); - // fflush(stderr); - // } - + fprintf(stderr, "[M::%s] removed %d/%d triangular/triangular_a overlaps\n", + __func__, n_reduced, n_reduced_a); + fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); } - free(b.a); free(b.S.a); free(b.T.a); free(b.b.a); free(b.e.a); - free(bub.a); free(bub.S.a); free(bub.T.a); free(bub.b.a); free(bub.e.a); - - if (n_reduced + n_reduced_a) { - asg_cleanup(g); - asg_symm(g); - } - - fprintf(stderr, "[M::%s] removed %d/%d triangular/triangular_a overlaps\n", - __func__, n_reduced, n_reduced_a); - fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); - return n_reduced + n_reduced_a; } + + + + int check_if_cross(asg_t *g, uint32_t v) { - uint32_t N_list[5]; + uint32_t N_list[5] = {0}; if (g->seq[v>>1].del) return 0; uint32_t nv = asg_arc_n(g, v); asg_arc_t *av = asg_arc_a(g, v); @@ -4615,77 +3170,7 @@ int check_if_cross(asg_t *g, uint32_t v) return todel; } -int asg_arc_identify_simple_bubbles(asg_t *g) -{ - double startTime = Get_T(); - ///the reason is that each read has two direction (query->target, target->query) - uint32_t v, w, n_vtx = g->n_seq * 2, n_reduced = 0; - buf_t b; - memset(&b, 0, sizeof(buf_t)); - memset(g->seq_vis, 0, g->n_seq*2*sizeof(uint8_t)); - long long l, i; - for (v = 0; v < n_vtx; ++v) - { - if (g->seq[v>>1].del) continue; - b.b.n = 0; - - if(g->seq_vis[v] != 1) - { - ///if(detect_bubble_with_bubbles(g, v, &w, &l, &b, (uint32_t)-1)) - if(detect_bubble_with_bubbles(g, v, &w, &l, &b, SMALL_BUBBLE_SIZE)) - { - for (i = 0; i < b.b.n; i++) - { - if(b.b.a[i] != (v>>1) && b.b.a[i] != (w>>1)) - { - g->seq_vis[b.b.a[i]<<1] = 1; - g->seq_vis[(b.b.a[i]<<1) + 1] = 1; - } - } - g->seq_vis[v] = 1; - g->seq_vis[w^1] = 1; - - - // if(asg_arc_n(g, v) != 2 || asg_arc_n(g, w^1) != 2) - // { - // fprintf(stderr, "error\n"); - // } - // if(v>>1 != b.b.a[0] || w>>1 != b.b.a[b.b.n-1]) - // { - // fprintf(stderr, "sbsbsbs\n"); - // } - - } - } - - - if(check_if_cross(g, v)) - { - g->seq_vis[v] = 2; - } - } - free(b.b.a); - - long long nodes, bub_nodes, cross_nodes; - bub_nodes = nodes = cross_nodes = 0; - for (v = 0; v < n_vtx; ++v) - { - if (g->seq[v>>1].del) continue; - nodes++; - if(g->seq_vis[v] == 1) bub_nodes++; - if(g->seq_vis[v] == 2) cross_nodes++; - } - - - fprintf(stderr, "[M::%s] nodes:%d, bub_nodes: %d, cross_nodes: %d\n", __func__, - nodes, bub_nodes, cross_nodes); - - - fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); - - return n_reduced; -} typedef struct { @@ -4703,7 +3188,7 @@ void* asg_arc_identify_simple_bubbles_pthread(void* arg) int check_cross = ((para_for_simple_bub*)arg)->check_cross; ///the reason is that each read has two direction (query->target, target->query) - uint32_t v, w, n_vtx = g->n_seq * 2, n_reduced = 0; + uint32_t v, w, n_vtx = g->n_seq * 2; buf_t b; memset(&b, 0, sizeof(buf_t)); long long l, i; @@ -4719,7 +3204,7 @@ void* asg_arc_identify_simple_bubbles_pthread(void* arg) ///if(detect_bubble_with_bubbles(g, v, &w, &l, &b, (uint32_t)-1)) if(detect_bubble_with_bubbles(g, v, &w, &l, &b, SMALL_BUBBLE_SIZE)) { - for (i = 0; i < b.b.n; i++) + for (i = 0; i < (long long)b.b.n; i++) { if(b.b.a[i] != (v>>1) && b.b.a[i] != (w>>1)) { @@ -4732,35 +3217,16 @@ void* asg_arc_identify_simple_bubbles_pthread(void* arg) } } - - ///if(v%10000 == 0) - //if(v >= 18020000) - // if(v == 18021291) - // { - // fprintf(stderr, "1 v: %d, thr_ID: %d\n", v, thr_ID); - // fflush(stderr); - // } - - if(check_cross == 1 && check_if_cross(g, v)) { g->seq_vis[v] = 2; } - - ///if(v%10000 == 0) - //if(v >= 18020000) - // if(v == 18021291) - // { - // fprintf(stderr, "2 v: %d, thr_ID: %d\n", v, thr_ID); - // fflush(stderr); - // } } free(b.b.a); free(arg); - // fprintf(stderr, "thr_ID: %d end\n", thr_ID); - // fflush(stderr); + return NULL; } int asg_arc_identify_simple_bubbles_multi(asg_t *g, int check_cross) @@ -4770,15 +3236,15 @@ int asg_arc_identify_simple_bubbles_multi(asg_t *g, int check_cross) pthread_t *_r_threads; - _r_threads = (pthread_t *)malloc(sizeof(pthread_t)*thread_num); + _r_threads = (pthread_t *)malloc(sizeof(pthread_t)*asm_opt.thread_num); int i = 0; - for (i = 0; i < thread_num; i++) + for (i = 0; i < asm_opt.thread_num; i++) { para_for_simple_bub* arg = (para_for_simple_bub*)malloc(sizeof(*arg)); arg->g = g; - arg->thread_num = thread_num; + arg->thread_num = asm_opt.thread_num; arg->threadID = i; arg->check_cross = check_cross; @@ -4786,7 +3252,7 @@ int asg_arc_identify_simple_bubbles_multi(asg_t *g, int check_cross) } - for (i = 0; i= 1) + { + fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + } return bub_nodes+cross_nodes; } @@ -4870,22 +3336,6 @@ long long* vLen, long long* wLen, uint32_t* endNode) return 1; } - /** - if(pLen1 != 1 && pLen2 != 1) - { - fprintf(stderr, "v: pLen1: %d, pLen2: %d\n", pLen1, pLen2); - pLen1 = single_edge_length(g, v^1, begNode>>1); - fprintf(stderr, "pLen_v: %d\n", pLen1); - fprintf(stderr, "vv>>1: %u, v>>1: %u, begNode>>1: %u\n", - vv>>1, v>>1, begNode>>1); - } - - if((asg_arc_a(g, vv)[0].v) != (v^1) && (asg_arc_a(g, vv)[1].v) != (v^1)) - { - fprintf(stderr, "vv: %u\n", vv); - } - **/ - } } @@ -4933,20 +3383,6 @@ long long* vLen, long long* wLen, uint32_t* endNode) return 1; } - - /** - if(pLen1 != 1 && pLen2 != 1) - { - fprintf(stderr, "w: pLen1: %d, pLen2: %d\n", pLen1, pLen2); - pLen1 = single_edge_length(g, w^1, begNode>>1); - fprintf(stderr, "pLen_w: %d\n", pLen1); - } - - if((asg_arc_a(g, ww)[0].v) != (w^1) && (asg_arc_a(g, ww)[1].v) != (w^1)) - { - fprintf(stderr, "ww: %u\n", ww); - } - **/ } } @@ -4961,7 +3397,7 @@ uint32_t startNode, uint32_t endNode) uint32_t i, v, w; uint32_t vEnd; - int flag0, flag1, node; + int flag0, flag1; int n_reduced = 0; long long Len[2], longLen; long long longLen_thres = 4; @@ -5102,7 +3538,7 @@ int test_single_node_bubble_directly(asg_t *g, uint32_t v, long long longLen_thr { uint32_t w, vEnd; - int flag0, flag1, node; + int flag0, flag1; int n_reduced = 0; long long Len[2], longLen; ///long long longLen_thres = 4; @@ -5235,17 +3671,15 @@ int test_single_node_bubble_directly(asg_t *g, uint32_t v, long long longLen_thr int asg_arc_del_single_node_bubble(asg_t *g, long long max_dist) { ///the reason is that each read has two direction (query->target, target->query) - uint32_t v, w, n_vtx = g->n_seq * 2, n_reduced = 0; + uint32_t v, n_vtx = g->n_seq * 2, n_reduced = 0; buf_t b; if (!g->is_symm) asg_symm(g); memset(&b, 0, sizeof(buf_t)); ///set information for each node b.a = (binfo_t*)calloc(n_vtx, sizeof(binfo_t)); - int flag0, flag1, node; for (v = 0; v < n_vtx; ++v) { uint32_t nv = asg_arc_n(g, v); - asg_arc_t *av = asg_arc_a(g, v); if (g->seq[v>>1].del) { continue; @@ -5280,11 +3714,10 @@ int asg_arc_del_single_node_directly(asg_t *g, long long longLen_thres, ma_hit_t { double startTime = Get_T(); ///the reason is that each read has two direction (query->target, target->query) - uint32_t v, w, n_vtx = g->n_seq * 2, n_reduced = 0; + uint32_t v, n_vtx = g->n_seq * 2, n_reduced = 0; for (v = 0; v < n_vtx; ++v) { uint32_t nv = asg_arc_n(g, v); - asg_arc_t *av = asg_arc_a(g, v); if (g->seq[v>>1].del) { continue; @@ -5304,8 +3737,11 @@ int asg_arc_del_single_node_directly(asg_t *g, long long longLen_thres, ma_hit_t asg_symm(g); } - fprintf(stderr, "[M::%s] removed %d small bubbles\n", __func__, n_reduced); - fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + if(VERBOSE >= 1) + { + fprintf(stderr, "[M::%s] removed %d small bubbles\n", __func__, n_reduced); + fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + } return n_reduced; } @@ -5316,8 +3752,6 @@ int asg_arc_del_self_circle_contig(asg_t *g) double startTime = Get_T(); uint32_t v; uint32_t n_vtx = g->n_seq * 2, n_reduced = 0; - uint32_t vEnd; - int flag0, flag1, node; long long Len[3]; for (v = 0; v < n_vtx; ++v) { @@ -5366,8 +3800,11 @@ int asg_arc_del_self_circle_contig(asg_t *g) asg_symm(g); } - fprintf(stderr, "[M::%s] removed %d self-circle contig\n", __func__, n_reduced); - fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + if(VERBOSE >= 1) + { + fprintf(stderr, "[M::%s] removed %d self-circle contig\n", __func__, n_reduced); + fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + } return n_reduced; } @@ -5377,7 +3814,7 @@ int test_cross(asg_t *g, uint32_t* nodes, uint32_t length, uint32_t startNode, uint32_t endNode) { uint32_t a1, a2; - uint32_t N_list[5]; + uint32_t N_list[5] = {0}; uint32_t i, v; int flag0, flag1; int n_reduced = 0; @@ -5530,13 +3967,6 @@ uint32_t startNode, uint32_t endNode) asg_arc_a(g, N_list[3])[a2].del = 1; asg_arc_del(g, asg_arc_a(g, N_list[3])[a2].v^1, asg_arc_a(g, N_list[3])[a2].ul>>32^1, 1); - /** - fprintf(stderr, "(N_list[0]>>1): %u, (N_list[1]>>1): %u, (N_list[2]>>1): %u, (N_list[3]>>1): %u\n", - (N_list[0]>>1), (N_list[1]>>1), (N_list[2]>>1), (N_list[3]>>1)); - fprintf(stderr, "a1: %u\n", asg_arc_a(g, N_list[0])[a1].v>>1); - fprintf(stderr, "a2: %u\n", asg_arc_a(g, N_list[3])[a2].v>>1); - **/ - n_reduced++; } } @@ -5549,17 +3979,15 @@ int asg_arc_del_cross_bubble(asg_t *g, long long max_dist) { double startTime = Get_T(); ///the reason is that each read has two direction (query->target, target->query) - uint32_t v, w, n_vtx = g->n_seq * 2, n_reduced = 0; + uint32_t v, n_vtx = g->n_seq * 2, n_reduced = 0; buf_t b; if (!g->is_symm) asg_symm(g); memset(&b, 0, sizeof(buf_t)); ///set information for each node b.a = (binfo_t*)calloc(n_vtx, sizeof(binfo_t)); - int flag0, flag1, node; for (v = 0; v < n_vtx; ++v) { uint32_t nv = asg_arc_n(g, v); - asg_arc_t *av = asg_arc_a(g, v); if (g->seq[v>>1].del) { continue; @@ -5585,8 +4013,11 @@ int asg_arc_del_cross_bubble(asg_t *g, long long max_dist) asg_symm(g); } - fprintf(stderr, "[M::%s] removed %d cross\n", __func__, n_reduced); - fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + if(VERBOSE >= 1) + { + fprintf(stderr, "[M::%s] removed %d cross\n", __func__, n_reduced); + fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + } return n_reduced; } @@ -5706,14 +4137,21 @@ int asg_arc_del_trans(asg_t *g, int fuzz) } } free(mark); - fprintf(stderr, "[M::%s] transitively reduced %d arcs\n", __func__, n_reduced); + + if(VERBOSE >= 1) + { + fprintf(stderr, "[M::%s] transitively reduced %d arcs\n", __func__, n_reduced); + } + if (n_reduced) { asg_cleanup(g); asg_symm(g); } - - fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + if(VERBOSE >= 1) + { + fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + } return n_reduced; } @@ -5765,10 +4203,11 @@ int asg_cut_tip(asg_t *g, int max_ext) } free(a.a); if (cnt > 0) asg_cleanup(g); - fprintf(stderr, "[M::%s] cut %d tips\n", __func__, cnt); - fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); - - + if(VERBOSE >= 1) + { + fprintf(stderr, "[M::%s] cut %d tips\n", __func__, cnt); + fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + } return cnt; } @@ -5927,80 +4366,6 @@ long long weakID) return 0; } -// delete short arcs -///for best graph? -int asg_arc_del_short_diploid(asg_t *g, float drop_ratio, ma_hit_t_alloc* sources, ma_hit_t_alloc* reverse_sources) -{ - float second_drop_ratio = 0.3; - uint32_t v, n_vtx = g->n_seq * 2, n_short = 0; - uint32_t last_e, flag; - for (v = 0; v < n_vtx; ++v) - { - asg_arc_t *av = asg_arc_a(g, v); - uint32_t i, thres, nv = asg_arc_n(g, v); - ///if there is just one overlap, do nothing - if (nv < 2) continue; - //av[0] has the most overlap length - ///remove short overlaps - thres = (uint32_t)(av[0].ol * drop_ratio + .499); - ///av has been sorted by overlap length - for (i = nv - 1; i >= 1 && av[i].ol < thres; --i); - last_e = i + 1; - - for (i = i + 1; i < nv; ++i) - av[i].del = 1, ++n_short; - - - if(nv >= 2 && av[1].del == 1) - { - thres = (uint32_t)(av[0].ol * second_drop_ratio + .499); - if(av[1].ol >= thres) - { - ///second longest - av[1].del = 0; - --n_short; - last_e++; - } - } - - /** - if(last_e > 1) - { - flag = 0; - ///at least one exact edge - for (i = 0; i < last_e; i++) - { - if(av[i].el) - { - flag = 1; - break; - } - } - - if(flag) - { - for (i = 0; i < last_e; i++) - { - //drop inexact overlaps - if(av[i].el == 0) - { - av[i].del = 1; - ++n_short; - } - } - } - } - **/ - - } - ///if (n_short) - { - asg_cleanup(g); - asg_symm(g); - } - fprintf(stderr, "[M::%s] removed %d short overlaps\n", __func__, n_short); - return n_short; -} // delete short arcs @@ -6010,7 +4375,7 @@ int asg_arc_del_short_diploid_unclean(asg_t *g, float drop_ratio, ma_hit_t_alloc double startTime = Get_T(); uint32_t v, n_vtx = g->n_seq * 2, n_short = 0; - uint32_t last_e, flag; + uint32_t last_e; for (v = 0; v < n_vtx; ++v) { if (g->seq[v>>1].del) continue; @@ -6037,35 +4402,6 @@ int asg_arc_del_short_diploid_unclean(asg_t *g, float drop_ratio, ma_hit_t_alloc --n_short; last_e++; } - - /** - if(last_e > 1) - { - flag = 0; - ///at least one exact edge - for (i = 0; i < last_e; i++) - { - if(av[i].el) - { - flag = 1; - break; - } - } - - if(flag) - { - for (i = 0; i < last_e; i++) - { - //drop inexact overlaps - if(av[i].el == 0) - { - av[i].del = 1; - ++n_short; - } - } - } - } - **/ } ///if (n_short) @@ -6169,7 +4505,7 @@ ma_hit_t_alloc* reverse_sources, long long min_edge_length) memset(&b_0, 0, sizeof(buf_t)); memset(&b_1, 0, sizeof(buf_t)); - uint32_t convex1, convex2, f1, f2; + uint32_t convex1, convex2; long long l1, l2; b_0.b.n = 0; @@ -6220,15 +4556,15 @@ ma_hit_t_alloc* reverse_sources, long long min_edge_length) double max_count = 0; double min_count = 0; uint32_t qn, tn; - for (i = 0; i < b_min->b.n; i++) + for (i = 0; i < (long long)b_min->b.n; i++) { qn = b_min->b.a[i]; - for (j = 0; j < reverse_sources[qn].length; j++) + for (j = 0; j < (long long)reverse_sources[qn].length; j++) { tn = Get_tn(reverse_sources[qn].buffer[j]); if(g->seq[tn].del == 1) continue; min_count++; - for (k = 0; k < b_max->b.n; k++) + for (k = 0; k < (long long)b_max->b.n; k++) { if(b_max->b.a[k]==tn) { @@ -6258,7 +4594,7 @@ ma_hit_t_alloc* reverse_sources, long long min_edge_length) memset(&b_0, 0, sizeof(buf_t)); memset(&b_1, 0, sizeof(buf_t)); - uint32_t convex1, convex2, f1, f2; + uint32_t convex1, convex2; long long l1, l2; b_0.b.n = 0; @@ -6309,15 +4645,15 @@ ma_hit_t_alloc* reverse_sources, long long min_edge_length) double max_count = 0; double min_count = 0; uint32_t qn, tn; - for (i = 0; i < b_min->b.n; i++) + for (i = 0; i < (long long)b_min->b.n; i++) { qn = b_min->b.a[i]; - for (j = 0; j < reverse_sources[qn].length; j++) + for (j = 0; j < (long long)reverse_sources[qn].length; j++) { tn = Get_tn(reverse_sources[qn].buffer[j]); if(g->seq[tn].del == 1) continue; min_count++; - for (k = 0; k < b_max->b.n; k++) + for (k = 0; k < (long long)b_max->b.n; k++) { if(b_max->b.a[k]==tn) { @@ -6344,139 +4680,13 @@ ma_hit_t_alloc* reverse_sources, long long min_edge_length) } -long long check_if_diploid_debug(uint32_t v1, uint32_t v2, asg_t *g, -ma_hit_t_alloc* reverse_sources, long long min_edge_length) -{ - buf_t b_0, b_1; - memset(&b_0, 0, sizeof(buf_t)); - memset(&b_1, 0, sizeof(buf_t)); - - uint32_t convex1, convex2, f1, f2; - long long l1, l2; - - b_0.b.n = 0; - b_1.b.n = 0; - uint32_t flag1 = detect_single_path(g, v1, &convex1, &l1, &b_0); - uint32_t flag2 = detect_single_path(g, v2, &convex2, &l2, &b_1); - - if(flag1 == LOOP || flag2 == LOOP) - { - return -1; - } - - if(flag1 != END_TIPS && flag1 != LONG_TIPS) - { - l1--; - b_0.b.n--; - } - - if(flag2 != END_TIPS && flag2 != LONG_TIPS) - { - l2--; - b_1.b.n--; - } - - - if(l1 <= min_edge_length || l2 <= min_edge_length) - { - return 0; - } - - buf_t* b_min; - buf_t* b_max; - if(l1<=l2) - { - b_min = &b_0; - b_max = &b_1; - } - else - { - b_min = &b_1; - b_max = &b_0; - } - - - fprintf(stderr, "b_0.n: %d\n", b_0.b.n); - fprintf(stderr, "b_1.n: %d\n", b_1.b.n); - - fprintf(stderr, "b_min.n: %d\n", b_min->b.n); - fprintf(stderr, "b_max.n: %d\n", b_max->b.n); - - long long i, j, k; - double max_count = 0; - double min_count = 0; - uint32_t qn, tn; - for (i = 0; i < b_min->b.n; i++) - { - qn = b_min->b.a[i]; - for (j = 0; j < reverse_sources[qn].length; j++) - { - tn = Get_tn(reverse_sources[qn].buffer[j]); - if(g->seq[tn].del == 1) continue; - min_count++; - for (k = 0; k < b_max->b.n; k++) - { - if(b_max->b.a[k]==tn) - { - max_count++; - break; - } - } - } - } - - - free(b_0.b.a); - free(b_1.b.a); - - if(min_count == 0) return -1; - if(max_count == 0) return 0; - if(max_count/min_count>0.3) return 1; - return 0; - -} - int asg_arc_del_too_short_overlaps(asg_t *g, long long dropLen, float drop_ratio, ma_hit_t_alloc* reverse_sources, long long min_edge_length) { double startTime = Get_T(); uint32_t v, v_max, v_maxLen, n_vtx = g->n_seq * 2, n_short = 0; long long drop_ratio_Len; - /** - for (v = 0; v < n_vtx; ++v) - { - if (g->seq[v>>1].del) continue; - if (g->seq_vis[v] != 0) continue; - - asg_arc_t *av = asg_arc_a(g, v); - uint32_t i, thres, nv = asg_arc_n(g, v); - ///if there is just one overlap, do nothing - if (nv < 2) continue; - //av[0] has the most overlap length - ///remove short overlaps - if(av[0].ol < dropLen) continue; - - drop_ratio_Len = av[0].ol * drop_ratio; - if(dropLen < drop_ratio_Len) - { - drop_ratio_Len = dropLen; - } - - for (i = nv - 1; i >= 1 && av[i].ol < drop_ratio_Len; --i); - - // for (i = i + 1; i < nv; ++i) - // av[i].del = 1, ++n_short; - for (i = i + 1; i < nv; ++i) - { - if(check_if_diploid(av[0].v, av[i].v, g, reverse_sources, min_edge_length) != 1) - { - av[i].del = 1, ++n_short; - } - } - } - **/ - - + for (v = 0; v < n_vtx; ++v) { if (g->seq[v>>1].del) continue; @@ -6523,8 +4733,11 @@ int asg_arc_del_too_short_overlaps(asg_t *g, long long dropLen, float drop_ratio asg_cleanup(g); asg_symm(g); - fprintf(stderr, "[M::%s] removed %d short overlaps\n", __func__, n_short); - fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + if(VERBOSE >= 1) + { + fprintf(stderr, "[M::%s] removed %d short overlaps\n", __func__, n_short); + fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + } return n_short; } @@ -6534,7 +4747,6 @@ int asg_arc_del_too_short_overlaps(asg_t *g, long long dropLen, float drop_ratio int asg_arc_del_short_diploid_unclean_exact(asg_t *g, float drop_ratio, ma_hit_t_alloc* sources) { uint32_t v, n_vtx = g->n_seq * 2, n_short = 0; - uint32_t last_e, flag; for (v = 0; v < n_vtx; ++v) { if (g->seq[v>>1].del) continue; @@ -6600,52 +4812,6 @@ long long single_edge(asg_t *g, uint32_t begNode, long long edgeLen) } -// delete short arcs -///for best graph? -int asg_arc_del_short_diploid_based_on_length_back(asg_t *g, float drop_ratio, ma_hit_t_alloc* sources, ma_hit_t_alloc* reverse_sources) -{ - uint32_t v, n_vtx = g->n_seq * 2, n_short = 0; - uint32_t last_e, flag; - for (v = 0; v < n_vtx; ++v) - { - asg_arc_t *av = asg_arc_a(g, v); - uint32_t i, thres, nv = asg_arc_n(g, v); - ///if there is just one overlap, do nothing - if (nv < 2) continue; - //av[0] has the most overlap length - ///remove short overlaps - thres = (uint32_t)(av[0].ol * drop_ratio + .499); - ///av has been sorted by overlap length - for (i = nv - 1; i >= 1 && av[i].ol < thres; --i); - last_e = i + 1; - - for (i = i + 1; i < nv; ++i) - av[i].del = 1, ++n_short; - - - if(nv >= 2 && av[1].del == 1) - { - if(single_edge(g, av[1].v, 4) != -1) - { - ///second longest - av[1].del = 0; - --n_short; - last_e++; - } - } - - - } - ///if (n_short) - { - asg_cleanup(g); - asg_symm(g); - } - fprintf(stderr, "[M::%s] removed %d short overlaps\n", __func__, n_short); - return n_short; -} - - ///check if v has only one branch static uint32_t asg_check_unambi1(asg_t *g, uint32_t v) { @@ -6668,13 +4834,7 @@ static int asg_topocut_aux(asg_t *g, uint32_t v, int max_ext) } v = asg_check_unambi1(g, v); } - /** - if(v == (uint32_t)-1 && n_ext < max_ext) //return max_ext + 1; - { - fprintf(stderr, "v: %llu, n_ext: %llu, max_ext: %llu \n", v, n_ext, max_ext); - } - **/ - ///if(v == (uint32_t)-1) return max_ext + 1; + return n_ext; } @@ -6697,21 +4857,17 @@ ma_hit_t_alloc* reverse_sources, long long miniedgeLen) asg_arc_t *av = asg_arc_a(g, v); uint32_t nv = asg_arc_n(g, v); if (nv < 2) continue; - long long i; + uint64_t i; for (i = 0; i < nv; ++i) { - kv_push(uint64_t, b, (uint64_t)(av[i].ol << 32 | (av - g->arc + i))); + kv_push(uint64_t, b, (uint64_t)((uint64_t)av[i].ol << 32 | (av - g->arc + i))); } } } - fprintf(stderr, "[M::%s] %lld unsorted pending overlaps\n", __func__, b.n); - radix_sort_arch64(b.a, b.a + b.n); - fprintf(stderr, "[M::%s] %lld sorted pending overlaps\n", __func__, b.n); - - long long k; + uint64_t k; for (k = 0; k < b.n; k++) { @@ -6719,7 +4875,7 @@ ma_hit_t_alloc* reverse_sources, long long miniedgeLen) ///v is self id, w is the id of another end uint32_t i, iv, iw, v = (a->ul)>>32, w = a->v^1, to_del = 0; uint32_t nv = asg_arc_n(g, v), nw = asg_arc_n(g, w), kv, kw; - uint32_t ov_max = 0, ov_max_i, ow_max = 0, ow_max_i; + uint32_t ov_max = 0, ow_max = 0; asg_arc_t *av, *aw; ///nv must be >= 2 if (nv == 1 && nw == 1) continue; @@ -6730,7 +4886,7 @@ ma_hit_t_alloc* reverse_sources, long long miniedgeLen) ///calculate the longest edge for v and w for (i = 0, kv = 0; i < nv; ++i) { if (av[i].del) continue; - if (ov_max < av[i].ol) ov_max = av[i].ol, ov_max_i = i; + if (ov_max < av[i].ol) ov_max = av[i].ol/**, ov_max_i = i**/; ++kv; } if (kv >= 2 && a->ol > ov_max * drop_ratio) continue; @@ -6738,7 +4894,7 @@ ma_hit_t_alloc* reverse_sources, long long miniedgeLen) for (i = 0, kw = 0; i < nw; ++i) { if (aw[i].del) continue; - if (ow_max < aw[i].ol) ow_max = aw[i].ol, ow_max_i = i; + if (ow_max < aw[i].ol) ow_max = aw[i].ol/**, ow_max_i = i**/; ++kw; } if (kw >= 2 && a->ol > ow_max * drop_ratio) continue; @@ -6800,205 +4956,12 @@ ma_hit_t_alloc* reverse_sources, long long miniedgeLen) asg_cleanup(g); asg_symm(g); } - fprintf(stderr, "[M::%s] removed %d short overlaps\n", __func__, n_cut); - fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); - return n_cut; -} - - - -// delete short arcs -///for best graph? -int asg_arc_del_short_false_link_back(asg_t *g, float drop_ratio, int max_ext) -{ - double startTime = Get_T(); - kvec_t(uint64_t) b; - memset(&b, 0, sizeof(b)); - - uint32_t v, n_vtx = g->n_seq * 2; - long long n_cut = 0; - - for (v = 0; v < n_vtx; ++v) + if(VERBOSE >= 1) { - if(g->seq_vis[v] == 0) - { - asg_arc_t *av = asg_arc_a(g, v); - uint32_t nv = asg_arc_n(g, v); - if (nv != 2) continue; - if(asg_arc_n(g, v^1)!=1) continue; - - - long long i; - for (i = 0; i < nv; ++i) - { - kv_push(uint64_t, b, (uint64_t)(av[i].ol << 32 | (av - g->arc + i))); - } - } - } - - fprintf(stderr, "[M::%s] %lld unsorted pending overlaps\n", __func__, b.n); - - radix_sort_arch64(b.a, b.a + b.n); - - fprintf(stderr, "[M::%s] %lld sorted pending overlaps\n", __func__, b.n); - - uint32_t v_s[2]; - uint32_t w_s[4]; - - long long k; - for (k = 0; k < b.n; k++) - { - asg_arc_t *a = &g->arc[(uint32_t)b.a[k]]; - if(a->del) continue; - ///v is self id, w is the id of another end - uint32_t i, iv, v = (a->ul)>>32, to_del = 0; - uint32_t nv = asg_arc_n(g, v), kv; - asg_arc_t *av, *aw; - if (nv < 2) continue; - av = asg_arc_a(g, v); - - - kv = get_real_length(g, v, NULL); - if (kv != 2) continue; - - if(get_real_length(g, v^1, NULL)!=1) continue; - - - get_real_length(g, v^1, v_s); - if(get_real_length(g, v_s[0]^1, NULL) < 2) continue; - - - - - //check the length - for (i = 0, kv = 0; i < nv; ++i) { - if (av[i].del) continue; - v_s[kv] = av[i].ol; - ++kv; - } - - uint32_t s_max = 0; - for (i = 0; i < asg_arc_n(g, v^1); i++) - { - if(asg_arc_a(g, v^1)[i].del) continue; - s_max = asg_arc_a(g, v^1)[i].ol; - break; - } - - uint32_t ov_max, ov_min; - if(v_s[0] >= v_s[1]) - { - ov_max = v_s[0]; - ov_min = v_s[1]; - } - else - { - ov_max = v_s[1]; - ov_min = v_s[0]; - } - - if(ov_min < ov_max * drop_ratio) - { - continue; - } - - if(ov_max > s_max * 0.5) - { - continue; - } - //check the length - - - - - get_real_length(g, v, v_s); - v_s[0] = v_s[0]^1; - v_s[1] = v_s[1]^1; - - if(v_s[0] == v_s[1]) continue; - - if(get_real_length(g, v_s[0], NULL)!=2) continue; - if(get_real_length(g, v_s[1], NULL)!=2) continue; - - - get_real_length(g, v_s[0], w_s); - get_real_length(g, v_s[1], w_s + 2); - - for (i = 0; i < 2; i++) - { - if((w_s[i]>>1) != (v>>1)) - { - w_s[0] = w_s[i]; - } - } - - for (i = 2; i < 4; i++) - { - if((w_s[i]>>1) != (v>>1)) - { - w_s[1] = w_s[i]; - } - } - - if(w_s[0] == w_s[1]) - { - to_del = 1; - } - - uint32_t convex1, f1; - long long l1; - if(to_del == 0) - { - f1 = detect_bubble_end_with_bubbles(g, w_s[0], w_s[1], &convex1, &l1, NULL); - if(f1) - { - to_del = 1; - } - } - - if(to_del == 0) - { - v_s[0] = v_s[0]^1; - v_s[1] = v_s[1]^1; - - if(get_real_length(g, v_s[0], NULL)!=1) continue; - if(get_real_length(g, v_s[1], NULL)!=1) continue; - - if(v_s[0] == v_s[1]) - { - to_del = 1; - } - - f1 = detect_bubble_end_with_bubbles(g, v_s[0], v_s[1], &convex1, &l1, NULL); - if(f1) - { - to_del = 1; - } - } - - - if (to_del) - { - for (i = 0; i < nv; ++i) - { - if (av[i].del) continue; - ++n_cut; - av[i].del = 1; - asg_arc_del(g, av[i].v^1, av[i].ul>>32^1, 1); - } - - } + fprintf(stderr, "[M::%s] removed %lld short overlaps\n", __func__, n_cut); + fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); } - - free(b.a); - if (n_cut) - { - asg_cleanup(g); - asg_symm(g); - } - fprintf(stderr, "[M::%s] removed %d false overlaps\n", __func__, n_cut); - fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); return n_cut; } @@ -7045,18 +5008,12 @@ ma_hit_t_alloc* reverse_sources, long long miniedgeLen) } } - fprintf(stderr, "[M::%s] %lld unsorted pending overlaps\n", __func__, b.n); - radix_sort_arch64(b.a, b.a + b.n); - fprintf(stderr, "[M::%s] %lld sorted pending overlaps\n", __func__, b.n); - - - uint32_t min_edge; - long long k, t; + uint64_t k, t; for (k = 0; k < b.n; k++) { ///v is the node @@ -7157,7 +5114,7 @@ ma_hit_t_alloc* reverse_sources, long long miniedgeLen) if(to_del_l != kv && b_r.n >= 2) { to_del_l = 0; - uint32_t w0, w1; + uint32_t w0 = 0, w1 = 0; w = b_r.a[0]; @@ -7239,16 +5196,6 @@ ma_hit_t_alloc* reverse_sources, long long miniedgeLen) to_del_l = 1; } - ///if(v>>1 == 4581428) fprintf(stderr, "to_del_l: %d, b_f.n: %d\n", to_del_l, b_f.n); - - ///if(to_del_l == 0) continue; - - - - - - - ////backward bubble to_del_r = 0; for (i = 0; i < b_r.n; i++) @@ -7274,16 +5221,7 @@ ma_hit_t_alloc* reverse_sources, long long miniedgeLen) } - // if(v>>1 == 4581428) - // { - // fprintf(stderr, "to_del_l: %d, b_f.n: %d\n", to_del_l, b_f.n); - // asg_bub_end_finder_with_del_advance_debug(g, b_r.a, b_r.n, max_dist, &bub, 1, v^1); - // } - - // if(v>>1 == 7318036) - // { - // fprintf(stderr, "to_del_l: %d, to_del_r: %d, b_f.n: %d\n", to_del_l, to_del_r, b_f.n); - // } + @@ -7292,7 +5230,6 @@ ma_hit_t_alloc* reverse_sources, long long miniedgeLen) for (i = 0; i < nv; ++i) { if (av[i].del) continue; - ///fprintf(stderr, "%.*s\n", Get_NAME_LENGTH((R_INF), v>>1), Get_NAME((R_INF), v>>1)); ++n_cut; av[i].del = 1; asg_arc_del(g, av[i].v^1, av[i].ul>>32^1, 1); @@ -7308,8 +5245,12 @@ ma_hit_t_alloc* reverse_sources, long long miniedgeLen) asg_cleanup(g); asg_symm(g); } - fprintf(stderr, "[M::%s] removed %d false overlaps\n", __func__, n_cut); - fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + + if(VERBOSE >= 1) + { + fprintf(stderr, "[M::%s] removed %u false overlaps\n", __func__, n_cut); + fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + } return n_cut; } @@ -7356,18 +5297,11 @@ ma_hit_t_alloc* reverse_sources, long long miniedgeLen) } } - fprintf(stderr, "[M::%s] %lld unsorted pending overlaps\n", __func__, b.n); - radix_sort_arch64(b.a, b.a + b.n); - fprintf(stderr, "[M::%s] %lld sorted pending overlaps\n", __func__, b.n); - - - uint32_t min_edge; - - long long k, t; + uint64_t k, t; for (k = 0; k < b.n; k++) { ///v is the node @@ -7468,7 +5402,7 @@ ma_hit_t_alloc* reverse_sources, long long miniedgeLen) if(to_del_l != kv && b_r.n >= 2) { to_del_l = 0; - uint32_t w0, w1; + uint32_t w0 = 0, w1 = 0; w = b_r.a[0]; @@ -7550,16 +5484,6 @@ ma_hit_t_alloc* reverse_sources, long long miniedgeLen) to_del_l = 1; } - ///if(v>>1 == 4581428) fprintf(stderr, "to_del_l: %d, b_f.n: %d\n", to_del_l, b_f.n); - - ///if(to_del_l == 0) continue; - - - - - - - ////backward bubble to_del_r = 0; for (i = 0; i < b_r.n; i++) @@ -7584,20 +5508,12 @@ ma_hit_t_alloc* reverse_sources, long long miniedgeLen) to_del_r = 1; } - - // if(v>>1 == 4581428) - // { - // fprintf(stderr, "to_del_l: %d, b_f.n: %d\n", to_del_l, b_f.n); - // asg_bub_end_finder_with_del_advance_debug(g, b_r.a, b_r.n, max_dist, &bub, 1, v^1); - // } - - - + if (to_del_l && to_del_r) { - fprintf(stderr, "%.*s\n", Get_NAME_LENGTH((R_INF), v>>1), Get_NAME((R_INF), v>>1)); + ///fprintf(stderr, "%.*s\n", Get_NAME_LENGTH((R_INF), v>>1), Get_NAME((R_INF), v>>1)); for (i = 0; i < nv; ++i) { if (av[i].del) continue; @@ -7640,7 +5556,6 @@ int asg_arc_del_tri_link(asg_t *g, int max_dist) uint32_t v, w, n_vtx = g->n_seq * 2, n_cut = 0; - uint32_t sink; buf_t bub; if (!g->is_symm) asg_symm(g); @@ -7703,18 +5618,11 @@ int asg_arc_del_tri_link(asg_t *g, int max_dist) } } - fprintf(stderr, "[M::%s] %lld unsorted pending overlaps\n", __func__, b.n); radix_sort_arch64(b.a, b.a + b.n); - - fprintf(stderr, "[M::%s] %lld sorted pending overlaps\n", __func__, b.n); - - - - uint32_t min_edge; - long long k, t; + uint64_t k; for (k = 0; k < b.n; k++) { ///v is the node @@ -7730,9 +5638,6 @@ int asg_arc_del_tri_link(asg_t *g, int max_dist) if (nv != 2) continue; if (kv != 2) continue; - - - uint32_t i; asg_arc_t *av = asg_arc_a(g, v), *aw; if(av[0].v == av[1].v) { @@ -7792,7 +5697,7 @@ int asg_arc_del_tri_link(asg_t *g, int max_dist) f2 = detect_bubble_end_with_bubbles(g, aw[0].v, aw[1].v, &convex2, &l2, NULL); if(f1 && f2) { - if(l1 <= min_thres || l2 <= min_thres) + if((l1 <= min_thres) || (l2 <= min_thres)) { continue; } @@ -7833,14 +5738,10 @@ int asg_arc_del_tri_link(asg_t *g, int max_dist) &bub, 0, (u_int32_t)-1, &convex2); l2 = min_thres + 10; } - - - - if(f1 && f2) { - if(l1 <= min_thres || l2 <= min_thres) + if((l1 <= min_thres) || (l2 <= min_thres)) { continue; } @@ -7932,18 +5833,13 @@ ma_hit_t_alloc* reverse_sources, long long miniedgeLen) } } - fprintf(stderr, "[M::%s] %lld unsorted pending overlaps\n", __func__, b.n); radix_sort_arch64(b.a, b.a + b.n); - fprintf(stderr, "[M::%s] %lld sorted pending overlaps\n", __func__, b.n); - - - uint32_t min_edge; - long long k, t; + uint64_t k, t; for (k = 0; k < b.n; k++) { ///v is the node @@ -8001,7 +5897,6 @@ ma_hit_t_alloc* reverse_sources, long long miniedgeLen) for (v = 0; v < n_vtx; ++v) { uint32_t nv = asg_arc_n(g, v); - asg_arc_t *av = asg_arc_a(g, v); if (g->seq[v>>1].del) { continue; @@ -8055,8 +5950,12 @@ ma_hit_t_alloc* reverse_sources, long long miniedgeLen) asg_cleanup(g); asg_symm(g); } - fprintf(stderr, "[M::%s] removed %d false overlaps\n", __func__, n_cut); - fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + + if(VERBOSE >= 1) + { + fprintf(stderr, "[M::%s] removed %d false overlaps\n", __func__, n_cut); + fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + } return n_cut; } @@ -8081,18 +5980,14 @@ int asg_arc_del_short_diploid_by_exact(asg_t *g, int max_ext, ma_hit_t_alloc* so long long i; for (i = 0; i < nv; ++i) { - kv_push(uint64_t, b, (uint64_t)(av[i].ol << 32 | (av - g->arc + i))); + kv_push(uint64_t, b, (uint64_t)((uint64_t)av[i].ol << 32 | (av - g->arc + i))); } } } - fprintf(stderr, "[M::%s] %lld unsorted pending overlaps\n", __func__, b.n); - radix_sort_arch64(b.a, b.a + b.n); - fprintf(stderr, "[M::%s] %lld sorted pending overlaps\n", __func__, b.n); - - long long k; + uint64_t k; for (k = 0; k < b.n; k++) { @@ -8100,7 +5995,7 @@ int asg_arc_del_short_diploid_by_exact(asg_t *g, int max_ext, ma_hit_t_alloc* so ///v is self id, w is the id of another end uint32_t i, iv, iw, v = (a->ul)>>32, w = a->v^1, to_del = 0; uint32_t nv = asg_arc_n(g, v), nw = asg_arc_n(g, w), kv, kw; - uint32_t ov_max = 0, ow_max = 0, ov_max_i, ow_max_i; + uint32_t ov_max = 0, ow_max = 0, ov_max_i = 0; asg_arc_t *av, *aw; ///nv must be >= 2 if (nv == 1 && nw == 1) continue; @@ -8126,7 +6021,6 @@ int asg_arc_del_short_diploid_by_exact(asg_t *g, int max_ext, ma_hit_t_alloc* so if (ow_max < aw[i].ol) { ow_max = aw[i].ol; - ow_max_i = i; } ++kw; } @@ -8191,8 +6085,12 @@ int asg_arc_del_short_diploid_by_exact(asg_t *g, int max_ext, ma_hit_t_alloc* so asg_cleanup(g); asg_symm(g); } - fprintf(stderr, "[M::%s] removed %d inexact overlaps\n", __func__, n_cut); - fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + + if(VERBOSE >= 1) + { + fprintf(stderr, "[M::%s] removed %lld inexact overlaps\n", __func__, n_cut); + fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + } return n_cut; } @@ -8221,19 +6119,17 @@ int asg_arc_del_short_diploi_by_suspect_edge(asg_t *g, int max_ext, ma_hit_t_all ///means there is a large indel at this edge if(av[i].no_l_indel == 0) { - kv_push(uint64_t, b, (uint64_t)(av[i].ol << 32 | (av - g->arc + i))); + kv_push(uint64_t, b, (uint64_t)((uint64_t)av[i].ol << 32 | (av - g->arc + i))); } } } } - fprintf(stderr, "[M::%s] %lld unsorted pending overlaps\n", __func__, b.n); radix_sort_arch64(b.a, b.a + b.n); - fprintf(stderr, "[M::%s] %lld sorted pending overlaps\n", __func__, b.n); - long long k; + uint64_t k; for (k = 0; k < b.n; k++) { @@ -8290,8 +6186,11 @@ int asg_arc_del_short_diploi_by_suspect_edge(asg_t *g, int max_ext, ma_hit_t_all asg_cleanup(g); asg_symm(g); } - fprintf(stderr, "[M::%s] removed %d suspect overlaps\n", __func__, n_cut); - fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + if(VERBOSE >= 1) + { + fprintf(stderr, "[M::%s] removed %lld suspect overlaps\n", __func__, n_cut); + fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + } return n_cut; } @@ -8330,39 +6229,26 @@ int asg_arc_del_false_node(asg_t *g, int max_ext) } asg_arc_t *av = asg_arc_a(g, v); - kv_push(uint64_t, b, (uint64_t)(av[0].ol << 32 | (av - g->arc))); + kv_push(uint64_t, b, (uint64_t)((uint64_t)av[0].ol << 32 | (av - g->arc))); } } radix_sort_arch64(b.a, b.a + b.n); - long long k; + uint64_t k; ///here all edges are inexact matches for (k = 0; k < b.n; k++) { asg_arc_t *a = &g->arc[(uint32_t)b.a[k]]; ///v is self id, w is the id of another end - uint32_t i, iv, iw, v = (a->ul)>>32, w = a->v^1, to_del = 0; + uint32_t i, iv, iw, v = (a->ul)>>32, w = a->v^1; uint32_t nv = asg_arc_n(g, v), nw = asg_arc_n(g, w), kv, kw; asg_arc_t *av, *aw; av = asg_arc_a(g, v); aw = asg_arc_a(g, w); - /** - uint32_t en; - long long pathLen; - detect_single_path(g, w^1, &en, &pathLen, NULL); - ///<=2 means there is just one single read from w - if(pathLen <= 2) - { - continue; - } - **/ - - - ///calculate the longest edge for v and w for (i = 0, kv = 0; i < nv; ++i) { if (av[i].del) continue; @@ -8411,8 +6297,12 @@ int asg_arc_del_false_node(asg_t *g, int max_ext) asg_cleanup(g); asg_symm(g); } - fprintf(stderr, "[M::%s] removed %d single nodes\n", __func__, n_cut); - fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + + if(VERBOSE >= 1) + { + fprintf(stderr, "[M::%s] removed %lld single nodes\n", __func__, n_cut); + fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + } return n_cut; } @@ -8627,62 +6517,18 @@ void ma_ug_print(const ma_ug_t *ug, All_reads *RNF, const ma_sub_t *coverage_cut sprintf(name, "utg%.6d%c", i + 1, "lc"[p->circ]); fprintf(fp, "S\t%s\t%s\tLN:i:%d\n", name, p->s? p->s : "*", p->len); - - for (j = l = 0; j < p->n; l += (uint32_t)p->a[j++]) { uint32_t x = p->a[j]>>33; fprintf(fp, "a\t%s\t%d\t%.*s(%u):%d-%d\t%c\t%d\n", name, l, - Get_NAME_LENGTH((*RNF), x), Get_NAME((*RNF), x), x, + (int)Get_NAME_LENGTH((*RNF), x), Get_NAME((*RNF), x), x, coverage_cut[x].s + 1, coverage_cut[x].e, "+-"[p->a[j]>>32&1], (uint32_t)p->a[j]); - - // if (sub) fprintf(fp, "a\t%s\t%d\t%s:%d-%d\t%c\t%d\n", name, l, d->seq[x].name, sub[x].s + 1, sub[x].e, "+-"[p->a[j]>>32&1], (uint32_t)p->a[j]); - // else fprintf(fp, "a\t%s\t%d\t%s\t%c\t%d\n", name, l, d->seq[x].name, "+-"[p->a[j]>>32&1], (uint32_t)p->a[j]); - } - - } for (i = 0; i < ug->g->n_arc; ++i) { // the Link lines in GFA uint32_t u = ug->g->arc[i].ul>>32, v = ug->g->arc[i].v; fprintf(fp, "L\tutg%.6d%c\t%c\tutg%.6d%c\t%c\t%dM\tSD:i:%d\n", (u>>1)+1, "lc"[ug->u.a[u>>1].circ], "+-"[u&1], (v>>1)+1, "lc"[ug->u.a[v>>1].circ], "+-"[v&1], ug->g->arc[i].ol, asg_arc_len(ug->g->arc[i])); - } - - - /** - for (i = 0; i < ug->u.n; ++i) { // summary of unitigs - uint32_t cnt[2]; - ma_utg_t *u = &ug->u.a[i]; - if (u->start == UINT32_MAX) { - fprintf(fp, "x\tutg%.6dc\t%d\t%d\n", i + 1, u->len, u->n); - } else - { - for (j = 0; j < 2; ++j) cnt[j] = asg_arc_n(ug->g, i<<1|j); - - fprintf(fp, "x\tutg%.6dl\t%d\t%d\t%d\t%d\t%.*s:%d-%d\t%c\t%.*s:%d-%d\t%c\n", - i + 1, u->len, u->n, cnt[1], cnt[0], - ///d->seq[u->start>>1].name, - Get_NAME_LENGTH((*RNF), u->start>>1), Get_NAME((*RNF), u->start>>1), - coverage_cut[u->start>>1].s + 1, coverage_cut[u->start>>1].e, - "+-"[u->start&1], - ///d->seq[u->end>>1].name, - Get_NAME_LENGTH((*RNF), u->end>>1), Get_NAME((*RNF), u->end>>1), - coverage_cut[u->end>>1].s + 1, coverage_cut[u->end>>1].e, - "+-"[u->end&1]); - - // if (sub) - // fprintf(fp, "x\tutg%.6dl\t%d\t%d\t%d\t%d\t%s:%d-%d\t%c\t%s:%d-%d\t%c\n", i + 1, u->len, u->n, cnt[1], cnt[0], - // d->seq[u->start>>1].name, sub[u->start>>1].s + 1, sub[u->start>>1].e, "+-"[u->start&1], - // d->seq[u->end>>1].name, sub[u->end>>1].s + 1, sub[u->end>>1].e, "+-"[u->end&1]); - // else - // fprintf(fp, "x\tutg%.6dl\t%d\t%d\t%d\t%d\t%s\t%c\t%s\t%c\n", i + 1, u->len, u->n, cnt[1], cnt[0], - // d->seq[u->start>>1].name, "+-"[u->start&1], d->seq[u->end>>1].name, "+-"[u->end&1]); - - - } - } - **/ - + } } int asg_cut_internal(asg_t *g, int max_ext) @@ -8719,22 +6565,8 @@ int asg_cut_internal(asg_t *g, int max_ext) void clean_weak_ma_hit_t(ma_hit_t_alloc* sources, ma_hit_t_alloc* reverse_sources, long long num_sources) { double startTime = Get_T(); - long long bi_overlaps = 0; - long long si_overlaps = 0; long long i, j, index; uint32_t qn, tn; - ma_hit_t new_element; - long long qLen_0, qLen_1; - - // if(memcmp("m64016_190918_162737/92668450/ccs", Get_NAME(R_INF, i), - // Get_NAME_LENGTH(R_INF, i)) == 0) - // { - // debug_info_of_specfic_read("m64016_190918_162737/92668450/ccs", - // sources, reverse_sources, -1, "clean_weak_ma_hit_t"); - - // debug_info_of_specfic_read("m64016_190918_162737/53545052/ccs", - // sources, reverse_sources, -1, "clean_weak_ma_hit_t"); - // } for (i = 0; i < num_sources; i++) { @@ -8788,86 +6620,15 @@ void clean_weak_ma_hit_t(ma_hit_t_alloc* sources, ma_hit_t_alloc* reverse_source current_overlaps += sources[i].length; } - - // if(memcmp("m64016_190918_162737/92668450/ccs", Get_NAME(R_INF, i), - // Get_NAME_LENGTH(R_INF, i)) == 0) - // { - // debug_info_of_specfic_read("m64016_190918_162737/92668450/ccs", - // sources, reverse_sources, -1, "clean_weak_ma_hit_t"); - - // debug_info_of_specfic_read("m64016_190918_162737/53545052/ccs", - // sources, reverse_sources, -1, "clean_weak_ma_hit_t"); - // } - - - fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); -} - - -void build_string_graph(int min_dp, ma_hit_t_alloc* sources, ma_hit_t_alloc* reverse_sources, long long n_read, uint64_t* readLen, -long long mini_overlap_length, long long max_hang_length, -long long clean_round, float min_ovlp_drop_ratio, float max_ovlp_drop_ratio, -float final_ovlp_drop_ratio, char* output_file_name, long long bubble_dist) -{ - ma_sub_t* coverage_cut; - normalize_ma_hit_t(sources, n_read); - ///normalize_ma_hit_t_single_side(sources, n_read); - ///debug_normalize_ma_hit_t(sources, n_read); - clean_weak_ma_hit_t(sources, reverse_sources, n_read); - ///debug_normalize_ma_hit_t(sources, n_read); - - ma_hit_sub(min_dp, sources, n_read, readLen, mini_overlap_length, &coverage_cut); - ma_hit_cut(min_dp, sources, n_read, readLen, mini_overlap_length, &coverage_cut); - ///it seems we do not need ma_hit_flt - ma_hit_flt(sources, n_read, coverage_cut, max_hang_length, mini_overlap_length); - ///debug_cut_ma_hit_t(sources, n_read, coverage_cut); - ma_hit_contained(sources, n_read, coverage_cut, max_hang_length, mini_overlap_length); - ///debug_cut_ma_hit_t(sources, n_read, coverage_cut); - asg_t *sg = NULL; - sg = ma_sg_gen(sources, n_read, coverage_cut, max_hang_length, mini_overlap_length); - asg_arc_del_trans(sg, GAP_FUZZ); - asg_cut_tip(sg, MAX_SHORT_TIPS); - - asg_arc_del_triangular_advance(sg, bubble_dist); - - - if (asg_arc_del_short_diploid(sg, final_ovlp_drop_ratio, sources, reverse_sources) != 0) + if(VERBOSE >= 1) { - ///asg_cut_tip(sg, MAX_SHORT_TIPS); - - } - - asg_arc_del_triangular_advance(sg, bubble_dist); - - while(asg_cut_tip(sg, MAX_SHORT_TIPS)!=0 && asg_arc_del_triangular_advance(sg, bubble_dist) != 0) - { - ; + fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); } - +} - ma_ug_t *ug = NULL; - ug = ma_ug_gen(sg); - ma_ug_seq(ug, &R_INF, coverage_cut, n_read); - - - fprintf(stdout, "Writing GFA to disk ...... \n"); - char* gfa_name = (char*)malloc(strlen(output_file_name)+5); - sprintf(gfa_name, "%s.gfa", output_file_name); - FILE* output_file = fopen(gfa_name, "w"); - ma_ug_print(ug, &R_INF, coverage_cut, output_file); - - - asg_destroy(sg); - ma_ug_destroy(ug); - free(coverage_cut); - - free(gfa_name); - fclose(output_file); -} - void debug_info_of_specfic_node(char* name, asg_t *g, char* command) @@ -8879,7 +6640,7 @@ void debug_info_of_specfic_node(char* name, asg_t *g, char* command) if(memcmp(name, Get_NAME(R_INF, (v>>1)), Get_NAME_LENGTH(R_INF, (v>>1))) == 0) { fprintf(stderr, "\nafter %s\n****************graph ref_read: %.*s, dir: %u****************\n", - command, Get_NAME_LENGTH(R_INF, (v>>1)), Get_NAME(R_INF, (v>>1)), v&1); + command, (int)Get_NAME_LENGTH(R_INF, (v>>1)), Get_NAME(R_INF, (v>>1)), v&1); if(g->seq[v>>1].del) { fprintf(stderr, "read has already been deleted.\n"); @@ -8891,7 +6652,7 @@ void debug_info_of_specfic_node(char* name, asg_t *g, char* command) for (i = 0; i < nv; ++i) { fprintf(stderr, "target: %.*s, el: %u, strong: %u, ol: %u, del: %u\n", - Get_NAME_LENGTH(R_INF, (av[i].v>>1)), + (int)Get_NAME_LENGTH(R_INF, (av[i].v>>1)), Get_NAME(R_INF, (av[i].v>>1)), av[i].el, av[i].strong, av[i].ol, av[i].del); } @@ -8903,7 +6664,7 @@ void debug_info_of_specfic_read(char* name, ma_hit_t_alloc* sources, ma_hit_t_alloc* reverse_sources, int id, char* command) { long long i, j, Len; - uint32_t qn, tn; + uint32_t tn; if(id == -1) { @@ -8923,8 +6684,8 @@ ma_hit_t_alloc* reverse_sources, int id, char* command) { fprintf(stderr, "\n\n\nafter %s\n", command); - fprintf(stderr, "****************ma_hit_t (%d)ref_read: %.*s****************\n", - i, Get_NAME_LENGTH(R_INF, i), Get_NAME(R_INF, i)); + fprintf(stderr, "****************ma_hit_t (%lld)ref_read: %.*s****************\n", + i, (int)Get_NAME_LENGTH(R_INF, i), Get_NAME(R_INF, i)); fprintf(stderr, "sources Len: %d, is_fully_corrected: %d\n", @@ -8932,10 +6693,9 @@ ma_hit_t_alloc* reverse_sources, int id, char* command) for (j = 0; j < sources[i].length; j++) { - qn = Get_qn(sources[i].buffer[j]); tn = Get_tn(sources[i].buffer[j]); - fprintf(stderr, "target: %.*s, qs: %d, qe: %d, ts: %d, te: %d, ml: %d, rev: %d, el: %d\n", - Get_NAME_LENGTH(R_INF, tn), Get_NAME(R_INF, tn), + fprintf(stderr, "target: %.*s, qs: %u, qe: %u, ts: %u, te: %u, ml: %u, rev: %u, el: %u\n", + (int)Get_NAME_LENGTH(R_INF, tn), Get_NAME(R_INF, tn), Get_qs(sources[i].buffer[j]), Get_qe(sources[i].buffer[j]), Get_ts(sources[i].buffer[j]), @@ -8943,22 +6703,6 @@ ma_hit_t_alloc* reverse_sources, int id, char* command) sources[i].buffer[j].ml, sources[i].buffer[j].rev, sources[i].buffer[j].el); - - - - /** - //if this is a weak overlap - if(sources[i].buffer[j].ml == 0) - { - if(!check_weak_ma_hit(&(sources[qn]), reverse_sources, tn, - Get_qs(sources[i].buffer[j]), Get_qe(sources[i].buffer[j]))) - { - int c_id = check_weak_ma_hit_debug(&(sources[qn]), reverse_sources, tn); - fprintf(stderr, "*************************conflict with %.*s\n", - Get_NAME_LENGTH(R_INF, c_id), Get_NAME(R_INF, c_id)); - } - } - **/ } @@ -8969,10 +6713,9 @@ ma_hit_t_alloc* reverse_sources, int id, char* command) for (j = 0; j < reverse_sources[i].length; j++) { - qn = Get_qn(reverse_sources[i].buffer[j]); tn = Get_tn(reverse_sources[i].buffer[j]); fprintf(stderr, "target: %.*s, qs: %u, qe: %u, ts: %u, te: %u\n", - Get_NAME_LENGTH(R_INF, tn), Get_NAME(R_INF, tn), + (int)Get_NAME_LENGTH(R_INF, tn), Get_NAME(R_INF, tn), Get_qs(reverse_sources[i].buffer[j]), Get_qe(reverse_sources[i].buffer[j]), Get_ts(reverse_sources[i].buffer[j]), @@ -8995,8 +6738,8 @@ void ma_sg_print(const asg_t *g, const All_reads *RNF, const ma_sub_t *sub, FILE if(!g->seq[i].del) { fprintf(fp, - "S\t%.*s\t*\tLN:i:%d\n", - Get_NAME_LENGTH((*RNF), i), + "S\t%.*s\t*\tLN:i:%u\n", + (int)Get_NAME_LENGTH((*RNF), i), Get_NAME((*RNF), i), g->seq[i].len); } @@ -9006,27 +6749,13 @@ void ma_sg_print(const asg_t *g, const All_reads *RNF, const ma_sub_t *sub, FILE const asg_arc_t *p = &g->arc[i]; if (sub) { const ma_sub_t *sq = &sub[p->ul>>33], *st = &sub[p->v>>1]; - /** - fprintf(fp, "L\t%s:%d-%d\t%c\t%s:%d-%d\t%c\t%d:\tL1:i:%d\n", - d->seq[p->ul>>33].name, sq->s + 1, sq->e, "+-"[p->ul>>32&1], - d->seq[p->v>>1].name, st->s + 1, st->e, "+-"[p->v&1], p->ol, (uint32_t)p->ul); - **/ - /** - fprintf(stderr, "Get_NAME_LENGTH((*RNF), p->ul>>33): %u, p->ul>>33: %u\n", - Get_NAME_LENGTH((*RNF), (p->ul>>33)), - (p->ul>>33)); - - fprintf(stderr, "Get_NAME_LENGTH((*RNF), p->v>>1): %u, p->v>>1: %u\n", - Get_NAME_LENGTH((*RNF), (p->v>>1)), - (p->v>>1)); - **/ fprintf(fp, - "L\t%.*s:%d-%d\t%c\t%.*s:%d-%d\t%c\t%d:\tL1:i:%d\n", - Get_NAME_LENGTH((*RNF), p->ul>>33), + "L\t%.*s:%d-%d\t%c\t%.*s:%d-%d\t%c\t%d:\tL1:i:%u\n", + (int)Get_NAME_LENGTH((*RNF), p->ul>>33), Get_NAME((*RNF), p->ul>>33), sq->s + 1, sq->e, "+-"[p->ul>>32&1], - Get_NAME_LENGTH((*RNF), p->v>>1), + (int)Get_NAME_LENGTH((*RNF), p->v>>1), Get_NAME((*RNF), p->v>>1), st->s + 1, st->e, "+-"[p->v&1], p->ol, (uint32_t)p->ul); @@ -9034,16 +6763,11 @@ void ma_sg_print(const asg_t *g, const All_reads *RNF, const ma_sub_t *sub, FILE } else { - /** - fprintf(fp, "L\t%s\t%c\t%s\t%c\t%d:\tL1:i:%d\n", - d->seq[p->ul>>33].name, "+-"[p->ul>>32&1], - d->seq[p->v>>1].name, "+-"[p->v&1], p->ol, (uint32_t)p->ul); - **/ - fprintf(fp, "L\t%.*s\t%c\t%.*s\t%c\t%d:\tL1:i:%d\n", - Get_NAME_LENGTH((*RNF), p->ul>>33), + fprintf(fp, "L\t%.*s\t%c\t%.*s\t%c\t%d:\tL1:i:%u\n", + (int)Get_NAME_LENGTH((*RNF), p->ul>>33), Get_NAME((*RNF), p->ul>>33), "+-"[p->ul>>32&1], - Get_NAME_LENGTH((*RNF), p->v>>1), + (int)Get_NAME_LENGTH((*RNF), p->v>>1), Get_NAME((*RNF), p->v>>1), "+-"[p->v&1], p->ol, (uint32_t)p->ul); } @@ -9053,7 +6777,7 @@ void ma_sg_print(const asg_t *g, const All_reads *RNF, const ma_sub_t *sub, FILE void ma_ug_print_simple(const ma_ug_t *ug, All_reads *RNF, const ma_sub_t *coverage_cut, FILE *fp) { - uint32_t i, j, l; + uint32_t i; char name[32]; for (i = 0; i < ug->u.n; ++i) { // the Segment lines in GFA ma_utg_t *p = &ug->u.a[i]; @@ -9072,7 +6796,7 @@ int asg_arc_cut_long_tip(asg_t *g, float drop_ratio) { double startTime = Get_T(); ///the reason is that each read has two direction (query->target, target->query) - uint32_t v, v_max, w, n_vtx = g->n_seq * 2, n_reduced = 0, convex, flag; + uint32_t v, n_vtx = g->n_seq * 2, n_reduced = 0, convex; long long ll, v_maxLen; buf_t b; @@ -9093,7 +6817,7 @@ int asg_arc_cut_long_tip(asg_t *g, float drop_ratio) { if (!av[i].del) { - flag = detect_single_path_with_dels(g, av[i].v, &convex, &ll, NULL); + detect_single_path_with_dels(g, av[i].v, &convex, &ll, NULL); if(v_maxLen < ll) { v_maxLen = ll; @@ -9111,7 +6835,7 @@ int asg_arc_cut_long_tip(asg_t *g, float drop_ratio) if(v_maxLen*drop_ratio > ll) { n_reduced++; - long long k; + uint64_t k; for (k = 0; k < b.b.n; k++) { asg_seq_del(g, b.b.a[k]); @@ -9126,10 +6850,12 @@ int asg_arc_cut_long_tip(asg_t *g, float drop_ratio) asg_cleanup(g); asg_symm(g); - - fprintf(stderr, "[M::%s] removed %d long tips\n", - __func__, n_reduced); - fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + if(VERBOSE >= 1) + { + fprintf(stderr, "[M::%s] removed %d long tips\n", + __func__, n_reduced); + fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + } return n_reduced; } @@ -9137,7 +6863,7 @@ int asg_arc_cut_long_tip(asg_t *g, float drop_ratio) uint32_t detect_single_path_with_dels_contigLen(asg_t *g, uint32_t begNode, uint32_t* endNode, long long* baseLen, buf_t* b) { - uint32_t v = begNode, w; + uint32_t v = begNode, w = 0; uint32_t kv, kw, k; (*baseLen) = 0; @@ -9211,93 +6937,12 @@ uint32_t detect_single_path_with_dels_contigLen(asg_t *g, uint32_t begNode, uint } -int asg_arc_cut_long_equal_tips_only_tips(asg_t *g, ma_hit_t_alloc* reverse_sources, long long miniedgeLen) -{ - double startTime = Get_T(); - ///the reason is that each read has two direction (query->target, target->query) - uint32_t v, v_max, w, n_vtx = g->n_seq * 2, n_reduced = 0, convex, flag; - long long ll, base_maxLen, base_maxLen_i; - - buf_t b; - memset(&b, 0, sizeof(buf_t)); - - for (v = 0; v < n_vtx; ++v) - { - uint32_t i, n_arc = 0, nv = asg_arc_n(g, v); - asg_arc_t *av = asg_arc_a(g, v); - ///some node could be deleted - if (nv < 2 || g->seq[v>>1].del) continue; - n_arc = get_real_length(g, v, NULL); - if (n_arc < 2) continue; - - base_maxLen = -1; - base_maxLen_i = -1; - - for (i = 0; i < nv; i++) - { - if (!av[i].del) - { - flag = detect_single_path_with_dels_contigLen(g, av[i].v, &convex, &ll, NULL); - if(flag != END_TIPS) - { - base_maxLen = -1; - base_maxLen_i = -1; - break; - } - if(base_maxLen < ll) - { - base_maxLen = ll; - base_maxLen_i = i; - } - } - } - - ///all outedges are tips - if(base_maxLen != -1) - { - for (i = 0; i < nv; i++) - { - if(i == base_maxLen_i) continue; - if (!av[i].del) - { - ///check_if_diploid_aggressive - if(check_if_diploid(av[base_maxLen_i].v, av[i].v, g, - reverse_sources, miniedgeLen)==1) - { - b.b.n = 0; - if(detect_single_path_with_dels_contigLen(g, av[i].v, &convex, &ll, &b) - == END_TIPS) - { - n_reduced++; - long long k; - for (k = 0; k < b.b.n; k++) - { - asg_seq_del(g, b.b.a[k]); - } - } - } - } - } - } - } - - - asg_cleanup(g); - asg_symm(g); - - - fprintf(stderr, "[M::%s] removed %d long tips\n", - __func__, n_reduced); - fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); - - return n_reduced; -} int asg_arc_cut_long_equal_tips(asg_t *g, ma_hit_t_alloc* reverse_sources, long long miniedgeLen) { double startTime = Get_T(); ///the reason is that each read has two direction (query->target, target->query) - uint32_t v, v_max, w, n_vtx = g->n_seq * 2, n_reduced = 0, convex, flag; + uint32_t v, n_vtx = g->n_seq * 2, n_reduced = 0, convex, flag; long long ll, base_maxLen, base_maxLen_i; buf_t b; @@ -9354,7 +6999,7 @@ int asg_arc_cut_long_equal_tips(asg_t *g, ma_hit_t_alloc* reverse_sources, long reverse_sources, miniedgeLen)==1) { n_reduced++; - long long k; + uint64_t k; for (k = 0; k < b.b.n; k++) { asg_seq_del(g, b.b.a[k]); @@ -9369,10 +7014,13 @@ int asg_arc_cut_long_equal_tips(asg_t *g, ma_hit_t_alloc* reverse_sources, long asg_cleanup(g); asg_symm(g); - - fprintf(stderr, "[M::%s] removed %d long tips\n", - __func__, n_reduced); - fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + if(VERBOSE >= 1) + { + fprintf(stderr, "[M::%s] removed %d long tips\n", + __func__, n_reduced); + fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + } + return n_reduced; } @@ -9383,7 +7031,7 @@ void output_unitig_graph(asg_t *sg, ma_sub_t* coverage_cut, char* output_file_na ug = ma_ug_gen(sg); ma_ug_seq(ug, &R_INF, coverage_cut, n_read); - fprintf(stdout, "Writing unitig GFA to disk ...... \n"); + fprintf(stderr, "Writing unitig GFA to disk... \n"); char* gfa_name = (char*)malloc(strlen(output_file_name)+25); sprintf(gfa_name, "%s.gfa", output_file_name); FILE* output_file = fopen(gfa_name, "w"); @@ -9406,7 +7054,7 @@ void output_unitig_graph(asg_t *sg, ma_sub_t* coverage_cut, char* output_file_na void output_read_graph(asg_t *sg, ma_sub_t* coverage_cut, char* output_file_name, long long n_read) { - fprintf(stdout, "Writing read GFA to disk ...... \n"); + fprintf(stderr, "Writing read GFA to disk... \n"); char* gfa_name = (char*)malloc(strlen(output_file_name)+25); sprintf(gfa_name, "%s.read.gfa", output_file_name); FILE* output_file = fopen(gfa_name, "w"); @@ -9418,32 +7066,32 @@ void output_read_graph(asg_t *sg, ma_sub_t* coverage_cut, char* output_file_name void read_ma(ma_hit_t* x, FILE* fp) { - fread(&(x->qns), sizeof(x->qns), 1, fp); - fread(&(x->qe), sizeof(x->qe), 1, fp); - fread(&(x->tn), sizeof(x->tn), 1, fp); - fread(&(x->ts), sizeof(x->ts), 1, fp); - fread(&(x->te), sizeof(x->te), 1, fp); - fread(&(x->el), sizeof(x->el), 1, fp); - fread(&(x->no_l_indel), sizeof(x->no_l_indel), 1, fp); + int f_flag; + f_flag = fread(&(x->qns), sizeof(x->qns), 1, fp); + f_flag += fread(&(x->qe), sizeof(x->qe), 1, fp); + f_flag += fread(&(x->tn), sizeof(x->tn), 1, fp); + f_flag += fread(&(x->ts), sizeof(x->ts), 1, fp); + f_flag += fread(&(x->te), sizeof(x->te), 1, fp); + f_flag += fread(&(x->el), sizeof(x->el), 1, fp); + f_flag += fread(&(x->no_l_indel), sizeof(x->no_l_indel), 1, fp); uint32_t t; - fread(&(t), sizeof(t), 1, fp); + f_flag += fread(&(t), sizeof(t), 1, fp); x->ml = t; - fread(&(t), sizeof(t), 1, fp); + f_flag += fread(&(t), sizeof(t), 1, fp); x->rev = t; - - fread(&(t), sizeof(t), 1, fp); + f_flag += fread(&(t), sizeof(t), 1, fp); x->bl = t; - fread(&(t), sizeof(t), 1, fp); + f_flag += fread(&(t), sizeof(t), 1, fp); x->del = t; } int load_ma_hit_ts(ma_hit_t_alloc** x, char* read_file_name) { - fprintf(stdout, "Loading ma_hit_ts to disk ...... \n"); + fprintf(stderr, "Loading ma_hit_ts from disk... \n"); char* index_name = (char*)malloc(strlen(read_file_name)+15); sprintf(index_name, "%s.bin", read_file_name); FILE* fp = fopen(index_name, "r"); @@ -9455,15 +7103,16 @@ int load_ma_hit_ts(ma_hit_t_alloc** x, char* read_file_name) long long n_read; long long i, k; - fread(&n_read, sizeof(n_read), 1, fp); + int f_flag; + f_flag += fread(&n_read, sizeof(n_read), 1, fp); (*x) = (ma_hit_t_alloc*)malloc(sizeof(ma_hit_t_alloc)*n_read); for (i = 0; i < n_read; i++) { - fread(&((*x)[i].is_fully_corrected), sizeof((*x)[i].is_fully_corrected), 1, fp); - fread(&((*x)[i].is_abnormal), sizeof((*x)[i].is_abnormal), 1, fp); - fread(&((*x)[i].length), sizeof((*x)[i].length), 1, fp); + f_flag += fread(&((*x)[i].is_fully_corrected), sizeof((*x)[i].is_fully_corrected), 1, fp); + f_flag += fread(&((*x)[i].is_abnormal), sizeof((*x)[i].is_abnormal), 1, fp); + f_flag += fread(&((*x)[i].length), sizeof((*x)[i].length), 1, fp); (*x)[i].buffer = (ma_hit_t*)malloc(sizeof(ma_hit_t)*(*x)[i].length); @@ -9475,7 +7124,9 @@ int load_ma_hit_ts(ma_hit_t_alloc** x, char* read_file_name) free(index_name); fclose(fp); - fprintf(stdout, "ma_hit_ts has been read.\n"); + fprintf(stderr, "ma_hit_ts has been read.\n"); + + return 1; } @@ -9505,7 +7156,7 @@ void write_ma(ma_hit_t* x, FILE* fp) void write_ma_hit_ts(ma_hit_t_alloc* x, long long n_read, char* read_file_name) { - fprintf(stdout, "Writing ma_hit_ts to disk ...... \n"); + fprintf(stderr, "Writing ma_hit_ts to disk... \n"); char* index_name = (char*)malloc(strlen(read_file_name)+15); sprintf(index_name, "%s.bin", read_file_name); FILE* fp = fopen(index_name, "w"); @@ -9528,7 +7179,7 @@ void write_ma_hit_ts(ma_hit_t_alloc* x, long long n_read, char* read_file_name) free(index_name); fflush(fp); fclose(fp); - fprintf(stdout, "ma_hit_ts has been written.\n"); + fprintf(stderr, "ma_hit_ts has been written.\n"); } void write_all_data_to_disk(ma_hit_t_alloc* sources, ma_hit_t_alloc* reverse_sources, @@ -9664,7 +7315,7 @@ static uint64_t asg_bub_pop1(asg_t *g, uint32_t v0, int max_dist, buf_t *b) kv_push(uint32_t, b->e, (g->idx[v]>>32) + i); ///find a too far path? directly terminate the whole bubble poping - if (d + l > max_dist) break; // too far + if (d + l > (uint32_t)max_dist) break; // too far ///if this node if (t->s == 0) { // this vertex has never been visited @@ -9743,7 +7394,10 @@ int asg_pop_bubble(asg_t *g, int max_dist) free(b.a); free(b.S.a); free(b.T.a); free(b.b.a); free(b.e.a); if (n_pop) asg_cleanup(g); ///fprintf(stderr, "[M::%s] popped %d bubbles and trimmed %d tips\n", __func__, (uint32_t)n_pop, (uint32_t)(n_pop>>32)); - fprintf(stderr, "[M::%s] popped %d bubbles\n", __func__, n_pop); + if(VERBOSE >= 1) + { + fprintf(stderr, "[M::%s] popped %lu bubbles\n", __func__, n_pop); + } return n_pop; } @@ -9764,11 +7418,7 @@ long long min_edge_length, ma_hit_t_alloc* reverse_sources) int todel; long long NodeLen_first[3]; long long NodeLen_second[3]; - - uint32_t Ns_first[3]; - uint32_t Ns_second[3]; - uint32_t nv = asg_arc_n(g, v); asg_arc_t *av = asg_arc_a(g, v); if(av[0].v == av[1].v) { @@ -9843,14 +7493,11 @@ int asg_arc_del_triangular_directly(asg_t *g, long long min_edge_length, ma_hit_ { double startTime = Get_T(); ///the reason is that each read has two direction (query->target, target->query) - uint32_t v, w, n_vtx = g->n_seq * 2, n_reduced = 0; + uint32_t v, n_vtx = g->n_seq * 2, n_reduced = 0; - - int flag0, flag1, node; for (v = 0; v < n_vtx; ++v) { uint32_t nv = asg_arc_n(g, v); - asg_arc_t *av = asg_arc_a(g, v); if (g->seq[v>>1].del) { continue; @@ -9872,10 +7519,12 @@ int asg_arc_del_triangular_directly(asg_t *g, long long min_edge_length, ma_hit_ asg_symm(g); } - fprintf(stderr, "[M::%s] removed %d triangular overlaps\n", - __func__, n_reduced); - fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); - + if(VERBOSE >= 1) + { + fprintf(stderr, "[M::%s] removed %d triangular overlaps\n", + __func__, n_reduced); + fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + } return n_reduced; } @@ -9886,7 +7535,7 @@ int asg_arc_del_orthology(asg_t *g, ma_hit_t_alloc* reverse_sources, float drop_ { double startTime = Get_T(); ///the reason is that each read has two direction (query->target, target->query) - uint32_t v, w, n_vtx = g->n_seq * 2, n_reduced = 0; + uint32_t v, n_vtx = g->n_seq * 2, n_reduced = 0; uint32_t idx[2]; for (v = 0; v < n_vtx; ++v) @@ -9946,7 +7595,7 @@ int asg_arc_del_orthology_multiple_way(asg_t *g, ma_hit_t_alloc* reverse_sources { double startTime = Get_T(); ///the reason is that each read has two direction (query->target, target->query) - uint32_t v, v_max, v_maxLen, w, n_vtx = g->n_seq * 2, n_reduced = 0; + uint32_t v, v_max, v_maxLen, n_vtx = g->n_seq * 2, n_reduced = 0; for (v = 0; v < n_vtx; ++v) { @@ -9988,9 +7637,12 @@ int asg_arc_del_orthology_multiple_way(asg_t *g, ma_hit_t_alloc* reverse_sources asg_symm(g); } - fprintf(stderr, "[M::%s] removed %d different hap overlaps\n", - __func__, n_reduced); - fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + if(VERBOSE >= 1) + { + fprintf(stderr, "[M::%s] removed %d different hap overlaps\n", + __func__, n_reduced); + fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime); + } return n_reduced; } @@ -10001,7 +7653,7 @@ int asg_arc_del_chimeric_read(asg_t *g, long long miniedgeLen) { double startTime = Get_T(); ///the reason is that each read has two direction (query->target, target->query) - uint32_t v, v_max, v_maxLen, w, n_vtx = g->n_seq * 2, n_reduced = 0, n_arc; + uint32_t v, n_vtx = g->n_seq * 2, n_reduced = 0; for (v = 0; v < n_vtx; ++v) { @@ -10116,7 +7768,7 @@ uint32_t detect_single_path_with_dels_by_length long long asg_arc_del_self_circle_untig(asg_t *g, long long circleLen) { - uint32_t v, v_max, w, n_vtx = g->n_seq * 2, n_reduced = 0, convex, flag; + uint32_t v, w, n_vtx = g->n_seq * 2, n_reduced = 0, convex, flag; long long ll; asg_arc_t *aw; uint32_t nw, k; @@ -10178,8 +7830,11 @@ long long asg_arc_del_self_circle_untig(asg_t *g, long long circleLen) asg_symm(g); } - fprintf(stderr, "[M::%s] removed %d self-circles\n", - __func__, n_reduced); + if(VERBOSE >= 1) + { + fprintf(stderr, "[M::%s] removed %d self-circles\n", + __func__, n_reduced); + } return n_reduced; } @@ -10196,7 +7851,7 @@ char* output_file_name, long long n_read, long long bubble_dist, long long tipsL ug = ma_ug_gen(sg); ma_ug_seq(ug, &R_INF, coverage_cut, n_read); - fprintf(stdout, "Writing unitig GFA to disk ...... \n"); + fprintf(stderr, "Writing unitig GFA to disk... \n"); char* gfa_name = (char*)malloc(strlen(output_file_name)+35); sprintf(gfa_name, "%s.no_s_bub.gfa", output_file_name); FILE* output_file = fopen(gfa_name, "w"); @@ -10245,7 +7900,7 @@ ma_hit_t_alloc* reverse_sources, long long miniedgeLen) ug = ma_ug_gen(sg); ma_ug_seq(ug, &R_INF, coverage_cut, n_read); - fprintf(stdout, "Writing unitig GFA to disk ...... \n"); + fprintf(stderr, "Writing unitig GFA to disk... \n"); char* gfa_name = (char*)malloc(strlen(output_file_name)+35); sprintf(gfa_name, "%s.contig.gfa", output_file_name); FILE* output_file = fopen(gfa_name, "w"); @@ -10268,7 +7923,7 @@ ma_hit_t_alloc* reverse_sources, long long miniedgeLen) int output_tips(asg_t *g, const All_reads *RNF) { - uint32_t v, w, n_vtx = g->n_seq * 2, n_reduced = 0; + uint32_t v, n_vtx = g->n_seq * 2; for (v = 0; v < n_vtx; ++v) { if (g->seq[v>>1].del) continue; @@ -10276,10 +7931,12 @@ int output_tips(asg_t *g, const All_reads *RNF) if(asg_arc_n(g, v) == 0) { fprintf(stderr, "%.*s\n", - Get_NAME_LENGTH((*RNF), v>>1), + (int)Get_NAME_LENGTH((*RNF), v>>1), Get_NAME((*RNF), v>>1)); } } + + return 1; } void collect_abnormal_edges(ma_hit_t_alloc* paf, ma_hit_t_alloc* rev_paf, long long readNum) @@ -10292,7 +7949,7 @@ void collect_abnormal_edges(ma_hit_t_alloc* paf, ma_hit_t_alloc* rev_paf, long l long long related_overlaps = 0; long long i, j; uint32_t qn, tn; - int is_equal_f, is_strong_f, is_exist_f; + int is_equal_f, is_strong_f; int is_equal_b, is_strong_b, is_exist_b; kvec_t(uint64_t) edge_vector; @@ -10300,7 +7957,7 @@ void collect_abnormal_edges(ma_hit_t_alloc* paf, ma_hit_t_alloc* rev_paf, long l for (i = 0; i < readNum; i++) { - for (j = 0; j < paf[i].length; j++) + for (j = 0; j < (long long)paf[i].length; j++) { qn = Get_qn(paf[i].buffer[j]); tn = Get_tn(paf[i].buffer[j]); @@ -10308,7 +7965,6 @@ void collect_abnormal_edges(ma_hit_t_alloc* paf, ma_hit_t_alloc* rev_paf, long l is_equal_f = paf[i].buffer[j].el; is_strong_f = paf[i].buffer[j].ml; - is_exist_f = 1; is_exist_b = get_specific_overlap(&(paf[tn]), tn, qn); if(is_exist_b == -1) @@ -10395,7 +8051,7 @@ void collect_abnormal_edges(ma_hit_t_alloc* paf, ma_hit_t_alloc* rev_paf, long l radix_sort_arch64(edge_vector.a, edge_vector.a + edge_vector.n); uint64_t pre = (uint64_t)-1; long long mn = 0; - for (i = 0; i < edge_vector.n; i++) + for (i = 0; i < (long long)edge_vector.n; i++) { if(pre != edge_vector.a[i]) { @@ -10409,17 +8065,16 @@ void collect_abnormal_edges(ma_hit_t_alloc* paf, ma_hit_t_alloc* rev_paf, long l - fprintf(stdout, "****************statistic for abnormal overlaps****************\n"); - fprintf(stdout, "overlaps #: %lld\n", T_edges); - fprintf(stdout, "one direction overlaps (different phasing)#: %lld\n", T_Single_Dir_Edges_0); - fprintf(stdout, "one direction overlaps (missing)#: %lld\n", T_Single_Dir_Edges_1); - fprintf(stdout, "one direction overlaps (missing) >= 1000#: %lld\n", T_Single_Dir_Edges_1_1000); - fprintf(stdout, "conflict strong/weak overlaps #: %lld\n", T_Conflict_Strong_Edges); - fprintf(stdout, "conflict exact/inexact overlaps #: %lld\n", T_Conflict_Equal_Edges); - fprintf(stdout, "related_reads #: %lld/%lld\n", related_reads, mn); - fprintf(stdout, "related_overlaps #: %lld\n", related_overlaps); - - fprintf(stdout, "****************statistic for abnormal overlaps****************\n"); + fprintf(stderr, "****************statistic for abnormal overlaps****************\n"); + fprintf(stderr, "overlaps #: %lld\n", T_edges); + fprintf(stderr, "one direction overlaps (different phasing)#: %lld\n", T_Single_Dir_Edges_0); + fprintf(stderr, "one direction overlaps (missing)#: %lld\n", T_Single_Dir_Edges_1); + fprintf(stderr, "one direction overlaps (missing) >= 1000#: %lld\n", T_Single_Dir_Edges_1_1000); + fprintf(stderr, "conflict strong/weak overlaps #: %lld\n", T_Conflict_Strong_Edges); + fprintf(stderr, "conflict exact/inexact overlaps #: %lld\n", T_Conflict_Equal_Edges); + fprintf(stderr, "related_reads #: %lld/%lld\n", related_reads, mn); + fprintf(stderr, "related_overlaps #: %lld\n", related_overlaps); + fprintf(stderr, "****************statistic for abnormal overlaps****************\n"); fprintf(stderr, "[M::%s] took %0.2fs\n\n", __func__, Get_T()-startTime); @@ -10448,7 +8103,7 @@ long long rescue_threshold) { edge_vector.n = 0; edge_vector_index.n = 0; - for (j = 0; j < rev_paf[i].length; j++) + for (j = 0; j < (long long)rev_paf[i].length; j++) { qn = Get_qn(rev_paf[i].buffer[j]); tn = Get_tn(rev_paf[i].buffer[j]); @@ -10466,11 +8121,11 @@ long long rescue_threshold) ///based on qn, all edges at edge_vector/edge_vector_index come from different haplotype ///but at another direction, all these edges come from the same haplotype //here we want to recover these edges - if(edge_vector_index.n >= rescue_threshold) + if((long long)edge_vector_index.n >= rescue_threshold) { kv_resize(uint32_t, b, edge_vector_index.n); b.n = 0; - for (j = 0; j < edge_vector_index.n; j++) + for (j = 0; j < (long long)edge_vector_index.n; j++) { qs = Get_qs(rev_paf[i].buffer[edge_vector_index.a[j]]); qe = Get_qe(rev_paf[i].buffer[edge_vector_index.a[j]]); @@ -10479,9 +8134,10 @@ long long rescue_threshold) } ks_introsort_uint32_t(b.n, b.a); - int dp, start, max_dp = 0; + int dp = 0, start = 0, max_dp = 0; ma_sub_t max_interval; - for (j = 0, dp = 0; j < b.n; ++j) + max_interval.s = max_interval.e = 0; + for (j = 0, dp = 0; j < (long long)b.n; ++j) { int old_dp = dp; ///if a[j] is qe @@ -10528,7 +8184,7 @@ long long rescue_threshold) if(max_dp>= rescue_threshold) { long long m = 0; - for (j = 0; j < edge_vector_index.n; j++) + for (j = 0; j < (long long)edge_vector_index.n; j++) { qs = Get_qs(rev_paf[i].buffer[edge_vector_index.a[j]]); qe = Get_qe(rev_paf[i].buffer[edge_vector_index.a[j]]); @@ -10542,11 +8198,6 @@ long long rescue_threshold) edge_vector_index.n = m; edge_vector.n = m; - // if(max_dp != edge_vector_index.n) - // { - // fprintf(stderr, "error\n"); - // } - ///the read itself do not have these overlaps, but all related reads have ///we need to remove all overlaps from rev_paf[i], and then add all overlaps to paf[i] // fprintf(stderr,"\nadd following %d edges...\n", edge_vector.n); @@ -10554,40 +8205,6 @@ long long rescue_threshold) remove_overlaps(&(rev_paf[i]), edge_vector_index.a, edge_vector_index.n); add_overlaps_from_different_sources(paf, &(paf[i]), edge_vector.a, edge_vector.n); revises = revises + edge_vector.n; - - ///for debug - /** - for (j = 0; j < edge_vector.n; j++) - { - int index_qn, index_tn; - qn = i; - tn = (uint32_t)(edge_vector.a[j] >> 32); - - index_qn = get_specific_overlap(&(paf[qn]), qn, tn); - if(index_qn == -1) fprintf(stderr, "error1\n"); - - index_tn = get_specific_overlap(&(paf[tn]), tn, qn); - if(index_tn == -1) fprintf(stderr, "error2\n"); - - if(paf[qn].buffer[index_qn].el != paf[tn].buffer[index_tn].el || - paf[qn].buffer[index_qn].ml != paf[tn].buffer[index_tn].ml || - paf[qn].buffer[index_qn].no_l_indel != paf[tn].buffer[index_tn].no_l_indel || - paf[qn].buffer[index_qn].rev != paf[tn].buffer[index_tn].rev || - Get_qs(paf[qn].buffer[index_qn]) != Get_ts(paf[tn].buffer[index_tn]) || - Get_qe(paf[qn].buffer[index_qn]) != Get_te(paf[tn].buffer[index_tn]) || - Get_qn(paf[qn].buffer[index_qn]) != Get_tn(paf[tn].buffer[index_tn]) || - Get_tn(paf[qn].buffer[index_qn]) != Get_qn(paf[tn].buffer[index_tn]) - ) - { - fprintf(stderr, "error full\n"); - } - - index_qn = get_specific_overlap(&(rev_paf[qn]), qn, tn); - if(index_qn != -1) fprintf(stderr, "error3\n"); - - - } - **/ } } } @@ -10596,153 +8213,59 @@ long long rescue_threshold) kv_destroy(edge_vector); kv_destroy(edge_vector_index); kv_destroy(b); - - fprintf(stderr, "[M::%s] took %0.2fs, rescue edges #: %lld\n\n", __func__, Get_T()-startTime, revises); + if(VERBOSE >= 1) + { + fprintf(stderr, "[M::%s] took %0.2fs, rescue edges #: %lld\n\n", + __func__, Get_T()-startTime, revises); + } + } void build_string_graph_without_clean( int min_dp, ma_hit_t_alloc* sources, ma_hit_t_alloc* reverse_sources, -long long n_read, uint64_t* readLen, -long long mini_overlap_length, long long max_hang_length, long long clean_round, -float min_ovlp_drop_ratio, float max_ovlp_drop_ratio, float corase_ovlp_drop_ratio, -char* output_file_name, long long bubble_dist, int read_graph, int write) +long long n_read, uint64_t* readLen, long long mini_overlap_length, +long long max_hang_length, long long clean_round, long long pop_bubble_size, +float min_ovlp_drop_ratio, float max_ovlp_drop_ratio, char* output_file_name, +long long bubble_dist, int read_graph, int write) { - - if (write_index_to_disk && write) + if (asm_opt.write_index_to_disk && write) { write_all_data_to_disk(sources, reverse_sources, &R_INF, output_file_name); } - - ///collect_abnormal_edges(sources, reverse_sources, n_read); - - try_rescue_overlaps(sources, reverse_sources, n_read, 4); - - ///collect_abnormal_edges(sources, reverse_sources, n_read); - - - - // debug_info_of_specfic_read("m64016_190918_162737/76482648/ccs", - // sources, reverse_sources, -1, "init"); - - // debug_info_of_specfic_read("m64016_190918_162737/133368200/ccs", - // sources, reverse_sources, -1, "init"); - - - - + try_rescue_overlaps(sources, reverse_sources, n_read, 4); ma_sub_t* coverage_cut; - ///normalize_ma_hit_t(sources, n_read); normalize_ma_hit_t_single_side(sources, n_read); - - - - - - - - - - - - ///debug_normalize_ma_hit_t(sources, n_read); clean_weak_ma_hit_t(sources, reverse_sources, n_read); - ///debug_normalize_ma_hit_t(sources, n_read); - - - - - // debug_info_of_specfic_read("m64013_190322_203854/82051959/ccs", - // sources, reverse_sources, -1, "clean"); - - // debug_info_of_specfic_read("m64013_190322_203854/74385680/ccs", - // sources, reverse_sources, -1, "clean"); // debug_info_of_specfic_read("m64011_190329_072846/80545633/ccs", // sources, reverse_sources, -1, "clean"); - - - - ma_hit_sub(min_dp, sources, n_read, readLen, mini_overlap_length, &coverage_cut); - - detect_chimeric_reads(sources, reverse_sources, n_read, readLen, coverage_cut, FINAL_OVERLAP_ERROR_RATE*2); - - ////这个会断开 - ///ma_hit_chimeric(1, sources, reverse_sources, n_read, readLen, coverage_cut); - ma_hit_cut(min_dp, sources, n_read, readLen, mini_overlap_length, &coverage_cut); ///it seems we do not need ma_hit_flt ma_hit_flt(sources, n_read, coverage_cut, max_hang_length, mini_overlap_length); - ///debug_cut_ma_hit_t(sources, n_read, coverage_cut); ma_hit_contained(sources, n_read, coverage_cut, max_hang_length, mini_overlap_length); - ///debug_cut_ma_hit_t(sources, n_read, coverage_cut); - - - - // debug_info_of_specfic_read("m64016_190918_162737/179635219/ccs", - // sources, reverse_sources, -1, "contain"); - - // debug_info_of_specfic_read("m64016_190918_162737/130811282/ccs", - // sources, reverse_sources, -1, "contain"); - - // debug_info_of_specfic_read("m64016_190918_162737/72220752/ccs", - // sources, reverse_sources, -1, "contain"); - - - asg_t *sg = NULL; sg = ma_sg_gen(sources, n_read, coverage_cut, max_hang_length, mini_overlap_length); - - - // debug_info_of_specfic_node("m64016_190918_162737/72220752/ccs", sg, "sg_gen"); - - asg_arc_del_trans(sg, GAP_FUZZ); - - // debug_info_of_specfic_node("m64016_190918_162737/72220752/ccs", sg, "del_trans"); - char* unlean_name = (char*)malloc(strlen(output_file_name)+25); sprintf(unlean_name, "%s.unclean", output_file_name); output_read_graph(sg, coverage_cut, unlean_name, n_read); free(unlean_name); - - - asg_cut_tip(sg, MAX_SHORT_TIPS); - - - ///goto out; // debug_info_of_specfic_node("m64016_190918_162737/72220752/ccs", sg, "cut_tip"); - - - - - ///asg_arc_del_short_diploid_unclean(sg, corase_ovlp_drop_ratio, sources, reverse_sources); - - - // debug_info_of_specfic_node("m64016_190918_162737/72220752/ccs", sg, "cut_corase"); - - - - // asg_arc_del_single_node_bubble(sg, bubble_dist); // asg_cut_tip(sg, MAX_SHORT_TIPS); ///asg_cut_tip(sg, MAX_SHORT_TIPS); - - ///clean_round = 0; - // fprintf(stderr, "\n\nWill perform %d round of clean...**********\n", - // clean_round); - if(clean_round > 0) { @@ -10764,8 +8287,12 @@ char* output_file_name, long long bubble_dist, int read_graph, int write) drop_ratio = max_ovlp_drop_ratio; } - fprintf(stderr, "\n\n**********%d-th round drop: drop_ratio = %f**********\n", - i, drop_ratio); + if(VERBOSE >= 1) + { + fprintf(stderr, "\n\n**********%d-th round drop: drop_ratio = %f**********\n", + i, drop_ratio); + } + while(1) @@ -10778,7 +8305,6 @@ char* output_file_name, long long bubble_dist, int read_graph, int write) tri_flag += asg_arc_del_cross_bubble(sg, bubble_dist); ///asg_arc_del_single_node_bubble(sg, bubble_dist); tri_flag += asg_arc_del_single_node_directly(sg, MAX_SHORT_TIPS, sources); - if(tri_flag == 0) { break; @@ -10791,7 +8317,6 @@ char* output_file_name, long long bubble_dist, int read_graph, int write) // asg_cut_tip(sg, MAX_SHORT_TIPS); /****************************may have bugs********************************/ - //asg_arc_identify_simple_bubbles(sg); asg_arc_identify_simple_bubbles_multi(sg, 1); //reomve edge between two chromesomes asg_arc_del_false_node(sg, MAX_SHORT_TIPS); @@ -10807,7 +8332,6 @@ char* output_file_name, long long bubble_dist, int read_graph, int write) /****************************may have bugs********************************/ - //asg_arc_identify_simple_bubbles(sg); asg_arc_identify_simple_bubbles_multi(sg, 1); asg_arc_del_short_diploid_by_length(sg, drop_ratio, MAX_SHORT_TIPS, reverse_sources, MAX_SHORT_TIPS); asg_cut_tip(sg, MAX_SHORT_TIPS); @@ -10822,33 +8346,19 @@ char* output_file_name, long long bubble_dist, int read_graph, int write) } } - - fprintf(stderr, "\n\n**********final clean**********\n"); - - ///debug_info_of_specfic_node("m64016_190918_162737/72220752/ccs", sg, "before final clean"); + if(VERBOSE >= 1) + { + fprintf(stderr, "\n\n**********final clean**********\n"); + } while(1) { int tri_flag = 0; tri_flag += asg_arc_del_self_circle_contig(sg); - // fprintf(stderr, "tri_flag: %d\n", tri_flag); - // fflush(stderr); - ///asg_arc_del_single_node_bubble(sg, bubble_dist); tri_flag += asg_arc_del_single_node_directly(sg, MAX_SHORT_TIPS, sources); - // fprintf(stderr, "tri_flag: %d\n", tri_flag); - // fflush(stderr); tri_flag += asg_arc_del_triangular_advance(sg, bubble_dist); - ///tri_flag += asg_arc_del_triangular_advance_debug(sg, bubble_dist); - - // fprintf(stderr, "tri_flag: %d\n", tri_flag); - // fflush(stderr); tri_flag += asg_arc_del_cross_bubble(sg, bubble_dist); - // fprintf(stderr, "tri_flag: %d\n", tri_flag); - // fflush(stderr); - ///asg_arc_del_single_node_bubble(sg, bubble_dist); tri_flag += asg_arc_del_single_node_directly(sg, MAX_SHORT_TIPS, sources); - // fprintf(stderr, "tri_flag: %d\n", tri_flag); - // fflush(stderr); if(tri_flag == 0) { @@ -10860,22 +8370,14 @@ char* output_file_name, long long bubble_dist, int read_graph, int write) asg_arc_del_short_diploi_by_suspect_edge(sg, MAX_SHORT_TIPS, sources); asg_cut_tip(sg, MAX_SHORT_TIPS); - - asg_arc_del_triangular_directly(sg, MAX_SHORT_TIPS, reverse_sources); - - - ///asg_arc_identify_simple_bubbles_multi(sg, 0); // asg_arc_del_chimeric_read(sg, MAX_SHORT_TIPS*2); // asg_cut_tip(sg, MAX_SHORT_TIPS); - - - asg_arc_identify_simple_bubbles_multi(sg, 0); asg_arc_del_orthology_multiple_way(sg, reverse_sources, 0.4, MAX_SHORT_TIPS); asg_cut_tip(sg, MAX_SHORT_TIPS); @@ -10898,7 +8400,6 @@ char* output_file_name, long long bubble_dist, int read_graph, int write) - ///asg_arc_del_triangular_advance_debug(sg, bubble_dist); /** fprintf(stderr, "\n\n**********final aggressive clean**********\n"); @@ -10946,7 +8447,7 @@ char* output_file_name, long long bubble_dist, int read_graph, int write) // debug_info_of_specfic_node("m64016_190918_162737/141297762/ccs", sg); - out: + ///out: ///output_tips(sg, &R_INF); @@ -10955,7 +8456,7 @@ char* output_file_name, long long bubble_dist, int read_graph, int write) /****************************may have bugs********************************/ output_unitig_graph_without_small_bubbles(sg, coverage_cut, output_file_name, n_read, - 100000, MAX_SHORT_TIPS); + pop_bubble_size, MAX_SHORT_TIPS); /****************************may have bugs********************************/ output_contig_graph(sg, coverage_cut, output_file_name, n_read, 10000000, MAX_SHORT_TIPS, 0.1, 20, diff --git a/Overlaps.h b/Overlaps.h index c28625a..167d7e7 100644 --- a/Overlaps.h +++ b/Overlaps.h @@ -3,7 +3,6 @@ #include #include "kvec.h" #include "kdq.h" -///#include "Hash_Table.h" ///#define MIN_OVERLAP_LEN 2000 ///#define MIN_OVERLAP_LEN 500 @@ -68,16 +67,7 @@ void ma_hit_sort_qns(ma_hit_t *a, long long n); int load_all_data_from_disk(ma_hit_t_alloc **sources, ma_hit_t_alloc **reverse_sources, char* output_file_name); - -typedef struct { - ma_hit_t_alloc overlaps; -} Assembly_Graph; - -void init_Assembly_Graph(Assembly_Graph* x); -void destory_Assembly_Graph(Assembly_Graph* x); -void collect_ma_hit_t(ma_hit_t_alloc* dest, ma_hit_t_alloc* sources, long long num_sources); void normalize_ma_hit_t(ma_hit_t_alloc* sources, long long num_sources); -void debug_normalize_ma_hit_t(ma_hit_t_alloc* sources, long long num_sources); typedef struct { @@ -93,7 +83,6 @@ void ma_hit_flt(ma_hit_t_alloc* sources, long long n_read, const ma_sub_t *cover int max_hang, int min_ovlp); long long get_specific_overlap(ma_hit_t_alloc* x, uint32_t qn, uint32_t tn); -void debug_cut_ma_hit_t(ma_hit_t_alloc* sources, long long num_sources, ma_sub_t *coverage_cut); typedef struct { uint64_t ul; @@ -139,7 +128,7 @@ static inline int ma_hit2arc(const ma_hit_t *h, int ql, int tl, int max_hang, fl ///ext5 and ext3 is the hang on left side and right side, respectively ext5 = qs < tl5? qs : tl5; - ext3 = ql - h->qe < tl3? ql - h->qe : tl3; + ext3 = ql - (int)h->qe < tl3? ql - (int)h->qe : tl3; /** @@ -179,11 +168,11 @@ static inline int ma_hit2arc(const ma_hit_t *h, int ql, int tl, int max_hang, fl ********************************target-to-query overlap**************************** **/ - if (qs <= tl5 && ql - h->qe <= tl3) return MA_HT_QCONT; // query contained in target - else if (qs >= tl5 && ql - h->qe >= tl3) return MA_HT_TCONT; // target contained in query + if (qs <= tl5 && ql - (int)h->qe <= tl3) return MA_HT_QCONT; // query contained in target + else if (qs >= tl5 && ql - (int)h->qe >= tl3) return MA_HT_TCONT; // target contained in query else if (qs > tl5) u = 0, v = !!h->rev, l = qs - tl5; ///u = 0 means query-to-target overlap, l is the length of node in string graph (not the overlap length) else u = 1, v = !h->rev, l = (ql - h->qe) - tl3; ///u = 1 means target-to-query overlaps, l is the length of node in string graph (not the overlap length) - if (h->qe - qs + ext5 + ext3 < min_ovlp || h->te - h->ts + ext5 + ext3 < min_ovlp) return MA_HT_SHORT_OVLP; // short overlap + if ((int)h->qe - qs + ext5 + ext3 < min_ovlp || (int)h->te - (int)h->ts + ext5 + ext3 < min_ovlp) return MA_HT_SHORT_OVLP; // short overlap ///u = 0 / 1 means query-to-target / target-to-query overlaps, ///l is the length of node in string graph (not the overlap length between two reads) u |= h->qns>>32<<1, v |= h->tn<<1; @@ -206,11 +195,6 @@ static inline int ma_hit2arc(const ma_hit_t *h, int ql, int tl, int max_hang, fl } -void build_string_graph(int min_dp, ma_hit_t_alloc* sources, ma_hit_t_alloc* reverse_sources, long long n_read, uint64_t* readLen, -long long mini_overlap_length, long long max_hang_length, -long long clean_round, float min_ovlp_drop_ratio, float max_ovlp_drop_ratio, -float final_ovlp_drop_ratio, char* output_file_name, long long bubble_dist); - #define asg_arc_len(arc) ((uint32_t)(arc).ul) #define asg_arc_n(g, v) ((uint32_t)(g)->idx[(v)]) @@ -296,13 +280,7 @@ typedef struct { // count the number of outgoing arcs, including reduced arcs static inline int count_out_with_del(const asg_t *g, uint32_t v) { - uint32_t i, n, nv = asg_arc_n(g, v); - const asg_arc_t *av = asg_arc_a(g, v); - /** - for (i = n = 0; i < nv; ++i) - if (!av[i].del) ++n; - return n; - **/ + uint32_t nv = asg_arc_n(g, v); return nv; } @@ -318,14 +296,13 @@ static inline int count_out_without_del(const asg_t *g, uint32_t v) return n; } -void debug_info_of_specfic_read(char* name, ma_hit_t_alloc* sources, -ma_hit_t_alloc* reverse_sources, int id, char* fun); -void build_string_graph_without_clean(int min_dp, ma_hit_t_alloc* sources, ma_hit_t_alloc* reverse_sources, long long n_read, uint64_t* readLen, -long long mini_overlap_length, long long max_hang_length, -long long clean_round, float min_ovlp_drop_ratio, float max_ovlp_drop_ratio, -float corase_ovlp_drop_ratio, char* output_file_name, long long bubble_dist, int read_graph, -int write); +void build_string_graph_without_clean( +int min_dp, ma_hit_t_alloc* sources, ma_hit_t_alloc* reverse_sources, +long long n_read, uint64_t* readLen, long long mini_overlap_length, +long long max_hang_length, long long clean_round, long long pop_bubble_size, +float min_ovlp_drop_ratio, float max_ovlp_drop_ratio, char* output_file_name, +long long bubble_dist, int read_graph, int write); void debug_info_of_specfic_read(char* name, ma_hit_t_alloc* sources, ma_hit_t_alloc* reverse_sources, int id, char* command); diff --git a/POA.cpp b/POA.cpp index 403482d..c519693 100644 --- a/POA.cpp +++ b/POA.cpp @@ -191,7 +191,7 @@ void init_Node_alloc(Node_alloc* list) list->sort.iterative_buffer_visit = NULL; - long long i; + uint64_t i; for (i = 0; i < list->size; i++) { list->list[i].insertion_edges.list=NULL; @@ -238,10 +238,8 @@ uint64_t append_Node_alloc(Node_alloc* list, char base) if (list->length + 1 > list->size) { - long long i = list->size; + uint64_t i = list->size; - ///list->topo_order这里用不到,所以不用先分配空间 - ///但是还是一起分配了吧,免得麻烦 list->size = list->size * 2; list->list = (Node*)realloc(list->list, sizeof(Node)*list->size); ///list->topo_order = (uint64_t*)realloc(list->topo_order, sizeof(uint64_t)*list->size); @@ -336,8 +334,6 @@ void addUnmatchedSeqToGraph(Graph* g, char* g_read_seq, long long g_read_length, for (i = 0; i < g_read_length; i++) { nodeID = add_Node_Graph(g, g_read_seq[i]); - - ////fprintf(stderr, "nodeID: %llu\n", nodeID); if (firstID == -1) { @@ -345,12 +341,7 @@ void addUnmatchedSeqToGraph(Graph* g, char* g_read_seq, long long g_read_length, } if (lastID != -1) { - /** - ///0是match边 - add_Edge_Graph(g, lastID, nodeID, 0); - **/ - ///只有match边长度是0 - ///mismatch边长度都是1 + ///the legnth of match edge is 0, while the length of musmatch is 1 append_Edge_alloc(&(g->g_nodes.list[lastID].mismatch_edges), lastID, nodeID, 1, 0); } @@ -367,287 +358,6 @@ void addUnmatchedSeqToGraph(Graph* g, char* g_read_seq, long long g_read_length, -inline void add_insertionEdge_weight_print(Graph* g, long long alignNodeID, char* insert, long long insert_length) -{ - - long long nodeID; - long long edgeID; - Edge_alloc* edge = &(g->g_nodes.list[alignNodeID].insertion_edges); - - /******************************for homopolymer*************************/ - long long i = 0; - char hom; - if (insert_length > 0) - { - hom = insert[0]; - } - - for (i = 0; i < insert_length; i++) - { - if(insert[i] != hom) - { - break; - } - } - - fprintf(stderr, "###insert_length: %d\n", insert_length); - - ///if it is a homopolymer - if(i == insert_length) - { - ///single base - edgeID = getEdge(g, edge, 1, insert[0]); - if (edgeID != -1) - { - ///这条路均只有一个出度 - edge->list[edgeID].weight++; - } - else ///不存在这么一条边 - { - nodeID = add_Node_Graph(g, insert[0]); - append_Edge_alloc(edge, alignNodeID, nodeID, 1, 1); - ///将新加入的节点通过insertion_edges接回backbone上 - ///应该连回到原节点,而不是原节点的下一个节点 - ///append_Edge_alloc(&(g->g_nodes.list[nodeID].insertion_edges), nodeID, alignNodeID + 1, 1, 0); - append_Edge_alloc(&(g->g_nodes.list[nodeID].insertion_edges), nodeID, alignNodeID, 1, 0); - } - - ///multiple bases - for (i = 1; i < insert_length; i++) - { - edgeID = get_insertion_Edges(g, edge, i + 1, insert); - if (edgeID != -1) - { - ///这条路均只有一个出度 - edge->list[edgeID].weight++; - } - else - { - create_insertion_Edges(g, alignNodeID, i + 1, insert); - } - } - - return; - } - /******************************for homopolymer*************************/ - - fprintf(stderr, "###not homopolymer: %d\n", insert_length); - - if (insert_length == 1) - { - edgeID = getEdge(g, edge, 1, insert[0]); - if (edgeID != -1) - { - ///这条路均只有一个出度 - edge->list[edgeID].weight++; - } - else ///不存在这么一条边 - { - nodeID = add_Node_Graph(g, insert[0]); - append_Edge_alloc(edge, alignNodeID, nodeID, 1, 1); - ///将新加入的节点通过insertion_edges接回backbone上 - ///应该连回到原节点,而不是原节点的下一个节点 - ///append_Edge_alloc(&(g->g_nodes.list[nodeID].insertion_edges), nodeID, alignNodeID + 1, 1, 0); - append_Edge_alloc(&(g->g_nodes.list[nodeID].insertion_edges), nodeID, alignNodeID, 1, 0); - } - } - else if (insert_length == 2) - { - /*******************第0个字符********************* */ - edgeID = getEdge(g, edge, 1, insert[0]); - fprintf(stderr, "edgeID[0]: %d, length: %d\n", edgeID, edge->list[edgeID].length); - if (edgeID != -1) - { - ///这条路均只有一个出度 - edge->list[edgeID].weight++; - } - else ///不存在这么一条边 - { - nodeID = add_Node_Graph(g, insert[0]); - append_Edge_alloc(edge, alignNodeID, nodeID, 1, 1); - ///将新加入的节点通过insertion_edges接回backbone上 - ///应该连回到原节点,而不是原节点的下一个节点 - ///append_Edge_alloc(&(g->g_nodes.list[nodeID].insertion_edges), nodeID, alignNodeID + 1, 1, 0); - append_Edge_alloc(&(g->g_nodes.list[nodeID].insertion_edges), nodeID, alignNodeID, 1, 0); - } - - fprintf(stderr, "edge->length: %d\n", edge->length); - - /*******************第0个字符********************* */ - - /*******************第1个字符********************* */ - if (insert[1] != insert[0]) - { - edgeID = getEdge(g, edge, 1, insert[1]); - fprintf(stderr, "edgeID[1]: %d, length: %d\n", edgeID, edge->list[edgeID].length); - if (edgeID != -1) - { - ///这条路均只有一个出度 - edge->list[edgeID].weight++; - } - else ///不存在这么一条边 - { - nodeID = add_Node_Graph(g, insert[1]); - append_Edge_alloc(edge, alignNodeID, nodeID, 1, 1); - ///将新加入的节点通过insertion_edges接回backbone上 - ///应该连回到原节点,而不是原节点的下一个节点 - ///append_Edge_alloc(&(g->g_nodes.list[nodeID].insertion_edges), nodeID, alignNodeID + 1, 1, 0); - append_Edge_alloc(&(g->g_nodes.list[nodeID].insertion_edges), nodeID, alignNodeID, 1, 0); - } - - fprintf(stderr, "edge->length: %d\n", edge->length); - } - /*******************第1个字符********************* */ - - /**********************两个字符******************* */ - - edgeID = get_insertion_Edges(g, edge, 2, insert); - fprintf(stderr, "edgeID[len2]: %d, length: %d\n", edgeID, edge->list[edgeID].length); - if (edgeID != -1) - { - ///这条路均只有一个出度 - edge->list[edgeID].weight++; - } - else - { - create_insertion_Edges(g, alignNodeID, insert_length, insert); - } - - fprintf(stderr, "edge->length: %d\n", edge->length); - - for (i = 0; i < edge->length; i++) - { - fprintf(stderr, "edgeID[%d].length: %d\n", i, edge->list[i].length); - } - - - /**********************两个字符******************* */ - } - else if (insert_length > 2) - { - ////fprintf(stderr, "too long insertion\n"); - /*************************大于2个字符************************** */ - - edgeID = get_insertion_Edges(g, edge, insert_length, insert); - if (edgeID != -1) - { - ///这条路均只有一个出度 - edge->list[edgeID].weight++; - } - else - { - create_insertion_Edges(g, alignNodeID, insert_length, insert); - } - } - - - -} - - - -void addmatchedSeqToGraph_print(Graph* backbone, long long currentNodeID, char* x_string, long long x_length, - char* y_string, long long y_length, CIGAR* cigar, long long backbone_start, long long backbone_end) -{ - - int x_i, y_i, cigar_i; - x_i = 0; - y_i = 0; - cigar_i = 0; - int operation; - int operationLen; - int i; - int last_operation = -1; - - fprintf(stderr,"*******\n"); - ///note that node 0 is the start node - ///0 is match, 1 is mismatch, 2 is up, 3 is left - ///2是x缺字符(y多字符),而3是y缺字符(x多字符) - while (cigar_i < cigar->length) - { - operation = cigar->C_C[cigar_i]; - operationLen = cigar->C_L[cigar_i]; - - // fprintf(stderr, "operation: %d, operationLen: %d\n", - // operation, operationLen); - - ///这种情况代表匹配和mismatch - if (operation == 0 || operation == 1) - { - - for (i = 0; i < operationLen; i++) - { - //backbone->g_nodes.list[currentNodeID].weight++; - ///前面是插入,后面有可能是误配,也有可能是匹配 - add_mismatchEdge_weight(backbone, currentNodeID, y_string[y_i], last_operation); - x_i++; - y_i++; - currentNodeID++; - } - }///insertion - else if (operation == 2) - { - ///cigar的起始和结尾不可能是2,所以这里-1没问题 - ///if (operationLen <= CORRECT_INDEL_LENGTH) - { - add_insertionEdge_weight_print(backbone, currentNodeID, y_string + y_i, operationLen); - backbone->g_nodes.list[currentNodeID].num_insertions++; - } - - ///fprintf(stderr, "y_string: %.*s\n", operationLen, y_string+y_i); - y_i += operationLen; - } - else if (operation == 3) - { - ///3是y缺字符(x多字符),也就是backbone多字符 - ///这个相当于在backbone对应字符处变成了‘——’ - ///因此可以用mismatch类似的方法处理 - ///if (operationLen <= CORRECT_INDEL_LENGTH) - { - ///add_deletion_to_backbone(backbone, ¤tNodeID, operationLen); - ///在编辑距离中,前面是个插入,后面是个删除,这种情况是不存在的 - ///为了保险要不还给他加上吧 - ///先不加 - add_deletionEdge_weight(backbone, currentNodeID, operationLen); - } - - - currentNodeID += operationLen; - x_i += operationLen; - } - - last_operation = operation; - - cigar_i++; - } - - - - /** - ///cigar的起始和结尾不可能是2 - if (cigar->C_C[0] == 2 || cigar->C_C[cigar->length - 1] == 2) - { - fprintf(stderr, "error\n"); - } - - - if (x_i != x_length) - { - fprintf(stderr, "x_i: %d, x_length: %d\n", x_i, x_length); - } - - if (y_i != y_length) - { - fprintf(stderr, "y_i: %d, y_length: %d\n", y_i, y_length); - } - **/ - -} - - - - - void addmatchedSeqToGraph(Graph* backbone, long long currentNodeID, char* x_string, long long x_length, char* y_string, long long y_length, CIGAR* cigar, long long backbone_start, long long backbone_end) { @@ -664,20 +374,19 @@ void addmatchedSeqToGraph(Graph* backbone, long long currentNodeID, char* x_stri ///note that node 0 is the start node ///0 is match, 1 is mismatch, 2 is up, 3 is left - ///2是x缺字符(y多字符),而3是y缺字符(x多字符) + ///2 mean y has more bases, while 3 means x has more bases while (cigar_i < cigar->length) { operation = cigar->C_C[cigar_i]; operationLen = cigar->C_L[cigar_i]; - ///这种情况代表匹配和mismatch + ///match/mismatch if (operation == 0 || operation == 1) { for (i = 0; i < operationLen; i++) { - //backbone->g_nodes.list[currentNodeID].weight++; - ///前面是插入,后面有可能是误配,也有可能是匹配 + ///if the previous node is insertion, this node might be mismatch/match add_mismatchEdge_weight(backbone, currentNodeID, y_string[y_i], last_operation); x_i++; y_i++; @@ -686,7 +395,7 @@ void addmatchedSeqToGraph(Graph* backbone, long long currentNodeID, char* x_stri }///insertion else if (operation == 2) { - ///cigar的起始和结尾不可能是2,所以这里-1没问题 + ///the begin and end of cigar cannot be 2, so -1 is right here ///if (operationLen <= CORRECT_INDEL_LENGTH) { add_insertionEdge_weight(backbone, currentNodeID, y_string + y_i, operationLen); @@ -696,15 +405,10 @@ void addmatchedSeqToGraph(Graph* backbone, long long currentNodeID, char* x_stri } else if (operation == 3) { - ///3是y缺字符(x多字符),也就是backbone多字符 - ///这个相当于在backbone对应字符处变成了‘——’ - ///因此可以用mismatch类似的方法处理 + ///3 means x has more bases, that means backbone has more bases + ///like a mismatch (-) ///if (operationLen <= CORRECT_INDEL_LENGTH) { - ///add_deletion_to_backbone(backbone, ¤tNodeID, operationLen); - ///在编辑距离中,前面是个插入,后面是个删除,这种情况是不存在的 - ///为了保险要不还给他加上吧 - ///先不加 add_deletionEdge_weight(backbone, currentNodeID, operationLen); } @@ -720,284 +424,6 @@ void addmatchedSeqToGraph(Graph* backbone, long long currentNodeID, char* x_stri } -void debug_graph(Graph* g, long long backbone_length) -{ - long long i = 0; - - if (g->s_start_nodeID != 0 || g->s_end_nodeID != backbone_length) - { - fprintf(stderr, "error\n"); - } - - - for (i = g->s_start_nodeID; i <= g->s_end_nodeID; i++) - { - if(g->g_nodes.list[i].weight != 1) - { - fprintf(stderr, "error node weight\n"); - } - - if(g->g_nodes.list[i].mismatch_edges.length > 4) - { - fprintf(stderr, "error mismatch_edges\n"); - } - - if(g->g_nodes.list[i].mismatch_edges.length < 1 && i != g->s_end_nodeID) - { - fprintf(stderr, "i: %d, error mismatch_edges: %d\n", i, g->g_nodes.list[i].mismatch_edges.length); - } - } - - - for (i = 0; i < g->g_nodes.length; i++) - { - if(g->g_nodes.list[i].ID < g->s_start_nodeID || g->g_nodes.list[i].ID > g->s_end_nodeID) - { - if (g->g_nodes.list[i].deletion_edges.length + - g->g_nodes.list[i].insertion_edges.length + - g->g_nodes.list[i].mismatch_edges.length - != 1) - { - fprintf(stderr, "g->s_start_nodeID: %lld\n", - g->s_start_nodeID); - fprintf(stderr, "g->s_end_nodeID: %lld\n", - g->s_end_nodeID); - fprintf(stderr, "deletion_edges_length: %lld\n", - g->g_nodes.list[i].deletion_edges.length); - fprintf(stderr, "insertion_edges_length: %lld, \n", - g->g_nodes.list[i].insertion_edges.length); - fprintf(stderr, "g->g_nodes.list[i].insertion_edges.list[0].length: %lld, \n", - g->g_nodes.list[i].insertion_edges.list[0].length); - fprintf(stderr, "g->g_nodes.list[i].insertion_edges.list[1].length: %lld, \n", - g->g_nodes.list[i].insertion_edges.list[1].length); - - fprintf(stderr, "mismatch_edges_length: %lld\n", - g->g_nodes.list[i].mismatch_edges.length); - } - else - { - ///不是0肯定是1 - if (g->g_nodes.list[i].deletion_edges.length != 0) - { - long long step = g->g_nodes.list[i].deletion_edges.list[0].length; - long long nodeID = i; - for (int j = 0; j < step; j++) - { - nodeID = g->g_nodes.list[nodeID].deletion_edges.list[0].out_node; - } - - nodeID = g->g_nodes.list[nodeID].deletion_edges.list[0].out_node; - - if (nodeID < g->s_start_nodeID || nodeID > g->s_end_nodeID) - { - fprintf(stderr, "error\n"); - } - } - - if (g->g_nodes.list[i].insertion_edges.length != 0) - { - - long long step = g->g_nodes.list[i].insertion_edges.list[0].length; - long long nodeID = i; - - - for (int j = 0; j < step; j++) - { - nodeID = g->g_nodes.list[nodeID].insertion_edges.list[0].out_node; - } - - nodeID = g->g_nodes.list[nodeID].insertion_edges.list[0].out_node; - - if ((nodeID < g->s_start_nodeID || nodeID > g->s_end_nodeID)) - { - fprintf(stderr, "error: step: %d\n", step); - } - - } - - - if (g->g_nodes.list[i].mismatch_edges.length != 0) - { - - long long step = g->g_nodes.list[i].mismatch_edges.list[0].length; - long long nodeID = i; - - for (int j = 0; j < step; j++) - { - nodeID = g->g_nodes.list[nodeID].mismatch_edges.list[0].out_node; - } - - nodeID = g->g_nodes.list[nodeID].mismatch_edges.list[0].out_node; - - if (nodeID < g->s_start_nodeID || nodeID > g->s_end_nodeID) - { - fprintf(stderr, "error\n"); - } - - } - - } - - - } - } - - - - -} - -void Graph_debug(Graph* backbone, long long currentNodeID, char* x_string, long long x_length, - char* y_string, long long y_length, CIGAR* cigar, long long backbone_start, long long backbone_end) -{ - /** - int x_i, y_i, cigar_i; - x_i = 0; - y_i = 0; - cigar_i = 0; - int operation; - int operationLen; - int i; - - - - - ///0 is match, 1 is mismatch, 2 is up, 3 is left - ///2是x缺字符(y多字符),而3是y缺字符(x多字符) - ///while (x_i < x_len && y_i < y_len && cigar_i < cigar->length) - while (cigar_i < cigar->length) - { - operation = cigar->C_C[cigar_i]; - operationLen = cigar->C_L[cigar_i]; - - ///这种情况代表匹配 - if (operation == 0) - { - - for (i = 0; i < operationLen; i++) - { - if (backbone->g_nodes.list[currentNodeID].base != y_string[y_i]) - { - fprintf(stderr, "error match\n"); - } - - backbone->g_nodes.list[currentNodeID].weight--; - - x_i++; - y_i++; - currentNodeID++; - } - } - else if (operation == 1) - { - for (i = 0; i < operationLen; i++) - { - if (backbone->g_nodes.list[currentNodeID].base == y_string[y_i]) - { - fprintf(stderr, "error mismatch 1\n"); - } - - long long mismatchID = get_alignToNode(backbone, currentNodeID, y_string[y_i]); - - - - if(mismatchID == -1) - { - fprintf(stderr, "error mismatch 2\n"); - } - else - { - backbone->g_nodes.list[mismatchID].weight--; - } - - - x_i++; - y_i++; - currentNodeID++; - } - } - else if (operation == 2) - { - long long nodeID = currentNodeID - 1; - long long mismatchID; - - for (i = 0; i < operationLen; i++) - { - mismatchID = get_insertion_Node(backbone, nodeID, y_string[y_i]); - - if (mismatchID == -1) - { - fprintf(stderr, "error insertion 1, i: %d\n", i); - } - else - { - backbone->g_nodes.list[mismatchID].weight--; - } - - nodeID = mismatchID; - - y_i++; - } - ///注意这里是x_string[x_i]而不是x_string[currentNodeID] - mismatchID = get_insertion_Node(backbone, nodeID, x_string[x_i]); - if (mismatchID == -1) - { - fprintf(stderr, "error insertion 2, i: %d, x_i: %d\n", i, x_i); - } - - - if (mismatchID != currentNodeID) - { - fprintf(stderr, "error insertion 3, i: mismatchID: %d, currentNodeID: %d\n", mismatchID, currentNodeID); - } - - - - - } - else if (operation == 3) - { - for (i = 0; i < operationLen; i++) - { - - long long mismatchID = get_alignToNode(backbone, currentNodeID, 'D'); - - if(mismatchID == -1) - { - fprintf(stderr, "error deletion 2\n"); - } - else - { - backbone->g_nodes.list[mismatchID].weight--; - } - - x_i++; - currentNodeID++; - } - } - - cigar_i++; - } - - - if (cigar->C_C[0] == 2 || cigar->C_C[cigar->length - 1] == 2) - { - fprintf(stderr, "error\n"); - } - - - if (x_i != x_length) - { - fprintf(stderr, "x_i: %d, x_length: %d\n", x_i, x_length); - } - - if (y_i != y_length) - { - fprintf(stderr, "y_i: %d, y_length: %d\n", y_i, y_length); - } - **/ - -} diff --git a/POA.h b/POA.h index b54dc2e..bb7529b 100644 --- a/POA.h +++ b/POA.h @@ -4,27 +4,6 @@ #include "Hash_Table.h" #include "Process_Read.h" -/** - 1. 单个节点信息 - (1) ID - (2) base - (3) 入边信息 - (4) 出边信息 - (5) 比对到什么节点 - 2. 各个节点信息,用数组下标组织,数组下标就是节点ID; 还要存拓扑排序后的下标和节点ID的对应关系 - 3. 边 - (1) 边的起始 - (2) 边的结束节点 - (3) 过这条边的序列的label,也就是名称 - 4. 各个序列信息 - (1) 这个序列本身 - (2) 这个序列的name或者ID - (3) 这个序列的在图中对应的起始和结束节点ID - 5. 有两个回溯矩阵,一个是graph的,一个是seq的 - **/ - - - typedef struct { long long beg; @@ -141,10 +120,9 @@ typedef struct { uint64_t in_node; uint64_t out_node; - ///0是match,1是mismatch,2是x缺字符(y多字符),而3是y缺字符(x多字符) + ///0 is match,1 is mismatch,2 means y has more bases, 3 means x has more bases uint64_t weight; uint64_t num_insertions; - ///这条路径上到backbone节点之前总共有多少节点 uint64_t length; uint64_t self_edge_ID; uint64_t reverse_edge_ID; @@ -180,7 +158,7 @@ typedef struct { uint64_t ID; uint64_t weight; - ///记录的是以当前节点为尾的deletion个数 + ///number of deletion end with current node uint64_t num_insertions; char base; Edge_alloc mismatch_edges; @@ -245,18 +223,20 @@ inline int Pop_Node(Graph* DAGCon, Node** node) inline int Push_Node(Graph* DAGCon, Node** node) { push_to_Queue(&(DAGCon->node_q), (**node).ID); + + return 1; } inline int getInputNodes(RSet* set, Graph* graph, Node* node, Node** get_Node) { - if(set->index >= Input_Edges(*node).length) + if(set->index >= (long long)Input_Edges(*node).length) { return 0; } ///skip all deleted edges while ( - set->index < Input_Edges(*node).length + set->index < (long long)Input_Edges(*node).length && !(If_Edge_Exist(Input_Edges(*node).list[set->index])) ) @@ -266,7 +246,7 @@ inline int getInputNodes(RSet* set, Graph* graph, Node* node, Node** get_Node) if( - set->index < Input_Edges(*node).length + set->index < (long long)Input_Edges(*node).length && If_Edge_Exist(Input_Edges(*node).list[set->index]) ) @@ -285,14 +265,14 @@ inline int getInputNodes(RSet* set, Graph* graph, Node* node, Node** get_Node) inline int getInputEdges(RSet* set, Graph* graph, Node* node, Edge** get_Edge) { - if(set->index >= Input_Edges(*node).length) + if(set->index >= (long long)Input_Edges(*node).length) { return 0; } ///skip all deleted edges while ( - set->index < Input_Edges(*node).length + set->index < (long long)Input_Edges(*node).length && !(If_Edge_Exist(Input_Edges(*node).list[set->index])) ) @@ -302,7 +282,7 @@ inline int getInputEdges(RSet* set, Graph* graph, Node* node, Edge** get_Edge) if( - set->index < Input_Edges(*node).length + set->index < (long long)Input_Edges(*node).length && If_Edge_Exist(Input_Edges(*node).list[set->index]) ) @@ -320,14 +300,14 @@ inline int getInputEdges(RSet* set, Graph* graph, Node* node, Edge** get_Edge) inline int getOutputNodes(RSet* set, Graph* graph, Node* node, Node** get_Node) { - if(set->index >= Output_Edges(*node).length) + if(set->index >= (long long)Output_Edges(*node).length) { return 0; } ///skip all deleted edges while ( - set->index < Output_Edges(*node).length + set->index < (long long)Output_Edges(*node).length && !(If_Edge_Exist(Output_Edges(*node).list[set->index])) ) @@ -335,7 +315,7 @@ inline int getOutputNodes(RSet* set, Graph* graph, Node* node, Node** get_Node) set->index++; } - if(set->index < Output_Edges(*node).length && + if(set->index < (long long)Output_Edges(*node).length && If_Edge_Exist(Output_Edges(*node).list[set->index])) { (*get_Node) = &(G_Node((*graph), Output_Edges(*node).list[set->index].out_node)); @@ -352,14 +332,14 @@ inline int getOutputNodes(RSet* set, Graph* graph, Node* node, Node** get_Node) inline int getOutputEdges(RSet* set, Graph* graph, Node* node, Edge** get_Edge) { - if(set->index >= Output_Edges(*node).length) + if(set->index >= (long long)Output_Edges(*node).length) { return 0; } ///skip all deleted edges while ( - set->index < Output_Edges(*node).length + set->index < (long long)Output_Edges(*node).length && !(If_Edge_Exist(Output_Edges(*node).list[set->index])) ) @@ -367,7 +347,7 @@ inline int getOutputEdges(RSet* set, Graph* graph, Node* node, Edge** get_Edge) set->index++; } - if(set->index < Output_Edges(*node).length && + if(set->index < (long long)Output_Edges(*node).length && If_Edge_Exist(Output_Edges(*node).list[set->index])) { (*get_Edge) = &(Output_Edges(*node).list[set->index]); @@ -389,9 +369,9 @@ inline void get_bi_direction_edges(Graph* DAGCon, Edge* edge, Edge** e_forward, if( edge->self_edge_ID < Output_Edges(G_Node(*DAGCon, in_node)).length && - Output_Edges(G_Node(*DAGCon, in_node)).list[edge->self_edge_ID].in_node == in_node + (long long)Output_Edges(G_Node(*DAGCon, in_node)).list[edge->self_edge_ID].in_node == in_node && - Output_Edges(G_Node(*DAGCon, in_node)).list[edge->self_edge_ID].out_node == out_node + (long long)Output_Edges(G_Node(*DAGCon, in_node)).list[edge->self_edge_ID].out_node == out_node ) { (*e_forward) = &(Output_Edges(G_Node(*DAGCon, in_node)).list[edge->self_edge_ID]); @@ -456,10 +436,7 @@ void destory_Graph(Graph* g); void clear_Graph(Graph* g); void Perform_POA(Graph* g, overlap_region_alloc* overlap_list, All_reads* R_INF, UC_Read* g_read); -void Graph_debug(Graph* backbone, long long currentNodeID, char* x_string, long long x_length, - char* y_string, long long y_length, CIGAR* cigar, long long backbone_start, long long backbone_end); -void debug_graph(Graph* g, long long backbone_length); uint64_t inline add_Node_Graph(Graph* g, char base) { @@ -503,26 +480,28 @@ uint64_t inline delete_Node_DAGCon(Graph* g, Node* node) clear_Edge_alloc(&(g->g_nodes.list[(*node).ID].insertion_edges)); clear_Edge_alloc(&(g->g_nodes.list[(*node).ID].mismatch_edges)); clear_Edge_alloc(&(g->g_nodes.list[(*node).ID].deletion_edges)); + + return 1; } -///仅仅用于误配边 +///just for mimatch edges inline void add_mismatchEdge_weight(Graph* g, uint64_t in_node, char base, int last_operation) { long long i = 0; long long nodeID; Edge_alloc* edge = &(g->g_nodes.list[in_node].mismatch_edges); - for (i = 0; i < edge->length; i++) + for (i = 0; i < (long long)edge->length; i++) { nodeID = edge->list[i].out_node; if(g->g_nodes.list[nodeID].base == base) { edge->list[i].weight++; - ///如果上一个操作是insertion + ///if last operation is insertion if (last_operation == 2) { edge->list[i].num_insertions++; @@ -532,25 +511,22 @@ inline void add_mismatchEdge_weight(Graph* g, uint64_t in_node, char base, int l } } - ///说明不存在这么一条边 - if (i == edge->length) + ///there are no such edge + if (i == (long long)edge->length) { nodeID = add_Node_Graph(g, base); - ///只有match边长度是0 - ///mismatch边长度都是1 + ///the length of match edge is 0, while the length of mismatch edge is 1 append_Edge_alloc(edge, in_node, nodeID, 1, 1); - ///如果上一个操作是insertion + ///if last operation is insertion if (last_operation == 2) { edge->list[edge->length - 1].num_insertions++; } - ///将新节点的mismatch_edges连到backbone上 + ///add the mismatch_edges of new node to the backbone append_Edge_alloc(&(g->g_nodes.list[nodeID].mismatch_edges), nodeID, in_node + 1, 1, 0); } - ///获得节点的mismatch_edges长度为1,其他均为0 - } @@ -561,7 +537,7 @@ inline void add_single_deletionEdge_weight(Graph* g, long long alignNodeID, long long long nodeID; Edge_alloc* edge = &(g->g_nodes.list[alignNodeID].deletion_edges); - for (i = 0; i < edge->length; i++) + for (i = 0; i < (long long)edge->length; i++) { nodeID = edge->list[i].out_node; if(nodeID == nextNodeID) @@ -571,8 +547,8 @@ inline void add_single_deletionEdge_weight(Graph* g, long long alignNodeID, long } } - ///说明不存在这么一条边 - if (i == edge->length) + ///there are no such edge + if (i == (long long)edge->length) { append_Edge_alloc(edge, alignNodeID, nextNodeID, 1, edge_length); } @@ -580,23 +556,6 @@ inline void add_single_deletionEdge_weight(Graph* g, long long alignNodeID, long inline void add_deletionEdge_weight(Graph* g, long long alignNodeID, long long deletion_length) { - /** - if (deletion_length == 1) - { - add_single_deletionEdge_weight(g, alignNodeID, alignNodeID + 1, 0); - } - else if (deletion_length == 2) - { - add_single_deletionEdge_weight(g, alignNodeID, alignNodeID + 1, 0); - add_single_deletionEdge_weight(g, alignNodeID, alignNodeID + 2, 0); - add_single_deletionEdge_weight(g, alignNodeID + 1, alignNodeID + 2, 0); - } - else if (deletion_length > 2) - { - ///fprintf(stderr, "too long deletion!\n"); - add_single_deletionEdge_weight(g, alignNodeID, alignNodeID + deletion_length, 0); - } - **/ long long i; for (i = 0; i < deletion_length; i++) { @@ -611,7 +570,7 @@ inline int getEdge(Graph* g, Edge_alloc* edge, uint64_t edge_length, char base) long long i = 0; long long nodeID; - for (i = 0; i < edge->length; i++) + for (i = 0; i < (long long)edge->length; i++) { if (edge->list[i].length == edge_length) { @@ -651,7 +610,7 @@ inline int get_insertion_Edges(Graph* g, Edge_alloc* edge, uint64_t edge_length, Edge_alloc* new_edge = edge; - for (i = 1; i < edge_length; i++) + for (i = 1; i < (long long)edge_length; i++) { nodeID = new_edge->list[edgeID].out_node; new_edge = &(g->g_nodes.list[nodeID].insertion_edges); @@ -662,7 +621,6 @@ inline int get_insertion_Edges(Graph* g, Edge_alloc* edge, uint64_t edge_length, } } /****************************may have bugs********************************/ - ///return edgeID; return return_edgeID; /****************************may have bugs********************************/ } @@ -673,7 +631,7 @@ inline int create_insertion_Edges(Graph* g, long long alignNodeID, uint64_t edge { long long i = 0; long long nodeID; - ///最后应该连回原节点 + ///should link back to the intial node ///long long backboneID = alignNodeID + 1; long long backboneID = alignNodeID; @@ -685,28 +643,29 @@ inline int create_insertion_Edges(Graph* g, long long alignNodeID, uint64_t edge nodeID = add_Node_Graph(g, bases[0]); - ///将新加入的节点通过insertion_edges接到alignNodeID上 + ///add the new node to alignNodeID by insertion_edges append_Edge_alloc(&(g->g_nodes.list[alignNodeID].insertion_edges), alignNodeID, nodeID, 1, edge_length); alignNodeID = nodeID; - for (i = 1; i < edge_length; i++) + for (i = 1; i < (long long)edge_length; i++) { nodeID = add_Node_Graph(g, bases[i]); - ///将新加入的节点通过insertion_edges接到alignNodeID上 + ///add the new node to alignNodeID by insertion_edges append_Edge_alloc(&(g->g_nodes.list[alignNodeID].insertion_edges), alignNodeID, nodeID, 1, edge_length - i); alignNodeID = nodeID; } append_Edge_alloc(&(g->g_nodes.list[alignNodeID].insertion_edges), alignNodeID, backboneID, 1, 0); + return 1; } inline void extract_path(Graph* backbone, int debug_node_in_backbone, int path_i, char* pre) { int step = G_Node(*backbone, debug_node_in_backbone).insertion_edges.list[path_i].length; - int string_i, preNode, j; + int string_i = 0, preNode = 0, j = 0; if(step != 0) { string_i = 0; @@ -723,65 +682,17 @@ inline void extract_path(Graph* backbone, int debug_node_in_backbone, int path_i } -inline void extract_path_debug(Graph* backbone, int debug_node_in_backbone, int path_i, char* pre) -{ - int step = G_Node(*backbone, debug_node_in_backbone).insertion_edges.list[path_i].length; - int string_i, preNode, preEdge, j; - if(step != 0) - { - string_i = 0; - preNode = G_Node(*backbone, debug_node_in_backbone).insertion_edges.list[path_i].out_node; - preEdge = G_Node(*backbone, debug_node_in_backbone).insertion_edges.list[path_i].length; - - for (j = 0; j < step; j++) - { - ///pre[string_i++] = G_Node(*backbone, preNode).base; - fprintf(stderr, "j: %d (%c%d), ", j, G_Node(*backbone, preNode).base, preEdge); - preEdge = G_Node(*backbone, preNode).insertion_edges.list[0].length; - preNode = G_Node(*backbone, preNode).insertion_edges.list[0].out_node; - } - } - - fprintf(stderr, "\n"); - - ///pre[string_i] = '\0'; -} - - -inline int getEdge_DEBUG(Graph* g, Edge_alloc* edge, uint64_t edge_length, char base) -{ - long long i = 0; - long long nodeID; - - for (i = 0; i < edge->length; i++) - { - ///fprintf(stderr, "************i:%d, edge->list[i].length: %d, edge_length: %d\n",i, edge->list[i].length, edge_length); - if (edge->list[i].length == edge_length) - { - nodeID = edge->list[i].out_node; - fprintf(stderr, "########i:%d, edge->list[i].length: %d, edge_length: %d, nodeID: %d, list[nodeID].base: %c, base: %c\n", - i, edge->list[i].length, edge_length, nodeID, g->g_nodes.list[nodeID].base, base); - - if(g->g_nodes.list[nodeID].base == base) - { - return i; - } - } - } - - return -1; -} inline int get_insertion_Edges_new(Graph* backbone, int debug_node_in_backbone, uint64_t edge_length, char* bases) { int path_i, j, step, preNode; - for (path_i = 0; path_i < G_Node(*backbone, debug_node_in_backbone).insertion_edges.length; path_i++) + for (path_i = 0; path_i < (long long)G_Node(*backbone, debug_node_in_backbone).insertion_edges.length; path_i++) { step = G_Node(*backbone, debug_node_in_backbone).insertion_edges.list[path_i].length; - if(step != edge_length) + if(step != (long long)edge_length) { continue; } @@ -793,8 +704,6 @@ inline int get_insertion_Edges_new(Graph* backbone, int debug_node_in_backbone, for (j = 0; j < step; j++) { - ///pre[string_i++] = G_Node(*backbone, preNode).base; - ///fprintf(stderr, "path_i: %d, ID: %d\n", path_i, G_Node(*backbone, preNode).ID); if(G_Node(*backbone, preNode).base != bases[j]) { break; @@ -815,53 +724,7 @@ inline int get_insertion_Edges_new(Graph* backbone, int debug_node_in_backbone, } -inline int get_insertion_Edges_debug(Graph* g, Edge_alloc* edge, uint64_t edge_length, char* bases) -{ - long long i = 0; - long long nodeID; - long long edgeID; - if (edge_length < 1) - { - return -1; - } - - - ///fprintf(stderr, "edge_length: %d, edge: %.*s\n", edge_length, edge_length, bases); - - - edgeID = getEdge_DEBUG(g, edge, edge_length, bases[0]); - fprintf(stderr, "i: %d, edgeID: %d, edge_length - i: %d\n", i, edgeID, edge_length); - - - - - long long return_edgeID = edgeID; - - if(edgeID == -1) - { - return -1; - } - - - Edge_alloc* new_edge = edge; - - for (i = 1; i < edge_length; i++) - { - nodeID = new_edge->list[edgeID].out_node; - new_edge = &(g->g_nodes.list[nodeID].insertion_edges); - edgeID = getEdge_DEBUG(g, new_edge, edge_length - i, bases[i]); - fprintf(stderr, "i: %d, edgeID: %d, edge_length - i: %d\n", i, edgeID, edge_length - i); - if(edgeID == -1) - { - return -1; - } - } - /****************************may have bugs********************************/ - ///return edgeID; - return return_edgeID; - /****************************may have bugs********************************/ -} inline void add_insertionEdge_weight(Graph* g, long long alignNodeID, char* insert, long long insert_length) { @@ -873,20 +736,16 @@ inline void add_insertionEdge_weight(Graph* g, long long alignNodeID, char* inse if (insert_length == 1) { edgeID = getEdge(g, edge, 1, insert[0]); - // if(edgeID != get_insertion_Edges_new(g, alignNodeID, insert_length, insert)) - // { - // fprintf(stderr, "error\n"); - // } if (edgeID != -1) { edge->list[edgeID].weight++; } - else ///不存在这么一条边 + else ///there is no such edge { nodeID = add_Node_Graph(g, insert[0]); append_Edge_alloc(edge, alignNodeID, nodeID, 1, 1); - ///将新加入的节点通过insertion_edges接回backbone上 - ///应该连回到原节点,而不是原节点的下一个节点 + ///add the new node to alignNodeID by insertion_edges + //should link to the initial node, instead of the next node of the initial node ///append_Edge_alloc(&(g->g_nodes.list[nodeID].insertion_edges), nodeID, alignNodeID + 1, 1, 0); append_Edge_alloc(&(g->g_nodes.list[nodeID].insertion_edges), nodeID, alignNodeID, 1, 0); } @@ -897,7 +756,7 @@ inline void add_insertionEdge_weight(Graph* g, long long alignNodeID, char* inse edgeID = get_insertion_Edges_new(g, alignNodeID, insert_length, insert); if (edgeID != -1) { - ///这条路均只有一个出度 + ///just one outdegree edge->list[edgeID].weight++; } else @@ -905,163 +764,8 @@ inline void add_insertionEdge_weight(Graph* g, long long alignNodeID, char* inse create_insertion_Edges(g, alignNodeID, insert_length, insert); } } - - // /******************************for homopolymer*************************/ - // long long i = 0; - // char hom; - // if (insert_length > 0) - // { - // hom = insert[0]; - // } - - // for (i = 0; i < insert_length; i++) - // { - // if(insert[i] != hom) - // { - // break; - // } - // } - - // ///if it is a homopolymer - // if(i == insert_length) - // { - // ///single base - // edgeID = getEdge(g, edge, 1, insert[0]); - // if (edgeID != -1) - // { - // ///这条路均只有一个出度 - // edge->list[edgeID].weight++; - // } - // else ///不存在这么一条边 - // { - // nodeID = add_Node_Graph(g, insert[0]); - // append_Edge_alloc(edge, alignNodeID, nodeID, 1, 1); - // ///将新加入的节点通过insertion_edges接回backbone上 - // ///应该连回到原节点,而不是原节点的下一个节点 - // ///append_Edge_alloc(&(g->g_nodes.list[nodeID].insertion_edges), nodeID, alignNodeID + 1, 1, 0); - // append_Edge_alloc(&(g->g_nodes.list[nodeID].insertion_edges), nodeID, alignNodeID, 1, 0); - // } - - // ///multiple bases - // for (i = 1; i < insert_length; i++) - // { - // edgeID = get_insertion_Edges(g, edge, i + 1, insert); - // if (edgeID != -1) - // { - // ///这条路均只有一个出度 - // edge->list[edgeID].weight++; - // } - // else - // { - // create_insertion_Edges(g, alignNodeID, i + 1, insert); - // } - // } - - // return; - // } - // /******************************for homopolymer*************************/ - - - - // if (insert_length == 1) - // { - // edgeID = getEdge(g, edge, 1, insert[0]); - // if (edgeID != -1) - // { - // ///这条路均只有一个出度 - // edge->list[edgeID].weight++; - // } - // else ///不存在这么一条边 - // { - // nodeID = add_Node_Graph(g, insert[0]); - // append_Edge_alloc(edge, alignNodeID, nodeID, 1, 1); - // ///将新加入的节点通过insertion_edges接回backbone上 - // ///应该连回到原节点,而不是原节点的下一个节点 - // ///append_Edge_alloc(&(g->g_nodes.list[nodeID].insertion_edges), nodeID, alignNodeID + 1, 1, 0); - // append_Edge_alloc(&(g->g_nodes.list[nodeID].insertion_edges), nodeID, alignNodeID, 1, 0); - // } - // } - // else if (insert_length == 2) - // { - // /*******************第0个字符********************* */ - // edgeID = getEdge(g, edge, 1, insert[0]); - // if (edgeID != -1) - // { - // ///这条路均只有一个出度 - // edge->list[edgeID].weight++; - // } - // else ///不存在这么一条边 - // { - // nodeID = add_Node_Graph(g, insert[0]); - // append_Edge_alloc(edge, alignNodeID, nodeID, 1, 1); - // ///将新加入的节点通过insertion_edges接回backbone上 - // ///应该连回到原节点,而不是原节点的下一个节点 - // ///append_Edge_alloc(&(g->g_nodes.list[nodeID].insertion_edges), nodeID, alignNodeID + 1, 1, 0); - // append_Edge_alloc(&(g->g_nodes.list[nodeID].insertion_edges), nodeID, alignNodeID, 1, 0); - // } - // /*******************第0个字符********************* */ - - // /*******************第1个字符********************* */ - // if (insert[1] != insert[0]) - // { - // edgeID = getEdge(g, edge, 1, insert[1]); - // if (edgeID != -1) - // { - // ///这条路均只有一个出度 - // edge->list[edgeID].weight++; - // } - // else ///不存在这么一条边 - // { - // nodeID = add_Node_Graph(g, insert[1]); - // append_Edge_alloc(edge, alignNodeID, nodeID, 1, 1); - // ///将新加入的节点通过insertion_edges接回backbone上 - // ///应该连回到原节点,而不是原节点的下一个节点 - // ///append_Edge_alloc(&(g->g_nodes.list[nodeID].insertion_edges), nodeID, alignNodeID + 1, 1, 0); - // append_Edge_alloc(&(g->g_nodes.list[nodeID].insertion_edges), nodeID, alignNodeID, 1, 0); - // } - // } - // /*******************第1个字符********************* */ - - // /**********************两个字符******************* */ - - // edgeID = get_insertion_Edges(g, edge, 2, insert); - // if (edgeID != -1) - // { - // ///这条路均只有一个出度 - // edge->list[edgeID].weight++; - // } - // else - // { - // create_insertion_Edges(g, alignNodeID, insert_length, insert); - // } - - // /**********************两个字符******************* */ - // } - // else if (insert_length > 2) - // { - // ////fprintf(stderr, "too long insertion\n"); - // /*************************大于2个字符************************** */ - - // edgeID = get_insertion_Edges(g, edge, insert_length, insert); - // if (edgeID != -1) - // { - // ///这条路均只有一个出度 - // edge->list[edgeID].weight++; - // } - // else - // { - // create_insertion_Edges(g, alignNodeID, insert_length, insert); - // } - // } - - - } -void addmatchedSeqToGraph_print(Graph* backbone, long long currentNodeID, char* x_string, long long x_length, - char* y_string, long long y_length, CIGAR* cigar, long long backbone_start, long long backbone_end); - - #endif \ No newline at end of file diff --git a/Process_Read.cpp b/Process_Read.cpp index 710752c..cc0a56f 100644 --- a/Process_Read.cpp +++ b/Process_Read.cpp @@ -21,30 +21,44 @@ pthread_cond_t i_readinputstallCond; pthread_mutex_t i_doneMutex; +uint8_t seq_nt6_table[256] = { + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 0, 5, 1, 5, 5, 5, 2, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 0, 5, 1, 5, 5, 5, 2, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 +}; + +char bit_t_seq_table[256][4] = {0}; +char bit_t_seq_table_rc[256][4] = {0}; +char s_H[5] = {'A', 'C', 'G', 'T', 'N'}; +char rc_Table[5] = {'T', 'G', 'C', 'A', 'N'}; + + void init_All_reads(All_reads* r) { r->index_size = READ_INIT_NUMBER; - /**********should remove**********/ - ///r->index = (uint64_t*)malloc(sizeof(uint64_t)*r->index_size); - ///r->index[0] = 0; - ///r->read = NULL; - /**********should remove**********/ r->read_length = (uint64_t*)malloc(sizeof(uint64_t)*r->index_size); r->read_sperate = NULL; - - r->N_site = NULL; r->total_reads_bases = 0; - - r->name_index_size = READ_INIT_NUMBER; r->name_index = (uint64_t*)malloc(sizeof(uint64_t)*r->name_index_size); r->name_index[0] = 0; r->name = NULL; r->total_name_length = 0; - r->total_reads = 0; - } void destory_All_reads(All_reads* r) @@ -60,24 +74,19 @@ void destory_All_reads(All_reads* r) } free(r->N_site); free(r->read_sperate); - - - - ///free(r->read); free(r->name); free(r->name_index); free(r->read_length); - } void write_All_reads(All_reads* r, char* read_file_name) { - fprintf(stdout, "Writing reads to disk ...... \n"); + fprintf(stderr, "Writing reads to disk... \n"); char* index_name = (char*)malloc(strlen(read_file_name)+15); sprintf(index_name, "%s.bin", read_file_name); FILE* fp = fopen(index_name, "w"); - fwrite(&adapterLen, sizeof(adapterLen), 1, fp); + fwrite(&asm_opt.adapterLen, sizeof(asm_opt.adapterLen), 1, fp); fwrite(&r->index_size, sizeof(r->index_size), 1, fp); fwrite(&r->name_index_size, sizeof(r->name_index_size), 1, fp); fwrite(&r->total_reads, sizeof(r->total_reads), 1, fp); @@ -90,12 +99,10 @@ void write_All_reads(All_reads* r, char* read_file_name) { if (r->N_site[i] != NULL) { - ///这个实际上是N的个数 + ///number of Ns fwrite(&r->N_site[i][0], sizeof(r->N_site[i][0]), 1, fp); if (r->N_site[i][0]) { - ///r->N_site[i]这实际是个长为r->N_site[i][0]+1 - ///这里从r->N_site[i] + 1写入了r->N_site[i][0]个元素 fwrite(r->N_site[i]+1, sizeof(r->N_site[i][0]), r->N_site[i][0], fp); } } @@ -108,36 +115,25 @@ void write_All_reads(All_reads* r, char* read_file_name) } - /**********should remove**********/ - ///fwrite(r->index, sizeof(uint64_t), r->index_size, fp); - /**********should remove**********/ fwrite(r->read_length, sizeof(uint64_t), r->total_reads, fp); - - /**********should remove**********/ - ///fwrite(r->read, sizeof(uint8_t), (r->total_reads_bases/4 + r->total_reads + 5), fp); - /**********should remove**********/ for (i = 0; i < r->total_reads; i++) { fwrite(r->read_sperate[i], sizeof(uint8_t), r->read_length[i]/4+1, fp); } - - fwrite(r->name, sizeof(char), r->total_name_length, fp); fwrite(r->name_index, sizeof(uint64_t), r->name_index_size, fp); - - free(index_name); fflush(fp); fclose(fp); - fprintf(stdout, "Reads has been written.\n"); + fprintf(stderr, "Reads has been written.\n"); } int load_All_reads(All_reads* r, char* read_file_name) { - fprintf(stdout, "Loading reads to disk ...... \n"); + fprintf(stderr, "Loading reads from disk... \n"); char* index_name = (char*)malloc(strlen(read_file_name)+15); sprintf(index_name, "%s.bin", read_file_name); FILE* fp = fopen(index_name, "r"); @@ -145,20 +141,20 @@ int load_All_reads(All_reads* r, char* read_file_name) { return 0; } - int local_adapterLen; - fread(&local_adapterLen, sizeof(local_adapterLen), 1, fp); - if(local_adapterLen != adapterLen) + int f_flag; + f_flag = fread(&local_adapterLen, sizeof(local_adapterLen), 1, fp); + if(local_adapterLen != asm_opt.adapterLen) { - fprintf(stdout, "the adapterLen of index is: %d, but the adapterLen set by user is: %d\n", - local_adapterLen, adapterLen); + fprintf(stderr, "the adapterLen of index is: %d, but the adapterLen set by user is: %d\n", + local_adapterLen, asm_opt.adapterLen); exit(1); } - fread(&r->index_size, sizeof(r->index_size), 1, fp); - fread(&r->name_index_size, sizeof(r->name_index_size), 1, fp); - fread(&r->total_reads, sizeof(r->total_reads), 1, fp); - fread(&r->total_reads_bases, sizeof(r->total_reads_bases), 1, fp); - fread(&r->total_name_length, sizeof(r->total_name_length), 1, fp); + f_flag += fread(&r->index_size, sizeof(r->index_size), 1, fp); + f_flag += fread(&r->name_index_size, sizeof(r->name_index_size), 1, fp); + f_flag += fread(&r->total_reads, sizeof(r->total_reads), 1, fp); + f_flag += fread(&r->total_reads_bases, sizeof(r->total_reads_bases), 1, fp); + f_flag += fread(&r->total_name_length, sizeof(r->total_name_length), 1, fp); uint64_t i = 0; uint64_t zero = 0; @@ -166,7 +162,7 @@ int load_All_reads(All_reads* r, char* read_file_name) for (i = 0; i < r->total_reads; i++) { - fread(&zero, sizeof(zero), 1, fp); + f_flag += fread(&zero, sizeof(zero), 1, fp); if (zero) { @@ -175,9 +171,7 @@ int load_All_reads(All_reads* r, char* read_file_name) r->N_site[i][0] = zero; if (r->N_site[i][0]) { - ///r->N_site[i]这实际是个长为r->N_site[i][0]+1 - ///这里从r->N_site[i] + 1写入了r->N_site[i][0]个元素 - fread(r->N_site[i]+1, sizeof(r->N_site[i][0]), r->N_site[i][0], fp); + f_flag += fread(r->N_site[i]+1, sizeof(r->N_site[i][0]), r->N_site[i][0], fp); } } else @@ -187,34 +181,25 @@ int load_All_reads(All_reads* r, char* read_file_name) } - - /**********should remove**********/ - ///r->index = (uint64_t*)malloc(sizeof(uint64_t)*r->index_size); - ///fread(r->index, sizeof(uint64_t), r->index_size, fp); - /**********should remove**********/ r->read_length = (uint64_t*)malloc(sizeof(uint64_t)*r->total_reads); - fread(r->read_length, sizeof(uint64_t), r->total_reads, fp); + f_flag += fread(r->read_length, sizeof(uint64_t), r->total_reads, fp); r->read_size = (uint64_t*)malloc(sizeof(uint64_t)*r->total_reads); memcpy (r->read_size, r->read_length, sizeof(uint64_t)*r->total_reads); - /**********should remove**********/ - ///r->read = (uint8_t*)malloc(sizeof(uint8_t)*(r->total_reads_bases/4 + r->total_reads + 5)); - ///fread(r->read, sizeof(uint8_t), (r->total_reads_bases/4 + r->total_reads + 5), fp); - /**********should remove**********/ r->read_sperate = (uint8_t**)malloc(sizeof(uint8_t*)*r->total_reads); for (i = 0; i < r->total_reads; i++) { r->read_sperate[i] = (uint8_t*)malloc(sizeof(uint8_t)*(r->read_length[i]/4+1)); - fread(r->read_sperate[i], sizeof(uint8_t), r->read_length[i]/4+1, fp); + f_flag += fread(r->read_sperate[i], sizeof(uint8_t), r->read_length[i]/4+1, fp); } r->name = (char*)malloc(sizeof(char)*r->total_name_length); - fread(r->name, sizeof(char), r->total_name_length, fp); + f_flag += fread(r->name, sizeof(char), r->total_name_length, fp); r->name_index = (uint64_t*)malloc(sizeof(uint64_t)*r->name_index_size); - fread(r->name_index, sizeof(uint64_t), r->name_index_size, fp); + f_flag += fread(r->name_index, sizeof(uint64_t), r->name_index_size, fp); r->cigars = (Compressed_Cigar_record*)malloc(sizeof(Compressed_Cigar_record)*r->total_reads); @@ -234,13 +219,9 @@ int load_All_reads(All_reads* r, char* read_file_name) init_ma_hit_t_alloc(&(r->reverse_paf[i])); } - - - - free(index_name); fclose(fp); - fprintf(stdout, "Reads has been loaded.\n"); + fprintf(stderr, "Reads has been loaded.\n"); return 1; } @@ -253,27 +234,17 @@ inline void insert_read(All_reads* r, kstring_t* read, kstring_t* name) r->total_reads_bases = r->total_reads_bases + read->l; r->total_name_length = r->total_name_length + name->l; - ///必须要+1 + ///must +1 if (r->index_size < r->total_reads + 2) { r->index_size = r->index_size * 2 + 2; - /**********should remove**********/ - ///r->index = (uint64_t*)realloc(r->index,sizeof(uint64_t)*(r->index_size)); - /**********should remove**********/ r->read_length = (uint64_t*)realloc(r->read_length,sizeof(uint64_t)*(r->index_size)); - r->name_index_size = r->name_index_size * 2 + 2; r->name_index = (uint64_t*)realloc(r->name_index,sizeof(uint64_t)*(r->name_index_size)); } - /**********should remove**********/ - ///r->index[r->total_reads] = r->index[r->total_reads-1] + read->l; - /**********should remove**********/ + r->read_length[r->total_reads - 1] = read->l; - - - //r->index[r->total_reads] = r->index[r->total_reads-1] + read->l/4 + 1; r->name_index[r->total_reads] = r->name_index[r->total_reads-1] + name->l; - } void malloc_All_reads(All_reads* r) @@ -282,13 +253,9 @@ void malloc_All_reads(All_reads* r) r->read_size = (uint64_t*)malloc(sizeof(uint64_t)*r->total_reads); memcpy (r->read_size, r->read_length, sizeof(uint64_t)*r->total_reads); - ///必须加r->total_reads - /**********should remove**********/ - ///r->read = (uint8_t*)malloc(sizeof(uint8_t)*(r->total_reads_bases/4 + r->total_reads + 5)); - /**********should remove**********/ r->read_sperate = (uint8_t**)malloc(sizeof(uint8_t*)*r->total_reads); long long i = 0; - for (i = 0; i < r->total_reads; i++) + for (i = 0; i < (long long)r->total_reads; i++) { r->read_sperate[i] = (uint8_t*)malloc(sizeof(uint8_t)*(r->read_length[i]/4+1)); } @@ -297,7 +264,7 @@ void malloc_All_reads(All_reads* r) r->second_round_cigar = (Compressed_Cigar_record*)malloc(sizeof(Compressed_Cigar_record)*r->total_reads); r->paf = (ma_hit_t_alloc*)malloc(sizeof(ma_hit_t_alloc)*r->total_reads); r->reverse_paf = (ma_hit_t_alloc*)malloc(sizeof(ma_hit_t_alloc)*r->total_reads); - for (i = 0; i < r->total_reads; i++) + for (i = 0; i < (long long)r->total_reads; i++) { r->second_round_cigar[i].size = r->cigars[i].size = 0; r->second_round_cigar[i].length = r->cigars[i].length = 0; @@ -310,15 +277,6 @@ void malloc_All_reads(All_reads* r) init_ma_hit_t_alloc(&(r->reverse_paf[i])); } - - - - - - - - - r->name = (char*)malloc(sizeof(char)*r->total_name_length); r->N_site = (uint64_t**)calloc(r->total_reads, sizeof(uint64_t*)); @@ -422,13 +380,13 @@ void recover_UC_Read_sub_region_begin_end if (R_INF->N_site[ID]) { - for (i = 1; i <= R_INF->N_site[ID][0]; i++) + for (i = 1; i <= (long long)R_INF->N_site[ID][0]; i++) { - if (R_INF->N_site[ID][i] >= start_pos && R_INF->N_site[ID][i] <= end_pos) + if ((long long)R_INF->N_site[ID][i] >= start_pos && (long long)R_INF->N_site[ID][i] <= end_pos) { r[R_INF->N_site[ID][i] - start_pos] = 'N'; } - else if(R_INF->N_site[ID][i] > end_pos) + else if((long long)R_INF->N_site[ID][i] > end_pos) { break; } @@ -468,14 +426,14 @@ void recover_UC_Read_sub_region_begin_end { long long offset = readLen - start_pos - 1; - for (i = 1; i <= R_INF->N_site[ID][0]; i++) + for (i = 1; i <= (long long)R_INF->N_site[ID][0]; i++) { - if (R_INF->N_site[ID][i] >= end_pos && R_INF->N_site[ID][i] <= start_pos) + if ((long long)R_INF->N_site[ID][i] >= end_pos && (long long)R_INF->N_site[ID][i] <= start_pos) { r[readLen - R_INF->N_site[ID][i] - 1 - offset] = 'N'; } - else if(R_INF->N_site[ID][i] > start_pos) + else if((long long)R_INF->N_site[ID][i] > start_pos) { break; } @@ -529,13 +487,13 @@ void recover_UC_Read_sub_region(char* r, long long start_pos, long long length, if (R_INF->N_site[ID]) { - for (i = 1; i <= R_INF->N_site[ID][0]; i++) + for (i = 1; i <= (long long)R_INF->N_site[ID][0]; i++) { - if (R_INF->N_site[ID][i] >= start_pos && R_INF->N_site[ID][i] <= end_pos) + if ((long long)R_INF->N_site[ID][i] >= start_pos && (long long)R_INF->N_site[ID][i] <= end_pos) { r[R_INF->N_site[ID][i] - start_pos] = 'N'; } - else if(R_INF->N_site[ID][i] > end_pos) + else if((long long)R_INF->N_site[ID][i] > end_pos) { break; } @@ -575,14 +533,14 @@ void recover_UC_Read_sub_region(char* r, long long start_pos, long long length, { long long offset = readLen - start_pos - 1; - for (i = 1; i <= R_INF->N_site[ID][0]; i++) + for (i = 1; i <= (long long)R_INF->N_site[ID][0]; i++) { - if (R_INF->N_site[ID][i] >= end_pos && R_INF->N_site[ID][i] <= start_pos) + if ((long long)R_INF->N_site[ID][i] >= end_pos && (long long)R_INF->N_site[ID][i] <= start_pos) { r[readLen - R_INF->N_site[ID][i] - 1 - offset] = 'N'; } - else if(R_INF->N_site[ID][i] > start_pos) + else if((long long)R_INF->N_site[ID][i] > start_pos) { break; } @@ -609,7 +567,7 @@ void recover_UC_Read(UC_Read* r, All_reads* R_INF, uint64_t ID) uint64_t i = 0; - while (i < r->length) + while ((long long)i < r->length) { memcpy(r->seq+i, bit_t_seq_table[src[i>>2]], 4); i = i + 4; @@ -660,7 +618,7 @@ void recover_UC_Read_RC(UC_Read* r, All_reads* R_INF, uint64_t ID) if (R_INF->N_site[ID]) { - for (i = 1; i <= R_INF->N_site[ID][0]; i++) + for (i = 1; i <= (long long)R_INF->N_site[ID][0]; i++) { r->seq[r->length - R_INF->N_site[ID][i] - 1] = 'N'; } @@ -670,7 +628,7 @@ void recover_UC_Read_RC(UC_Read* r, All_reads* R_INF, uint64_t ID) -#define COMPRESS_BASE {c = seq_nt6_table[src[i]];\ +#define COMPRESS_BASE {c = seq_nt6_table[(uint8_t)src[i]];\ if (c >= 4)\ {\ c = 0;\ @@ -699,59 +657,31 @@ void compress_base(uint8_t* dest, char* src, uint64_t src_l, uint64_t** N_site_l uint64_t dest_i = 0; uint8_t tmp = 0; uint8_t c = 0; - /** - fprintf(stderr, "src_l: %lld\n", src_l); - fflush(stderr); - **/ - + while (i + 4 <= src_l) { - // fprintf(stderr, "0 i: %d, dest_i: %d, src_l: %d\n", - // i, dest_i, src_l); - // fflush(stderr); - tmp = 0; COMPRESS_BASE; tmp = tmp | (c<<6); - // fprintf(stderr, "*******1******1 i: %d, tmp: %d, c: %d\n", - // i, tmp, c); - // fflush(stderr); - COMPRESS_BASE; tmp = tmp | (c<<4); - // fprintf(stderr, "*******2******1 i: %d, tmp: %d, c: %d\n", - // i, tmp, c); - // fflush(stderr); - COMPRESS_BASE; tmp = tmp | (c<<2); - // fprintf(stderr, "*******3******1 i: %d, tmp: %d, c: %d\n", - // i, tmp, c); - // fflush(stderr); - COMPRESS_BASE; tmp = tmp | c; - // fprintf(stderr, "*******4******1 i: %d, tmp: %d, c: %d\n", - // i, tmp, c); - // fflush(stderr); - dest[dest_i] = tmp; - // fprintf(stderr, "2 i: %d, dest_i: %d, src_l: %d\n", - // i, dest_i, src_l); - // fflush(stderr); - dest_i++; } - //最多还剩3个字符 + //at most 3 bases here uint64_t shift = 6; if (i < src_l) { @@ -773,6 +703,11 @@ void compress_base(uint8_t* dest, char* src, uint64_t src_l, uint64_t** N_site_l void init_kseq(char* file) { fp = gzopen(file, "r"); + if (fp == 0) + { + fprintf(stderr, "[ERROR] Cannot find the input file: %s\n", file); + exit(0); + } seq = kseq_init(fp); } @@ -805,14 +740,14 @@ int get_read(kseq_t *s, int adapterLen) if(adapterLen > 0) { - if(s->seq.l <= adapterLen*2) + if((int)s->seq.l <= adapterLen*2) { s->seq.l = 0; } else { long long i; - for (i = 0; i < (s->seq.l - adapterLen*2); i++) + for (i = 0; i < ((int)s->seq.l - adapterLen*2); i++) { s->seq.s[i] = s->seq.s[i + adapterLen]; } @@ -864,7 +799,6 @@ void init_R_buffer(int thread_num) void destory_R_buffer_block(R_buffer_block* curr_sub_block) { kseq_destroy(curr_sub_block->read); - ///free(curr_sub_block->read); } @@ -975,7 +909,6 @@ void* input_reads_muti_threads(void* arg) total_reads = 0; - int i = 0; int file_flag = 1; R_buffer_block tmp_buf; @@ -986,10 +919,7 @@ void* input_reads_muti_threads(void* arg) while (1) { - - - - load_read_block(&tmp_buf, RDB.block_inner_size, &file_flag, is_insert, adapterLen); + load_read_block(&tmp_buf, RDB.block_inner_size, &file_flag, is_insert, asm_opt.adapterLen); if (file_flag == 0) { @@ -1020,13 +950,11 @@ void* input_reads_muti_threads(void* arg) destory_R_buffer_block(&tmp_buf); - fprintf(stdout, "total_reads: %llu\n",total_reads); - ///fprintf(stdout, "R_INF.total_reads: %llu\n",R_INF.total_reads); - ///fprintf(stdout, "R_INF.index[R_INF.total_reads]: %llu\n",R_INF.index[R_INF.total_reads]); - fprintf(stdout, "R_INF.total_reads_bases: %llu\n",R_INF.total_reads_bases); - ///fprintf(stdout, "R_INF.name_index[R_INF.total_reads]: %llu\n",R_INF.name_index[R_INF.total_reads]); - fprintf(stdout, "R_INF.total_name_length: %llu\n",R_INF.total_name_length); + fprintf(stderr, "Reads #: %lu\n",total_reads); + fprintf(stderr, "Bases #: %lu\n",R_INF.total_reads_bases); + + return NULL; } @@ -1078,7 +1006,7 @@ int get_reads_mul_thread(R_buffer_block* curr_sub_block) void reverse_complement(char* pattern, uint64_t length) { - int i = 0; + uint64_t i = 0; uint64_t end = length / 2; char k; uint64_t index; @@ -1099,44 +1027,3 @@ void reverse_complement(char* pattern, uint64_t length) } -void Counting_block() -{ - - long long read_number = 0; - int i = 0; - int file_flag = 1; - - R_buffer_block tmp_buf; - - init_R_buffer_block(&tmp_buf); - - while (1) - { - - - load_read_block(&tmp_buf, RDB.block_inner_size, - &file_flag, 0, adapterLen); - - - if (file_flag == 0) - { - break; - } - - for (i = 0; i < tmp_buf.num; i++) - { - fprintf(stderr,"@%s\n", tmp_buf.read[i].name.s); - fprintf(stderr,"%s\n",tmp_buf.read[i].seq.s); - fprintf(stderr,"+\n"); - fprintf(stderr,"%s\n",tmp_buf.read[i].qual.s); - - read_number++; - } - - } - - fprintf(stdout, "read_number: %lld\n",read_number); - - - -} \ No newline at end of file diff --git a/Process_Read.h b/Process_Read.h index a9426ec..5e57c5e 100644 --- a/Process_Read.h +++ b/Process_Read.h @@ -30,29 +30,13 @@ KSEQ_INIT(gzFile, gzread) -static uint8_t seq_nt6_table[256] = { - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 0, 5, 1, 5, 5, 5, 2, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 0, 5, 1, 5, 5, 5, 2, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 -}; +extern uint8_t seq_nt6_table[256]; +extern char bit_t_seq_table[256][4]; +extern char bit_t_seq_table_rc[256][4]; +extern char s_H[5]; +extern char rc_Table[5]; + -static char bit_t_seq_table[256][4] = {0}; -static char bit_t_seq_table_rc[256][4] = {0}; -static char s_H[5] = {'A', 'C', 'G', 'T', 'N'}; -static char rc_Table[5] = {'T', 'G', 'C', 'A', 'N'}; #define RC_CHAR(x) rc_Table[seq_nt6_table[(uint8_t)x]] @@ -204,7 +188,6 @@ void write_All_reads(All_reads* r, char* read_file_name); int load_All_reads(All_reads* r, char* read_file_name); void destory_All_reads(All_reads* r); -void Counting_block(); void destory_R_buffer_block(R_buffer_block* curr_sub_block); void destory_R_buffer(); void clear_R_buffer(); diff --git a/debug_assembly.sh b/debug_assembly.sh deleted file mode 100644 index 506f1a8..0000000 --- a/debug_assembly.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/bash -# My first script -if [ $# -eq 1 ] -then -echo "../minimap2/minimap2 -ax asm20 -t 32 ../minimap2/Homo_sapiens.GRCh38.dna.primary_assembly.fa.gz "$1" >"$1".sam" -../minimap2/minimap2 -ax asm20 -t 32 ../minimap2/Homo_sapiens.GRCh38.dna.primary_assembly.fa $1 >$1.sam -echo "samtools view -Sb "$1".sam >"$1".bam" -samtools view -Sb $1.sam >$1.bam -echo "samtools sort "$1".bam sort_"$1 -samtools sort $1.bam sort_$1 -echo "rm sort_"$1".bam.bai" -rm sort_$1.bam.bai -echo "samtools index sort_"$1".bam" -samtools index sort_$1.bam -else -echo "debug_assembly.sh intput.fa" -fi diff --git a/edlib.cpp b/edlib.cpp deleted file mode 100644 index 1d75a84..0000000 --- a/edlib.cpp +++ /dev/null @@ -1,1461 +0,0 @@ -#include "edlib.h" - -#include -#include -#include -#include -#include -#include - -using namespace std; - -typedef uint64_t Word; -static const int WORD_SIZE = sizeof(Word) * 8; // Size of Word in bits -static const Word WORD_1 = (Word)1; -static const Word HIGH_BIT_MASK = WORD_1 << (WORD_SIZE - 1); // 100..00 -static const int MAX_UCHAR = 255; - -// Data needed to find alignment. -struct AlignmentData { - Word* Ps; - Word* Ms; - int* scores; - int* firstBlocks; - int* lastBlocks; - - AlignmentData(int maxNumBlocks, int targetLength) { - // We build a complete table and mark first and last block for each column - // (because algorithm is banded so only part of each columns is used). - // TODO: do not build a whole table, but just enough blocks for each column. - Ps = new Word[maxNumBlocks * targetLength]; - Ms = new Word[maxNumBlocks * targetLength]; - scores = new int[maxNumBlocks * targetLength]; - firstBlocks = new int[targetLength]; - lastBlocks = new int[targetLength]; - } - - ~AlignmentData() { - delete[] Ps; - delete[] Ms; - delete[] scores; - delete[] firstBlocks; - delete[] lastBlocks; - } -}; - -struct Block { - Word P; // Pvin - Word M; // Mvin - int score; // score of last cell in block; - - Block() {} - Block(Word P, Word M, int score) :P(P), M(M), score(score) {} -}; - - -/** - * Defines equality relation on alphabet characters. - * By default each character is always equal only to itself, but you can also provide additional equalities. - */ -class EqualityDefinition { -private: - bool matrix[MAX_UCHAR + 1][MAX_UCHAR + 1]; -public: - EqualityDefinition(const string& alphabet, - const EdlibEqualityPair* additionalEqualities = NULL, - const int additionalEqualitiesLength = 0) { - for (int i = 0; i < (int) alphabet.size(); i++) { - for (int j = 0; j < (int) alphabet.size(); j++) { - matrix[i][j] = (i == j); - } - } - if (additionalEqualities != NULL) { - for (int i = 0; i < additionalEqualitiesLength; i++) { - size_t firstTransformed = alphabet.find(additionalEqualities[i].first); - size_t secondTransformed = alphabet.find(additionalEqualities[i].second); - if (firstTransformed != string::npos && secondTransformed != string::npos) { - matrix[firstTransformed][secondTransformed] = matrix[secondTransformed][firstTransformed] = true; - } - } - } - } - - /** - * @param a Element from transformed sequence. - * @param b Element from transformed sequence. - * @return True if a and b are defined as equal, false otherwise. - */ - bool areEqual(unsigned char a, unsigned char b) const { - return matrix[a][b]; - } -}; - -static int myersCalcEditDistanceSemiGlobal(const Word* Peq, int W, int maxNumBlocks, - int queryLength, - const unsigned char* target, int targetLength, - int k, EdlibAlignMode mode, - int* bestScore_, int** positions_, int* numPositions_); - -static int myersCalcEditDistanceNW(const Word* Peq, int W, int maxNumBlocks, - int queryLength, - const unsigned char* target, int targetLength, - int k, int* bestScore_, - int* position_, bool findAlignment, - AlignmentData** alignData, int targetStopPosition); - - -static int obtainAlignment( - const unsigned char* query, const unsigned char* rQuery, int queryLength, - const unsigned char* target, const unsigned char* rTarget, int targetLength, - const EqualityDefinition& equalityDefinition, int alphabetLength, int bestScore, - unsigned char** alignment, int* alignmentLength); - -static int obtainAlignmentHirschberg( - const unsigned char* query, const unsigned char* rQuery, int queryLength, - const unsigned char* target, const unsigned char* rTarget, int targetLength, - const EqualityDefinition& equalityDefinition, int alphabetLength, int bestScore, - unsigned char** alignment, int* alignmentLength); - -static int obtainAlignmentTraceback(int queryLength, int targetLength, - int bestScore, const AlignmentData* alignData, - unsigned char** alignment, int* alignmentLength); - -static string transformSequences(const char* queryOriginal, int queryLength, - const char* targetOriginal, int targetLength, - unsigned char** queryTransformed, - unsigned char** targetTransformed); - -static inline int ceilDiv(int x, int y); - -static inline unsigned char* createReverseCopy(const unsigned char* seq, int length); - -static inline Word* buildPeq(const int alphabetLength, - const unsigned char* query, - const int queryLength, - const EqualityDefinition& equalityDefinition); - - -/** - * Main edlib method. - */ -extern "C" EdlibAlignResult edlibAlign(const char* const queryOriginal, const int queryLength, - const char* const targetOriginal, const int targetLength, - const EdlibAlignConfig config) { - EdlibAlignResult result; - result.status = EDLIB_STATUS_OK; - result.editDistance = -1; - result.endLocations = result.startLocations = NULL; - result.numLocations = 0; - result.alignment = NULL; - result.alignmentLength = 0; - result.alphabetLength = 0; - - /*------------ TRANSFORM SEQUENCES AND RECOGNIZE ALPHABET -----------*/ - unsigned char* query, * target; - string alphabet = transformSequences(queryOriginal, queryLength, targetOriginal, targetLength, - &query, &target); - result.alphabetLength = (int) alphabet.size(); - /*-------------------------------------------------------*/ - - /*--------------------- INITIALIZATION ------------------*/ - int maxNumBlocks = ceilDiv(queryLength, WORD_SIZE); // bmax in Myers - int W = maxNumBlocks * WORD_SIZE - queryLength; // number of redundant cells in last level blocks - EqualityDefinition equalityDefinition(alphabet, config.additionalEqualities, config.additionalEqualitiesLength); - Word* Peq = buildPeq((int) alphabet.size(), query, queryLength, equalityDefinition); - /*-------------------------------------------------------*/ - - /*------------------ MAIN CALCULATION -------------------*/ - // TODO: Store alignment data only after k is determined? That could make things faster. - int positionNW; // Used only when mode is NW. - AlignmentData* alignData = NULL; - bool dynamicK = false; - int k = config.k; - if (k < 0) { // If valid k is not given, auto-adjust k until solution is found. - dynamicK = true; - k = WORD_SIZE; // Gives better results than smaller k. - } - - do { - if (config.mode == EDLIB_MODE_HW || config.mode == EDLIB_MODE_SHW) { - myersCalcEditDistanceSemiGlobal(Peq, W, maxNumBlocks, - queryLength, target, targetLength, - k, config.mode, &(result.editDistance), - &(result.endLocations), &(result.numLocations)); - } else { // mode == EDLIB_MODE_NW - myersCalcEditDistanceNW(Peq, W, maxNumBlocks, - queryLength, target, targetLength, - k, &(result.editDistance), &positionNW, - false, &alignData, -1); - } - k *= 2; - } while(dynamicK && result.editDistance == -1); - - if (result.editDistance >= 0) { // If there is solution. - // If NW mode, set end location explicitly. - if (config.mode == EDLIB_MODE_NW) { - result.endLocations = (int *) malloc(sizeof(int) * 1); - result.endLocations[0] = targetLength - 1; - result.numLocations = 1; - } - - // Find starting locations. - if (config.task == EDLIB_TASK_LOC || config.task == EDLIB_TASK_PATH) { - result.startLocations = (int*) malloc(result.numLocations * sizeof(int)); - if (config.mode == EDLIB_MODE_HW) { // If HW, I need to calculate start locations. - const unsigned char* rTarget = createReverseCopy(target, targetLength); - const unsigned char* rQuery = createReverseCopy(query, queryLength); - // Peq for reversed query. - Word* rPeq = buildPeq((int) alphabet.size(), rQuery, queryLength, equalityDefinition); - for (int i = 0; i < result.numLocations; i++) { - int endLocation = result.endLocations[i]; - if (endLocation == -1) { - // NOTE: Sometimes one of optimal solutions is that query starts before target, like this: - // AAGG <- target - // CCTT <- query - // It will never be only optimal solution and it does not happen often, however it is - // possible and in that case end location will be -1. What should we do with that? - // Should we just skip reporting such end location, although it is a solution? - // If we do report it, what is the start location? -4? -1? Nothing? - // TODO: Figure this out. This has to do in general with how we think about start - // and end locations. - // Also, we have alignment later relying on this locations to limit the space of it's - // search -> how can it do it right if these locations are negative or incorrect? - result.startLocations[i] = 0; // I put 0 for now, but it does not make much sense. - } else { - int bestScoreSHW, numPositionsSHW; - int* positionsSHW; - myersCalcEditDistanceSemiGlobal( - rPeq, W, maxNumBlocks, - queryLength, rTarget + targetLength - endLocation - 1, endLocation + 1, - result.editDistance, EDLIB_MODE_SHW, - &bestScoreSHW, &positionsSHW, &numPositionsSHW); - // Taking last location as start ensures that alignment will not start with insertions - // if it can start with mismatches instead. - result.startLocations[i] = endLocation - positionsSHW[numPositionsSHW - 1]; - free(positionsSHW); - } - } - delete[] rTarget; - delete[] rQuery; - delete[] rPeq; - } else { // If mode is SHW or NW - for (int i = 0; i < result.numLocations; i++) { - result.startLocations[i] = 0; - } - } - } - - // Find alignment -> all comes down to finding alignment for NW. - // Currently we return alignment only for first pair of locations. - if (config.task == EDLIB_TASK_PATH) { - int alnStartLocation = result.startLocations[0]; - int alnEndLocation = result.endLocations[0]; - const unsigned char* alnTarget = target + alnStartLocation; - const int alnTargetLength = alnEndLocation - alnStartLocation + 1; - const unsigned char* rAlnTarget = createReverseCopy(alnTarget, alnTargetLength); - const unsigned char* rQuery = createReverseCopy(query, queryLength); - obtainAlignment(query, rQuery, queryLength, - alnTarget, rAlnTarget, alnTargetLength, - equalityDefinition, (int) alphabet.size(), result.editDistance, - &(result.alignment), &(result.alignmentLength)); - delete[] rAlnTarget; - delete[] rQuery; - } - } - /*-------------------------------------------------------*/ - - //--- Free memory ---// - delete[] Peq; - free(query); - free(target); - if (alignData) delete alignData; - //-------------------// - - return result; -} - -extern "C" char* edlibAlignmentToCigar(const unsigned char* const alignment, const int alignmentLength, - const EdlibCigarFormat cigarFormat) { - if (cigarFormat != EDLIB_CIGAR_EXTENDED && cigarFormat != EDLIB_CIGAR_STANDARD) { - return 0; - } - - // Maps move code from alignment to char in cigar. - // 0 1 2 3 - char moveCodeToChar[] = {'=', 'I', 'D', 'X'}; - if (cigarFormat == EDLIB_CIGAR_STANDARD) { - moveCodeToChar[0] = moveCodeToChar[3] = 'M'; - } - - vector* cigar = new vector(); - char lastMove = 0; // Char of last move. 0 if there was no previous move. - int numOfSameMoves = 0; - for (int i = 0; i <= alignmentLength; i++) { - // if new sequence of same moves started - if (i == alignmentLength || (moveCodeToChar[alignment[i]] != lastMove && lastMove != 0)) { - // Write number of moves to cigar string. - int numDigits = 0; - for (; numOfSameMoves; numOfSameMoves /= 10) { - cigar->push_back('0' + numOfSameMoves % 10); - numDigits++; - } - reverse(cigar->end() - numDigits, cigar->end()); - // Write code of move to cigar string. - cigar->push_back(lastMove); - // If not at the end, start new sequence of moves. - if (i < alignmentLength) { - // Check if alignment has valid values. - if (alignment[i] > 3) { - delete cigar; - return 0; - } - numOfSameMoves = 0; - } - } - if (i < alignmentLength) { - lastMove = moveCodeToChar[alignment[i]]; - numOfSameMoves++; - } - } - cigar->push_back(0); // Null character termination. - char* cigar_ = (char*) malloc(cigar->size() * sizeof(char)); - memcpy(cigar_, &(*cigar)[0], cigar->size() * sizeof(char)); - delete cigar; - - return cigar_; -} - -/** - * Build Peq table for given query and alphabet. - * Peq is table of dimensions alphabetLength+1 x maxNumBlocks. - * Bit i of Peq[s * maxNumBlocks + b] is 1 if i-th symbol from block b of query equals symbol s, otherwise it is 0. - * NOTICE: free returned array with delete[]! - */ -static inline Word* buildPeq(const int alphabetLength, - const unsigned char* const query, - const int queryLength, - const EqualityDefinition& equalityDefinition) { - int maxNumBlocks = ceilDiv(queryLength, WORD_SIZE); - // table of dimensions alphabetLength+1 x maxNumBlocks. Last symbol is wildcard. - Word* Peq = new Word[(alphabetLength + 1) * maxNumBlocks]; - - // Build Peq (1 is match, 0 is mismatch). NOTE: last column is wildcard(symbol that matches anything) with just 1s - for (unsigned char symbol = 0; symbol <= alphabetLength; symbol++) { - for (int b = 0; b < maxNumBlocks; b++) { - if (symbol < alphabetLength) { - Peq[symbol * maxNumBlocks + b] = 0; - for (int r = (b+1) * WORD_SIZE - 1; r >= b * WORD_SIZE; r--) { - Peq[symbol * maxNumBlocks + b] <<= 1; - // NOTE: We pretend like query is padded at the end with W wildcard symbols - if (r >= queryLength || equalityDefinition.areEqual(query[r], symbol)) - Peq[symbol * maxNumBlocks + b] += 1; - } - } else { // Last symbol is wildcard, so it is all 1s - Peq[symbol * maxNumBlocks + b] = (Word)-1; - } - } - } - - return Peq; -} - - -/** - * Returns new sequence that is reverse of given sequence. - * Free returned array with delete[]. - */ -static inline unsigned char* createReverseCopy(const unsigned char* const seq, const int length) { - unsigned char* rSeq = new unsigned char[length]; - for (int i = 0; i < length; i++) { - rSeq[i] = seq[length - i - 1]; - } - return rSeq; -} - -/** - * Corresponds to Advance_Block function from Myers. - * Calculates one word(block), which is part of a column. - * Highest bit of word (one most to the left) is most bottom cell of block from column. - * Pv[i] and Mv[i] define vin of cell[i]: vin = cell[i] - cell[i-1]. - * @param [in] Pv Bitset, Pv[i] == 1 if vin is +1, otherwise Pv[i] == 0. - * @param [in] Mv Bitset, Mv[i] == 1 if vin is -1, otherwise Mv[i] == 0. - * @param [in] Eq Bitset, Eq[i] == 1 if match, 0 if mismatch. - * @param [in] hin Will be +1, 0 or -1. - * @param [out] PvOut Bitset, PvOut[i] == 1 if vout is +1, otherwise PvOut[i] == 0. - * @param [out] MvOut Bitset, MvOut[i] == 1 if vout is -1, otherwise MvOut[i] == 0. - * @param [out] hout Will be +1, 0 or -1. - */ -static inline int calculateBlock(Word Pv, Word Mv, Word Eq, const int hin, - Word &PvOut, Word &MvOut) { - // hin can be 1, -1 or 0. - // 1 -> 00...01 - // 0 -> 00...00 - // -1 -> 11...11 (2-complement) - - Word hinIsNeg = (Word)(hin >> 2) & WORD_1; // 00...001 if hin is -1, 00...000 if 0 or 1 - - Word Xv = Eq | Mv; - // This is instruction below written using 'if': if (hin < 0) Eq |= (Word)1; - Eq |= hinIsNeg; - Word Xh = (((Eq & Pv) + Pv) ^ Pv) | Eq; - - Word Ph = Mv | ~(Xh | Pv); - Word Mh = Pv & Xh; - - int hout = 0; - // This is instruction below written using 'if': if (Ph & HIGH_BIT_MASK) hout = 1; - hout = (Ph & HIGH_BIT_MASK) >> (WORD_SIZE - 1); - // This is instruction below written using 'if': if (Mh & HIGH_BIT_MASK) hout = -1; - hout -= (Mh & HIGH_BIT_MASK) >> (WORD_SIZE - 1); - - Ph <<= 1; - Mh <<= 1; - - // This is instruction below written using 'if': if (hin < 0) Mh |= (Word)1; - Mh |= hinIsNeg; - // This is instruction below written using 'if': if (hin > 0) Ph |= (Word)1; - Ph |= (Word)((hin + 1) >> 1); - - PvOut = Mh | ~(Xv | Ph); - MvOut = Ph & Xv; - - return hout; -} - -/** - * Does ceiling division x / y. - * Note: x and y must be non-negative and x + y must not overflow. - */ -static inline int ceilDiv(const int x, const int y) { - return x % y ? x / y + 1 : x / y; -} - -static inline int min(const int x, const int y) { - return x < y ? x : y; -} - -static inline int max(const int x, const int y) { - return x > y ? x : y; -} - - -/** - * @param [in] block - * @return Values of cells in block, starting with bottom cell in block. - */ -static inline vector getBlockCellValues(const Block block) { - vector scores(WORD_SIZE); - int score = block.score; - Word mask = HIGH_BIT_MASK; - for (int i = 0; i < WORD_SIZE - 1; i++) { - scores[i] = score; - if (block.P & mask) score--; - if (block.M & mask) score++; - mask >>= 1; - } - scores[WORD_SIZE - 1] = score; - return scores; -} - -/** - * Writes values of cells in block into given array, starting with first/top cell. - * @param [in] block - * @param [out] dest Array into which cell values are written. Must have size of at least WORD_SIZE. - */ -static inline void readBlock(const Block block, int* const dest) { - int score = block.score; - Word mask = HIGH_BIT_MASK; - for (int i = 0; i < WORD_SIZE - 1; i++) { - dest[WORD_SIZE - 1 - i] = score; - if (block.P & mask) score--; - if (block.M & mask) score++; - mask >>= 1; - } - dest[0] = score; -} - -/** - * Writes values of cells in block into given array, starting with last/bottom cell. - * @param [in] block - * @param [out] dest Array into which cell values are written. Must have size of at least WORD_SIZE. - */ -static inline void readBlockReverse(const Block block, int* const dest) { - int score = block.score; - Word mask = HIGH_BIT_MASK; - for (int i = 0; i < WORD_SIZE - 1; i++) { - dest[i] = score; - if (block.P & mask) score--; - if (block.M & mask) score++; - mask >>= 1; - } - dest[WORD_SIZE - 1] = score; -} - -/** - * @param [in] block - * @param [in] k - * @return True if all cells in block have value larger than k, otherwise false. - */ -static inline bool allBlockCellsLarger(const Block block, const int k) { - vector scores = getBlockCellValues(block); - for (int i = 0; i < WORD_SIZE; i++) { - if (scores[i] <= k) return false; - } - return true; -} - - -/** - * Uses Myers' bit-vector algorithm to find edit distance for one of semi-global alignment methods. - * @param [in] Peq Query profile. - * @param [in] W Size of padding in last block. - * TODO: Calculate this directly from query, instead of passing it. - * @param [in] maxNumBlocks Number of blocks needed to cover the whole query. - * TODO: Calculate this directly from query, instead of passing it. - * @param [in] queryLength - * @param [in] target - * @param [in] targetLength - * @param [in] k - * @param [in] mode EDLIB_MODE_HW or EDLIB_MODE_SHW - * @param [out] bestScore_ Edit distance. - * @param [out] positions_ Array of 0-indexed positions in target at which best score was found. - Make sure to free this array with free(). - * @param [out] numPositions_ Number of positions in the positions_ array. - * @return Status. - */ -static int myersCalcEditDistanceSemiGlobal( - const Word* const Peq, const int W, const int maxNumBlocks, - const int queryLength, - const unsigned char* const target, const int targetLength, - int k, const EdlibAlignMode mode, - int* const bestScore_, int** const positions_, int* const numPositions_) { - *positions_ = NULL; - *numPositions_ = 0; - - // firstBlock is 0-based index of first block in Ukkonen band. - // lastBlock is 0-based index of last block in Ukkonen band. - int firstBlock = 0; - int lastBlock = min(ceilDiv(k + 1, WORD_SIZE), maxNumBlocks) - 1; // y in Myers - Block *bl; // Current block - - Block* blocks = new Block[maxNumBlocks]; - - // For HW, solution will never be larger then queryLength. - if (mode == EDLIB_MODE_HW) { - k = min(queryLength, k); - } - - // Each STRONG_REDUCE_NUM column is reduced in more expensive way. - // This gives speed up of about 2 times for small k. - const int STRONG_REDUCE_NUM = 2048; - - // Initialize P, M and score - bl = blocks; - for (int b = 0; b <= lastBlock; b++) { - bl->score = (b + 1) * WORD_SIZE; - bl->P = (Word)-1; // All 1s - bl->M = (Word)0; - bl++; - } - - int bestScore = -1; - vector positions; // TODO: Maybe put this on heap? - const int startHout = mode == EDLIB_MODE_HW ? 0 : 1; // If 0 then gap before query is not penalized; - const unsigned char* targetChar = target; - for (int c = 0; c < targetLength; c++) { // for each column - const Word* Peq_c = Peq + (*targetChar) * maxNumBlocks; - - //----------------------- Calculate column -------------------------// - int hout = startHout; - bl = blocks + firstBlock; - Peq_c += firstBlock; - for (int b = firstBlock; b <= lastBlock; b++) { - hout = calculateBlock(bl->P, bl->M, *Peq_c, hout, bl->P, bl->M); - bl->score += hout; - bl++; Peq_c++; - } - bl--; Peq_c--; - //------------------------------------------------------------------// - - //---------- Adjust number of blocks according to Ukkonen ----------// - if ((lastBlock < maxNumBlocks - 1) && (bl->score - hout <= k) // bl is pointing to last block - && ((*(Peq_c + 1) & WORD_1) || hout < 0)) { // Peq_c is pointing to last block - // If score of left block is not too big, calculate one more block - lastBlock++; bl++; Peq_c++; - bl->P = (Word)-1; // All 1s - bl->M = (Word)0; - bl->score = (bl - 1)->score - hout + WORD_SIZE + calculateBlock(bl->P, bl->M, *Peq_c, hout, bl->P, bl->M); - } else { - while (lastBlock >= firstBlock && bl->score >= k + WORD_SIZE) { - lastBlock--; bl--; Peq_c--; - } - } - - // Every some columns, do some expensive but also more efficient block reducing. - // This is important! - // - // Reduce the band by decreasing last block if possible. - if (c % STRONG_REDUCE_NUM == 0) { - while (lastBlock >= 0 && lastBlock >= firstBlock && allBlockCellsLarger(*bl, k)) { - lastBlock--; bl--; Peq_c--; - } - } - // For HW, even if all cells are > k, there still may be solution in next - // column because starting conditions at upper boundary are 0. - // That means that first block is always candidate for solution, - // and we can never end calculation before last column. - if (mode == EDLIB_MODE_HW && lastBlock == -1) { - lastBlock++; bl++; Peq_c++; - } - - // Reduce band by increasing first block if possible. Not applicable to HW. - if (mode != EDLIB_MODE_HW) { - while (firstBlock <= lastBlock && blocks[firstBlock].score >= k + WORD_SIZE) { - firstBlock++; - } - if (c % STRONG_REDUCE_NUM == 0) { // Do strong reduction every some blocks - while (firstBlock <= lastBlock && allBlockCellsLarger(blocks[firstBlock], k)) { - firstBlock++; - } - } - } - - // If band stops to exist finish - if (lastBlock < firstBlock) { - *bestScore_ = bestScore; - if (bestScore != -1) { - *positions_ = (int *) malloc(sizeof(int) * (int) positions.size()); - *numPositions_ = (int) positions.size(); - copy(positions.begin(), positions.end(), *positions_); - } - delete[] blocks; - return EDLIB_STATUS_OK; - } - //------------------------------------------------------------------// - - //------------------------- Update best score ----------------------// - if (lastBlock == maxNumBlocks - 1) { - int colScore = bl->score; - if (colScore <= k) { // Scores > k dont have correct values (so we cannot use them), but are certainly > k. - // NOTE: Score that I find in column c is actually score from column c-W - if (bestScore == -1 || colScore <= bestScore) { - if (colScore != bestScore) { - positions.clear(); - bestScore = colScore; - // Change k so we will look only for equal or better - // scores then the best found so far. - k = bestScore; - } - positions.push_back(c - W); - } - } - } - //------------------------------------------------------------------// - - targetChar++; - } - - - // Obtain results for last W columns from last column. - if (lastBlock == maxNumBlocks - 1) { - vector blockScores = getBlockCellValues(*bl); - for (int i = 0; i < W; i++) { - int colScore = blockScores[i + 1]; - if (colScore <= k && (bestScore == -1 || colScore <= bestScore)) { - if (colScore != bestScore) { - positions.clear(); - k = bestScore = colScore; - } - positions.push_back(targetLength - W + i); - } - } - } - - *bestScore_ = bestScore; - if (bestScore != -1) { - *positions_ = (int *) malloc(sizeof(int) * (int) positions.size()); - *numPositions_ = (int) positions.size(); - copy(positions.begin(), positions.end(), *positions_); - } - - delete[] blocks; - return EDLIB_STATUS_OK; -} - - -/** - * Uses Myers' bit-vector algorithm to find edit distance for global(NW) alignment method. - * @param [in] Peq Query profile. - * @param [in] W Size of padding in last block. - * TODO: Calculate this directly from query, instead of passing it. - * @param [in] maxNumBlocks Number of blocks needed to cover the whole query. - * TODO: Calculate this directly from query, instead of passing it. - * @param [in] queryLength - * @param [in] target - * @param [in] targetLength - * @param [in] k - * @param [out] bestScore_ Edit distance. - * @param [out] position_ 0-indexed position in target at which best score was found. - * @param [in] findAlignment If true, whole matrix is remembered and alignment data is returned. - * Quadratic amount of memory is consumed. - * @param [out] alignData Data needed for alignment traceback (for reconstruction of alignment). - * Set only if findAlignment is set to true, otherwise it is NULL. - * Make sure to free this array using delete[]. - * @param [out] targetStopPosition If set to -1, whole calculation is performed normally, as expected. - * If set to p, calculation is performed up to position p in target (inclusive) - * and column p is returned as the only column in alignData. - * @return Status. - */ -static int myersCalcEditDistanceNW(const Word* const Peq, const int W, const int maxNumBlocks, - const int queryLength, - const unsigned char* const target, const int targetLength, - int k, int* const bestScore_, - int* const position_, const bool findAlignment, - AlignmentData** const alignData, const int targetStopPosition) { - if (targetStopPosition > -1 && findAlignment) { - // They can not be both set at the same time! - return EDLIB_STATUS_ERROR; - } - - // Each STRONG_REDUCE_NUM column is reduced in more expensive way. - const int STRONG_REDUCE_NUM = 2048; // TODO: Choose this number dinamically (based on query and target lengths?), so it does not affect speed of computation - - if (k < abs(targetLength - queryLength)) { - *bestScore_ = *position_ = -1; - return EDLIB_STATUS_OK; - } - - k = min(k, max(queryLength, targetLength)); // Upper bound for k - - // firstBlock is 0-based index of first block in Ukkonen band. - // lastBlock is 0-based index of last block in Ukkonen band. - int firstBlock = 0; - // This is optimal now, by my formula. - int lastBlock = min(maxNumBlocks, ceilDiv(min(k, (k + queryLength - targetLength) / 2) + 1, WORD_SIZE)) - 1; - Block* bl; // Current block - - Block* blocks = new Block[maxNumBlocks]; - - // Initialize P, M and score - bl = blocks; - for (int b = 0; b <= lastBlock; b++) { - bl->score = (b + 1) * WORD_SIZE; - bl->P = (Word)-1; // All 1s - bl->M = (Word)0; - bl++; - } - - // If we want to find alignment, we have to store needed data. - if (findAlignment) - *alignData = new AlignmentData(maxNumBlocks, targetLength); - else if (targetStopPosition > -1) - *alignData = new AlignmentData(maxNumBlocks, 1); - else - *alignData = NULL; - - const unsigned char* targetChar = target; - for (int c = 0; c < targetLength; c++) { // for each column - const Word* Peq_c = Peq + *targetChar * maxNumBlocks; - - //----------------------- Calculate column -------------------------// - int hout = 1; - bl = blocks + firstBlock; - for (int b = firstBlock; b <= lastBlock; b++) { - hout = calculateBlock(bl->P, bl->M, Peq_c[b], hout, bl->P, bl->M); - bl->score += hout; - bl++; - } - bl--; - //------------------------------------------------------------------// - // bl now points to last block - - // Update k. I do it only on end of column because it would slow calculation too much otherwise. - // NOTICE: I add W when in last block because it is actually result from W cells to the left and W cells up. - k = min(k, bl->score - + max(targetLength - c - 1, queryLength - ((1 + lastBlock) * WORD_SIZE - 1) - 1) - + (lastBlock == maxNumBlocks - 1 ? W : 0)); - - //---------- Adjust number of blocks according to Ukkonen ----------// - //--- Adjust last block ---// - // If block is not beneath band, calculate next block. Only next because others are certainly beneath band. - if (lastBlock + 1 < maxNumBlocks - && !(//score[lastBlock] >= k + WORD_SIZE || // NOTICE: this condition could be satisfied if above block also! - ((lastBlock + 1) * WORD_SIZE - 1 - > k - bl->score + 2 * WORD_SIZE - 2 - targetLength + c + queryLength))) { - lastBlock++; bl++; - bl->P = (Word)-1; // All 1s - bl->M = (Word)0; - int newHout = calculateBlock(bl->P, bl->M, Peq_c[lastBlock], hout, bl->P, bl->M); - bl->score = (bl - 1)->score - hout + WORD_SIZE + newHout; - hout = newHout; - } - - // While block is out of band, move one block up. - // NOTE: Condition used here is more loose than the one from the article, since I simplified the max() part of it. - // I could consider adding that max part, for optimal performance. - while (lastBlock >= firstBlock - && (bl->score >= k + WORD_SIZE - || ((lastBlock + 1) * WORD_SIZE - 1 > - // TODO: Does not work if do not put +1! Why??? - k - bl->score + 2 * WORD_SIZE - 2 - targetLength + c + queryLength + 1))) { - lastBlock--; bl--; - } - //-------------------------// - - //--- Adjust first block ---// - // While outside of band, advance block - while (firstBlock <= lastBlock - && (blocks[firstBlock].score >= k + WORD_SIZE - || ((firstBlock + 1) * WORD_SIZE - 1 < - blocks[firstBlock].score - k - targetLength + queryLength + c))) { - firstBlock++; - } - //--------------------------/ - - - // TODO: consider if this part is useful, it does not seem to help much - if (c % STRONG_REDUCE_NUM == 0) { // Every some columns do more expensive but more efficient reduction - while (lastBlock >= firstBlock) { - // If all cells outside of band, remove block - vector scores = getBlockCellValues(*bl); - int numCells = lastBlock == maxNumBlocks - 1 ? WORD_SIZE - W : WORD_SIZE; - int r = lastBlock * WORD_SIZE + numCells - 1; - bool reduce = true; - for (int i = WORD_SIZE - numCells; i < WORD_SIZE; i++) { - // TODO: Does not work if do not put +1! Why??? - if (scores[i] <= k && r <= k - scores[i] - targetLength + c + queryLength + 1) { - reduce = false; - break; - } - r--; - } - if (!reduce) break; - lastBlock--; bl--; - } - - while (firstBlock <= lastBlock) { - // If all cells outside of band, remove block - vector scores = getBlockCellValues(blocks[firstBlock]); - int numCells = firstBlock == maxNumBlocks - 1 ? WORD_SIZE - W : WORD_SIZE; - int r = firstBlock * WORD_SIZE + numCells - 1; - bool reduce = true; - for (int i = WORD_SIZE - numCells; i < WORD_SIZE; i++) { - if (scores[i] <= k && r >= scores[i] - k - targetLength + c + queryLength) { - reduce = false; - break; - } - r--; - } - if (!reduce) break; - firstBlock++; - } - } - - - // If band stops to exist finish - if (lastBlock < firstBlock) { - *bestScore_ = *position_ = -1; - delete[] blocks; - return EDLIB_STATUS_OK; - } - //------------------------------------------------------------------// - - - //---- Save column so it can be used for reconstruction ----// - if (findAlignment && c < targetLength) { - bl = blocks + firstBlock; - for (int b = firstBlock; b <= lastBlock; b++) { - (*alignData)->Ps[maxNumBlocks * c + b] = bl->P; - (*alignData)->Ms[maxNumBlocks * c + b] = bl->M; - (*alignData)->scores[maxNumBlocks * c + b] = bl->score; - (*alignData)->firstBlocks[c] = firstBlock; - (*alignData)->lastBlocks[c] = lastBlock; - bl++; - } - } - //----------------------------------------------------------// - //---- If this is stop column, save it and finish ----// - if (c == targetStopPosition) { - for (int b = firstBlock; b <= lastBlock; b++) { - (*alignData)->Ps[b] = (blocks + b)->P; - (*alignData)->Ms[b] = (blocks + b)->M; - (*alignData)->scores[b] = (blocks + b)->score; - (*alignData)->firstBlocks[0] = firstBlock; - (*alignData)->lastBlocks[0] = lastBlock; - } - *bestScore_ = -1; - *position_ = targetStopPosition; - delete[] blocks; - return EDLIB_STATUS_OK; - } - //----------------------------------------------------// - - targetChar++; - } - - if (lastBlock == maxNumBlocks - 1) { // If last block of last column was calculated - // Obtain best score from block -> it is complicated because query is padded with W cells - int bestScore = getBlockCellValues(blocks[lastBlock])[W]; - if (bestScore <= k) { - *bestScore_ = bestScore; - *position_ = targetLength - 1; - delete[] blocks; - return EDLIB_STATUS_OK; - } - } - - *bestScore_ = *position_ = -1; - delete[] blocks; - return EDLIB_STATUS_OK; -} - - -/** - * Finds one possible alignment that gives optimal score by moving back through the dynamic programming matrix, - * that is stored in alignData. Consumes large amount of memory: O(queryLength * targetLength). - * @param [in] queryLength Normal length, without W. - * @param [in] targetLength Normal length, without W. - * @param [in] bestScore Best score. - * @param [in] alignData Data obtained during finding best score that is useful for finding alignment. - * @param [out] alignment Alignment. - * @param [out] alignmentLength Length of alignment. - * @return Status code. - */ -static int obtainAlignmentTraceback(const int queryLength, const int targetLength, - const int bestScore, const AlignmentData* const alignData, - unsigned char** const alignment, int* const alignmentLength) { - const int maxNumBlocks = ceilDiv(queryLength, WORD_SIZE); - const int W = maxNumBlocks * WORD_SIZE - queryLength; - - *alignment = (unsigned char*) malloc((queryLength + targetLength - 1) * sizeof(unsigned char)); - *alignmentLength = 0; - int c = targetLength - 1; // index of column - int b = maxNumBlocks - 1; // index of block in column - int currScore = bestScore; // Score of current cell - int lScore = -1; // Score of left cell - int uScore = -1; // Score of upper cell - int ulScore = -1; // Score of upper left cell - Word currP = alignData->Ps[c * maxNumBlocks + b]; // P of current block - Word currM = alignData->Ms[c * maxNumBlocks + b]; // M of current block - // True if block to left exists and is in band - bool thereIsLeftBlock = c > 0 && b >= alignData->firstBlocks[c-1] && b <= alignData->lastBlocks[c-1]; - // We set initial values of lP and lM to 0 only to avoid compiler warnings, they should not affect the - // calculation as both lP and lM should be initialized at some moment later (but compiler can not - // detect it since this initialization is guaranteed by "business" logic). - Word lP = 0, lM = 0; - if (thereIsLeftBlock) { - lP = alignData->Ps[(c - 1) * maxNumBlocks + b]; // P of block to the left - lM = alignData->Ms[(c - 1) * maxNumBlocks + b]; // M of block to the left - } - currP <<= W; - currM <<= W; - int blockPos = WORD_SIZE - W - 1; // 0 based index of current cell in blockPos - - // TODO(martin): refactor this whole piece of code. There are too many if-else statements, - // it is too easy for a bug to hide and to hard to effectively cover all the edge-cases. - // We need better separation of logic and responsibilities. - while (true) { - if (c == 0) { - thereIsLeftBlock = true; - lScore = b * WORD_SIZE + blockPos + 1; - ulScore = lScore - 1; - } - - // TODO: improvement: calculate only those cells that are needed, - // for example if I calculate upper cell and can move up, - // there is no need to calculate left and upper left cell - //---------- Calculate scores ---------// - if (lScore == -1 && thereIsLeftBlock) { - lScore = alignData->scores[(c - 1) * maxNumBlocks + b]; // score of block to the left - for (int i = 0; i < WORD_SIZE - blockPos - 1; i++) { - if (lP & HIGH_BIT_MASK) lScore--; - if (lM & HIGH_BIT_MASK) lScore++; - lP <<= 1; - lM <<= 1; - } - } - if (ulScore == -1) { - if (lScore != -1) { - ulScore = lScore; - if (lP & HIGH_BIT_MASK) ulScore--; - if (lM & HIGH_BIT_MASK) ulScore++; - } - else if (c > 0 && b-1 >= alignData->firstBlocks[c-1] && b-1 <= alignData->lastBlocks[c-1]) { - // This is the case when upper left cell is last cell in block, - // and block to left is not in band so lScore is -1. - ulScore = alignData->scores[(c - 1) * maxNumBlocks + b - 1]; - } - } - if (uScore == -1) { - uScore = currScore; - if (currP & HIGH_BIT_MASK) uScore--; - if (currM & HIGH_BIT_MASK) uScore++; - currP <<= 1; - currM <<= 1; - } - //-------------------------------------// - - // TODO: should I check if there is upper block? - - //-------------- Move --------------// - // Move up - insertion to target - deletion from query - if (uScore != -1 && uScore + 1 == currScore) { - currScore = uScore; - lScore = ulScore; - uScore = ulScore = -1; - if (blockPos == 0) { // If entering new (upper) block - if (b == 0) { // If there are no cells above (only boundary cells) - (*alignment)[(*alignmentLength)++] = EDLIB_EDOP_INSERT; // Move up - for (int i = 0; i < c + 1; i++) // Move left until end - (*alignment)[(*alignmentLength)++] = EDLIB_EDOP_DELETE; - break; - } else { - blockPos = WORD_SIZE - 1; - b--; - currP = alignData->Ps[c * maxNumBlocks + b]; - currM = alignData->Ms[c * maxNumBlocks + b]; - if (c > 0 && b >= alignData->firstBlocks[c-1] && b <= alignData->lastBlocks[c-1]) { - thereIsLeftBlock = true; - lP = alignData->Ps[(c - 1) * maxNumBlocks + b]; // TODO: improve this, too many operations - lM = alignData->Ms[(c - 1) * maxNumBlocks + b]; - } else { - thereIsLeftBlock = false; - // TODO(martin): There may not be left block, but there can be left boundary - do we - // handle this correctly then? Are l and ul score set correctly? I should check that / refactor this. - } - } - } else { - blockPos--; - lP <<= 1; - lM <<= 1; - } - // Mark move - (*alignment)[(*alignmentLength)++] = EDLIB_EDOP_INSERT; - } - // Move left - deletion from target - insertion to query - else if (lScore != -1 && lScore + 1 == currScore) { - currScore = lScore; - uScore = ulScore; - lScore = ulScore = -1; - c--; - if (c == -1) { // If there are no cells to the left (only boundary cells) - (*alignment)[(*alignmentLength)++] = EDLIB_EDOP_DELETE; // Move left - int numUp = b * WORD_SIZE + blockPos + 1; - for (int i = 0; i < numUp; i++) // Move up until end - (*alignment)[(*alignmentLength)++] = EDLIB_EDOP_INSERT; - break; - } - currP = lP; - currM = lM; - if (c > 0 && b >= alignData->firstBlocks[c-1] && b <= alignData->lastBlocks[c-1]) { - thereIsLeftBlock = true; - lP = alignData->Ps[(c - 1) * maxNumBlocks + b]; - lM = alignData->Ms[(c - 1) * maxNumBlocks + b]; - } else { - if (c == 0) { // If there are no cells to the left (only boundary cells) - thereIsLeftBlock = true; - lScore = b * WORD_SIZE + blockPos + 1; - ulScore = lScore - 1; - } else { - thereIsLeftBlock = false; - } - } - // Mark move - (*alignment)[(*alignmentLength)++] = EDLIB_EDOP_DELETE; - } - // Move up left - (mis)match - else if (ulScore != -1) { - unsigned char moveCode = ulScore == currScore ? EDLIB_EDOP_MATCH : EDLIB_EDOP_MISMATCH; - currScore = ulScore; - uScore = lScore = ulScore = -1; - c--; - if (c == -1) { // If there are no cells to the left (only boundary cells) - (*alignment)[(*alignmentLength)++] = moveCode; // Move left - int numUp = b * WORD_SIZE + blockPos; - for (int i = 0; i < numUp; i++) // Move up until end - (*alignment)[(*alignmentLength)++] = EDLIB_EDOP_INSERT; - break; - } - if (blockPos == 0) { // If entering upper left block - if (b == 0) { // If there are no more cells above (only boundary cells) - (*alignment)[(*alignmentLength)++] = moveCode; // Move up left - for (int i = 0; i < c + 1; i++) // Move left until end - (*alignment)[(*alignmentLength)++] = EDLIB_EDOP_DELETE; - break; - } - blockPos = WORD_SIZE - 1; - b--; - currP = alignData->Ps[c * maxNumBlocks + b]; - currM = alignData->Ms[c * maxNumBlocks + b]; - } else { // If entering left block - blockPos--; - currP = lP; - currM = lM; - currP <<= 1; - currM <<= 1; - } - // Set new left block - if (c > 0 && b >= alignData->firstBlocks[c-1] && b <= alignData->lastBlocks[c-1]) { - thereIsLeftBlock = true; - lP = alignData->Ps[(c - 1) * maxNumBlocks + b]; - lM = alignData->Ms[(c - 1) * maxNumBlocks + b]; - } else { - if (c == 0) { // If there are no cells to the left (only boundary cells) - thereIsLeftBlock = true; - lScore = b * WORD_SIZE + blockPos + 1; - ulScore = lScore - 1; - } else { - thereIsLeftBlock = false; - } - } - // Mark move - (*alignment)[(*alignmentLength)++] = moveCode; - } else { - // Reached end - finished! - break; - } - //----------------------------------// - } - - *alignment = (unsigned char*) realloc(*alignment, (*alignmentLength) * sizeof(unsigned char)); - reverse(*alignment, *alignment + (*alignmentLength)); - return EDLIB_STATUS_OK; -} - - -/** - * Finds one possible alignment that gives optimal score (bestScore). - * It will split problem into smaller problems using Hirschberg's algorithm and when they are small enough, - * it will solve them using traceback algorithm. - * @param [in] query - * @param [in] rQuery Reversed query. - * @param [in] queryLength - * @param [in] target - * @param [in] rTarget Reversed target. - * @param [in] targetLength - * @param [in] equalityDefinition - * @param [in] alphabetLength - * @param [in] bestScore Best(optimal) score. - * @param [out] alignment Sequence of edit operations that make target equal to query. - * @param [out] alignmentLength Length of alignment. - * @return Status code. - */ -static int obtainAlignment( - const unsigned char* const query, const unsigned char* const rQuery, const int queryLength, - const unsigned char* const target, const unsigned char* const rTarget, const int targetLength, - const EqualityDefinition& equalityDefinition, const int alphabetLength, const int bestScore, - unsigned char** const alignment, int* const alignmentLength) { - - // Handle special case when one of sequences has length of 0. - if (queryLength == 0 || targetLength == 0) { - *alignmentLength = targetLength + queryLength; - *alignment = (unsigned char*) malloc((*alignmentLength) * sizeof(unsigned char)); - for (int i = 0; i < *alignmentLength; i++) { - (*alignment)[i] = queryLength == 0 ? EDLIB_EDOP_DELETE : EDLIB_EDOP_INSERT; - } - return EDLIB_STATUS_OK; - } - - const int maxNumBlocks = ceilDiv(queryLength, WORD_SIZE); - const int W = maxNumBlocks * WORD_SIZE - queryLength; - int statusCode; - - // TODO: think about reducing number of memory allocations in alignment functions, probably - // by sharing some memory that is allocated only once. That refers to: Peq, columns in Hirschberg, - // and it could also be done for alignments - we could have one big array for alignment that would be - // sparsely populated by each of steps in recursion, and at the end we would just consolidate those results. - - // If estimated memory consumption for traceback algorithm is smaller than 1MB use it, - // otherwise use Hirschberg's algorithm. By running few tests I choose boundary of 1MB as optimal. - long long alignmentDataSize = (long long) (2 * sizeof(Word) + sizeof(int)) * maxNumBlocks * targetLength - + (long long) 2 * sizeof(int) * targetLength; - if (alignmentDataSize < 1024 * 1024) { - int score_, endLocation_; // Used only to call function. - AlignmentData* alignData = NULL; - Word* Peq = buildPeq(alphabetLength, query, queryLength, equalityDefinition); - myersCalcEditDistanceNW(Peq, W, maxNumBlocks, - queryLength, - target, targetLength, - bestScore, - &score_, &endLocation_, true, &alignData, -1); - //assert(score_ == bestScore); - //assert(endLocation_ == targetLength - 1); - - statusCode = obtainAlignmentTraceback(queryLength, targetLength, - bestScore, alignData, alignment, alignmentLength); - delete alignData; - delete[] Peq; - } else { - statusCode = obtainAlignmentHirschberg(query, rQuery, queryLength, - target, rTarget, targetLength, - equalityDefinition, alphabetLength, bestScore, - alignment, alignmentLength); - } - return statusCode; -} - - -/** - * Finds one possible alignment that gives optimal score (bestScore). - * Uses Hirschberg's algorithm to split problem into two sub-problems, solve them and combine them together. - * @param [in] query - * @param [in] rQuery Reversed query. - * @param [in] queryLength - * @param [in] target - * @param [in] rTarget Reversed target. - * @param [in] targetLength - * @param [in] alphabetLength - * @param [in] bestScore Best(optimal) score. - * @param [out] alignment Sequence of edit operations that make target equal to query. - * @param [out] alignmentLength Length of alignment. - * @return Status code. - */ -static int obtainAlignmentHirschberg( - const unsigned char* const query, const unsigned char* const rQuery, const int queryLength, - const unsigned char* const target, const unsigned char* const rTarget, const int targetLength, - const EqualityDefinition& equalityDefinition, const int alphabetLength, const int bestScore, - unsigned char** const alignment, int* const alignmentLength) { - - const int maxNumBlocks = ceilDiv(queryLength, WORD_SIZE); - const int W = maxNumBlocks * WORD_SIZE - queryLength; - - Word* Peq = buildPeq(alphabetLength, query, queryLength, equalityDefinition); - Word* rPeq = buildPeq(alphabetLength, rQuery, queryLength, equalityDefinition); - - // Used only to call functions. - int score_, endLocation_; - - // Divide dynamic matrix into two halfs, left and right. - const int leftHalfWidth = targetLength / 2; - const int rightHalfWidth = targetLength - leftHalfWidth; - - // Calculate left half. - AlignmentData* alignDataLeftHalf = NULL; - int leftHalfCalcStatus = myersCalcEditDistanceNW( - Peq, W, maxNumBlocks, queryLength, target, targetLength, bestScore, - &score_, &endLocation_, false, &alignDataLeftHalf, leftHalfWidth - 1); - - // Calculate right half. - AlignmentData* alignDataRightHalf = NULL; - int rightHalfCalcStatus = myersCalcEditDistanceNW( - rPeq, W, maxNumBlocks, queryLength, rTarget, targetLength, bestScore, - &score_, &endLocation_, false, &alignDataRightHalf, rightHalfWidth - 1); - - delete[] Peq; - delete[] rPeq; - - if (leftHalfCalcStatus == EDLIB_STATUS_ERROR || rightHalfCalcStatus == EDLIB_STATUS_ERROR) { - if (alignDataLeftHalf) delete alignDataLeftHalf; - if (alignDataRightHalf) delete alignDataRightHalf; - return EDLIB_STATUS_ERROR; - } - - // Unwrap the left half. - int firstBlockIdxLeft = alignDataLeftHalf->firstBlocks[0]; - int lastBlockIdxLeft = alignDataLeftHalf->lastBlocks[0]; - // TODO: avoid this allocation by using some shared array? - // scoresLeft contains scores from left column, starting with scoresLeftStartIdx row (query index) - // and ending with scoresLeftEndIdx row (0-indexed). - int scoresLeftLength = (lastBlockIdxLeft - firstBlockIdxLeft + 1) * WORD_SIZE; - int* scoresLeft = new int[scoresLeftLength]; - for (int blockIdx = firstBlockIdxLeft; blockIdx <= lastBlockIdxLeft; blockIdx++) { - Block block(alignDataLeftHalf->Ps[blockIdx], alignDataLeftHalf->Ms[blockIdx], - alignDataLeftHalf->scores[blockIdx]); - readBlock(block, scoresLeft + (blockIdx - firstBlockIdxLeft) * WORD_SIZE); - } - int scoresLeftStartIdx = firstBlockIdxLeft * WORD_SIZE; - // If last block contains padding, shorten the length of scores for the length of padding. - if (lastBlockIdxLeft == maxNumBlocks - 1) { - scoresLeftLength -= W; - } - - // Unwrap the right half (I also reverse it while unwraping). - int firstBlockIdxRight = alignDataRightHalf->firstBlocks[0]; - int lastBlockIdxRight = alignDataRightHalf->lastBlocks[0]; - int scoresRightLength = (lastBlockIdxRight - firstBlockIdxRight + 1) * WORD_SIZE; - int* scoresRight = new int[scoresRightLength]; - int* scoresRightOriginalStart = scoresRight; - for (int blockIdx = firstBlockIdxRight; blockIdx <= lastBlockIdxRight; blockIdx++) { - Block block(alignDataRightHalf->Ps[blockIdx], alignDataRightHalf->Ms[blockIdx], - alignDataRightHalf->scores[blockIdx]); - readBlockReverse(block, scoresRight + (lastBlockIdxRight - blockIdx) * WORD_SIZE); - } - int scoresRightStartIdx = queryLength - (lastBlockIdxRight + 1) * WORD_SIZE; - // If there is padding at the beginning of scoresRight (that can happen because of reversing that we do), - // move pointer forward to remove the padding (that is why we remember originalStart). - if (scoresRightStartIdx < 0) { - //assert(scoresRightStartIdx == -1 * W); - scoresRight += W; - scoresRightStartIdx += W; - scoresRightLength -= W; - } - - delete alignDataLeftHalf; - delete alignDataRightHalf; - - //--------------------- Find the best move ----------------// - // Find the query/row index of cell in left column which together with its lower right neighbour - // from right column gives the best score (when summed). We also have to consider boundary cells - // (those cells at -1 indexes). - // x| - // -+- - // |x - int queryIdxLeftStart = max(scoresLeftStartIdx, scoresRightStartIdx - 1); - int queryIdxLeftEnd = min(scoresLeftStartIdx + scoresLeftLength - 1, - scoresRightStartIdx + scoresRightLength - 2); - int leftScore, rightScore; - int queryIdxLeftAlignment; // Query/row index of cell in left column where alignment is passing through. - bool queryIdxLeftAlignmentFound = false; - for (int queryIdx = queryIdxLeftStart; queryIdx <= queryIdxLeftEnd; queryIdx++) { - leftScore = scoresLeft[queryIdx - scoresLeftStartIdx]; - rightScore = scoresRight[queryIdx + 1 - scoresRightStartIdx]; - if (leftScore + rightScore == bestScore) { - queryIdxLeftAlignment = queryIdx; - queryIdxLeftAlignmentFound = true; - break; - } - } - // Check boundary cells. - if (!queryIdxLeftAlignmentFound && scoresLeftStartIdx == 0 && scoresRightStartIdx == 0) { - leftScore = leftHalfWidth; - rightScore = scoresRight[0]; - if (leftScore + rightScore == bestScore) { - queryIdxLeftAlignment = -1; - queryIdxLeftAlignmentFound = true; - } - } - if (!queryIdxLeftAlignmentFound && scoresLeftStartIdx + scoresLeftLength == queryLength - && scoresRightStartIdx + scoresRightLength == queryLength) { - leftScore = scoresLeft[scoresLeftLength - 1]; - rightScore = rightHalfWidth; - if (leftScore + rightScore == bestScore) { - queryIdxLeftAlignment = queryLength - 1; - queryIdxLeftAlignmentFound = true; - } - } - - delete[] scoresLeft; - delete[] scoresRightOriginalStart; - - if (queryIdxLeftAlignmentFound == false) { - // If there was no move that is part of optimal alignment, then there is no such alignment - // or given bestScore is not correct! - return EDLIB_STATUS_ERROR; - } - //----------------------------------------------------------// - - // Calculate alignments for upper half of left half (upper left - ul) - // and lower half of right half (lower right - lr). - const int ulHeight = queryIdxLeftAlignment + 1; - const int lrHeight = queryLength - ulHeight; - const int ulWidth = leftHalfWidth; - const int lrWidth = rightHalfWidth; - unsigned char* ulAlignment = NULL; int ulAlignmentLength; - int ulStatusCode = obtainAlignment(query, rQuery + lrHeight, ulHeight, - target, rTarget + lrWidth, ulWidth, - equalityDefinition, alphabetLength, leftScore, - &ulAlignment, &ulAlignmentLength); - unsigned char* lrAlignment = NULL; int lrAlignmentLength; - int lrStatusCode = obtainAlignment(query + ulHeight, rQuery, lrHeight, - target + ulWidth, rTarget, lrWidth, - equalityDefinition, alphabetLength, rightScore, - &lrAlignment, &lrAlignmentLength); - if (ulStatusCode == EDLIB_STATUS_ERROR || lrStatusCode == EDLIB_STATUS_ERROR) { - if (ulAlignment) free(ulAlignment); - if (lrAlignment) free(lrAlignment); - return EDLIB_STATUS_ERROR; - } - - // Build alignment by concatenating upper left alignment with lower right alignment. - *alignmentLength = ulAlignmentLength + lrAlignmentLength; - *alignment = (unsigned char*) malloc((*alignmentLength) * sizeof(unsigned char)); - memcpy(*alignment, ulAlignment, ulAlignmentLength); - memcpy(*alignment + ulAlignmentLength, lrAlignment, lrAlignmentLength); - - free(ulAlignment); - free(lrAlignment); - return EDLIB_STATUS_OK; -} - - -/** - * Takes char query and char target, recognizes alphabet and transforms them into unsigned char sequences - * where elements in sequences are not any more letters of alphabet, but their index in alphabet. - * Most of internal edlib functions expect such transformed sequences. - * This function will allocate queryTransformed and targetTransformed, so make sure to free them when done. - * Example: - * Original sequences: "ACT" and "CGT". - * Alphabet would be recognized as "ACTG". Alphabet length = 4. - * Transformed sequences: [0, 1, 2] and [1, 3, 2]. - * @param [in] queryOriginal - * @param [in] queryLength - * @param [in] targetOriginal - * @param [in] targetLength - * @param [out] queryTransformed It will contain values in range [0, alphabet length - 1]. - * @param [out] targetTransformed It will contain values in range [0, alphabet length - 1]. - * @return Alphabet as a string of unique characters, where index of each character is its value in transformed - * sequences. - */ -static string transformSequences(const char* const queryOriginal, const int queryLength, - const char* const targetOriginal, const int targetLength, - unsigned char** const queryTransformed, - unsigned char** const targetTransformed) { - // Alphabet is constructed from letters that are present in sequences. - // Each letter is assigned an ordinal number, starting from 0 up to alphabetLength - 1, - // and new query and target are created in which letters are replaced with their ordinal numbers. - // This query and target are used in all the calculations later. - *queryTransformed = (unsigned char *) malloc(sizeof(unsigned char) * queryLength); - *targetTransformed = (unsigned char *) malloc(sizeof(unsigned char) * targetLength); - - string alphabet = ""; - - // Alphabet information, it is constructed on fly while transforming sequences. - // letterIdx[c] is index of letter c in alphabet. - unsigned char letterIdx[MAX_UCHAR + 1]; - bool inAlphabet[MAX_UCHAR + 1]; // inAlphabet[c] is true if c is in alphabet - for (int i = 0; i < MAX_UCHAR + 1; i++) inAlphabet[i] = false; - - for (int i = 0; i < queryLength; i++) { - unsigned char c = static_cast(queryOriginal[i]); - if (!inAlphabet[c]) { - inAlphabet[c] = true; - letterIdx[c] = (unsigned char) alphabet.size(); - alphabet += queryOriginal[i]; - } - (*queryTransformed)[i] = letterIdx[c]; - } - for (int i = 0; i < targetLength; i++) { - unsigned char c = static_cast(targetOriginal[i]); - if (!inAlphabet[c]) { - inAlphabet[c] = true; - letterIdx[c] = (unsigned char) alphabet.size(); - alphabet += targetOriginal[i]; - } - (*targetTransformed)[i] = letterIdx[c]; - } - - return alphabet; -} - - -extern "C" EdlibAlignConfig edlibNewAlignConfig(int k, EdlibAlignMode mode, EdlibAlignTask task, - EdlibEqualityPair* additionalEqualities, - int additionalEqualitiesLength) { - EdlibAlignConfig config; - config.k = k; - config.mode = mode; - config.task = task; - config.additionalEqualities = additionalEqualities; - config.additionalEqualitiesLength = additionalEqualitiesLength; - return config; -} - -extern "C" EdlibAlignConfig edlibDefaultAlignConfig(void) { - return edlibNewAlignConfig(-1, EDLIB_MODE_NW, EDLIB_TASK_DISTANCE, NULL, 0); -} - -extern "C" void edlibFreeAlignResult(EdlibAlignResult result) { - if (result.endLocations) free(result.endLocations); - if (result.startLocations) free(result.startLocations); - if (result.alignment) free(result.alignment); -} diff --git a/edlib.h b/edlib.h deleted file mode 100644 index 4ed7c3b..0000000 --- a/edlib.h +++ /dev/null @@ -1,258 +0,0 @@ -#ifndef EDLIB_H -#define EDLIB_H - -/** - * @file - * @author Martin Sosic - * @brief Main header file, containing all public functions and structures. - */ - -#ifdef __cplusplus -extern "C" { -#endif - -// Status codes -#define EDLIB_STATUS_OK 0 -#define EDLIB_STATUS_ERROR 1 - - /** - * Alignment methods - how should Edlib treat gaps before and after query? - */ - typedef enum { - /** - * Global method. This is the standard method. - * Useful when you want to find out how similar is first sequence to second sequence. - */ - EDLIB_MODE_NW, - /** - * Prefix method. Similar to global method, but with a small twist - gap at query end is not penalized. - * What that means is that deleting elements from the end of second sequence is "free"! - * For example, if we had "AACT" and "AACTGGC", edit distance would be 0, because removing "GGC" from the end - * of second sequence is "free" and does not count into total edit distance. This method is appropriate - * when you want to find out how well first sequence fits at the beginning of second sequence. - */ - EDLIB_MODE_SHW, - /** - * Infix method. Similar as prefix method, but with one more twist - gaps at query end and start are - * not penalized. What that means is that deleting elements from the start and end of second sequence is "free"! - * For example, if we had ACT and CGACTGAC, edit distance would be 0, because removing CG from the start - * and GAC from the end of second sequence is "free" and does not count into total edit distance. - * This method is appropriate when you want to find out how well first sequence fits at any part of - * second sequence. - * For example, if your second sequence was a long text and your first sequence was a sentence from that text, - * but slightly scrambled, you could use this method to discover how scrambled it is and where it fits in - * that text. In bioinformatics, this method is appropriate for aligning read to a sequence. - */ - EDLIB_MODE_HW - } EdlibAlignMode; - - /** - * Alignment tasks - what do you want Edlib to do? - */ - typedef enum { - EDLIB_TASK_DISTANCE, //!< Find edit distance and end locations. - EDLIB_TASK_LOC, //!< Find edit distance, end locations and start locations. - EDLIB_TASK_PATH //!< Find edit distance, end locations and start locations and alignment path. - } EdlibAlignTask; - - /** - * Describes cigar format. - * @see http://samtools.github.io/hts-specs/SAMv1.pdf - * @see http://drive5.com/usearch/manual/cigar.html - */ - typedef enum { - EDLIB_CIGAR_STANDARD, //!< Match: 'M', Insertion: 'I', Deletion: 'D', Mismatch: 'M'. - EDLIB_CIGAR_EXTENDED //!< Match: '=', Insertion: 'I', Deletion: 'D', Mismatch: 'X'. - } EdlibCigarFormat; - -// Edit operations. -#define EDLIB_EDOP_MATCH 0 //!< Match. -#define EDLIB_EDOP_INSERT 1 //!< Insertion to target = deletion from query. -#define EDLIB_EDOP_DELETE 2 //!< Deletion from target = insertion to query. -#define EDLIB_EDOP_MISMATCH 3 //!< Mismatch. - - /** - * @brief Defines two given characters as equal. - */ - typedef struct { - char first; - char second; - } EdlibEqualityPair; - - /** - * @brief Configuration object for edlibAlign() function. - */ - typedef struct { - /** - * Set k to non-negative value to tell edlib that edit distance is not larger than k. - * Smaller k can significantly improve speed of computation. - * If edit distance is larger than k, edlib will set edit distance to -1. - * Set k to negative value and edlib will internally auto-adjust k until score is found. - */ - int k; - - /** - * Alignment method. - * EDLIB_MODE_NW: global (Needleman-Wunsch) - * EDLIB_MODE_SHW: prefix. Gap after query is not penalized. - * EDLIB_MODE_HW: infix. Gaps before and after query are not penalized. - */ - EdlibAlignMode mode; - - /** - * Alignment task - tells Edlib what to calculate. Less to calculate, faster it is. - * EDLIB_TASK_DISTANCE - find edit distance and end locations of optimal alignment paths in target. - * EDLIB_TASK_LOC - find edit distance and start and end locations of optimal alignment paths in target. - * EDLIB_TASK_PATH - find edit distance, alignment path (and start and end locations of it in target). - */ - EdlibAlignTask task; - - /** - * List of pairs of characters, where each pair defines two characters as equal. - * This way you can extend edlib's definition of equality (which is that each character is equal only - * to itself). - * This can be useful if you have some wildcard characters that should match multiple other characters, - * or e.g. if you want edlib to be case insensitive. - * Can be set to NULL if there are none. - */ - EdlibEqualityPair* additionalEqualities; - - /** - * Number of additional equalities, which is non-negative number. - * 0 if there are none. - */ - int additionalEqualitiesLength; - } EdlibAlignConfig; - - /** - * Helper method for easy construction of configuration object. - * @return Configuration object filled with given parameters. - */ - EdlibAlignConfig edlibNewAlignConfig(int k, EdlibAlignMode mode, EdlibAlignTask task, - EdlibEqualityPair* additionalEqualities, - int additionalEqualitiesLength); - - /** - * @return Default configuration object, with following defaults: - * k = -1, mode = EDLIB_MODE_NW, task = EDLIB_TASK_DISTANCE, no additional equalities. - */ - EdlibAlignConfig edlibDefaultAlignConfig(void); - - - /** - * Container for results of alignment done by edlibAlign() function. - */ - typedef struct { - /** - * EDLIB_STATUS_OK or EDLIB_STATUS_ERROR. If error, all other fields will have undefined values. - */ - int status; - - /** - * -1 if k is non-negative and edit distance is larger than k. - */ - int editDistance; - - /** - * Array of zero-based positions in target where optimal alignment paths end. - * If gap after query is penalized, gap counts as part of query (NW), otherwise not. - * Set to NULL if edit distance is larger than k. - * If you do not free whole result object using edlibFreeAlignResult(), do not forget to use free(). - */ - int* endLocations; - - /** - * Array of zero-based positions in target where optimal alignment paths start, - * they correspond to endLocations. - * If gap before query is penalized, gap counts as part of query (NW), otherwise not. - * Set to NULL if not calculated or if edit distance is larger than k. - * If you do not free whole result object using edlibFreeAlignResult(), do not forget to use free(). - */ - int* startLocations; - - /** - * Number of end (and start) locations. - */ - int numLocations; - - /** - * Alignment is found for first pair of start and end locations. - * Set to NULL if not calculated. - * Alignment is sequence of numbers: 0, 1, 2, 3. - * 0 stands for match. - * 1 stands for insertion to target. - * 2 stands for insertion to query. - * 3 stands for mismatch. - * Alignment aligns query to target from begining of query till end of query. - * If gaps are not penalized, they are not in alignment. - * If you do not free whole result object using edlibFreeAlignResult(), do not forget to use free(). - */ - unsigned char* alignment; - - /** - * Length of alignment. - */ - int alignmentLength; - - /** - * Number of different characters in query and target together. - */ - int alphabetLength; - } EdlibAlignResult; - - /** - * Frees memory in EdlibAlignResult that was allocated by edlib. - * If you do not use it, make sure to free needed members manually using free(). - */ - void edlibFreeAlignResult(EdlibAlignResult result); - - - /** - * Aligns two sequences (query and target) using edit distance (levenshtein distance). - * Through config parameter, this function supports different alignment methods (global, prefix, infix), - * as well as different modes of search (tasks). - * It always returns edit distance and end locations of optimal alignment in target. - * It optionally returns start locations of optimal alignment in target and alignment path, - * if you choose appropriate tasks. - * @param [in] query First sequence. - * @param [in] queryLength Number of characters in first sequence. - * @param [in] target Second sequence. - * @param [in] targetLength Number of characters in second sequence. - * @param [in] config Additional alignment parameters, like alignment method and wanted results. - * @return Result of alignment, which can contain edit distance, start and end locations and alignment path. - * Make sure to clean up the object using edlibFreeAlignResult() or by manually freeing needed members. - */ - EdlibAlignResult edlibAlign(const char* query, int queryLength, - const char* target, int targetLength, - const EdlibAlignConfig config); - - - /** - * Builds cigar string from given alignment sequence. - * @param [in] alignment Alignment sequence. - * 0 stands for match. - * 1 stands for insertion to target. - * 2 stands for insertion to query. - * 3 stands for mismatch. - * @param [in] alignmentLength - * @param [in] cigarFormat Cigar will be returned in specified format. - * @return Cigar string. - * I stands for insertion. - * D stands for deletion. - * X stands for mismatch. (used only in extended format) - * = stands for match. (used only in extended format) - * M stands for (mis)match. (used only in standard format) - * String is null terminated. - * Needed memory is allocated and given pointer is set to it. - * Do not forget to free it later using free()! - */ - char* edlibAlignmentToCigar(const unsigned char* alignment, int alignmentLength, - EdlibCigarFormat cigarFormat); - - - -#ifdef __cplusplus -} -#endif - -#endif // EDLIB_H diff --git a/khash.h b/khash.h index 9e010f2..71b1e3e 100644 --- a/khash.h +++ b/khash.h @@ -372,18 +372,19 @@ static const double __ac_HASH_UPPER = 0.77; } \ SCOPE void kh_load_##name(kh_##name##_t *h, FILE* fp)\ {\ - fread(&(h->n_buckets), sizeof(khint_t), 1, fp);\ - fread(&(h->size), sizeof(khint_t), 1, fp);\ - fread(&(h->n_occupied), sizeof(khint_t), 1, fp);\ - fread(&(h->upper_bound), sizeof(khint_t), 1, fp);\ + int f_flag;\ + f_flag = fread(&(h->n_buckets), sizeof(khint_t), 1, fp);\ + f_flag += fread(&(h->size), sizeof(khint_t), 1, fp);\ + f_flag += fread(&(h->n_occupied), sizeof(khint_t), 1, fp);\ + f_flag += fread(&(h->upper_bound), sizeof(khint_t), 1, fp);\ if (h->n_buckets)\ {\ h->flags = (khint32_t*)kmalloc(__ac_fsize(h->n_buckets) * sizeof(khint32_t));\ - fread(h->flags, sizeof(khint32_t), __ac_fsize(h->n_buckets), fp);\ + f_flag += fread(h->flags, sizeof(khint32_t), __ac_fsize(h->n_buckets), fp);\ h->keys = (khkey_t*)kmalloc(sizeof(khkey_t)*h->n_buckets);\ - fread(h->keys, sizeof(khkey_t), h->n_buckets, fp);\ + f_flag += fread(h->keys, sizeof(khkey_t), h->n_buckets, fp);\ h->vals = (khval_t*)kmalloc(sizeof(khval_t)*h->n_buckets);\ - fread(h->vals, sizeof(khval_t), h->n_buckets, fp);\ + f_flag += fread(h->vals, sizeof(khval_t), h->n_buckets, fp);\ }\ } diff --git a/kmer.h b/kmer.h index b3f735d..016a531 100644 --- a/kmer.h +++ b/kmer.h @@ -15,11 +15,8 @@ typedef struct { - ///最大64-mer - ///x[0]低位 - ///x[1]高位 + //can represent at most 64-mer uint64_t x[2]; - } Hash_code; typedef struct { @@ -64,7 +61,7 @@ inline uint64_t get_HPC_code(HPC_seq* seq, uint64_t* end_pos) for (; seq->i < seq->l; seq->i++) { - ///统计N的个数 + ///number of Ns if (seq_nt6_table[(uint8_t)seq->str[seq->i]] >= 4) { seq->N_occ++; diff --git a/ksw2.h b/ksw2.h deleted file mode 100644 index 213c27f..0000000 --- a/ksw2.h +++ /dev/null @@ -1,177 +0,0 @@ -#ifndef KSW2_H_ -#define KSW2_H_ - -#include - -#define KSW_NEG_INF -0x40000000 - -#define KSW_EZ_SCORE_ONLY 0x01 // don't record alignment path/cigar -#define KSW_EZ_RIGHT 0x02 // right-align gaps -#define KSW_EZ_GENERIC_SC 0x04 // without this flag: match/mismatch only; last symbol is a wildcard -#define KSW_EZ_APPROX_MAX 0x08 // approximate max; this is faster with sse -#define KSW_EZ_APPROX_DROP 0x10 // approximate Z-drop; faster with sse -#define KSW_EZ_EXTZ_ONLY 0x40 // only perform extension -#define KSW_EZ_REV_CIGAR 0x80 // reverse CIGAR in the output -#define KSW_EZ_SPLICE_FOR 0x100 -#define KSW_EZ_SPLICE_REV 0x200 -#define KSW_EZ_SPLICE_FLANK 0x400 - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct { - uint32_t max:31, zdropped:1; - int max_q, max_t; // max extension coordinate - int mqe, mqe_t; // max score when reaching the end of query - int mte, mte_q; // max score when reaching the end of target - int score; // max score reaching both ends; may be KSW_NEG_INF - int m_cigar, n_cigar; - int reach_end; - uint32_t *cigar; -} ksw_extz_t; - -/** - * NW-like extension - * - * @param km memory pool, when used with kalloc - * @param qlen query length - * @param query query sequence with 0 <= query[i] < m - * @param tlen target length - * @param target target sequence with 0 <= target[i] < m - * @param m number of residue types - * @param mat m*m scoring mattrix in one-dimension array - * @param gapo gap open penalty; a gap of length l cost "-(gapo+l*gape)" - * @param gape gap extension penalty - * @param w band width (<0 to disable) - * @param zdrop off-diagonal drop-off to stop extension (positive; <0 to disable) - * @param flag flag (see KSW_EZ_* macros) - * @param ez (out) scores and cigar - */ -void ksw_extz(void *km, int qlen, const uint8_t *query, int tlen, const uint8_t *target, int8_t m, const int8_t *mat, - int8_t q, int8_t e, int w, int zdrop, int flag, ksw_extz_t *ez); - -void ksw_extz2_sse(void *km, int qlen, const uint8_t *query, int tlen, const uint8_t *target, int8_t m, const int8_t *mat, - int8_t q, int8_t e, int w, int zdrop, int end_bonus, int flag, ksw_extz_t *ez); - -void ksw_extd(void *km, int qlen, const uint8_t *query, int tlen, const uint8_t *target, int8_t m, const int8_t *mat, - int8_t gapo, int8_t gape, int8_t gapo2, int8_t gape2, int w, int zdrop, int flag, ksw_extz_t *ez); - -void ksw_extd2_sse(void *km, int qlen, const uint8_t *query, int tlen, const uint8_t *target, int8_t m, const int8_t *mat, - int8_t gapo, int8_t gape, int8_t gapo2, int8_t gape2, int w, int zdrop, int end_bonus, int flag, ksw_extz_t *ez); - -void ksw_exts2_sse(void *km, int qlen, const uint8_t *query, int tlen, const uint8_t *target, int8_t m, const int8_t *mat, - int8_t gapo, int8_t gape, int8_t gapo2, int8_t noncan, int zdrop, int flag, ksw_extz_t *ez); - -void ksw_extf2_sse(void *km, int qlen, const uint8_t *query, int tlen, const uint8_t *target, int8_t mch, int8_t mis, int8_t e, int w, int xdrop, ksw_extz_t *ez); - -/** - * Global alignment - * - * (first 10 parameters identical to ksw_extz_sse()) - * @param m_cigar (modified) max CIGAR length; feed 0 if cigar==0 - * @param n_cigar (out) number of CIGAR elements - * @param cigar (out) BAM-encoded CIGAR; caller need to deallocate with kfree(km, ) - * - * @return score of the alignment - */ -int ksw_gg(void *km, int qlen, const uint8_t *query, int tlen, const uint8_t *target, int8_t m, const int8_t *mat, int8_t gapo, int8_t gape, int w, int *m_cigar_, int *n_cigar_, uint32_t **cigar_); -int ksw_gg2(void *km, int qlen, const uint8_t *query, int tlen, const uint8_t *target, int8_t m, const int8_t *mat, int8_t gapo, int8_t gape, int w, int *m_cigar_, int *n_cigar_, uint32_t **cigar_); -int ksw_gg2_sse(void *km, int qlen, const uint8_t *query, int tlen, const uint8_t *target, int8_t m, const int8_t *mat, int8_t gapo, int8_t gape, int w, int *m_cigar_, int *n_cigar_, uint32_t **cigar_); - -void *ksw_ll_qinit(void *km, int size, int qlen, const uint8_t *query, int m, const int8_t *mat); -int ksw_ll_i16(void *q, int tlen, const uint8_t *target, int gapo, int gape, int *qe, int *te); - -#ifdef __cplusplus -} -#endif - -/************************************ - *** Private macros and functions *** - ************************************/ - -#ifdef HAVE_KALLOC -#include "kalloc.h" -#else -#include -#define kmalloc(km, size) malloc((size)) -#define kcalloc(km, count, size) calloc((count), (size)) -#define krealloc(km, ptr, size) realloc((ptr), (size)) -#define kfree(km, ptr) free((ptr)) -#endif - -static inline uint32_t *ksw_push_cigar(void *km, int *n_cigar, int *m_cigar, uint32_t *cigar, uint32_t op, int len) -{ - if (*n_cigar == 0 || op != (cigar[(*n_cigar) - 1]&0xf)) { - if (*n_cigar == *m_cigar) { - *m_cigar = *m_cigar? (*m_cigar)<<1 : 4; - cigar = (uint32_t*)krealloc(km, cigar, (*m_cigar) << 2); - } - cigar[(*n_cigar)++] = len<<4 | op; - } else cigar[(*n_cigar)-1] += len<<4; - return cigar; -} - -// In the backtrack matrix, value p[] has the following structure: -// bit 0-2: which type gets the max - 0 for H, 1 for E, 2 for F, 3 for \tilde{E} and 4 for \tilde{F} -// bit 3/0x08: 1 if a continuation on the E state (bit 5/0x20 for a continuation on \tilde{E}) -// bit 4/0x10: 1 if a continuation on the F state (bit 6/0x40 for a continuation on \tilde{F}) -static inline void ksw_backtrack(void *km, int is_rot, int is_rev, int min_intron_len, const uint8_t *p, const int *off, const int *off_end, int n_col, int i0, int j0, - int *m_cigar_, int *n_cigar_, uint32_t **cigar_) -{ // p[] - lower 3 bits: which type gets the max; bit - int n_cigar = 0, m_cigar = *m_cigar_, i = i0, j = j0, r, state = 0; - uint32_t *cigar = *cigar_, tmp; - while (i >= 0 && j >= 0) { // at the beginning of the loop, _state_ tells us which state to check - int force_state = -1; - if (is_rot) { - r = i + j; - if (i < off[r]) force_state = 2; - if (off_end && i > off_end[r]) force_state = 1; - tmp = force_state < 0? p[(size_t)r * n_col + i - off[r]] : 0; - } else { - if (j < off[i]) force_state = 2; - if (off_end && j > off_end[i]) force_state = 1; - tmp = force_state < 0? p[(size_t)i * n_col + j - off[i]] : 0; - } - if (state == 0) state = tmp & 7; // if requesting the H state, find state one maximizes it. - else if (!(tmp >> (state + 2) & 1)) state = 0; // if requesting other states, _state_ stays the same if it is a continuation; otherwise, set to H - if (state == 0) state = tmp & 7; // TODO: probably this line can be merged into the "else if" line right above; not 100% sure - if (force_state >= 0) state = force_state; - if (state == 0) cigar = ksw_push_cigar(km, &n_cigar, &m_cigar, cigar, 0, 1), --i, --j; // match - else if (state == 1 || (state == 3 && min_intron_len <= 0)) cigar = ksw_push_cigar(km, &n_cigar, &m_cigar, cigar, 2, 1), --i; // deletion - else if (state == 3 && min_intron_len > 0) cigar = ksw_push_cigar(km, &n_cigar, &m_cigar, cigar, 3, 1), --i; // intron - else cigar = ksw_push_cigar(km, &n_cigar, &m_cigar, cigar, 1, 1), --j; // insertion - } - if (i >= 0) cigar = ksw_push_cigar(km, &n_cigar, &m_cigar, cigar, min_intron_len > 0 && i >= min_intron_len? 3 : 2, i + 1); // first deletion - if (j >= 0) cigar = ksw_push_cigar(km, &n_cigar, &m_cigar, cigar, 1, j + 1); // first insertion - if (!is_rev) - for (i = 0; i < n_cigar>>1; ++i) // reverse CIGAR - tmp = cigar[i], cigar[i] = cigar[n_cigar-1-i], cigar[n_cigar-1-i] = tmp; - *m_cigar_ = m_cigar, *n_cigar_ = n_cigar, *cigar_ = cigar; -} - -static inline void ksw_reset_extz(ksw_extz_t *ez) -{ - ez->max_q = ez->max_t = ez->mqe_t = ez->mte_q = -1; - ez->max = 0, ez->score = ez->mqe = ez->mte = KSW_NEG_INF; - ez->n_cigar = 0, ez->zdropped = 0, ez->reach_end = 0; -} - -static inline int ksw_apply_zdrop(ksw_extz_t *ez, int is_rot, int32_t H, int a, int b, int zdrop, int8_t e) -{ - int r, t; - if (is_rot) r = a, t = b; - else r = a + b, t = a; - if (H > (int32_t)ez->max) { - ez->max = H, ez->max_t = t, ez->max_q = r - t; - } else if (t >= ez->max_t && r - t >= ez->max_q) { - int tl = t - ez->max_t, ql = (r - t) - ez->max_q, l; - l = tl > ql? tl - ql : ql - tl; - if (zdrop >= 0 && ez->max - H > zdrop + l * e) { - ez->zdropped = 1; - return 1; - } - } - return 0; -} -#endif diff --git a/ksw2_extz2_sse.cpp b/ksw2_extz2_sse.cpp deleted file mode 100644 index 02bb4c2..0000000 --- a/ksw2_extz2_sse.cpp +++ /dev/null @@ -1,305 +0,0 @@ -#include -#include -#include "ksw2.h" - -#ifdef __SSE2__ -#include - -#ifdef KSW_SSE2_ONLY -#undef __SSE4_1__ -#endif - -#ifdef __SSE4_1__ -#include -#endif - -#ifdef KSW_CPU_DISPATCH -#ifdef __SSE4_1__ -void ksw_extz2_sse41(void *km, int qlen, const uint8_t *query, int tlen, const uint8_t *target, int8_t m, const int8_t *mat, int8_t q, int8_t e, int w, int zdrop, int end_bonus, int flag, ksw_extz_t *ez) -#else -void ksw_extz2_sse2(void *km, int qlen, const uint8_t *query, int tlen, const uint8_t *target, int8_t m, const int8_t *mat, int8_t q, int8_t e, int w, int zdrop, int end_bonus, int flag, ksw_extz_t *ez) -#endif -#else -void ksw_extz2_sse(void *km, int qlen, const uint8_t *query, int tlen, const uint8_t *target, int8_t m, const int8_t *mat, int8_t q, int8_t e, int w, int zdrop, int end_bonus, int flag, ksw_extz_t *ez) -#endif // ~KSW_CPU_DISPATCH -{ -#define __dp_code_block1 \ - z = _mm_add_epi8(_mm_load_si128(&s[t]), qe2_); \ - xt1 = _mm_load_si128(&x[t]); /* xt1 <- x[r-1][t..t+15] */ \ - tmp = _mm_srli_si128(xt1, 15); /* tmp <- x[r-1][t+15] */ \ - xt1 = _mm_or_si128(_mm_slli_si128(xt1, 1), x1_); /* xt1 <- x[r-1][t-1..t+14] */ \ - x1_ = tmp; \ - vt1 = _mm_load_si128(&v[t]); /* vt1 <- v[r-1][t..t+15] */ \ - tmp = _mm_srli_si128(vt1, 15); /* tmp <- v[r-1][t+15] */ \ - vt1 = _mm_or_si128(_mm_slli_si128(vt1, 1), v1_); /* vt1 <- v[r-1][t-1..t+14] */ \ - v1_ = tmp; \ - a = _mm_add_epi8(xt1, vt1); /* a <- x[r-1][t-1..t+14] + v[r-1][t-1..t+14] */ \ - ut = _mm_load_si128(&u[t]); /* ut <- u[t..t+15] */ \ - b = _mm_add_epi8(_mm_load_si128(&y[t]), ut); /* b <- y[r-1][t..t+15] + u[r-1][t..t+15] */ - -#define __dp_code_block2 \ - z = _mm_max_epu8(z, b); /* z = max(z, b); this works because both are non-negative */ \ - z = _mm_min_epu8(z, max_sc_); \ - _mm_store_si128(&u[t], _mm_sub_epi8(z, vt1)); /* u[r][t..t+15] <- z - v[r-1][t-1..t+14] */ \ - _mm_store_si128(&v[t], _mm_sub_epi8(z, ut)); /* v[r][t..t+15] <- z - u[r-1][t..t+15] */ \ - z = _mm_sub_epi8(z, q_); \ - a = _mm_sub_epi8(a, z); \ - b = _mm_sub_epi8(b, z); - - int r, t, qe = q + e, n_col_, *off = 0, *off_end = 0, tlen_, qlen_, last_st, last_en, wl, wr, max_sc, min_sc; - int with_cigar = !(flag&KSW_EZ_SCORE_ONLY), approx_max = !!(flag&KSW_EZ_APPROX_MAX); - int32_t *H = 0, H0 = 0, last_H0_t = 0; - uint8_t *qr, *sf, *mem, *mem2 = 0; - __m128i q_, qe2_, zero_, flag1_, flag2_, flag8_, flag16_, sc_mch_, sc_mis_, sc_N_, m1_, max_sc_; - __m128i *u, *v, *x, *y, *s, *p = 0; - - ksw_reset_extz(ez); - if (m <= 0 || qlen <= 0 || tlen <= 0) return; - - zero_ = _mm_set1_epi8(0); - q_ = _mm_set1_epi8(q); - qe2_ = _mm_set1_epi8((q + e) * 2); - flag1_ = _mm_set1_epi8(1); - flag2_ = _mm_set1_epi8(2); - flag8_ = _mm_set1_epi8(0x08); - flag16_ = _mm_set1_epi8(0x10); - sc_mch_ = _mm_set1_epi8(mat[0]); - sc_mis_ = _mm_set1_epi8(mat[1]); - sc_N_ = mat[m*m-1] == 0? _mm_set1_epi8(-e) : _mm_set1_epi8(mat[m*m-1]); - m1_ = _mm_set1_epi8(m - 1); // wildcard - max_sc_ = _mm_set1_epi8(mat[0] + (q + e) * 2); - - if (w < 0) w = tlen > qlen? tlen : qlen; - wl = wr = w; - tlen_ = (tlen + 15) / 16; - n_col_ = qlen < tlen? qlen : tlen; - n_col_ = ((n_col_ < w + 1? n_col_ : w + 1) + 15) / 16 + 1; - qlen_ = (qlen + 15) / 16; - for (t = 1, max_sc = mat[0], min_sc = mat[1]; t < m * m; ++t) { - max_sc = max_sc > mat[t]? max_sc : mat[t]; - min_sc = min_sc < mat[t]? min_sc : mat[t]; - } - if (-min_sc > 2 * (q + e)) return; // otherwise, we won't see any mismatches - - mem = (uint8_t*)kcalloc(km, tlen_ * 6 + qlen_ + 1, 16); - u = (__m128i*)(((size_t)mem + 15) >> 4 << 4); // 16-byte aligned - v = u + tlen_, x = v + tlen_, y = x + tlen_, s = y + tlen_, sf = (uint8_t*)(s + tlen_), qr = sf + tlen_ * 16; - if (!approx_max) { - H = (int32_t*)kmalloc(km, tlen_ * 16 * 4); - for (t = 0; t < tlen_ * 16; ++t) H[t] = KSW_NEG_INF; - } - if (with_cigar) { - mem2 = (uint8_t*)kmalloc(km, ((size_t)(qlen + tlen - 1) * n_col_ + 1) * 16); - p = (__m128i*)(((size_t)mem2 + 15) >> 4 << 4); - off = (int*)kmalloc(km, (qlen + tlen - 1) * sizeof(int) * 2); - off_end = off + qlen + tlen - 1; - } - - for (t = 0; t < qlen; ++t) qr[t] = query[qlen - 1 - t]; - memcpy(sf, target, tlen); - - for (r = 0, last_st = last_en = -1; r < qlen + tlen - 1; ++r) { - int st = 0, en = tlen - 1, st0, en0, st_, en_; - int8_t x1, v1; - uint8_t *qrr = qr + (qlen - 1 - r), *u8 = (uint8_t*)u, *v8 = (uint8_t*)v; - __m128i x1_, v1_; - // find the boundaries - if (st < r - qlen + 1) st = r - qlen + 1; - if (en > r) en = r; - if (st < (r-wr+1)>>1) st = (r-wr+1)>>1; // take the ceil - if (en > (r+wl)>>1) en = (r+wl)>>1; // take the floor - if (st > en) { - ez->zdropped = 1; - break; - } - st0 = st, en0 = en; - st = st / 16 * 16, en = (en + 16) / 16 * 16 - 1; - // set boundary conditions - if (st > 0) { - if (st - 1 >= last_st && st - 1 <= last_en) - x1 = ((uint8_t*)x)[st - 1], v1 = v8[st - 1]; // (r-1,s-1) calculated in the last round - else x1 = v1 = 0; // not calculated; set to zeros - } else x1 = 0, v1 = r? q : 0; - if (en >= r) ((uint8_t*)y)[r] = 0, u8[r] = r? q : 0; - // loop fission: set scores first - if (!(flag & KSW_EZ_GENERIC_SC)) { - for (t = st0; t <= en0; t += 16) { - __m128i sq, st, tmp, mask; - sq = _mm_loadu_si128((__m128i*)&sf[t]); - st = _mm_loadu_si128((__m128i*)&qrr[t]); - mask = _mm_or_si128(_mm_cmpeq_epi8(sq, m1_), _mm_cmpeq_epi8(st, m1_)); - tmp = _mm_cmpeq_epi8(sq, st); -#ifdef __SSE4_1__ - tmp = _mm_blendv_epi8(sc_mis_, sc_mch_, tmp); - tmp = _mm_blendv_epi8(tmp, sc_N_, mask); -#else - tmp = _mm_or_si128(_mm_andnot_si128(tmp, sc_mis_), _mm_and_si128(tmp, sc_mch_)); - tmp = _mm_or_si128(_mm_andnot_si128(mask, tmp), _mm_and_si128(mask, sc_N_)); -#endif - _mm_storeu_si128((__m128i*)((uint8_t*)s + t), tmp); - } - } else { - for (t = st0; t <= en0; ++t) - ((uint8_t*)s)[t] = mat[sf[t] * m + qrr[t]]; - } - // core loop - x1_ = _mm_cvtsi32_si128(x1); - v1_ = _mm_cvtsi32_si128(v1); - st_ = st / 16, en_ = en / 16; - assert(en_ - st_ + 1 <= n_col_); - if (!with_cigar) { // score only - for (t = st_; t <= en_; ++t) { - __m128i z, a, b, xt1, vt1, ut, tmp; - __dp_code_block1; -#ifdef __SSE4_1__ - z = _mm_max_epi8(z, a); // z = z > a? z : a (signed) -#else // we need to emulate SSE4.1 intrinsics _mm_max_epi8() - z = _mm_and_si128(z, _mm_cmpgt_epi8(z, zero_)); // z = z > 0? z : 0; - z = _mm_max_epu8(z, a); // z = max(z, a); this works because both are non-negative -#endif - __dp_code_block2; -#ifdef __SSE4_1__ - _mm_store_si128(&x[t], _mm_max_epi8(a, zero_)); - _mm_store_si128(&y[t], _mm_max_epi8(b, zero_)); -#else - tmp = _mm_cmpgt_epi8(a, zero_); - _mm_store_si128(&x[t], _mm_and_si128(a, tmp)); - tmp = _mm_cmpgt_epi8(b, zero_); - _mm_store_si128(&y[t], _mm_and_si128(b, tmp)); -#endif - } - } else if (!(flag&KSW_EZ_RIGHT)) { // gap left-alignment - __m128i *pr = p + (size_t)r * n_col_ - st_; - off[r] = st, off_end[r] = en; - for (t = st_; t <= en_; ++t) { - __m128i d, z, a, b, xt1, vt1, ut, tmp; - __dp_code_block1; - d = _mm_and_si128(_mm_cmpgt_epi8(a, z), flag1_); // d = a > z? 1 : 0 -#ifdef __SSE4_1__ - z = _mm_max_epi8(z, a); // z = z > a? z : a (signed) - tmp = _mm_cmpgt_epi8(b, z); - d = _mm_blendv_epi8(d, flag2_, tmp); // d = b > z? 2 : d -#else // we need to emulate SSE4.1 intrinsics _mm_max_epi8() and _mm_blendv_epi8() - z = _mm_and_si128(z, _mm_cmpgt_epi8(z, zero_)); // z = z > 0? z : 0; - z = _mm_max_epu8(z, a); // z = max(z, a); this works because both are non-negative - tmp = _mm_cmpgt_epi8(b, z); - d = _mm_or_si128(_mm_andnot_si128(tmp, d), _mm_and_si128(tmp, flag2_)); // d = b > z? 2 : d; emulating blendv -#endif - __dp_code_block2; - tmp = _mm_cmpgt_epi8(a, zero_); - _mm_store_si128(&x[t], _mm_and_si128(tmp, a)); - d = _mm_or_si128(d, _mm_and_si128(tmp, flag8_)); // d = a > 0? 0x08 : 0 - tmp = _mm_cmpgt_epi8(b, zero_); - _mm_store_si128(&y[t], _mm_and_si128(tmp, b)); - d = _mm_or_si128(d, _mm_and_si128(tmp, flag16_)); // d = b > 0? 0x10 : 0 - _mm_store_si128(&pr[t], d); - } - } else { // gap right-alignment - __m128i *pr = p + (size_t)r * n_col_ - st_; - off[r] = st, off_end[r] = en; - for (t = st_; t <= en_; ++t) { - __m128i d, z, a, b, xt1, vt1, ut, tmp; - __dp_code_block1; - d = _mm_andnot_si128(_mm_cmpgt_epi8(z, a), flag1_); // d = z > a? 0 : 1 -#ifdef __SSE4_1__ - z = _mm_max_epi8(z, a); // z = z > a? z : a (signed) - tmp = _mm_cmpgt_epi8(z, b); - d = _mm_blendv_epi8(flag2_, d, tmp); // d = z > b? d : 2 -#else // we need to emulate SSE4.1 intrinsics _mm_max_epi8() and _mm_blendv_epi8() - z = _mm_and_si128(z, _mm_cmpgt_epi8(z, zero_)); // z = z > 0? z : 0; - z = _mm_max_epu8(z, a); // z = max(z, a); this works because both are non-negative - tmp = _mm_cmpgt_epi8(z, b); - d = _mm_or_si128(_mm_andnot_si128(tmp, flag2_), _mm_and_si128(tmp, d)); // d = z > b? d : 2; emulating blendv -#endif - __dp_code_block2; - tmp = _mm_cmpgt_epi8(zero_, a); - _mm_store_si128(&x[t], _mm_andnot_si128(tmp, a)); - d = _mm_or_si128(d, _mm_andnot_si128(tmp, flag8_)); // d = 0 > a? 0 : 0x08 - tmp = _mm_cmpgt_epi8(zero_, b); - _mm_store_si128(&y[t], _mm_andnot_si128(tmp, b)); - d = _mm_or_si128(d, _mm_andnot_si128(tmp, flag16_)); // d = 0 > b? 0 : 0x10 - _mm_store_si128(&pr[t], d); - } - } - if (!approx_max) { // find the exact max with a 32-bit score array - int32_t max_H, max_t; - // compute H[], max_H and max_t - if (r > 0) { - int32_t HH[4], tt[4], en1 = st0 + (en0 - st0) / 4 * 4, i; - __m128i max_H_, max_t_, qe_; - max_H = H[en0] = en0 > 0? H[en0-1] + u8[en0] - qe : H[en0] + v8[en0] - qe; // special casing the last element - max_t = en0; - max_H_ = _mm_set1_epi32(max_H); - max_t_ = _mm_set1_epi32(max_t); - qe_ = _mm_set1_epi32(q + e); - for (t = st0; t < en1; t += 4) { // this implements: H[t]+=v8[t]-qe; if(H[t]>max_H) max_H=H[t],max_t=t; - __m128i H1, tmp, t_; - H1 = _mm_loadu_si128((__m128i*)&H[t]); - t_ = _mm_setr_epi32(v8[t], v8[t+1], v8[t+2], v8[t+3]); - H1 = _mm_add_epi32(H1, t_); - H1 = _mm_sub_epi32(H1, qe_); - _mm_storeu_si128((__m128i*)&H[t], H1); - t_ = _mm_set1_epi32(t); - tmp = _mm_cmpgt_epi32(H1, max_H_); -#ifdef __SSE4_1__ - max_H_ = _mm_blendv_epi8(max_H_, H1, tmp); - max_t_ = _mm_blendv_epi8(max_t_, t_, tmp); -#else - max_H_ = _mm_or_si128(_mm_and_si128(tmp, H1), _mm_andnot_si128(tmp, max_H_)); - max_t_ = _mm_or_si128(_mm_and_si128(tmp, t_), _mm_andnot_si128(tmp, max_t_)); -#endif - } - _mm_storeu_si128((__m128i*)HH, max_H_); - _mm_storeu_si128((__m128i*)tt, max_t_); - for (i = 0; i < 4; ++i) - if (max_H < HH[i]) max_H = HH[i], max_t = tt[i] + i; - for (; t < en0; ++t) { // for the rest of values that haven't been computed with SSE - H[t] += (int32_t)v8[t] - qe; - if (H[t] > max_H) - max_H = H[t], max_t = t; - } - } else H[0] = v8[0] - qe - qe, max_H = H[0], max_t = 0; // special casing r==0 - // update ez - if (en0 == tlen - 1 && H[en0] > ez->mte) - ez->mte = H[en0], ez->mte_q = r - en; - if (r - st0 == qlen - 1 && H[st0] > ez->mqe) - ez->mqe = H[st0], ez->mqe_t = st0; - if (ksw_apply_zdrop(ez, 1, max_H, r, max_t, zdrop, e)) break; - if (r == qlen + tlen - 2 && en0 == tlen - 1) - ez->score = H[tlen - 1]; - } else { // find approximate max; Z-drop might be inaccurate, too. - if (r > 0) { - if (last_H0_t >= st0 && last_H0_t <= en0 && last_H0_t + 1 >= st0 && last_H0_t + 1 <= en0) { - int32_t d0 = v8[last_H0_t] - qe; - int32_t d1 = u8[last_H0_t + 1] - qe; - if (d0 > d1) H0 += d0; - else H0 += d1, ++last_H0_t; - } else if (last_H0_t >= st0 && last_H0_t <= en0) { - H0 += v8[last_H0_t] - qe; - } else { - ++last_H0_t, H0 += u8[last_H0_t] - qe; - } - if ((flag & KSW_EZ_APPROX_DROP) && ksw_apply_zdrop(ez, 1, H0, r, last_H0_t, zdrop, e)) break; - } else H0 = v8[0] - qe - qe, last_H0_t = 0; - if (r == qlen + tlen - 2 && en0 == tlen - 1) - ez->score = H0; - } - last_st = st, last_en = en; - //for (t = st0; t <= en0; ++t) printf("(%d,%d)\t(%d,%d,%d,%d)\t%d\n", r, t, ((int8_t*)u)[t], ((int8_t*)v)[t], ((int8_t*)x)[t], ((int8_t*)y)[t], H[t]); // for debugging - } - kfree(km, mem); - if (!approx_max) kfree(km, H); - if (with_cigar) { // backtrack - int rev_cigar = !!(flag & KSW_EZ_REV_CIGAR); - if (!ez->zdropped && !(flag&KSW_EZ_EXTZ_ONLY)) { - ksw_backtrack(km, 1, rev_cigar, 0, (uint8_t*)p, off, off_end, n_col_*16, tlen-1, qlen-1, &ez->m_cigar, &ez->n_cigar, &ez->cigar); - } else if (!ez->zdropped && (flag&KSW_EZ_EXTZ_ONLY) && ez->mqe + end_bonus > (int)ez->max) { - ez->reach_end = 1; - ksw_backtrack(km, 1, rev_cigar, 0, (uint8_t*)p, off, off_end, n_col_*16, ez->mqe_t, qlen-1, &ez->m_cigar, &ez->n_cigar, &ez->cigar); - } else if (ez->max_t >= 0 && ez->max_q >= 0) { - ksw_backtrack(km, 1, rev_cigar, 0, (uint8_t*)p, off, off_end, n_col_*16, ez->max_t, ez->max_q, &ez->m_cigar, &ez->n_cigar, &ez->cigar); - } - kfree(km, mem2); kfree(km, off); - } -} -#endif // __SSE2__ diff --git a/main.cpp b/main.cpp index 735b92c..351dfed 100644 --- a/main.cpp +++ b/main.cpp @@ -4,224 +4,14 @@ #include "Process_Read.h" #include "Assembly.h" #include "Levenshtein_distance.h" -#include "edlib.h" -/********************************for debug***************************************/ -///使用这个函数的时候,必须把Counting_multiple_thr()里的destory_Total_Count_Table(&TCB)注释掉 -void debug_Counting() -{ - init_kseq(read_file_name); - Verify_Counting(); - fprintf(stderr, "debug over!\n"); - destory_kseq(); -} - - -int matrix[1000][1000] = {0}; -///y_length > x_length -int edit_distance_normal(char* y, int y_length, char* x, int x_length) -{ memset(matrix, 0, sizeof(matrix)); - - int i, j; - for (i = 0; i <= x_length; i++) - { - matrix[i][0] = i; - } - - int digonal, up, left, min; - - ///一列列算的 - for (i = 0; i < x_length; i++) - { - for (j = 0; j < y_length; j++) - { - ///matrix[i + 1][j + 1] - digonal = matrix[i][j] + (x[i] != y[j]); - up = matrix[i + 1][j] + 1; - left = matrix[i][j + 1] + 1; - min = digonal; - if (up < min) - { - min = up; - } - - if (left< min) - { - min = left; - } - - matrix[i + 1][j + 1] = min; - } - } - - min = 999999; - for (j = 0; j <= y_length; j++) - { - if (matrix[i][j] < min) - { - min = matrix[i][j]; - } - } - - - return min; -} - - -///y_length > x_length -int edit_distance_normal_banded(char* y, int y_length, char* x, int x_length, int error) -{ memset(matrix, 0, sizeof(matrix)); - - int i, j; - for (i = 0; i <= x_length; i++) - { - for (j = 0; j <= y_length; j++) - { - matrix[i][j] = 1000000; - } - } - - for (i = 0; i <= x_length; i++) - { - matrix[i][0] = i; - } - - for (i = 0; i <= y_length; i++) - { - matrix[0][i] = 0; - } - - int banded_length = error*2 + 1; - - int digonal, up, left, min; - - - for (i = 0; i < x_length; i++) - { - ///for (j = 0; j < y_length; j++) - for (j = i; j < banded_length + i; j++) - { - ///matrix[i + 1][j + 1] - digonal = matrix[i][j] + (x[i] != y[j]); - up = matrix[i + 1][j] + 1; - left = matrix[i][j + 1] + 1; - min = digonal; - if (up < min) - { - min = up; - } - - if (left< min) - { - min = left; - } - - matrix[i + 1][j + 1] = min; - } - } - min = 999999; - for (j = 0; j <= y_length; j++) - ///for (j = i; j < banded_length + i; j++) - { - if (matrix[i][j] < min) - { - min = matrix[i][j]; - } - } - - - return min; -} - -void debug_edit_distance() -{ - /** - char* x = "TTCCATACGATTCCATTCAATTCGAGACCATTCTATTCCTGTCCATTCCTTGTGGTTCGATTCCATTTCACTCTAGTCCATTCCATTCCATTCAATTCCATTCGACTCTATTCCGTTCCACTCAATTCCATTCCATTCGATTCCATTTTTTTCGAGAACCTTCCATTACACTCCCTTCCATTCCAGTGCATTCCATTCCAGTCTCTTCAGTTCGATTCCATTCCATTCGTTTCGATTCCTTTCCATTCCAGCCCATTCCATTCCATTCCATTCCTTTCCTTTCCGTTTCATTAGATTCCATTGCATTCGATTCCATTCAAATCAATTCCGTTCTATTCAATTTGATTCAT"; - char* y = "CCATACGATTCCATTCAATTCGAGACCATTCTATTCCTGTCCATTCCTTGTGGTTCGATTCCATTTCACTCTAGTCCATTCCATTCCATTCAATTCCATTCGACTCTATTCCGTTCCATTCAATTCCATTCCATTCGATTCCATTTTTTTCGAGAACCTTCCATTACACTCCCTTCCATTCCAGTGCATTCCATTCCAGTCTCTTCACTTCGATTCCATTCCATTCGTTTCGATTCCTTTCCATTCCAGCCCATTCCATTCCATTCCATTCCTTTCCTTTCCGTTTCATTAGATTCCATTGCATTCCATTCCATTCAATTCAATTCCGTGCTATTCAATTTGATTCATTTCCATTTAATTCCATTCCATTAGATTCCATT"; - **/ - unsigned short toold = 15; - char* x - = "GAAAGAGAATCAAATGGAATTGAATCGAATGGAATCGAATGGATTGGAAAGGAATAGAATGGAATGGAATGGAATTGACTCAAATGGAATGGACTAGAATGGAATGGATTCGAATGGAAGGCAAAGGAATGGAATCTATCGGAATGGACTGTAATGGAATGGAATGGAAGGGATTGGAATGGATTCGAATGTAATGGACTGCAATAGAAAGGATTCGAATGGAATGAAAAAGAATTGAATGGAATAGAACAGAATGGAATCAAATCGAAGGAAATGGAATGGAATAGAAAGGAATGGAATGAAATGGAATGGAAAGGATTCGAATGGAATGCAATCGAATGGAATGGAATCGAACGGAATGGAATAAAATGGAAG"; - char* y = - "GAAAGAGAATCAAATGGAATTGAATCGAATGGAATCGAATGGATTGGAAAGGAATAGAATGGAATGGAATGGAATGGACTCAAATGGAATGTACTAGAATGGAATGGATTCGAATGGAAGGCAAAGGAATGGAATCTATTGGAATGGACTGTAATGGAATGGAATGGAAGGGATTGGAATGGACTCGAATGGAATGGACTGCAATAGAAAGGATTCGAATGGAATGAAAAAGAATTGAATGGAATAGAACAGAATGGAATCAAATCGAATGAAATGGAATGGAATAGAAAGGAATGGAATGAAATGGAATGGAAAGGATTCGAATGGAATGCAATCGAATGGAATGGAATCGAACGGAATGGAATAAATTTTCTG"; - fprintf(stderr, "x_length: %u\n", strlen(x)); - fprintf(stderr, "y_length: %u\n", strlen(y)); - - - EdlibAlignResult result = edlibAlign(x, strlen(x), y, strlen(y), - edlibNewAlignConfig(toold, EDLIB_MODE_HW, EDLIB_TASK_PATH, NULL, 0)); - - if (result.status == EDLIB_STATUS_OK) { - - fprintf(stderr, "****\nedlib: %d, alignmentLength: %d, startLocations: %d, endLocations: %d\n", - result.editDistance, result.alignmentLength, result.startLocations[0], result.endLocations[0]); - char* cigar = edlibAlignmentToCigar(result.alignment, result.alignmentLength, EDLIB_CIGAR_STANDARD); - fprintf(stderr,"%s\n", cigar); - free(cigar); - } - edlibFreeAlignResult(result); - - - - unsigned int error; - int end_site = Reserve_Banded_BPM(y, strlen(y), x, strlen(x), toold, &error); - - fprintf(stderr, "BPM: error: %u, end_site: %u\n", error, end_site); - - - unsigned short band_length=(toold+1)*3-1-1-toold; - unsigned short band_down=toold-1; - unsigned short band_blew=2*(toold+1)-1-1; - - - - int return_err = 99999; - ///注意pattern/text和band_down/band_blew是反的 - Reserve_Banded_BPM_new(y, strlen(y), x, strlen(x), - toold,band_blew,band_down,band_length, &return_err, 0); - - fprintf(stderr, "new BPM: error: %u\n", return_err); - - - return_err = edit_distance_normal(y, strlen(y), x, strlen(x)); - fprintf(stderr, "edit_distance_normal: error: %u\n", return_err); - - return_err = edit_distance_normal_banded(y, strlen(y), x, strlen(x), toold); - fprintf(stderr, "edit_distance_normal_banded: error: %u\n", return_err); - - end_site = Reserve_Banded_BPM_debug(y, strlen(y), x, strlen(x), toold, &error, matrix); - - fprintf(stderr, "BPM debug: error: %u, end_site: %u\n", error, end_site); -} - int main(int argc, char *argv[]) { + init_opt(&asm_opt); - if (!CommandLine_process(argc, argv)) - return 1; + if (!CommandLine_process(argc, argv, &asm_opt)) return 1; - fprintf(stdout, "Will perform %d round of error correction...\n", number_of_round); + Correct_Reads(asm_opt.number_of_round); - fprintf(stdout, "defined k_mer_min_freq by user: %d\n", k_mer_min_freq); - fprintf(stdout, "defined k_mer_max_freq by user: %d\n", k_mer_max_freq); - - - fprintf(stdout, "k-mer length: %d\n",k_mer_length); - fprintf(stdout, "coverage: %d\n",coverage); - fprintf(stdout, "read_graph: %d\n", read_graph); - fprintf(stdout, "adapterLen: %d\n", adapterLen); - fflush(stdout); - - - - - - if(required_read_name) - { - fprintf(stdout, "required_read_name: %s\n", required_read_name); - } - - Correct_Reads(number_of_round); - return 1; }