forked from lh3/fermi-lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bseq.c
61 lines (55 loc) · 1.17 KB
/
bseq.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <zlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "fml.h"
#include "kseq.h"
KSEQ_INIT(gzFile, gzread)
fml_seq1_t *fml_seq_read(const char *fn, int *n_)
{
gzFile fp;
fml_seq1_t *seqs;
kseq_t *ks;
int m, n;
uint64_t size = 0;
*n_ = 0;
fp = fn && strcmp(fn, "-")? gzopen(fn, "r") : gzdopen(fileno(stdin), "r");
if (fp == 0) return 0;
ks = kseq_init(fp);
m = n = 0; seqs = 0;
while (kseq_read(ks) >= 0) {
fml_seq1_t *s;
if (n >= m) {
m = m? m<<1 : 256;
seqs = realloc(seqs, m * sizeof(fml_seq1_t));
}
s = &seqs[n];
s->seq = strdup(ks->seq.s);
s->qual = ks->qual.l? strdup(ks->qual.s) : 0;
s->l_seq = ks->seq.l;
size += seqs[n++].l_seq;
}
*n_ = n;
kseq_destroy(ks);
gzclose(fp);
return seqs;
}
void seq_reverse(int l, unsigned char *s)
{
int i;
for (i = 0; i < l>>1; ++i) {
int tmp = s[l-1-i];
s[l-1-i] = s[i]; s[i] = tmp;
}
}
void seq_revcomp6(int l, unsigned char *s)
{
int i;
for (i = 0; i < l>>1; ++i) {
int tmp = s[l-1-i];
tmp = (tmp >= 1 && tmp <= 4)? 5 - tmp : tmp;
s[l-1-i] = (s[i] >= 1 && s[i] <= 4)? 5 - s[i] : s[i];
s[i] = tmp;
}
if (l&1) s[i] = (s[i] >= 1 && s[i] <= 4)? 5 - s[i] : s[i];
}