Debian Codegraph @

src/gaf.h

raw symbols SCIP
1 #ifndef GAF_H
2 #define GAF_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 AlignRecord {
19 int32_t qlen;
20 int32_t qstart;
21 int32_t qend;
22 int32_t plen;
23 int32_t pstart;
24 int32_t pend;
25 int32_t matches;
26 int32_t alignlen;
27 int32_t mapq;
28
29 char strand;
30 char hap;
31 std::size_t seed;
32 std::vector<std::pair<bool, uint32_t> > path; // simplified path (forward, tid)
33 std::vector<int8_t> cigarop;
34 std::vector<uint32_t> cigarlen;
35
36 AlignRecord() : qlen(0), qstart(0), qend(0), plen(0), pstart(0), pend(0), matches(0), alignlen(0), mapq(0), strand('*'), hap('*'), seed(0) {}
37 AlignRecord(int32_t const q, std::size_t const s) : qlen(0), qstart(q), qend(0), plen(0), pstart(0), pend(0), matches(0), alignlen(0), mapq(0), strand('*'), hap('*'), seed(s) {}
38
39 bool operator<(const AlignRecord& s2) const {
40 return ((seed < s2.seed) || ((seed == s2.seed) && (qstart < s2.qstart)));
41 }
42 };
43
44 inline void
45 parseGafCigar(std::string const& cigar, AlignRecord& ar) {
46 uint32_t nstart = 0;
47 uint32_t nend = 0;
48 for(uint32_t i = 0; i < cigar.size(); ++i) {
49 if (isdigit(cigar[i])) ++nend;
50 else {
51 uint32_t oplen = boost::lexical_cast<uint32_t>(cigar.substr(nstart, nend - nstart));
52 ar.cigarlen.push_back(oplen);
53 ar.cigarop.push_back(bam_cigar_table[(int) cigar[i]]);
54 nstart = i + 1;
55 nend = i + 1;
56 }
57 }
58 }
59
60 inline bool
61 parseGafPath(std::string const& path, Graph const& g, AlignRecord& ar) {
62 if (path.size()) {
63 if ((path[0] == '>') || (path[0] == '<')) {
64 std::size_t index = 0;
65 std::vector<uint32_t> breaks;
66 while ((index = path.find("<", index)) != std::string::npos) breaks.push_back(index++);
67 index = 0;
68 while ((index = path.find(">", index)) != std::string::npos) breaks.push_back(index++);
69 std::sort(breaks.begin(), breaks.end());
70 for(uint32_t i = 0; i < breaks.size(); ++i) {
71 bool forward = 1;
72 if (path[breaks[i]] == '<') forward = false;
73 std::string segment;
74 if (i + 1 < breaks.size()) segment = path.substr(breaks[i] + 1, breaks[i+1] - breaks[i] - 1);
75 else segment = path.substr(breaks[i] + 1);
76 typename Graph::TSegmentIdMap::const_iterator it = g.smap.find(segment);
77 if (it != g.smap.end()) ar.path.push_back(std::make_pair(forward, it->second));
78 else {
79 std::cerr << "Unknown segment " << segment << std::endl;
80 return false;
81 }
82 }
83 } else {
84 std::cerr << "Unknown path format!" << std::endl;
85 return false;
86 }
87 } else {
88 std::cerr << "Empty path!" << std::endl;
89 return false;
90 }
91 return true;
92 }
93
94 inline bool
95 parseAlignRecord(std::istream& instream, Graph const& g, AlignRecord& ar, std::string& qname, std::set<std::size_t>& validSR) {
96 std::string gline;
97 if(std::getline(instream, gline)) {
98 if (!validSR.empty()) {
99 std::size_t found = gline.find('\t');
100 if (found != std::string::npos) {
101 if (validSR.find(hash_lr(gline.substr(0, found))) == validSR.end()) return false;
102 }
103 }
104
105 typedef boost::tokenizer< boost::char_separator<char> > Tokenizer;
106 boost::char_separator<char> sep("\t");
107 Tokenizer tokens(gline, sep);
108 Tokenizer::iterator tokIter = tokens.begin();
109 if (tokIter == tokens.end()) { std::cerr << "GAF parsing error!" << std::endl; return false; }
110 qname = *tokIter;
111 ar.seed = hash_lr(qname);
112 if (++tokIter == tokens.end()) { std::cerr << "GAF parsing error!" << std::endl; return false; }
113 ar.qlen = boost::lexical_cast<int32_t>(*tokIter);
114 if (++tokIter == tokens.end()) { std::cerr << "GAF parsing error!" << std::endl; return false; }
115 ar.qstart = boost::lexical_cast<int32_t>(*tokIter);
116 if (++tokIter == tokens.end()) { std::cerr << "GAF parsing error!" << std::endl; return false; }
117 ar.qend = boost::lexical_cast<int32_t>(*tokIter);
118 if (++tokIter == tokens.end()) { std::cerr << "GAF parsing error!" << std::endl; return false; }
119 ar.strand = boost::lexical_cast<char>(*tokIter);
120 if (++tokIter == tokens.end()) { std::cerr << "GAF parsing error!" << std::endl; return false; }
121 if (!g.empty()) {
122 if (!parseGafPath(*tokIter, g, ar)) return false;
123 }
124 if (++tokIter == tokens.end()) { std::cerr << "GAF parsing error!" << std::endl; return false; }
125 ar.plen = boost::lexical_cast<int32_t>(*tokIter);
126 if (++tokIter == tokens.end()) { std::cerr << "GAF parsing error!" << std::endl; return false; }
127 ar.pstart = boost::lexical_cast<int32_t>(*tokIter);
128 if (++tokIter == tokens.end()) { std::cerr << "GAF parsing error!" << std::endl; return false; }
129 ar.pend = boost::lexical_cast<int32_t>(*tokIter);
130 if (++tokIter == tokens.end()) { std::cerr << "GAF parsing error!" << std::endl; return false; }
131 ar.matches = boost::lexical_cast<int32_t>(*tokIter);
132 if (++tokIter == tokens.end()) { std::cerr << "GAF parsing error!" << std::endl; return false; }
133 ar.alignlen = boost::lexical_cast<int32_t>(*tokIter);
134 if (++tokIter == tokens.end()) { std::cerr << "GAF parsing error!" << std::endl; return false; }
135 ar.mapq = boost::lexical_cast<int32_t>(*tokIter);
136 ++tokIter;
137 for(; tokIter != tokens.end(); ++tokIter) {
138 // Optional fields
139 boost::char_separator<char> kvsep(":");
140 Tokenizer tokopt(*tokIter, kvsep);
141 Tokenizer::iterator tikv = tokopt.begin();
142 if (*tikv == "cg") {
143 ++tikv; ++tikv;
144 parseGafCigar(*tikv, ar);
145 }
146 }
147 return true;
148 } else return false;
149 }
150
151 inline bool
152 parseAlignRecord(std::istream& instream, Graph const& g, AlignRecord& ar, std::string& qname) {
153 std::set<std::size_t> validSR;
154 return parseAlignRecord(instream, g, ar, qname, validSR);
155 }
156
157 inline bool
158 parseAlignRecord(std::istream& instream, Graph const& g, AlignRecord& ar) {
159 std::string qname;
160 return parseAlignRecord(instream, g, ar, qname);
161 }
162
163 inline bool
164 parseAlignRecord(std::istream& instream, AlignRecord& ar) {
165 Graph g;
166 return parseAlignRecord(instream, g, ar);
167 }
168
169
170 }
171
172 #endif