| 1 | #include "SeqLib/Fractions.h" |
| 2 | |
| 3 | namespace SeqLib { |
| 4 | |
| 5 | FracRegion::FracRegion(const std::string& c, const std::string& p1, const std::string& p2, bam_hdr_t * h, const std::string& f) : SeqLib::GenomicRegion(c, p1, p2, h) |
| 6 | { |
| 7 | // convert frac to double |
| 8 | try { |
| 9 | frac = std::stod(f); |
| 10 | } catch (...) { |
| 11 | std::cerr << "FracRegion::FracRegion - Error converting fraction " << f << " to double " << std::endl; |
| 12 | exit(EXIT_FAILURE); |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | size_t Fractions::size() const { |
| 17 | return m_frc.size(); |
| 18 | } |
| 19 | |
| 20 | |
| 21 | std::ostream& operator<<(std::ostream& out, const FracRegion& f) { |
| 22 | out << f.chr << ":" << SeqLib::AddCommas<int32_t>(f.pos1) << "-" << |
| 23 | SeqLib::AddCommas<int32_t>(f.pos2) << " Frac: " << f.frac; |
| 24 | return out; |
| 25 | } |
| 26 | |
| 27 | void Fractions::readFromBed(const std::string& file, bam_hdr_t * h) { |
| 28 | |
| 29 | std::ifstream iss(file.c_str()); |
| 30 | if (!iss || file.length() == 0) { |
| 31 | std::cerr << "BED file does not exist: " << file << std::endl; |
| 32 | exit(EXIT_FAILURE); |
| 33 | } |
| 34 | |
| 35 | std::string line; |
| 36 | std::string curr_chr = "-1"; |
| 37 | while (std::getline(iss, line, '\n')) { |
| 38 | |
| 39 | size_t counter = 0; |
| 40 | std::string chr, pos1, pos2, f; |
| 41 | std::istringstream iss_line(line); |
| 42 | std::string val; |
| 43 | |
| 44 | if (line.find("#") == std::string::npos) { |
| 45 | while(std::getline(iss_line, val, '\t')) { |
| 46 | switch (counter) { |
| 47 | case 0 : chr = val; break; |
| 48 | case 1 : pos1 = val; break; |
| 49 | case 2 : pos2 = val; break; |
| 50 | case 3 : f = val; break; |
| 51 | } |
| 52 | if (counter >= 3) |
| 53 | break; |
| 54 | ++counter; |
| 55 | |
| 56 | if (chr != curr_chr) { |
| 57 | //std::cerr << "...reading from BED - chr" << chr << std::endl; |
| 58 | curr_chr = chr; |
| 59 | } |
| 60 | |
| 61 | } |
| 62 | |
| 63 | // construct the GenomicRegion |
| 64 | FracRegion ff(chr, pos1, pos2, h, f); |
| 65 | |
| 66 | if (ff.valid()) { |
| 67 | //gr.pad(pad); |
| 68 | //m_grv.push_back(gr); |
| 69 | m_frc.add(ff); |
| 70 | } |
| 71 | |
| 72 | //} |
| 73 | } // end "keep" conditional |
| 74 | } // end main while |
| 75 | |
| 76 | |
| 77 | |
| 78 | } |
| 79 | |
| 80 | } |