Example #1
0
///////////////////////////////////////////////////////
///
/// Read a string from a stream. The string is following
/// by its size
///
inline string ReadStringFromStream(CNcbiIstream& is)
{
    string str;
    if (!is.good() || is.eof())
        return str;

    size_t size;
    is >> size;
    if (!is.good() || is.eof())
        return str;
    vector<char> buf(size+1, 0);
    is.read(&*buf.begin(), size+1);
    size_t count = is.gcount();
    if (count > 0)
        str.append((&*buf.begin())+1, count-1);

    return str;
}
bool CBasicFastaWrapper::ReadFile(CNcbiIstream& iStream) 
{
    bool result = (iStream.good());

    if (!result) {
        m_error = "Read Error:  invalid stream.\n";
    } else {

        CNcbiOstrstream oss;
        oss << iStream.rdbuf();
        iStream.seekg(0);  

        m_activeFastaString = CNcbiOstrstreamToString(oss);
        if (m_cacheRawFasta) m_rawFastaString = m_activeFastaString;

        //  temporarily turn off warning messages (in case of '.' in *.a2m files)
        EDiagSev originalDiagSev = SetDiagPostLevel(eDiag_Error);  

	    try{
            CStreamLineReader lineReader(iStream);
            CFastaReader fastaReader(lineReader, m_readFastaFlags);
            //CCounterManager counterMgr(reader.SetIDGenerator(), NULL);
            m_seqEntry = fastaReader.ReadSet();

            //  If there is only one sequence in the fasta, the Seq-entry returned is a Bioseq and not a Bioseq-set.
            //  In that case, change the Bioseq to a Bioseq-set so caller doesn't have to manage multiple Seq-entry choices.
            if (m_seqEntry->IsSeq() && m_useBioseqSet) {
                CRef<CSeq_entry> bioseqFromFasta(new CSeq_entry);
                bioseqFromFasta->Assign(*m_seqEntry);

                m_seqEntry->Select(CSeq_entry::e_Set);
                m_seqEntry->SetSet().SetSeq_set().push_back(bioseqFromFasta);
            }

	    } catch (...) {
            result = false;
            m_seqEntry.Reset();
        }

        if (m_seqEntry.Empty()) {
            result = false;
            m_error = "Read Error:  empty seq entry.\n";
        }
        SetDiagPostLevel(originalDiagSev);

    }
    return result;
}