Debian Codegraph @

src/gfa.h

raw symbols SCIP
1 #ifndef GFA_H
2 #define GFA_H
3
4
5 #include <iostream>
6 #include <fstream>
7
8 #include <htslib/faidx.h>
9 #include <htslib/vcf.h>
10 #include <htslib/sam.h>
11 #include <htslib/tbx.h>
12
13 #include "util.h"
14
15 namespace torali
16 {
17
18 struct Link {
19 bool fromfwd;
20 bool tofwd;
21 uint32_t from;
22 uint32_t to;
23
24 Link() {}
25 Link(bool const fv, bool const tv, uint32_t const fr, uint32_t tos) : fromfwd(fv), tofwd(tv), from(fr), to(tos) {}
26
27 bool operator<(const Link& l2) const {
28 return ((from < l2.from) || ((from==l2.from) && (to < l2.to)));
29 }
30 };
31
32 struct LinkCargo {
33 bool fromfwd;
34 bool tofwd;
35 uint32_t from;
36 uint32_t to;
37 uint32_t support;
38 uint32_t mapq;
39
40 LinkCargo() {}
41 explicit LinkCargo(Link const lk) : fromfwd(lk.fromfwd), tofwd(lk.tofwd), from(lk.from), to(lk.to), support(0), mapq(0) {}
42 LinkCargo(bool const fv, bool const tv, uint32_t const fr, uint32_t tos) : fromfwd(fv), tofwd(tv), from(fr), to(tos), support(0), mapq(0) {}
43
44 bool operator<(const LinkCargo& l2) const {
45 return ((from < l2.from) || ((from==l2.from) && (to < l2.to)));
46 }
47 };
48
49 struct Graph {
50 typedef boost::unordered_map<std::string, uint32_t> TSegmentIdMap;
51
52 std::vector<uint32_t> offset;
53 std::vector<Link> links;
54 TSegmentIdMap smap;
55 std::string sequence;
56
57 bool empty() const { return sequence.empty(); }
58 uint32_t nodelen(uint32_t const id) const {
59 // Node length
60 if (id + 1 == offset.size()) return sequence.size() - offset[id];
61 else if (id + 1 < offset.size()) return (offset[id+1] - offset[id]);
62 else return 0;
63 }
64 std::string nodeseq(uint32_t const id) const {
65 return sequence.substr(offset[id], nodelen(id));
66 }
67 };
68
69
70 template<typename TConfig>
71 inline bool
72 parseGfa(TConfig const& c, Graph& g) {
73 // Open GFA
74 std::ifstream gfaFile;
75 boost::iostreams::filtering_streambuf<boost::iostreams::input> dataIn;
76 if (is_gz(c.genome)) {
77 gfaFile.open(c.genome.string().c_str(), std::ios_base::in | std::ios_base::binary);
78 dataIn.push(boost::iostreams::gzip_decompressor(), 16*1024);
79 } else gfaFile.open(c.genome.string().c_str(), std::ios_base::in);
80 dataIn.push(gfaFile);
81
82 // Parse GFA
83 uint32_t id_counter = 0;
84 std::istream instream(&dataIn);
85 std::string gline;
86 while(std::getline(instream, gline)) {
87 typedef boost::tokenizer< boost::char_separator<char> > Tokenizer;
88 boost::char_separator<char> sep("\t");
89 Tokenizer tokens(gline, sep);
90 Tokenizer::iterator tokIter = tokens.begin();
91 if (tokIter != tokens.end()) {
92 // What element
93 if (*tokIter == "#") continue;
94 else if (*tokIter == "S") {
95 // Segment
96 ++tokIter;
97 if (tokIter != tokens.end()) {
98 // Name
99 std::string segname = *tokIter;
100 ++tokIter;
101 if (tokIter != tokens.end()) {
102 // Sequence
103 std::string sequence = *tokIter;
104 // New segment
105 g.offset.push_back(g.sequence.size());
106 g.sequence += sequence;
107 // Keep segment name <-> id relationship
108 g.smap.insert(std::make_pair(segname, id_counter));
109 ++id_counter;
110 } else {
111 std::cerr << "S segment lacks sequence information!" << std::endl;
112 return false;
113 }
114 } else {
115 std::cerr << "S line lacks segment name!" << std::endl;
116 return false;
117 }
118 }
119 else if (*tokIter == "L") {
120 // Link
121 ++tokIter;
122 if (tokIter != tokens.end()) {
123 // From
124 if (g.smap.find(*tokIter) == g.smap.end()) {
125 std::cerr << "Link with unknown from segment! " << *tokIter << std::endl;
126 return false;
127 }
128 uint32_t fromId = g.smap[*tokIter];
129 ++tokIter;
130 if (tokIter != tokens.end()) {
131 // FromOrient
132 bool fromfwd = true;
133 if (*tokIter == "-") fromfwd = false;
134 ++tokIter;
135 if (tokIter != tokens.end()) {
136 // To
137 if (g.smap.find(*tokIter) == g.smap.end()) {
138 std::cerr << "Link with unknown to segment! " << *tokIter << std::endl;
139 return false;
140 }
141 uint32_t toId = g.smap[*tokIter];
142 ++tokIter;
143 if (tokIter != tokens.end()) {
144 // ToOrient
145 bool tofwd = true;
146 if (*tokIter == "-") tofwd = false;
147 ++tokIter;
148 if (tokIter != tokens.end()) {
149 // Overlap CIGAR
150 if (*tokIter != "0M") {
151 std::cerr << "Currently only 0M links are supported!" << std::endl;
152 return false;
153 }
154 g.links.push_back(Link(fromfwd, tofwd, fromId, toId));
155 }
156 }
157 }
158 }
159 }
160 } else {
161 // Todo
162 std::cerr << "Warning: Unknown line " << *tokIter << std::endl;
163 continue;
164 }
165 }
166 }
167 dataIn.pop();
168 if (is_gz(c.genome)) dataIn.pop();
169 gfaFile.close();
170
171 // Graph statistics
172 boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
173 std::cerr << '[' << boost::posix_time::to_simple_string(now) << "] " << "GFA loaded: " << g.offset.size() << " segments, " << g.links.size() << " links, seq.size: " << g.sequence.size() << std::endl;
174
175 return true;
176 }
177
178 inline void
179 writeGfa(Graph const& g) {
180 // Vertex map
181 std::vector<std::string> idSegment(g.smap.size());
182 for(typename Graph::TSegmentIdMap::const_iterator it = g.smap.begin(); it != g.smap.end(); ++it) idSegment[it->second] = it->first;
183
184 // Temporary output file
185 std::string filename = "test.out.gfa";
186
187 // Output rGFA
188 std::ofstream sfile;
189 sfile.open(filename.c_str());
190
191 // Output segments
192 for(uint32_t i = 0; i < g.offset.size(); ++i) {
193 std::string seqid = idSegment[i];
194 sfile << "S\t" << seqid;
195 sfile << "\t" << g.nodeseq(i);
196 sfile << std::endl;
197 }
198
199 // Output links
200 for(uint32_t i = 0; i < g.links.size(); ++i) {
201 sfile << "L\t" << idSegment[g.links[i].from];
202 if (g.links[i].fromfwd) sfile << "\t+";
203 else sfile << "\t-";
204 sfile << "\t" << idSegment[g.links[i].to];
205 if (g.links[i].tofwd) sfile << "\t+";
206 else sfile << "\t-";
207 sfile << "\t0M" << std::endl;
208 }
209 sfile.close();
210 }
211
212 }
213
214 #endif