Debian Codegraph @

src/pangenome.h

raw symbols SCIP
1 #ifndef PANGENOME_H
2 #define PANGENOME_H
3
4 #include <boost/unordered_map.hpp>
5 #include <boost/date_time/posix_time/posix_time.hpp>
6 #include <boost/date_time/gregorian/gregorian.hpp>
7 #include <boost/program_options/cmdline.hpp>
8 #include <boost/program_options/options_description.hpp>
9 #include <boost/program_options/parsers.hpp>
10 #include <boost/program_options/variables_map.hpp>
11 #include <boost/filesystem.hpp>
12 #include <boost/tokenizer.hpp>
13 #include <iostream>
14 #include <vector>
15 #include <htslib/vcf.h>
16 #include <htslib/sam.h>
17 #include <math.h>
18 #include <stdio.h>
19
20 #include <htslib/sam.h>
21 #include <htslib/vcf.h>
22 #include <htslib/faidx.h>
23
24 #include "gfa.h"
25 #include "gaf.h"
26 #include "util.h"
27
28 namespace torali {
29
30
31 template<typename TReadBp>
32 inline void
33 _insertGraphJunction(TReadBp& readBp, std::size_t const seed, AlignRecord const& ar, uint32_t const pathidx, int32_t const rp, int32_t const sp, bool const scleft) {
34 typedef typename TReadBp::mapped_type TJunctionVector;
35
36 bool fw = ar.path[pathidx].first;
37 int32_t readStart = ar.qstart; // Query start (not needed)
38 if (sp <= ar.qlen) {
39 typename TReadBp::iterator it = readBp.find(seed);
40 if (it != readBp.end()) it->second.push_back(Junction(fw, scleft, ar.path[pathidx].second, readStart, rp, sp, ar.mapq));
41 else readBp.insert(std::make_pair(seed, TJunctionVector(1, Junction(fw, scleft, ar.path[pathidx].second, readStart, rp, sp, ar.mapq))));
42 }
43 }
44
45
46 template<typename TConfig, typename TReadBp>
47 inline bool
48 findGraphJunctions(TConfig& c, Graph const& g, TReadBp& readBp, std::set<std::size_t>& validSR) {
49 boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
50 std::cerr << '[' << boost::posix_time::to_simple_string(now) << "] " << "Split-read scanning" << std::endl;
51
52 // Vertex map
53 std::vector<std::string> idSegment(g.smap.size());
54 for(typename Graph::TSegmentIdMap::const_iterator it = g.smap.begin(); it != g.smap.end(); ++it) idSegment[it->second] = it->first;
55
56 // Iterate graph alignments
57 for(unsigned int file_c = 0; file_c < c.files.size(); ++file_c) {
58
59 // Open GAF
60 std::ifstream gafFile;
61 boost::iostreams::filtering_streambuf<boost::iostreams::input> dataIn;
62 if (is_gz(c.files[file_c])) {
63 gafFile.open(c.files[file_c].string().c_str(), std::ios_base::in | std::ios_base::binary);
64 dataIn.push(boost::iostreams::gzip_decompressor(), 16*1024);
65 } else gafFile.open(c.files[file_c].string().c_str(), std::ios_base::in);
66 dataIn.push(gafFile);
67
68 // Parse GAF
69 std::istream instream(&dataIn);
70 while (!instream.eof()) {
71 AlignRecord ar;
72 std::string qname;
73 if (parseAlignRecord(instream, g, ar, qname, validSR)) {
74 if (ar.mapq < c.minMapQual) continue;
75
76 // Iterate path alignments
77 uint32_t refstart = 0;
78 for(uint32_t pi = 0; pi < ar.path.size(); ++pi) {
79 // Vertex coordinates
80 //std::string seqname = idSegment[ar.path[pi].second];
81 uint32_t seqlen = g.nodelen(ar.path[pi].second);
82 uint32_t pstart = 0;
83 uint32_t plen = seqlen;
84 if (pi == 0) {
85 plen -= ar.pstart;
86 if (ar.path[pi].first) pstart = ar.pstart;
87 }
88 if (pi + 1 == ar.path.size()) {
89 plen = ar.pend - ar.pstart - refstart;
90 if (!ar.path[pi].first) {
91 if (pi == 0) pstart = seqlen - ar.pend;
92 else pstart = ar.pstart + refstart + seqlen - ar.pend;
93 }
94 }
95
96 // Compute local alignment end
97 uint32_t refend = refstart + plen;
98 uint32_t rp = 0; // Reference pointer
99 uint32_t srpend = 0; // Segment reference pointer
100 for (uint32_t i = 0; i < ar.cigarop.size(); ++i) {
101 if ((ar.cigarop[i] == BAM_CMATCH) || (ar.cigarop[i] == BAM_CEQUAL) || (ar.cigarop[i] == BAM_CDIFF)) {
102 for(uint32_t k = 0; k < ar.cigarlen[i]; ++k, ++rp) {
103 if ((rp >= refstart) && (rp < refend)) ++srpend;
104 }
105 }
106 else if (ar.cigarop[i] == BAM_CDEL) {
107 for(uint32_t k = 0; k < ar.cigarlen[i]; ++k, ++rp) {
108 if ((rp >= refstart) && (rp < refend)) ++srpend;
109 }
110 }
111 }
112
113 // Parse CIGAR
114 rp = 0; // Reference pointer
115 uint32_t srp = 0;
116 uint32_t sp = ar.qstart;
117
118 // Leading junction
119 if ((pi == 0) && (sp > c.minRefSep)) {
120 int32_t locbeg = pstart + 1 + srp;
121 if (!ar.path[pi].first) locbeg = pstart + 1 + (srpend - srp);
122 if ((locbeg > 0) && (locbeg < (int32_t) seqlen)) {
123 //std::cerr << seqname << '\t' << locbeg << "\tRead\t" << qname << '\t' << ar.seed << '\t' << ar.qlen << "\tPath\t" << pi << '\t' << (int32_t) ar.path[pi].first << '\t' << ar.pstart << "\tReadBp\t" << sp << std::endl;
124 _insertGraphJunction(readBp, ar.seed, ar, pi, locbeg, sp, ar.path[pi].first);
125 }
126 }
127 // Internal junctions
128 for (uint32_t i = 0; i < ar.cigarop.size(); ++i) {
129 if ((ar.cigarop[i] == BAM_CMATCH) || (ar.cigarop[i] == BAM_CEQUAL) || (ar.cigarop[i] == BAM_CDIFF)) {
130 for(uint32_t k = 0; k < ar.cigarlen[i]; ++k, ++sp, ++rp) {
131 if ((rp >= refstart) && (rp < refend)) ++srp;
132 }
133 }
134 else if (ar.cigarop[i] == BAM_CDEL) {
135 // Insert start-junction
136 if (ar.cigarlen[i] > c.minRefSep) {
137 if ((rp >= refstart) && (rp < refend)) {
138 int32_t locbeg = pstart + 1 + srp;
139 if (!ar.path[pi].first) locbeg = pstart + 1 + (srpend - srp - ar.cigarlen[i]);
140 if ((locbeg > 0) && (locbeg < (int32_t) seqlen)) {
141 //std::cerr << seqname << '\t' << locbeg << "\tRead\t" << qname << '\t' << ar.seed << '\t' << ar.qlen << "\tPath\t" << pi << '\t' << (int32_t) ar.path[pi].first << '\t' << ar.pstart << "\tReadBp\t" << sp << '\t' << ar.cigarlen[i] << std::endl;
142 _insertGraphJunction(readBp, ar.seed, ar, pi, locbeg, sp, false);
143 }
144 }
145 }
146 // Adjust segment reference pointer for deletion
147 for(uint32_t k = 0; k < ar.cigarlen[i]; ++k, ++rp) {
148 if ((rp >= refstart) && (rp < refend)) ++srp;
149 }
150 // Insert end-junction
151 if (ar.cigarlen[i] > c.minRefSep) {
152 if ((rp >= refstart) && (rp < refend)) {
153 int32_t locbeg = pstart + 1 + srp;
154 if (!ar.path[pi].first) locbeg = pstart + 1 + (srpend - srp) + ar.cigarlen[i];
155 if ((locbeg > 0) && (locbeg < (int32_t) seqlen)) {
156 //std::cerr << seqname << '\t' << locbeg << "\tRead\t" << qname << '\t' << ar.seed << '\t' << ar.qlen << "\tPath\t" << pi << '\t' << (int32_t) ar.path[pi].first << '\t' << ar.pstart << "\tReadBp\t" << sp << '\t' << ar.cigarlen[i] << std::endl;
157 _insertGraphJunction(readBp, ar.seed, ar, pi, locbeg, sp, true);
158 }
159 }
160 }
161 }
162 else if (ar.cigarop[i] == BAM_CINS) {
163 // Insert start-junction
164 if (ar.cigarlen[i] > c.minRefSep) {
165 if ((rp >= refstart) && (rp < refend)) {
166 int32_t locbeg = pstart + 1 + srp;
167 if (!ar.path[pi].first) locbeg = pstart + 1 + (srpend - srp);
168 if ((locbeg > 0) && (locbeg < (int32_t) seqlen)) {
169 //std::cerr << seqname << '\t' << locbeg << "\tRead\t" << qname << '\t' << ar.seed << '\t' << ar.qlen << "\tPath\t" << pi << '\t' << (int32_t) ar.path[pi].first << '\t' << ar.pstart << "\tReadBp\t" << sp << '\t' << ar.cigarlen[i] << std::endl;
170 _insertGraphJunction(readBp, ar.seed, ar, pi, locbeg, sp, !ar.path[pi].first);
171 }
172 }
173 }
174 sp += ar.cigarlen[i];
175 // Insert end-junction
176 if (ar.cigarlen[i] > c.minRefSep) {
177 if ((rp >= refstart) && (rp < refend)) {
178 int32_t locbeg = pstart + 1 + srp;
179 if (!ar.path[pi].first) locbeg = pstart + 1 + (srpend - srp);
180 if ((locbeg > 0) && (locbeg < (int32_t) seqlen)) {
181 //std::cerr << seqname << '\t' << locbeg << "\tRead\t" << qname << '\t' << ar.seed << '\t' << ar.qlen << "\tPath\t" << pi << '\t' << (int32_t) ar.path[pi].first << '\t' << ar.pstart << "\tReadBp\t" << sp << '\t' << ar.cigarlen[i] << std::endl;
182 _insertGraphJunction(readBp, ar.seed, ar, pi, locbeg, sp, ar.path[pi].first);
183 }
184 }
185 }
186 }
187 else {
188 std::cerr << "Warning: Unknown Cigar option " << ar.cigarop[i] << std::endl;
189 return false;
190 }
191 }
192 // Trailing junction
193 if ((pi + 1 == ar.path.size()) && ((int32_t) (sp + c.minRefSep) < ar.qlen)) {
194 int32_t locbeg = pstart + 1 + srp;
195 if (!ar.path[pi].first) locbeg = pstart + 1 + (srpend - srp);
196 if ((locbeg > 0) && (locbeg < (int32_t) seqlen)) {
197 //std::cerr << seqname << '\t' << locbeg << "\tRead\t" << qname << '\t' << ar.seed << '\t' << ar.qlen << "\tPath\t" << pi << '\t' << (int32_t) ar.path[pi].first << '\t' << ar.pstart << "\tReadBp\t" << sp << std::endl;
198 _insertGraphJunction(readBp, ar.seed, ar, pi, locbeg, sp, !ar.path[pi].first);
199 }
200 }
201
202 // Next segment
203 refstart = refend;
204 }
205
206 //std::cerr << ar.seed << ',' << ar.qlen << ',' << ar.qstart << ',' << ar.qend << ',' << ar.strand << ',' << ar.plen << ',' << ar.pstart << ',' << ar.pend << ',' << ar.matches << ',' << ar.alignlen << ',' << ar.mapq << std::endl;
207 }
208 }
209
210 // Sort junctions
211 for(typename TReadBp::iterator it = readBp.begin(); it != readBp.end(); ++it) std::sort(it->second.begin(), it->second.end());
212
213 // Close file
214 dataIn.pop();
215 if (is_gz(c.files[file_c])) dataIn.pop();
216 gafFile.close();
217 }
218
219 return true;
220 }
221
222 template<typename TConfig, typename TSvtSRBamRecord>
223 inline void
224 _findGraphSRBreakpoints(TConfig const& c, Graph const& g, TSvtSRBamRecord& srBR, std::set<std::size_t>& validSR) {
225 // Breakpoints
226 typedef std::vector<Junction> TJunctionVector;
227 typedef boost::unordered_map<std::size_t, TJunctionVector> TReadBp;
228 TReadBp readBp;
229 findGraphJunctions(c, g, readBp, validSR);
230 fetchSVs(c, readBp, srBR);
231 }
232
233
234 template<typename TConfig>
235 inline void
236 outputGraphSRBamRecords(TConfig const& c, Graph const& g, std::vector<std::vector<SRBamRecord> > const& br) {
237 // Vertex map
238 std::vector<std::string> idSegment(g.smap.size());
239 for(typename Graph::TSegmentIdMap::const_iterator it = g.smap.begin(); it != g.smap.end(); ++it) idSegment[it->second] = it->first;
240
241 // Hash reads
242 typedef boost::unordered_map<std::size_t, std::string> THashMap;
243 THashMap hm;
244 for(unsigned int file_c = 0; file_c < c.files.size(); ++file_c) {
245
246 // Open GAF
247 std::ifstream gafFile;
248 boost::iostreams::filtering_streambuf<boost::iostreams::input> dataIn;
249 if (is_gz(c.files[file_c])) {
250 gafFile.open(c.files[file_c].string().c_str(), std::ios_base::in | std::ios_base::binary);
251 dataIn.push(boost::iostreams::gzip_decompressor(), 16*1024);
252 } else gafFile.open(c.files[file_c].string().c_str(), std::ios_base::in);
253 dataIn.push(gafFile);
254
255 // Parse GAF
256 std::istream instream(&dataIn);
257 while (!instream.eof()) {
258 AlignRecord ar;
259 std::string qname;
260 if (parseAlignRecord(instream, g, ar, qname)) {
261 if (hm.find(ar.seed) == hm.end()) hm.insert(std::make_pair(ar.seed, qname));
262 else {
263 if (hm[ar.seed] != qname) {
264 std::cerr << "Warning: Hash collision! " << ar.seed << ',' << hm[ar.seed] << ',' << qname << std::endl;
265 }
266 }
267 }
268 }
269
270 // Close file
271 dataIn.pop();
272 if (is_gz(c.files[file_c])) dataIn.pop();
273 gafFile.close();
274 }
275
276 // Header
277 std::cerr << "id\tsegment1\tpos1\tsegment2\tpos2\tsvtype\tct\tqual\tinslen" << std::endl;
278
279 // SVs
280 for(uint32_t svt = 0; svt < br.size(); ++svt) {
281 for(uint32_t i = 0; i < br[svt].size(); ++i) {
282 std::cerr << hm[br[svt][i].id] << '\t' << br[svt][i].id << '\t' << idSegment[br[svt][i].chr] << '\t' << br[svt][i].pos << '\t' << idSegment[br[svt][i].chr2] << '\t' << br[svt][i].pos2 << '\t' << _addID(svt) << '\t' << _addOrientation(svt) << '\t' << br[svt][i].qual << '\t' << br[svt][i].inslen << std::endl;
283 }
284 }
285 }
286
287 }
288
289 #endif