eXpress “1.5”

src/fragments.h

00001 
00009 #ifndef FRAGMENTS_H
00010 #define FRAGMENTS_H
00011 
00012 #include <string>
00013 #include <vector>
00014 #include <iostream>
00015 #include <fstream>
00016 #include <cassert>
00017 #include <api/BamAlignment.h>
00018 #include "sequence.h"
00019 #include "boost/scoped_ptr.hpp"
00020 
00021 typedef size_t TargID;
00022 struct Library;
00023 class Target;
00024 class TargetTable;
00025 
00034 enum PairStatus { PAIRED, LEFT_ONLY, RIGHT_ONLY };
00035 
00042 struct Indel {
00046   size_t pos;
00050   size_t len;
00054   Indel(size_t p, size_t l) : pos(p), len(l) {}
00055 };
00056 
00063 struct ReadHit {
00067   std::string name;
00072   bool first;
00078   bool reversed;
00082   size_t targ_id;
00087   size_t left;
00092   size_t right;
00096   SequenceFwd seq;
00101   std::vector<Indel> inserts;
00106   std::vector<Indel> deletes;
00111   BamTools::BamAlignment bam;
00116   std::string sam;
00122   int mate_l;
00123 };
00124 
00129 struct HitParams {
00130   double align_likelihood;
00131   double full_likelihood;
00132   double posterior;
00133 };
00134 
00141 class FragHit {
00145   Target* _target;
00150   boost::scoped_ptr<ReadHit> _read_l;
00155   boost::scoped_ptr<ReadHit> _read_r;
00160   std::vector<const Target*> _neighbors;
00161   HitParams _params;
00162   
00163 public:
00168   FragHit(ReadHit* h) : _target(NULL) {
00169     if (h->reversed) {
00170       _read_r.reset(h);
00171     } else {
00172       _read_l.reset(h);
00173     }
00174   }
00180   FragHit(ReadHit* l, ReadHit* r) : _target(NULL), _read_l(l), _read_r(r) {
00181     assert(!l->reversed);
00182     assert(r->reversed);
00183     assert(l->name == r->name);
00184     assert(l->targ_id == r->targ_id);
00185     assert(l->left <= r->left);
00186     assert(l->first != r->first);
00187   }
00192   std::string frag_name() const {
00193     if (_read_l) {
00194       return _read_l->name;
00195     }
00196     assert(_read_r);
00197     return _read_r->name;
00198   }
00203   HitParams* params() { return &_params; }
00208   const HitParams* params() const { return &_params; }
00213   Target* target() const {
00214     return _target;
00215   }
00220   void target(Target* target) {
00221     _target = target;
00222   }
00228   const std::vector<const Target*>* neighbors() const { return &_neighbors; }
00233   void neighbors(const std::vector<const Target*>& neighbors) {
00234     _neighbors = neighbors;
00235   }
00240   TargID target_id() const {
00241     if (_read_l) {
00242       return _read_l->targ_id;
00243     }
00244     assert(_read_r);
00245     return _read_r->targ_id;
00246   }
00251   size_t left() const {
00252     if (_read_l) {
00253       return _read_l->left;
00254     }
00255     assert(_read_r);
00256     return _read_r->left;
00257   }
00262   size_t right() const {
00263     if (_read_r) {
00264       return _read_r->right;
00265     }
00266     assert(_read_l);
00267     return _read_l->right;
00268   }
00274   size_t length() const {
00275     if (_read_l && _read_r) {
00276       return _read_r->right - _read_l->left;
00277     }
00278     return 0;
00279   }
00285   const ReadHit* left_read() const {
00286     if (_read_l) {
00287       return _read_l.get();
00288     }
00289     return NULL;
00290   }
00296   const ReadHit* right_read() const {
00297     if (_read_r) {
00298       return _read_r.get();
00299     }
00300     return NULL;
00301   }
00307   const ReadHit* first_read() const {
00308     if (_read_l && _read_l->first) {
00309       return _read_l.get();
00310     }
00311     assert(_read_r);
00312     return _read_r.get();
00313   }
00319   const ReadHit* second_read() const {
00320     if (_read_l && !_read_l->first) {
00321       return _read_l.get();
00322     } else if (_read_r && !_read_r->first) {
00323       return _read_r.get();
00324     } else {
00325       return NULL;
00326     }
00327   }
00333   ReadHit* left_read() {
00334     return const_cast<ReadHit*>(const_cast<const FragHit*>(this)->left_read());
00335   }
00341   ReadHit* right_read() {
00342     return const_cast<ReadHit*>(const_cast<const FragHit*>(this)->right_read());
00343   }
00349   ReadHit* first_read() {
00350     return const_cast<ReadHit*>(const_cast<const FragHit*>(this)->first_read());
00351   }
00357   ReadHit* second_read() {
00358     return const_cast<ReadHit*>(const_cast<const FragHit*>(this)->second_read());
00359   }
00365   PairStatus pair_status() const {
00366     if (_read_l && _read_r) {
00367       return PAIRED;
00368     }
00369     if (_read_l) {
00370       return LEFT_ONLY;
00371     }
00372     return RIGHT_ONLY;
00373   }
00374 };
00375 
00384 class Fragment {
00389   std::vector<FragHit*> _frag_hits;
00395   std::vector<ReadHit*> _open_mates;
00399   std::string _name;
00404   double _mass;
00409   Library* _lib;
00416   void add_open_mate(ReadHit* om);
00417 
00418 public:
00422   Fragment(Library* lib);
00427   ~Fragment();
00432   const Library* lib() { return _lib; }
00442   bool add_map_end(ReadHit* r);
00447   const std::string& name() const { return _name; }
00452   size_t num_hits() const { return _frag_hits.size(); }
00458   FragHit* operator[](size_t i) const { return _frag_hits[i]; }
00464   const std::vector<FragHit*>& hits() const { return _frag_hits; }
00471   const FragHit* sample_hit() const;
00476   void mass(double m) { _mass = m; }
00482   double mass() const { return _mass; }
00487   void sort_hits();
00492   bool paired() const {
00493     if (_frag_hits.empty()) {
00494       return false;
00495     }
00496     return (_frag_hits[0]->pair_status() == PAIRED);
00497   }
00498 };
00499 
00500 #endif
 All Classes Functions Variables