| 1 | #include "SeqLib/SeqPlot.h" |
| 2 | |
| 3 | namespace SeqLib { |
| 4 | |
| 5 | std::string SeqPlot::PlotAlignmentRecords(const BamRecordVector& brv) const { |
| 6 | |
| 7 | PlottedReadVector plot_vec; |
| 8 | |
| 9 | for (BamRecordVector::const_iterator i = brv.begin(); i != brv.end(); ++i) { |
| 10 | |
| 11 | // get the position in the view window |
| 12 | if (i->ChrID() != m_view.chr) |
| 13 | continue; |
| 14 | |
| 15 | int pos = i->Position() - m_view.pos1; |
| 16 | if (pos < 0) |
| 17 | continue; |
| 18 | |
| 19 | if (i->PositionEnd() > m_view.pos2) |
| 20 | continue; |
| 21 | |
| 22 | // plot with gaps |
| 23 | std::string tseq = i->Sequence(); |
| 24 | std::string gapped_seq; |
| 25 | |
| 26 | size_t p = i->AlignmentPosition(); // move along on sequence, starting at first non-clipped base |
| 27 | Cigar cc = i->GetCigar(); |
| 28 | for (Cigar::const_iterator c = cc.begin(); c != cc.end(); ++c) { |
| 29 | if (c->Type() == 'M') { // |
| 30 | assert(p + c->Length() <= tseq.length()); |
| 31 | gapped_seq += tseq.substr(p, c->Length()); |
| 32 | } else if (c->Type() == 'D') { |
| 33 | gapped_seq += std::string(c->Length(), '-'); |
| 34 | } |
| 35 | |
| 36 | if (c->Type() == 'I' || c->Type() == 'M') |
| 37 | p += c->Length(); |
| 38 | } |
| 39 | |
| 40 | std::stringstream msg; |
| 41 | msg << i->Qname() << ">>>" << (i->ChrID() + 1) << ":" << i->Position(); |
| 42 | |
| 43 | // add to the read plot |
| 44 | plot_vec.push_back(PlottedRead(pos, gapped_seq, msg.str())); |
| 45 | |
| 46 | } |
| 47 | |
| 48 | // sort them |
| 49 | std::sort(plot_vec.begin(), plot_vec.end()); |
| 50 | |
| 51 | // make a list of lines |
| 52 | PlottedReadLineVector line_vec; |
| 53 | |
| 54 | // plot the reads from the ReadPlot vector |
| 55 | for (PlottedReadVector::iterator i = plot_vec.begin(); i != plot_vec.end(); ++i) { |
| 56 | bool found = false; |
| 57 | for (PlottedReadLineVector::iterator j = line_vec.begin(); j != line_vec.end(); ++j) { |
| 58 | if (j->readFits(*i)) { // it fits here |
| 59 | j->addRead(&(*i)); |
| 60 | found = true; |
| 61 | break; |
| 62 | } |
| 63 | } |
| 64 | if (!found) { // didn't fit anywhere, so make a new line |
| 65 | PlottedReadLine prl; |
| 66 | prl.pad = m_pad; |
| 67 | prl.contig_len = m_view.Width(); //ac.getSequence().length(); |
| 68 | prl.addRead(&(*i)); |
| 69 | line_vec.push_back(prl); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | std::stringstream ss; |
| 74 | |
| 75 | // plot the lines. Add contig identifier to each |
| 76 | for (PlottedReadLineVector::const_iterator i = line_vec.begin(); i != line_vec.end(); ++i) |
| 77 | ss << (*i) << std::endl; |
| 78 | |
| 79 | return ss.str(); |
| 80 | |
| 81 | |
| 82 | } |
| 83 | |
| 84 | |
| 85 | } |