diff --git a/src/obi_align.c b/src/obi_align.c index 390ff60..a23e27f 100644 --- a/src/obi_align.c +++ b/src/obi_align.c @@ -31,6 +31,360 @@ // use openMP pragmas +/************************************************************************** + * + * D E C L A R A T I O N O F T H E P R I V A T E F U N C T I O N S + * + **************************************************************************/ + + +/** + * @brief Internal function creating the columns where the alignment results are written. + * + * @param output_view A pointer on the writable view where the columns should be created. + * @param id1_indexer_name The name of the indexer where the id of the 1st sequence aligned is indexed. + * @param id2_indexer_name The name of the indexer where the id of the 2nd sequence aligned is indexed. + * @param seq1_indexer_name The name of the indexer where the 1st sequence aligned is indexed (needed only if print_seq is True). + * @param seq2_indexer_name The name of the indexer where the 2nd sequence aligned is indexed (needed only if print_seq is True). + * @param print_seq A boolean indicating whether the aligned sequences should be copied in the output view. + * @param print_count A boolean indicating whether the aligned sequence counts should be copied in the output view. + * @param normalize Whether the score should be normalized with the reference sequence length. + * @param reference The reference length. 0: The alignement length; 1: The longest sequence's length; 2: The shortest sequence's length. + * @param similarity_mode Whether the score should be expressed in similarity (true) or distance (false). + * + * @retval 0 if the operation was successfully completed. + * @retval -1 if an error occurred. + * + * @since December 2016 + * @author Celine Mercier (celine.mercier@metabarcoding.org) + */ +static int create_alignment_output_columns(Obiview_p output_view, + const char* id1_indexer_name, + const char* id2_indexer_name, + const char* seq1_indexer_name, + const char* seq2_indexer_name, + bool print_seq, bool print_count, + bool normalize, int reference, bool similarity_mode); + + +/** + * @brief Internal function printing the result of one alignment to the output view. + * + * @param output_view A pointer on the writable view where the columns should be created. + * @param line The line in the output view where the result should be written. + * @param idx1_column A pointer on the column where the index referring to the line of the first sequence aligned in the input view should be written. + * @param idx2_column A pointer on the column where the index referring to the line of the second sequence aligned in the input view should be written. + * @param idx1 The index referring to the line of the first sequence aligned in the input view. + * @param idx2 The index referring to the line of the second sequence aligned in the input view. + * @param id1_column A pointer on the column where the identifier of the first sequence aligned should be written. + * @param id2_column A pointer on the column where the identifier of the second sequence aligned should be written. + * @param id1_idx The index of the identifier of the first sequence aligned. + * @param id2_idx The index of the identifier of the second sequence aligned. + * @param print_seq A boolean indicating whether the aligned sequences should be copied in the output view. + * @param seq1_column A pointer on the column where the first sequence aligned should be written. + * @param seq2_column A pointer on the column where the second sequence aligned should be written. + * @param seq1_idx The index of the sequence of the first sequence aligned. + * @param seq2_idx The index of the sequence of the second sequence aligned. + * @param print_count A boolean indicating whether the aligned sequence counts should be copied in the output view. // Count columns not implement yet + * @param count1_column A pointer on the column where the count of the first sequence aligned should be written. + * @param count2_column A pointer on the column where the count of the second sequence aligned should be written. + * @param count1 The count of the first sequence aligned. + * @param count2 The count of the second sequence aligned. + * @param ali_length_column A pointer on the column where the alignment length should be written. + * @param ali_length The alignment length. + * @param lcs_length_column A pointer on the column where the LCS length should be written. + * @param lcs_length The LCS length. + * @param score_column A pointer on the column where the score should be written. + * @param score The alignment score. + * @param reference The reference length. 0: The alignment length; 1: The longest sequence's length; 2: The shortest sequence's length. + * @param normalize Whether the score should be normalized with the reference sequence length. + * @param similarity_mode Whether the score should be expressed in similarity (true) or distance (false). + * + * @retval 0 if the operation was successfully completed. + * @retval -1 if an error occurred. + * + * @since December 2016 + * @author Celine Mercier (celine.mercier@metabarcoding.org) + */ +static int print_alignment_result(Obiview_p output_view, + index_t line, + OBIDMS_column_p idx1_column, + OBIDMS_column_p idx2_column, + index_t idx1, + index_t idx2, + OBIDMS_column_p id1_column, + OBIDMS_column_p id2_column, + index_t id1_idx, + index_t id2_idx, + bool print_seq, + OBIDMS_column_p seq1_column, + OBIDMS_column_p seq2_column, + index_t seq1_idx, + index_t seq2_idx, +// bool print_count, +// OBIDMS_column_p count1_column, +// OBIDMS_column_p count2_column, +// int count1, +// int count2, + OBIDMS_column_p ali_length_column, + int ali_length, + OBIDMS_column_p lcs_length_column, + int lcs_length, + OBIDMS_column_p score_column, + double score, + int reference, + bool normalize, + bool similarity_mode); + + + +/************************************************************************ + * + * D E F I N I T I O N O F T H E P R I V A T E F U N C T I O N S + * + ************************************************************************/ + + +static int create_alignment_output_columns(Obiview_p output_view, + const char* id1_indexer_name, + const char* id2_indexer_name, + const char* seq1_indexer_name, + const char* seq2_indexer_name, + bool print_seq, bool print_count, + bool normalize, int reference, bool similarity_mode) +{ + // Create the column for the ids of the 1st sequence aligned + if (obi_view_add_column(output_view, ID1_COLUMN_NAME, -1, ID1_COLUMN_NAME, OBI_STR, 0, 1, NULL, id1_indexer_name, NULL, -1, ID1_COLUMN_COMMENTS, true) < 0) + { + obidebug(1, "\nError creating the first column for the sequence ids when aligning"); + return -1; + } + + // Create the column for the ids of the 2nd sequence aligned + if (obi_view_add_column(output_view, ID2_COLUMN_NAME, -1, ID2_COLUMN_NAME, OBI_STR, 0, 1, NULL, id2_indexer_name, NULL, -1, ID2_COLUMN_COMMENTS, true) < 0) + { + obidebug(1, "\nError creating the second column for the sequence ids when aligning"); + return -1; + } + + // Create the column for the index (in the input view) of the first sequences aligned + if (obi_view_add_column(output_view, IDX1_COLUMN_NAME, -1, IDX1_COLUMN_NAME, OBI_INT, 0, 1, NULL, NULL, NULL, -1, IDX1_COLUMN_COMMENTS, true) < 0) + { + obidebug(1, "\nError creating the first column for the sequence indices when aligning"); + return -1; + } + + // Create the column for the index (in the input view) of the second sequences aligned + if (obi_view_add_column(output_view, IDX2_COLUMN_NAME, -1, IDX2_COLUMN_NAME, OBI_INT, 0, 1, NULL, NULL, NULL, -1, IDX2_COLUMN_COMMENTS, true) < 0) + { + obidebug(1, "\nError creating the second column for the sequence indices when aligning"); + return -1; + } + + // Create the column for the LCS length + if (obi_view_add_column(output_view, LCS_LENGTH_COLUMN_NAME, -1, LCS_LENGTH_COLUMN_NAME, OBI_INT, 0, 1, NULL, NULL, NULL, -1, LCS_LENGTH_COLUMN_COMMENTS, true) < 0) + { + obidebug(1, "\nError creating the column for the LCS length when aligning"); + return -1; + } + + // Create the column for the alignment length if it is computed + if ((reference == ALILEN) && (normalize || !similarity_mode)) + { + if (obi_view_add_column(output_view, ALI_LENGTH_COLUMN_NAME, -1, ALI_LENGTH_COLUMN_NAME, OBI_INT, 0, 1, NULL, NULL, NULL, -1, ALI_LENGTH_COLUMN_COMMENTS, true) < 0) + { + obidebug(1, "\nError creating the column for the alignment length when aligning"); + return -1; + } + } + // Create the column for the alignment score + if (normalize) + { + if (obi_view_add_column(output_view, SCORE_COLUMN_NAME, -1, SCORE_COLUMN_NAME, OBI_FLOAT, 0, 1, NULL, NULL, NULL, -1, SCORE_COLUMN_NAME, true) < 0) + { + obidebug(1, "\nError creating the column for the score when aligning"); + return -1; + } + } + else + { + if (obi_view_add_column(output_view, SCORE_COLUMN_NAME, -1, SCORE_COLUMN_NAME, OBI_INT, 0, 1, NULL, NULL, NULL, -1, SCORE_COLUMN_NAME, true) < 0) + { + obidebug(1, "\nError creating the column for the score when aligning"); + return -1; + } + } + + if (print_seq) + { + // Create the column for the first sequences aligned + if (obi_view_add_column(output_view, SEQ1_COLUMN_NAME, -1, SEQ1_COLUMN_NAME, OBI_SEQ, 0, 1, NULL, seq1_indexer_name, NULL, -1, SEQ1_COLUMN_COMMENTS, true) < 0) + { + obidebug(1, "\nError creating the first column for the sequences when aligning"); + return -1; + } + + // Create the column for the second sequences aligned + if (obi_view_add_column(output_view, SEQ2_COLUMN_NAME, -1, SEQ2_COLUMN_NAME, OBI_SEQ, 0, 1, NULL, seq2_indexer_name, NULL, -1, SEQ2_COLUMN_COMMENTS, true) < 0) + { + obidebug(1, "\nError creating the second column for the sequences when aligning"); + return -1; + } + } +// if (print_count) // TODO count columns not implemented yet +// { +// // Create the column for the count of the first sequences aligned +// if (obi_view_add_column(output_view, COUNT1_COLUMN_NAME, -1, COUNT1_COLUMN_NAME, OBI_INT, 0, 1, NULL, NULL, NULL, -1, COUNT1_COLUMN_COMMENTS, true) < 0) +// { +// obidebug(1, "\nError creating the first column for the sequence counts when aligning"); +// return -1; +// } +// +// // Create the column for the count of the second sequences aligned +// if (obi_view_add_column(output_view, COUNT2_COLUMN_NAME, -1, COUNT2_COLUMN_NAME, OBI_INT, 0, 1, NULL, NULL, NULL, -1, COUNT2_COLUMN_COMMENTS, true) < 0) +// { +// obidebug(1, "\nError creating the second column for the sequence counts when aligning"); +// return -1; +// } +// } + + return 0; +} + + +static int print_alignment_result(Obiview_p output_view, + index_t line, + OBIDMS_column_p idx1_column, + OBIDMS_column_p idx2_column, + index_t idx1, + index_t idx2, + OBIDMS_column_p id1_column, + OBIDMS_column_p id2_column, + index_t id1_idx, + index_t id2_idx, + bool print_seq, + OBIDMS_column_p seq1_column, + OBIDMS_column_p seq2_column, + index_t seq1_idx, + index_t seq2_idx, +// bool print_count, +// OBIDMS_column_p count1_column, +// OBIDMS_column_p count2_column, +// int count1, +// int count2, + OBIDMS_column_p ali_length_column, + int ali_length, + OBIDMS_column_p lcs_length_column, + int lcs_length, + OBIDMS_column_p score_column, + double score, + int reference, + bool normalize, + bool similarity_mode) +{ + // Write line indices of the input view in the output view (to easily refer to the input sequences from the output view) + if (obi_set_int_with_elt_idx_and_col_p_in_view(output_view, idx1_column, line, 0, idx1) < 0) + { + obidebug(1, "\nError writing idx1 in a column"); + return -1; + } + if (obi_set_int_with_elt_idx_and_col_p_in_view(output_view, idx2_column, line, 0, idx2) < 0) + { + obidebug(1, "\nError writing idx2 in a column"); + return -1; + } + + // Write ids in output view + if (obi_set_index_with_elt_idx_and_col_p_in_view(output_view, id1_column, line, 0, id1_idx) < 0) + { + obidebug(1, "\nError writing id1 in a column"); + return -1; + } + if (obi_set_index_with_elt_idx_and_col_p_in_view(output_view, id2_column, line, 0, id2_idx) < 0) + { + obidebug(1, "\nError writing id2 in a column"); + return -1; + } + + // Write the sequences if needed + if (print_seq) + { + if (obi_set_index_with_elt_idx_and_col_p_in_view(output_view, seq1_column, line, 0, seq1_idx) < 0) + { + obidebug(1, "\nError writing seq1 in a column"); + return -1; + } + + if (obi_set_index_with_elt_idx_and_col_p_in_view(output_view, seq2_column, line, 0, seq2_idx) < 0) + { + obidebug(1, "\nError writing seq2 in a column"); + return -1; + } + } + +// // Write the counts if needed // TODO count columns not implemented yet +// if (print_count) +// { +// if (obi_set_index_with_elt_idx_and_col_p_in_view(output_view, count1_column, line, 0, count1) < 0) +// { +// obidebug(1, "\nError writing count1 in a column"); +// return -1; +// } +// +// if (obi_set_index_with_elt_idx_and_col_p_in_view(output_view, count2_column, line, 0, count2) < 0) +// { +// obidebug(1, "\nError writing count2 in a column"); +// return -1; +// } +// } + + // Write the alignment length if it was computed + if ((reference == ALILEN) && (normalize || !similarity_mode)) + { + if (obi_set_int_with_elt_idx_and_col_p_in_view(output_view, ali_length_column, line, 0, ali_length) < 0) + { + obidebug(1, "\nError writing alignment length in a column"); + return -1; + } + } + + // Write the LCS length + if (obi_set_int_with_elt_idx_and_col_p_in_view(output_view, lcs_length_column, line, 0, lcs_length) < 0) + { + obidebug(1, "\nError writing LCS length in a column"); + return -1; + } + + // Write score + if (normalize) + { + if (obi_set_float_with_elt_idx_and_col_p_in_view(output_view, score_column, line, 0, (obifloat_t) score) < 0) + { + obidebug(1, "\nError writing alignment score in a column"); + return -1; + } + } + else + { + if (obi_set_int_with_elt_idx_and_col_p_in_view(output_view, score_column, line, 0, (obiint_t) score) < 0) + { + obidebug(1, "\nError writing alignment score in a column"); + return -1; + } + } + + return 0; +} + + + +/********************************************************************** + * + * D E F I N I T I O N O F T H E P U B L I C F U N C T I O N S + * + **********************************************************************/ + + int obi_lcs_align_one_column(OBIDMS_p dms, const char* seq_view_name, const char* seq_column_name, const char* seq_elt_name, const char* id_column_name, const char* output_view_name, const char* output_view_comments, @@ -140,114 +494,30 @@ int obi_lcs_align_one_column(OBIDMS_p dms, const char* seq_view_name, const char } // Create the output columns - - // Create the column for the ids of the 1st sequence aligned - if (obi_view_add_column(output_view, ID1_COLUMN_NAME, -1, ID1_COLUMN_NAME, OBI_STR, 0, 1, NULL, (id_column->header)->indexer_name, NULL, -1, ID1_COLUMN_COMMENTS, true) < 0) - { - obidebug(1, "\nError creating the first column for the sequence ids when aligning"); + if (create_alignment_output_columns(output_view, + (id_column->header)->indexer_name, (id_column->header)->indexer_name, + (iseq_column->header)->indexer_name, (iseq_column->header)->indexer_name, + print_seq, print_count, normalize, reference, similarity_mode) < 0) return -1; - } id1_column = obi_view_get_column(output_view, ID1_COLUMN_NAME); - - // Create the column for the ids of the 2nd sequence aligned - if (obi_view_add_column(output_view, ID2_COLUMN_NAME, -1, ID2_COLUMN_NAME, OBI_STR, 0, 1, NULL, (id_column->header)->indexer_name, NULL, -1, ID2_COLUMN_COMMENTS, true) < 0) - { - obidebug(1, "\nError creating the second column for the sequence ids when aligning"); - return -1; - } id2_column = obi_view_get_column(output_view, ID2_COLUMN_NAME); - - // Create the column for the index (in the input view) of the first sequences aligned - if (obi_view_add_column(output_view, IDX1_COLUMN_NAME, -1, IDX1_COLUMN_NAME, OBI_INT, 0, 1, NULL, NULL, NULL, -1, IDX1_COLUMN_COMMENTS, true) < 0) - { - obidebug(1, "\nError creating the first column for the sequence indices when aligning"); - return -1; - } idx1_column = obi_view_get_column(output_view, IDX1_COLUMN_NAME); - - // Create the column for the index (in the input view) of the second sequences aligned - if (obi_view_add_column(output_view, IDX2_COLUMN_NAME, -1, IDX2_COLUMN_NAME, OBI_INT, 0, 1, NULL, NULL, NULL, -1, IDX2_COLUMN_COMMENTS, true) < 0) - { - obidebug(1, "\nError creating the second column for the sequence indices when aligning"); - return -1; - } idx2_column = obi_view_get_column(output_view, IDX2_COLUMN_NAME); - - // Create the column for the LCS length - if (obi_view_add_column(output_view, LCS_LENGTH_COLUMN_NAME, -1, LCS_LENGTH_COLUMN_NAME, OBI_INT, 0, 1, NULL, NULL, NULL, -1, LCS_LENGTH_COLUMN_COMMENTS, true) < 0) - { - obidebug(1, "\nError creating the column for the LCS length when aligning"); - return -1; - } - lcs_length_column = obi_view_get_column(output_view, LCS_LENGTH_COLUMN_NAME); - - // Create the column for the alignment length if it is computed + lcs_length_column = obi_view_get_column(output_view, LCS_LENGTH_COLUMN_NAME); if ((reference == ALILEN) && (normalize || !similarity_mode)) - { - if (obi_view_add_column(output_view, ALI_LENGTH_COLUMN_NAME, -1, ALI_LENGTH_COLUMN_NAME, OBI_INT, 0, 1, NULL, NULL, NULL, -1, ALI_LENGTH_COLUMN_COMMENTS, true) < 0) - { - obidebug(1, "\nError creating the column for the alignment length when aligning"); - return -1; - } ali_length_column = obi_view_get_column(output_view, ALI_LENGTH_COLUMN_NAME); - } - // Create the column for the alignment score - if (normalize) - { - if (obi_view_add_column(output_view, SCORE_COLUMN_NAME, -1, SCORE_COLUMN_NAME, OBI_FLOAT, 0, 1, NULL, NULL, NULL, -1, SCORE_COLUMN_NAME, true) < 0) - { - obidebug(1, "\nError creating the column for the score when aligning"); - return -1; - } - } - else - { - if (obi_view_add_column(output_view, SCORE_COLUMN_NAME, -1, SCORE_COLUMN_NAME, OBI_INT, 0, 1, NULL, NULL, NULL, -1, SCORE_COLUMN_NAME, true) < 0) - { - obidebug(1, "\nError creating the column for the score when aligning"); - return -1; - } - } score_column = obi_view_get_column(output_view, SCORE_COLUMN_NAME); - if (print_seq) { - // Create the column for the first sequences aligned - if (obi_view_add_column(output_view, SEQ1_COLUMN_NAME, -1, SEQ1_COLUMN_NAME, OBI_SEQ, 0, 1, NULL, (iseq_column->header)->indexer_name, NULL, -1, SEQ1_COLUMN_COMMENTS, true) < 0) - { - obidebug(1, "\nError creating the first column for the sequences when aligning"); - return -1; - } seq1_column = obi_view_get_column(output_view, SEQ1_COLUMN_NAME); - - // Create the column for the second sequences aligned - if (obi_view_add_column(output_view, SEQ2_COLUMN_NAME, -1, SEQ2_COLUMN_NAME, OBI_SEQ, 0, 1, NULL, (iseq_column->header)->indexer_name, NULL, -1, SEQ2_COLUMN_COMMENTS, true) < 0) - { - obidebug(1, "\nError creating the second column for the sequences when aligning"); - return -1; - } seq2_column = obi_view_get_column(output_view, SEQ2_COLUMN_NAME); } // if (print_count) // TODO count columns not implemented yet // { -// // Create the column for the count of the first sequences aligned -// if (obi_view_add_column(output_view, COUNT1_COLUMN_NAME, -1, COUNT1_COLUMN_NAME, OBI_INT, 0, 1, NULL, NULL, NULL, -1, COUNT1_COLUMN_COMMENTS, true) < 0) -// { -// obidebug(1, "\nError creating the first column for the sequence counts when aligning"); -// return -1; -// } // count1_column = obi_view_get_column(seq_view, COUNT1_COLUMN_NAME); -// -// // Create the column for the count of the second sequences aligned -// if (obi_view_add_column(output_view, COUNT2_COLUMN_NAME, -1, COUNT2_COLUMN_NAME, OBI_INT, 0, 1, NULL, NULL, NULL, -1, COUNT2_COLUMN_COMMENTS, true) < 0) -// { -// obidebug(1, "\nError creating the second column for the sequence counts when aligning"); -// return -1; -// } // count2_column = obi_view_get_column(seq_view, COUNT2_COLUMN_NAME); // } - // Build kmer tables ktable = hash_seq_column(seq_view, iseq_column, seq_elt_idx); if (ktable == NULL) @@ -301,100 +571,20 @@ int obi_lcs_align_one_column(OBIDMS_p dms, const char* seq_view_name, const char if ((score >= 0) && (((normalize || similarity_mode) && (score >= threshold)) || ((!similarity_mode && !normalize) && (score <= threshold)))) { // Print result // TODO make separate function maybe - // Write line indices of the input view in the output view (to easily refer to the input sequences from the output view) - if (obi_set_int_with_elt_idx_and_col_p_in_view(output_view, idx1_column, k, 0, i) < 0) - { - obidebug(1, "\nError writing idx1 in a column"); - return -1; - } - if (obi_set_int_with_elt_idx_and_col_p_in_view(output_view, idx2_column, k, 0, j) < 0) - { - obidebug(1, "\nError writing idx2 in a column"); - return -1; - } - // Get ids idx id1_idx = obi_get_index_with_elt_idx_and_col_p_in_view(seq_view, id_column, i, 0); // TODO Could there be multiple IDs per line? id2_idx = obi_get_index_with_elt_idx_and_col_p_in_view(seq_view, id_column, j, 0); - // Write ids in output view - if (obi_set_index_with_elt_idx_and_col_p_in_view(output_view, id1_column, k, 0, id1_idx) < 0) - { - obidebug(1, "\nError writing id1 in a column"); + if (print_alignment_result(output_view, k, + idx1_column, idx2_column, i, j, + id1_column, id2_column, id1_idx, id2_idx, + print_seq, seq1_column, seq2_column, seq1_idx, seq2_idx, + //print_count, count1_column, count2_column, count1, count2, + ali_length_column, ali_length, + lcs_length_column, lcs_length, + score_column, score, + reference, normalize, similarity_mode) < 0) return -1; - } - if (obi_set_index_with_elt_idx_and_col_p_in_view(output_view, id2_column, k, 0, id2_idx) < 0) - { - obidebug(1, "\nError writing id2 in a column"); - return -1; - } - - // Write the sequences if needed - if (print_seq) - { - if (obi_set_index_with_elt_idx_and_col_p_in_view(output_view, seq1_column, k, 0, seq1_idx) < 0) - { - obidebug(1, "\nError writing seq1 in a column"); - return -1; - } - - if (obi_set_index_with_elt_idx_and_col_p_in_view(output_view, seq2_column, k, 0, seq2_idx) < 0) - { - obidebug(1, "\nError writing seq2 in a column"); - return -1; - } - } - -// // Write the counts if needed // TODO count columns not implemented yet -// if (print_count) -// { -// if (obi_set_index_with_elt_idx_and_col_p_in_view(output_view, count1_column, k, 0, count1) < 0) -// { -// obidebug(1, "\nError writing count1 in a column"); -// return -1; -// } -// -// if (obi_set_index_with_elt_idx_and_col_p_in_view(output_view, count2_column, k, 0, count2) < 0) -// { -// obidebug(1, "\nError writing count2 in a column"); -// return -1; -// } -// } - - // Write the alignment length if it was computed - if ((reference == ALILEN) && (normalize || !similarity_mode)) - { - if (obi_set_int_with_elt_idx_and_col_p_in_view(output_view, ali_length_column, k, 0, ali_length) < 0) - { - obidebug(1, "\nError writing alignment length in a column"); - return -1; - } - } - - // Write the LCS length - if (obi_set_int_with_elt_idx_and_col_p_in_view(output_view, lcs_length_column, k, 0, lcs_length) < 0) - { - obidebug(1, "\nError writing LCS length in a column"); - return -1; - } - - // Write score - if (normalize) - { - if (obi_set_float_with_elt_idx_and_col_p_in_view(output_view, score_column, k, 0, (obifloat_t) score) < 0) - { - obidebug(1, "\nError writing alignment score in a column"); - return -1; - } - } - else - { - if (obi_set_int_with_elt_idx_and_col_p_in_view(output_view, score_column, k, 0, (obiint_t) score) < 0) - { - obidebug(1, "\nError writing alignment score in a column"); - return -1; - } - } k++; }