diff --git a/src/obi_clean.c b/src/obi_clean.c index a611603..2eecab8 100755 --- a/src/obi_clean.c +++ b/src/obi_clean.c @@ -160,33 +160,23 @@ int obi_clean(const char* dms_name, { char* o_view_name_temp = NULL; float p; - index_t i, j, l; + index_t l; + index_t k; index_t seq_count; index_t* line_selection = NULL; - double score; - bool above_threshold; - int lcs_length; - int ali_length; Kmer_table_p ktable; - Obi_blob_p blob1; - Obi_blob_p blob2; - int lcs_min; int sample_count; - int sample; - int s1_count; - int s2_count; bool head; int head_count; int internal_count; int singleton_count; int ind_sample_count; char status; + int samp; byte_t* alignment_result_array = NULL; - byte_t ali_result; int* complete_sample_count_array = NULL; - int* sample_count_array = NULL; Obi_blob_p* blob_array = NULL; OBIDMS_p dms = NULL; @@ -201,11 +191,8 @@ int obi_clean(const char* dms_name, OBIDMS_column_p singletoncount_column = NULL; OBIDMS_column_p samplecount_column = NULL; - byte_t no; - byte_t yes; - bool normalize = false; - int reference = 0; + int reference = 0; bool similarity_mode = false; bool stop = false; @@ -352,11 +339,10 @@ int obi_clean(const char* dms_name, obidebug(1, "\nError allocating memory for the array of sample counts, size: %lld", seq_count * sample_count * sizeof(int)); return -1; } - for (sample=0; sample < sample_count; sample++) + for (samp=0; samp < sample_count; samp++) { - sample_count_array = complete_sample_count_array+(sample*seq_count); - for (i=0; i0 && s2_count>0)) && - ((((s1_count >= s2_count) && (((double) s2_count / (double) s1_count) <= max_ratio))) || - (((s2_count >= s1_count) && (((double) s1_count / (double) s2_count) <= max_ratio))))) +#pragma omp parallel default(none) \ + shared(ali_result_array, thread_count, seq_count, blob_array, complete_sample_count_array, alignment_result_array, stop, blob1, i, \ + obi_errno, stderr, max_ratio, iseq_column, i_view, similarity_mode, reference, normalize, threshold, ktable, status_column, o_view, sample_count) + //private(ali_result_array, thread_id, i, j, p, blob1, blob2, s1_count, s2_count, \ + sample_count_array, sample, yes, no, above_threshold, ali_length, lcs_length, ali_result, score) + +{ + + //byte_t* ali_result_array = NULL; + int thread_id = 0; + index_t j; + float p; + Obi_blob_p blob2; + int s1_count; + int s2_count; + int* sample_count_array; + int sample; + byte_t no; + byte_t yes; + double score; + int lcs_min; + bool above_threshold; + int lcs_length; + int ali_length; + byte_t ali_result; + +// #ifdef _OPENMP +// thread_id = omp_get_thread_num(); +// ali_result_array = alignment_result_array+thread_id; +// #else +// ali_result_array = alignment_result_array; +// #endif + + #pragma omp for schedule(dynamic, sample_count/thread_count) + for (sample=0; sample < sample_count; sample++) + { + + sample_count_array = complete_sample_count_array+(sample*seq_count); + + // Get count for this sample + s1_count = sample_count_array[i]; + //s1_count = obi_get_int_with_elt_idx_and_col_p_in_view(i_view, sample_column, i, sample); // slower + + + for (j=i+1; j < seq_count; j++) { - yes = 0; - no = 0; - above_threshold = false; - ali_result = alignment_result_array[j]; - if (ali_result > 0) // already aligned + //fprintf(stderr, "\nthread=%d, i=%d, sample=%d, j=%d", omp_get_thread_num(),i,sample,j); + // Get second sequence + blob2 = blob_array[j]; + // blob2 = obi_get_blob_with_elt_idx_and_col_p_in_view(i_view, iseq_column, j, 0); // slower + if (blob2 == NULL) { - if (ali_result == 2) - no = 1; - else if (ali_result == 1) - yes = 1; + obidebug(1, "\nError retrieving sequences to align"); + stop = true; } - else // never compared before + + // Get count for this sample + s2_count = sample_count_array[j]; + //s2_count = obi_get_int_with_elt_idx_and_col_p_in_view(i_view, sample_column, j, sample); // slower + + // Check all ratios + if (((s1_count!=OBIInt_NA && s2_count!=OBIInt_NA) && (s1_count>0 && s2_count>0)) && + ((((s1_count >= s2_count) && (((double) s2_count / (double) s1_count) <= max_ratio))) || + (((s2_count >= s1_count) && (((double) s1_count / (double) s2_count) <= max_ratio))))) { - // Check if the sequences are identical in a quick way (same index in the same indexer) - if (obi_get_index_with_elt_idx_and_col_p_in_view(i_view, iseq_column, i, 0) == obi_get_index_with_elt_idx_and_col_p_in_view(i_view, iseq_column, j, 0)) - above_threshold = true; - else // the sequences aren't identical + + yes = 0; + no = 0; + above_threshold = false; + ali_result = ali_result_array[j]; + if (ali_result > 0) // already aligned { - // kmer filter - align_filters(ktable, blob1, blob2, i, j, threshold, normalize, reference, similarity_mode, &score, &lcs_min, false); - - // Compute alignment score if filter passed - if (score == -1.0) - score = obiblob_sse_banded_lcs_align(blob1, blob2, threshold, normalize, reference, similarity_mode, &lcs_length, &ali_length); - - above_threshold = ((score >= 0) && (score <= threshold)); + if (ali_result == 2) + no = 1; + else if (ali_result == 1) + yes = 1; } - } - - if (yes || above_threshold) - { - if (yes == 0) - // Set ali result as above the threshold (value 1) - alignment_result_array[j] = 1; - - // Might be worth having arrays to read values too for some datasets but unlikely - // label as head or internal - #pragma omp critical + else // never compared before { - if (s1_count >= s2_count) + // Check if the sequences are identical in a quick way (same index in the same indexer) + if (obi_get_index_with_elt_idx_and_col_p_in_view(i_view, iseq_column, i, 0) == obi_get_index_with_elt_idx_and_col_p_in_view(i_view, iseq_column, j, 0)) + above_threshold = true; + else // the sequences aren't identical { - if (obi_get_char_with_elt_idx_and_col_p_in_view(o_view, status_column, i, sample) == 's') // seq can become head ONLY if it's a singleton - { - if (obi_set_char_with_elt_idx_and_col_p_in_view(o_view, status_column, i, sample, 'h') < 0) - stop = true; - } - // Otherwise it's an internal (do nothing) - // Label other sequence as internal no matter what - if (obi_set_char_with_elt_idx_and_col_p_in_view(o_view, status_column, j, sample, 'i') < 0) - stop = true; + // kmer filter + align_filters(ktable, blob1, blob2, i, j, threshold, normalize, reference, similarity_mode, &score, &lcs_min, false); + + // Compute alignment score if filter passed + if (score == -1.0) + score = obiblob_sse_banded_lcs_align(blob1, blob2, threshold, normalize, reference, similarity_mode, &lcs_length, &ali_length); + + above_threshold = ((score >= 0) && (score <= threshold)); } - else // Same thing but with sequences switched + } + + if (yes || above_threshold) + { + if (yes == 0) + // Set ali result as above the threshold (value 1) + ali_result_array[j] = 1; + + // Might be worth having arrays to read values too for some datasets but unlikely + // label as head or internal + #pragma omp critical { - if (obi_get_char_with_elt_idx_and_col_p_in_view(o_view, status_column, j, sample) == 's') // seq can become head ONLY if it's a singleton + if (s1_count >= s2_count) + { + if (obi_get_char_with_elt_idx_and_col_p_in_view(o_view, status_column, i, sample) == 's') // seq can become head ONLY if it's a singleton { - if (obi_set_char_with_elt_idx_and_col_p_in_view(o_view, status_column, j, sample, 'h') < 0) + if (obi_set_char_with_elt_idx_and_col_p_in_view(o_view, status_column, i, sample, 'h') < 0) stop = true; } - if (obi_set_char_with_elt_idx_and_col_p_in_view(o_view, status_column, i, sample, 'i') < 0) - stop = true; + // Otherwise it's an internal (do nothing) + // Label other sequence as internal no matter what + if (obi_set_char_with_elt_idx_and_col_p_in_view(o_view, status_column, j, sample, 'i') < 0) + stop = true; + } + else // Same thing but with sequences switched + { + if (obi_get_char_with_elt_idx_and_col_p_in_view(o_view, status_column, j, sample) == 's') // seq can become head ONLY if it's a singleton + { + if (obi_set_char_with_elt_idx_and_col_p_in_view(o_view, status_column, j, sample, 'h') < 0) + stop = true; + } + if (obi_set_char_with_elt_idx_and_col_p_in_view(o_view, status_column, i, sample, 'i') < 0) + stop = true; + } } } + + else if (no == 0) + // Set ali result as above the threshold (value 2) + ali_result_array[j] = 2; } - else if (no == 0) - // Set ali result as above the threshold (value 2) - alignment_result_array[j] = 2; } - } + } - if (stop) - return -1; + + // Reset ali result array to 0 + memset(ali_result_array, 0, seq_count); + } - // Reset ali result array to 0 - memset(alignment_result_array, 0, seq_count); } free_kmer_tables(ktable, seq_count); @@ -533,6 +561,9 @@ int obi_clean(const char* dms_name, fprintf(stderr, "\n"); + if (stop) + return -1; + if (heads_only) { line_selection = malloc((o_view->infos)->line_count * sizeof(index_t)); @@ -545,11 +576,11 @@ int obi_clean(const char* dms_name, l=0; } - for (i=0; i