Commit ee994721 by Celine Mercier

alignpairedend: fixed the worst memory leak and the handling of the case

where 0 common kmers are found
parent ceaafca4
......@@ -2,7 +2,6 @@
from obitools3.apps.progress cimport ProgressBar # @UnresolvedImport
from obitools3.dms import DMS
from obitools3.dms.view import RollbackException
from obitools3.dms.view.typed_view.view_NUC_SEQS cimport View_NUC_SEQS
from obitools3.dms.column.column cimport Column
from obitools3.dms.capi.obiview cimport QUALITY_COLUMN
......@@ -201,26 +200,23 @@ def run(config):
pb(i)
consensus = view[i]
if not two_views:
seqF = entries[i]
else:
seqF = forward[i]
if smin > 0:
if (ali.score > smin) :
buildConsensus(ali, consensus, seqF)
else:
if not two_views:
seqR = Nuc_Seq(seqF.id, seqF[REVERSE_SEQ_COLUMN_NAME], quality = seqF[REVERSE_QUALITY_COLUMN_NAME])
else:
seqR = reverse[i]
buildJoinedSequence(ali, seqR, consensus, forward=seqF)
consensus[b"smin"] = smin
else:
if ali.score > smin and ali.consensus_len > 0 :
buildConsensus(ali, consensus, seqF)
else:
if not two_views:
seqR = Nuc_Seq(seqF.id, seqF[REVERSE_SEQ_COLUMN_NAME], quality = seqF[REVERSE_QUALITY_COLUMN_NAME])
else:
seqR = reverse[i]
buildJoinedSequence(ali, seqR, consensus, forward=seqF)
consensus[b"smin"] = smin
if kmer_ali :
ali.free()
......
......@@ -35,10 +35,10 @@ cdef extern from "kmer_similarity.h" nogil:
OBIDMS_column_p qual_col1,
OBIDMS_column_p qual_col2,
uint8_t kmer_size,
int32_t* kmer_pos_array,
int32_t** kmer_pos_array,
int32_t* kmer_pos_array_height_p,
int32_t* shift_array,
int32_t** shift_array,
int32_t* shift_array_height_p,
int32_t* shift_count_array,
int32_t** shift_count_array,
int32_t* shift_count_array_height_p,
bint build_consensus)
......@@ -112,9 +112,9 @@ cdef class Kmer_similarity:
self.view2_p, self.column2_p, seq2.index, 0, \
self.qual_col1_p, self.qual_col2_p, \
self.kmer_size, \
self.kmer_pos_array_p, self.kmer_pos_array_height_a, \
self.shift_array_p, self.shift_array_height_a, \
self.shift_count_array_p, self.shift_count_array_height_a,
&(self.kmer_pos_array_p), self.kmer_pos_array_height_a, \
&(self.shift_array_p), self.shift_array_height_a, \
&(self.shift_count_array_p), self.shift_count_array_height_a,
self.build_consensus)
ali = Ali_shifted.new_ali(ali_p)
......
......@@ -67,11 +67,14 @@ Obi_ali_p kmer_similarity(Obiview_p view1, OBIDMS_column_p column1, index_t idx1
Obiview_p view2, OBIDMS_column_p column2, index_t idx2, index_t elt_idx2,
OBIDMS_column_p qual_col1, OBIDMS_column_p qual_col2,
uint8_t kmer_size,
int32_t* kmer_pos_array, int32_t* kmer_pos_array_height_p,
int32_t* shift_array, int32_t* shift_array_height_p,
int32_t* shift_count_array, int32_t* shift_count_array_length_p,
int32_t** kmer_pos_array_p, int32_t* kmer_pos_array_height_p,
int32_t** shift_array_p, int32_t* shift_array_height_p,
int32_t** shift_count_array_p, int32_t* shift_count_array_length_p,
bool build_consensus)
{
int32_t* kmer_pos_array;
int32_t* shift_array;
int32_t* shift_count_array;
Obi_ali_p ali = NULL;
int i, j;
bool switched_seqs;
......@@ -191,11 +194,11 @@ Obi_ali_p kmer_similarity(Obiview_p view1, OBIDMS_column_p column1, index_t idx1
total_len = len1 + len2 + 1; // +1 for shift 0
// Allocate or reallocate memory for the array of shift counts if necessary
if (shift_count_array == NULL)
if (*shift_count_array_p == NULL)
{
shift_count_array_length = total_len;
shift_count_array = (int32_t*) malloc(shift_count_array_length * sizeof(int32_t));
if (shift_count_array == NULL)
*shift_count_array_p = (int32_t*) malloc(shift_count_array_length * sizeof(int32_t));
if (*shift_count_array_p == NULL)
{
obi_set_errno(OBI_MALLOC_ERROR);
obidebug(1, "\nError computing the kmer similarity between two sequences: error allocating memory");
......@@ -205,8 +208,8 @@ Obi_ali_p kmer_similarity(Obiview_p view1, OBIDMS_column_p column1, index_t idx1
else if (total_len >= shift_count_array_length)
{
shift_count_array_length = total_len;
shift_count_array = (int32_t*) realloc(shift_count_array, shift_count_array_length * sizeof(int32_t));
if (shift_count_array == NULL)
*shift_count_array_p = (int32_t*) realloc(*shift_count_array_p, shift_count_array_length * sizeof(int32_t));
if (*shift_count_array_p == NULL)
{
obi_set_errno(OBI_MALLOC_ERROR);
obidebug(1, "\nError computing the kmer similarity between two sequences: error allocating memory");
......@@ -215,11 +218,11 @@ Obi_ali_p kmer_similarity(Obiview_p view1, OBIDMS_column_p column1, index_t idx1
}
// Allocate or reallocate memory for the array of shifts if necessary
if (shift_array == NULL)
if (*shift_array_p == NULL)
{
shift_array_height = total_len;
shift_array = (int32_t*) malloc(ARRAY_LENGTH * shift_array_height * sizeof(int32_t));
if (shift_array == NULL)
*shift_array_p = (int32_t*) malloc(ARRAY_LENGTH * shift_array_height * sizeof(int32_t));
if (*shift_array_p == NULL)
{
obi_set_errno(OBI_MALLOC_ERROR);
obidebug(1, "\nError computing the kmer similarity between two sequences: error allocating memory");
......@@ -229,8 +232,8 @@ Obi_ali_p kmer_similarity(Obiview_p view1, OBIDMS_column_p column1, index_t idx1
else if (len1 >= shift_array_height)
{
shift_array_height = total_len;
shift_array = (int32_t*) realloc(shift_array, ARRAY_LENGTH * shift_array_height * sizeof(int32_t));
if (shift_array == NULL)
*shift_array_p = (int32_t*) realloc(*shift_array_p, ARRAY_LENGTH * shift_array_height * sizeof(int32_t));
if (*shift_array_p == NULL)
{
obi_set_errno(OBI_MALLOC_ERROR);
obidebug(1, "\nError computing the kmer similarity between two sequences: error allocating memory");
......@@ -239,11 +242,11 @@ Obi_ali_p kmer_similarity(Obiview_p view1, OBIDMS_column_p column1, index_t idx1
}
// Allocate or reallocate memory for the array of positions if necessary
if (kmer_pos_array == NULL)
if (*kmer_pos_array_p == NULL)
{
kmer_pos_array_height = len1;
kmer_pos_array = (int32_t*) malloc(ARRAY_LENGTH * kmer_pos_array_height * sizeof(int32_t));
if (kmer_pos_array == NULL)
*kmer_pos_array_p = (int32_t*) malloc(ARRAY_LENGTH * kmer_pos_array_height * sizeof(int32_t));
if (*kmer_pos_array_p == NULL)
{
obi_set_errno(OBI_MALLOC_ERROR);
obidebug(1, "\nError computing the kmer similarity between two sequences: error allocating memory");
......@@ -253,8 +256,8 @@ Obi_ali_p kmer_similarity(Obiview_p view1, OBIDMS_column_p column1, index_t idx1
else if (len1 >= kmer_pos_array_height)
{
kmer_pos_array_height = len1;
kmer_pos_array = (int32_t*) realloc(kmer_pos_array, ARRAY_LENGTH * kmer_pos_array_height * sizeof(int32_t));
if (kmer_pos_array == NULL)
*kmer_pos_array_p = (int32_t*) realloc(*kmer_pos_array_p, ARRAY_LENGTH * kmer_pos_array_height * sizeof(int32_t));
if (*kmer_pos_array_p == NULL)
{
obi_set_errno(OBI_MALLOC_ERROR);
obidebug(1, "\nError computing the kmer similarity between two sequences: error allocating memory");
......@@ -262,6 +265,10 @@ Obi_ali_p kmer_similarity(Obiview_p view1, OBIDMS_column_p column1, index_t idx1
}
}
shift_count_array = *shift_count_array_p;
shift_array = *shift_array_p;
kmer_pos_array = *kmer_pos_array_p;
// Initialize all positions to -1
for (i=0; i<(ARRAY_LENGTH * kmer_pos_array_height); i++)
kmer_pos_array[i] = -1;
......
......@@ -110,11 +110,11 @@ Obi_ali_p kmer_similarity(Obiview_p view1,
OBIDMS_column_p qual_col1,
OBIDMS_column_p qual_col2,
uint8_t kmer_size,
int32_t* kmer_pos_array,
int32_t** kmer_pos_array_p,
int32_t* kmer_pos_array_height_p,
int32_t* shift_array,
int32_t** shift_array_p,
int32_t* shift_array_height_p,
int32_t* shift_count_array,
int32_t** shift_count_array_p,
int32_t* shift_count_array_height_p,
bool build_consensus);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment