-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchromosomes.cpp
More file actions
60 lines (53 loc) · 1.47 KB
/
chromosomes.cpp
File metadata and controls
60 lines (53 loc) · 1.47 KB
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
#include "chromosomes.hpp"
KSEQ_INIT(gzFile, gzread)
std::vector<std::string> chromosomes ;
std::unordered_map<std::string, char*> chromosome_seqs ;
string reverse_complement(string s) {
string rc(s) ;
int l = s.length() ;
for (int i = 0; i < l; i++) {
if (s[i] == 'A') {
rc[l - 1 - i] = 'T' ;
}
if (s[i] == 'C') {
rc[l - 1 - i] = 'G' ;
}
if (s[i] == 'G') {
rc[l - 1 - i] = 'C' ;
}
if (s[i] == 'T') {
rc[l - 1 - i] = 'A' ;
}
}
return rc ;
}
string canonicalize(string s) {
auto rc = reverse_complement(s) ;
return rc < s ? rc : s ;
}
int get_reference_size(ifstream &fasta_file) {
fasta_file.seekg(0, ios_base::end) ;
int l = fasta_file.tellg() ;
fasta_file.seekg(0, ios_base::beg) ;
return l ;
}
void load_chromosomes(string path) {
lprint({"Loading reference genome from", path, ".."});
gzFile fp = gzopen(path.c_str(), "r");
kseq_t *seq = kseq_init(fp);
int l;
while ((l = kseq_read(seq)) >= 0) {
lprint({"Extracted", seq->name.s, "with", to_string(l), "bases"});
for(uint i = 0; i<l; ++i)
seq->seq.s[i] = toupper(seq->seq.s[i]);
chromosomes.push_back(seq->name.s);
char* s = (char*) malloc(sizeof(char) * (l + 1)) ;
memcpy(s, seq->seq.s, l + 1) ;
s[l] = '\0';
chromosome_seqs[seq->name.s] = s;
}
kseq_destroy(seq);
gzclose(fp);
for(const auto s : chromosomes)
cerr << s << endl;
}