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;
}
示例#2
0
//  ----------------------------------------------------------------------------
void CMultiReaderApp::xProcessNewick(
    const CArgs& args,
    CNcbiIstream& istr,
    CNcbiOstream& ostr)
//  ----------------------------------------------------------------------------
{
    string strTree;
    char c = istr.get();
    while (!istr.eof()) {
        strTree += c;
        if (c == ';') {
            CNcbiIstrstream istrstr(strTree.c_str());
            auto_ptr<TPhyTreeNode>  pTree(ReadNewickTree(istrstr) );
            CRef<CBioTreeContainer> btc = MakeBioTreeContainer(pTree.get());
            xWriteObject(*btc, ostr);
            strTree.clear();
        }
        c = istr.get();
    }
}
 /**
  **\brief Object constructor.
  **
  **\param newInputStream input stream to read data from.
  **\param is_nucl true if the input is DNA, false if it is protein
  **\param parse_seqids false to disable parsing of deflines
  **
  **/
 CMaskFastaReader( CNcbiIstream & newInputStream, bool is_nucl = true,
                   bool parse_seqids = false )
     : CMaskReader( newInputStream ), is_nucleotide_(is_nucl),
       parse_seqids_(parse_seqids), 
       fasta_reader_( newInputStream,
                      CONST_FLAGS | 
                      (is_nucl ? objects::CFastaReader::fAssumeNuc 
                               : objects::CFastaReader::fAssumeProt) |
                      (parse_seqids ? 0 
                                    : objects::CFastaReader::fNoParseID) )
 {
     if( !newInputStream && !newInputStream.eof() ) {
         NCBI_THROW( Exception, eBadStream, 
                 "bad stream state at fasta mask reader initialization" );
     }
 }
示例#4
0
void CAlnMrgApp::LoadSeqEntry(CNcbiIstream& is)
{
    string se_asn_type;
    {{
         auto_ptr<CObjectIStream> obj_is
             (CObjectIStream::Open(eSerial_AsnText, is));
         
         se_asn_type = obj_is->ReadFileHeader();
         obj_is->Close();
         is.seekg(0);
    }}
        
    auto_ptr<CObjectIStream> obj_is
        (CObjectIStream::Open(eSerial_AsnText, is));
    
    if (se_asn_type == "Seq-entry") {
        CRef<CSeq_entry> se (new CSeq_entry);
        *obj_is >> *se;
        GetScope().AddTopLevelSeqEntry(*se);
    } else {
示例#5
0
// Helper function for loading alignments. Returns number of alignments loaded
// into the container. The expected format is:
// <int-number-of-alignments>
// <Seq-align>+
size_t LoadAligns(CNcbiIstream& in,
                  CAlnContainer& aligns,
                  size_t limit = 0)
{
    size_t num_aligns = 0;
    while ( !in.eof() ) {
        try {
            CRef<CSeq_align> align(new CSeq_align);
            in >> MSerial_AsnText >> *align;
            align->Validate(true);
            aligns.insert(*align);
            num_aligns++;
            if (limit > 0  &&  num_aligns >= limit) break;
        }
        catch (CIOException e) {
            break;
        }
    }
    return num_aligns;
}
示例#6
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;
}
示例#7
0
void g_GZip_ScanForChunks(CNcbiIstream& is, IChunkHandler& handler)
{
    typedef IChunkHandler::TPosition TPos;

    // Use our own total counters to avoid 4GB limit
    TPos in_total  = 0;           // Offset in input compressed data
    TPos out_total = 0;           // Offset in output decompressed data
    z_stream strm;                // Compressed stream structure
    int      ret = Z_STREAM_END;  // zlib return status
    bool     initialized = false;


    // Default buffer size
    size_t in_size  = kCompressionDefaultBufSize;
    size_t out_size = kCompressionDefaultBufSize * 2;

    // Allocate buffers
    AutoArray<unsigned char> in_buf_arr(in_size);
    unsigned char* in_buf = in_buf_arr.get();
    if ( !in_buf ) {
        NCBI_THROW(CCoreException, eNullPtr, kEmptyStr);
    }
    AutoArray<unsigned char> out_buf_arr(out_size);
    unsigned char* out_buf = out_buf_arr.get();
    if ( !out_buf ) {
        NCBI_THROW(CCoreException, eNullPtr, kEmptyStr);
    }

    try {
        IChunkHandler::EAction action = IChunkHandler::eAction_Continue;
        // Process all decompressed data in the input stream
        while ( is  &&  action != IChunkHandler::eAction_Stop) {
            // Get some compressed data
            is.read((char*)in_buf, in_size);
            size_t nread = (size_t)is.gcount();
            if ( !nread ) {
                break;
            }
            // Process all data in the buffer
            strm.next_in  = in_buf;
            strm.avail_in = (unsigned int)nread;
            do {
                // Next gzip-file?
                if (ret == Z_STREAM_END) {
                    // Save current position
                    action = handler.OnChunk(in_total, out_total);
                    if (action == IChunkHandler::eAction_Stop) {
                        // Stop scanning
                        break;
                    }
                    // (Re)Initialize inflate
                    strm.zalloc   = Z_NULL;
                    strm.zfree    = Z_NULL;
                    strm.opaque   = Z_NULL;
                    ret = inflateInit2(&strm, 15+16 /* max windowbits + automatic gzip header decoding */);
                    if (ret != Z_OK) {
                        throw "inflateInit2() failed: " + string(zError(ret));
                    }
                    initialized = true;
                }
                // We don't need uncompressed data -- discard it
                strm.next_out  = out_buf;
                strm.avail_out = (unsigned int)out_size;

                // Decompress
                ret = inflate(&strm, Z_SYNC_FLUSH);
                if (ret != Z_OK  &&  ret != Z_STREAM_END ) {
                    // Error
                    throw "inflate() failed: " + string(zError(ret));
                }
                // Increase counters
                out_total += (out_size - strm.avail_out);
                in_total  += (nread - strm.avail_in);
                nread = strm.avail_in;
                // If found end of compressed stream -- cleanup
                if (ret == Z_STREAM_END) {
                    inflateEnd(&strm);
                    initialized = false;
                }
            } while (strm.avail_in != 0);
        }
        if ( initialized ) {
            inflateEnd(&strm);
        }
    }
    // Cleanup
    catch (string& e) {
        if ( initialized ) {
            inflateEnd(&strm);
        }
        NCBI_THROW(CCompressionException, eCompression, e);
    }
}