eXpress “1.5”

src/fragments.cpp

00001 //
00002 //  fragments.cpp
00003 //  express
00004 //
00005 //  Created by Adam Roberts on 3/23/11.
00006 //  Copyright 2011 Adam Roberts. All rights reserved.
00007 //
00008 
00009 #include "fragments.h"
00010 #include "main.h"
00011 #include <string.h>
00012 #include <stdlib.h>
00013 
00014 using namespace std;
00015 
00016 Fragment::Fragment(Library* lib) : _lib(lib) {}
00017 
00018 Fragment::~Fragment() {
00019   for (size_t i = 0; i < num_hits(); i++) {
00020     delete _frag_hits[i];
00021   }
00022 
00023   for (size_t i = 0; i < _open_mates.size(); i++) {
00024     delete _open_mates[i];
00025   }
00026 }
00027 
00028 bool Fragment::add_map_end(ReadHit* r)
00029 {
00030   if (_name.empty()) {
00031     _name = r->name;
00032   } else if (_name != r->name) {
00033     return false;
00034   }
00035 
00036   if (r->mate_l >= 0) {
00037     add_open_mate(r);
00038   } else {  // single-end fragment
00039     _frag_hits.push_back(new FragHit(r));
00040   }
00041 
00042   return true;
00043 }
00044 
00045 void Fragment::add_open_mate(ReadHit* nm) {
00046   bool found = false;
00047 
00048   for(vector<ReadHit*>::iterator it = _open_mates.begin();
00049       it != _open_mates.end(); ++it) {
00050     ReadHit* om = *it;
00051     if (nm->targ_id == om->targ_id &&
00052         (size_t)nm->mate_l == om->left &&
00053         (size_t)om->mate_l == nm->left &&
00054         nm->first != om->first &&
00055         nm->reversed != om->reversed) {
00056       FragHit* h = NULL;
00057        if (nm->left < om->left || (nm->left == om->left && om->reversed)) {
00058         h = new FragHit(nm, om);
00059       } else {
00060         h = new FragHit(om, nm);
00061       }
00062 
00063       found = true;
00064       _frag_hits.push_back(h);
00065       _open_mates.erase(it);
00066       break;
00067     }
00068   }
00069 
00070   if (!found) {
00071     _open_mates.push_back(nm);
00072   }
00073 }
00074 
00075 const FragHit* Fragment::sample_hit() const {
00076   vector<double> probs(_frag_hits.size());
00077   probs[0] = sexp(_frag_hits[0]->params()->posterior);
00078   for (size_t i=1; i < _frag_hits.size(); ++i) {
00079     probs[i] = probs[i-1] + sexp(_frag_hits[i]->params()->posterior);
00080   }
00081 
00082   double r = rand()/double(RAND_MAX)*probs.back();
00083   size_t i = lower_bound(probs.begin(), probs.end(), r) - probs.begin();
00084   return _frag_hits[i];
00085 }
00086 
00087 bool fraghit_compare(FragHit* h1, FragHit* h2) {
00088   return h1->target_id() < h2->target_id();
00089 }
00090 
00091 void Fragment::sort_hits() {
00092   sort(_frag_hits.begin(), _frag_hits.end(), fraghit_compare);
00093 }
 All Classes Functions Variables