Skip to content

Commit c0155d3

Browse files
committed
fix bug in reading TE annotation fasta(#12)
1 parent 81cfd58 commit c0155d3

1 file changed

Lines changed: 17 additions & 10 deletions

File tree

src/kmer.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,26 @@ int make_te_kmer_idx(call_var_opt_t *opt) {
122122
if (f == 0) {
123123
_err_error_exit("Cannot open TE sequence file: %s\n", opt->te_seq_fn);
124124
}
125-
opt->te_seq_names = (char**)malloc(3*sizeof(char*));
126-
opt->te_for_h = (kmer32_hash_t**)malloc(3*sizeof(kmer32_hash_t*));
127-
opt->te_rev_h = (kmer32_hash_t**)malloc(3*sizeof(kmer32_hash_t*));
125+
int m_te_seqs = 3; // Alu, L1, SVA
126+
opt->te_seq_names = (char**)malloc(m_te_seqs*sizeof(char*));
127+
opt->te_for_h = (kmer32_hash_t**)malloc(m_te_seqs*sizeof(kmer32_hash_t*));
128+
opt->te_rev_h = (kmer32_hash_t**)malloc(m_te_seqs*sizeof(kmer32_hash_t*));
128129
kseq_t *ks = kseq_init(f);
129130
kseq_rewind(ks);
130-
int seq_id = 0;
131+
int n_te_seqs = 0;
131132
while (kseq_read(ks) >= 0) {
132-
opt->te_seq_names[seq_id] = strdup(ks->name.s);
133-
opt->te_for_h[seq_id] = make_kmer_hash_tables(ks->seq.s, ks->seq.l, seq_id, opt->te_kmer_len, 0);
134-
opt->te_rev_h[seq_id] = make_kmer_hash_tables(ks->seq.s, ks->seq.l, seq_id, opt->te_kmer_len, 1);
135-
seq_id++;
133+
if (n_te_seqs >= m_te_seqs) {
134+
m_te_seqs = m_te_seqs * 2;
135+
opt->te_seq_names = (char**)realloc(opt->te_seq_names, m_te_seqs*sizeof(char*));
136+
opt->te_for_h = (kmer32_hash_t**)realloc(opt->te_for_h, m_te_seqs*sizeof(kmer32_hash_t*));
137+
opt->te_rev_h = (kmer32_hash_t**)realloc(opt->te_rev_h, m_te_seqs*sizeof(kmer32_hash_t*));
138+
}
139+
opt->te_seq_names[n_te_seqs] = strdup(ks->name.s);
140+
opt->te_for_h[n_te_seqs] = make_kmer_hash_tables(ks->seq.s, ks->seq.l, n_te_seqs, opt->te_kmer_len, 0);
141+
opt->te_rev_h[n_te_seqs] = make_kmer_hash_tables(ks->seq.s, ks->seq.l, n_te_seqs, opt->te_kmer_len, 1);
142+
n_te_seqs++;
136143
}
137-
opt->n_te_seqs = seq_id;
144+
opt->n_te_seqs = n_te_seqs;
138145
kseq_destroy(ks);
139146
gzclose(f);
140147
return 0;
@@ -259,4 +266,4 @@ int test_te_kmer_query(call_var_opt_t *opt, char *query_fn) {
259266
kseq_destroy(ks);
260267
gzclose(f);
261268
return 0;
262-
}
269+
}

0 commit comments

Comments
 (0)