RleIter(RObject& rle): rlens(as<IntegerVector>(rle.slot("lengths"))), values(as<IntegerVector>(rle.slot("values"))), names(as<CharacterVector>(values.attr("levels"))), run(0), rpos(-1) { next(); }
//parses the GR object. void parseRegions(std::vector<GArray>& container, RObject& gr, samfile_t* in){ if (not gr.inherits("GRanges")) stop("must provide a GRanges object"); IntegerVector starts = as<IntegerVector>(as<RObject>(gr.slot("ranges")).slot("start")); IntegerVector lens = as<IntegerVector>(as<RObject>(gr.slot("ranges")).slot("width")); RObject chrsRle = as<RObject>(gr.slot("seqnames")); RObject strandsRle = as<RObject>(gr.slot("strand")); RleIter chrs(chrsRle); RleIter strands(strandsRle); container.reserve(container.size() + starts.length()); Iint e_starts = starts.end(); Iint i_starts = starts.begin(); Iint i_lens = lens.begin(); int lastStrandRun = -1; int strand = -1; int lastChrsRun = -1; int rid = -1; for (; i_starts < e_starts; ++i_starts, ++i_lens, chrs.next(), strands.next()){ //if new run, update chromosome if (lastChrsRun != chrs.run){ lastChrsRun = chrs.run; rid = getRefId(in, chrs.getValue()); if (rid == -1) stop("chromosome " + (std::string)chrs.getValue() + " not present in the bam file"); } //if new run, update strand if (lastStrandRun != strands.run){ lastStrandRun = strands.run; const std::string& s = strands.getValue(); if (s == "-"){ strand = -1; } else if (s == "+"){ strand = +1; } else { strand = 0; } } container.push_back(GArray(rid, *i_starts - 1, *i_lens, strand)); } }