/* * hddm_r.cpp - DO NOT EDIT THIS FILE * * This file was generated automatically by hddm-cpp from the file * exam2.xml * This c++ source implements the methods for the classes * described in the data model (from exam2.xml). * * The hddm data model tool set was written by * Richard Jones, University of Connecticut. * * For more information see the following web site * * http://zeus.phys.uconn.edu/halld/datamodel/doc */ #include "hddm_r.hpp" #ifndef _FILE_OFFSET_BITS # define _FILE_OFFSET_BITS 64 #endif using namespace hddm_r; istream::istream(std::istream &src) : m_xstr(0), m_istr(src), m_xcmp(0), m_xraw(0), m_status_bits(0) { std::string line; m_documentString = ""; while(std::getline(src,line).good()) { m_documentString += line + "\n"; if (line == "") { break; } } if (src.bad()) { throw std::runtime_error("hddm_r::istream::istream error - invalid hddm header"); } m_genome.m_tagname = "HDDM"; m_genome.m_sequence = synthesize(m_documentString,0,HDDM::DocumentString(),0); m_event_buffer = new char[m_event_buffer_size = 100000]; m_sstr.rdbuf()->pubsetbuf(m_event_buffer,m_event_buffer_size); configure_streambufs(); m_next_event_size = 0; m_events_to_skip = 0; } istream::~istream() { delete [] m_event_buffer; clear_streambufs(); } void istream::configure_streambufs() { clear_streambufs(); m_xstr = new xstream::xdr::istream(m_sstr); if (m_status_bits & k_z_compression) { //std::cerr << "input switched on z compression" << std::endl; m_xraw = m_istr.rdbuf(); m_xcmp = new xstream::z::istreambuf(m_xraw); m_istr.rdbuf(m_xcmp); } else if (m_status_bits & k_bz2_compression) { //std::cerr << "input switched on bz2 compression" << std::endl; m_xraw = m_istr.rdbuf(); m_xcmp = new xstream::bz::istreambuf(m_xraw); m_istr.rdbuf(m_xcmp); } else if (m_status_bits & k_bits_compression) { throw std::runtime_error("hddm_r::istream::configure_streambufs error - " "unrecognized compression flag requested."); } } void istream::clear_streambufs() { if (m_xraw) { m_istr.rdbuf(m_xraw); m_xraw = 0; } if (m_xcmp) { delete m_xcmp; m_xcmp = 0; } if (m_xstr) { delete m_xstr; m_xstr = 0; } } istream &istream::operator>>(HDDM &record) { if (m_next_event_size == 0) { m_istr.read(m_event_buffer,4); if (!m_istr.good()) { throw std::runtime_error("hddm_r::istream::operator>> error - " "attempt to read past end of file!"); } m_sstr.seekg(0); *m_xstr >> m_next_event_size; return *this >> record; } else if (m_next_event_size == 1) { m_istr.read(m_event_buffer+4,4); int size; *m_xstr >> size; m_istr.read(m_event_buffer+8,size); int format, flags; *m_xstr >> format >> flags; if (format != 0) { throw std::runtime_error("hddm_r::istream::operator>> error - " "unsupported compression format!"); } else if (flags != m_status_bits) { m_status_bits = flags; configure_streambufs(); } m_next_event_size = 0; return *this >> record; } else if (m_next_event_size+4 > m_event_buffer_size) { char *newbuf = new char[m_event_buffer_size = m_next_event_size+1000]; m_sstr.rdbuf()->pubsetbuf(newbuf, m_event_buffer_size); memcpy(newbuf,m_event_buffer,4); delete [] m_event_buffer; m_event_buffer = newbuf; } m_istr.read(m_event_buffer+4,m_next_event_size); if (m_events_to_skip) { --m_events_to_skip; m_next_event_size = 0; return *this >> record; } m_sstr.seekg(0); m_sequencing = 0; m_codon = &m_genome; *this >> (streamable&)record; m_istr.read(m_event_buffer,4); m_sstr.seekg(0); *m_xstr >> m_next_event_size; return *this; } ostream::ostream(std::ostream &src) : m_xstr(0), m_ostr(src), m_xcmp(0), m_xraw(0), m_status_bits(k_default_status) { m_ostr << HDDM::DocumentString(); m_event_buffer = new char[m_event_buffer_size = 100000]; m_sstr.rdbuf()->pubsetbuf(m_event_buffer,m_event_buffer_size); configure_streambufs(); } ostream::~ostream() { delete [] m_event_buffer; clear_streambufs(); } void ostream::setCompression(int flags) { if ((flags ^ m_status_bits) & k_bits_compression) { if ((m_status_bits & k_bits_compression) != k_no_compression) { std::cerr << "hddm_r::ostream::setCompression warning - " << "compression already enabled, cannot change." << std::endl; return; } m_status_bits &= ~k_bits_compression; m_status_bits |= flags; m_sstr.str(""); *m_xstr << 1 << 8 << 0 << m_status_bits; m_ostr << m_sstr.str(); configure_streambufs(); } } void ostream::configure_streambufs() { clear_streambufs(); m_xstr = new xstream::xdr::ostream(m_sstr); if (m_status_bits & k_z_compression) { //std::cerr << "output switched on z compression" << std::endl; m_xraw = m_ostr.rdbuf(); m_xcmp = new xstream::z::ostreambuf(m_xraw); m_ostr.rdbuf(m_xcmp); } else if (m_status_bits & k_bz2_compression) { //std::cerr << "output switched on bz2 compression" << std::endl; m_xraw = m_ostr.rdbuf(); m_xcmp = new xstream::bz::ostreambuf(m_xraw); m_ostr.rdbuf(m_xcmp); } else if (m_status_bits & k_bits_compression) { throw std::runtime_error("hddm_r::ostream::configure_streambufs error - " "unrecognized compression flag requested."); } } void ostream::clear_streambufs() { if (m_xstr) { delete m_xstr; m_xstr = 0; } if (m_xraw) { m_ostr.flush(); m_ostr.rdbuf(m_xraw); m_xraw = 0; } if (m_xcmp) { delete m_xcmp; m_xcmp = 0; } } int istream::getTag(const std::string &src, int start, std::string &tag, int &level) { tag = ""; size_t p_btag = src.find("<",start); size_t p_bline = src.find_last_of("\n",p_btag); if (p_bline == std::string::npos) { p_bline = 0; } else { ++p_bline; } level = (p_btag-p_bline)/2; size_t p_etag = p_btag; for (size_t quotes=0; p_etag < src.size(); ++p_etag) { if (src[p_etag] == '"') { tag += "\""; ++quotes; } else if (quotes/2*2 != quotes) { tag += src[p_etag]; } else if (src.find_first_of(" \t\n",p_etag) == 0) { tag += " "; p_etag = src.find_first_not_of(" \t\n",p_etag)-1; } else if (src[p_etag] == '>') { tag += ">"; break; } else { tag += src[p_etag]; } } if (p_etag == src.size()) { std::stringstream sstr; sstr << "hddm_r::istream::getTag" << " error - bad header format" << std::endl << " tag " << tag << " at position " << start << std::endl; throw std::runtime_error(sstr.str()); } return p_etag+2; } int istream::getEndTag(const std::string &src, int start, const std::string &tag) { if (tag.rfind("/>") == tag.size()-2) { return src.find(tag,start) + tag.size()+1; } else { std::string etag = ""; size_t p_etag = src.find(etag,start); size_t p_quote = src.find_first_of('"',start); while (p_quote != std::string::npos && p_quote < p_etag) { p_quote = src.find_first_of('"',p_quote+1); if (p_quote > p_etag) { p_etag = src.find(etag,p_quote+1); } p_quote = src.find_first_of('"',p_quote+1); } if (p_etag == std::string::npos) { std::stringstream sstr; sstr << "hddm_r::istream::getEndTag" << " error - bad header format" << std::endl << " tag " << tag << " at position " << start << std::endl << " end tag " << etag << " not found." << std::endl; throw std::runtime_error(sstr.str()); } return p_etag + etag.size()+1; } } void istream::collide(const std::string &itag, const std::string &rtag) { std::string itagname = itag.substr(1,itag.find(" ")-1); std::string rtagname = rtag.substr(1,rtag.find(" ")-1); std::string errmsg = "hddm_r::istream::collide warning:\n" "tag " + itagname + " in input file " "does not match c++ header hddm_r.hpp\n" " input file: " + itag + "\n" " c++ header: " + rtag + "\n" " === Tag " + itagname + " will be ignored," " rebuild to cure the problem ==="; if (itagname != "HDDM") { std::cerr << errmsg << std::endl; } else { throw std::runtime_error(errmsg); } } chromosome istream::synthesize(const std::string &src, int p_src, const std::string &ref, int p_ref) { chromosome chrom; int slevel, rlevel; std::string stag, rtag; p_src = getTag(src,p_src,stag,slevel); p_ref = getTag(ref,p_ref,rtag,rlevel); std::string stagname = stag.substr(1,stag.find(" ")-1); std::string rtagname = rtag.substr(1,rtag.find(" ")-1); if (stagname != rtagname) { throw std::runtime_error("hddm_r::istream::synthesize error - matching algorithm error #2"); } else if (stag != rtag) { collide(stag,rtag); return chrom; } int p2_src, p2_ref; int s2level, r2level; std::string s2tag, r2tag; getTag(src,p2_src=p_src,s2tag,s2level); while (s2level > slevel) { codon *gene = new codon(); std::string s2tagname = s2tag.substr(1,s2tag.find(" ")-1); getTag(ref,p2_ref=p_ref,r2tag,r2level); int order_of_this_tag_in_ref = 1; while (r2level == s2level) { std::string r2tagname = r2tag.substr(1,r2tag.find(" ")-1); if (s2tagname == r2tagname) { if (s2tag != r2tag) { collide(s2tag,r2tag); break; } else { gene->m_order = order_of_this_tag_in_ref; } gene->m_sequence = synthesize(src,p2_src,ref,p2_ref); break; } p2_ref = getEndTag(ref,p2_ref,r2tag); getTag(ref,p2_ref,r2tag,r2level); ++order_of_this_tag_in_ref; } gene->m_tagname = s2tagname; chrom.push_back(*gene); delete gene; p2_src = getEndTag(src,p2_src,s2tag); getTag(src,p2_src,s2tag,s2level); } return chrom; }