示例#1
0
/**
 * Open the file stream to a file with the name used in the constructor.
 */
void NexusWriter::openStream( void ) 
{
    RbFileManager f = RbFileManager(fileName);
    f.createDirectoryForFile();
    
    // open the stream to the file
    outStream.open( fileName.c_str(), std::fstream::out );
    
    // write header line
    outStream << "#NEXUS" << std::endl;
    
}
void PowerPosteriorAnalysis::summarizeStones( void )
{
    // create the directory if necessary
    RbFileManager f = RbFileManager(filename);
    f.createDirectoryForFile();
    
    std::ofstream outStream;
    outStream.open( filename.c_str(), std::fstream::out);
    outStream << "state\t" << "power\t" << "likelihood" << std::endl;

    /* Append each stone */
    for (size_t idx = 0; idx < powers.size(); ++idx)
    {
        RbFileManager fm = RbFileManager(filename);
        std::string stoneFileName = fm.getFileNameWithoutExtension() + "_stone_" + idx + "." + fm.getFileExtension();
        
        RbFileManager f = RbFileManager(fm.getFilePath(), stoneFileName);

        // read the i-th stone
        std::ifstream inStream;
        inStream.open( f.getFullFileName().c_str(), std::fstream::in);
        if (inStream.is_open())
        {
            bool header = true;
            std::string line = "";
            while ( std::getline (inStream,line) )
            {
                // we need to skip the header line
                if ( header == true )
                {
                    header  = false;
                }
                else
                {
                    outStream << line << std::endl;
                }
                
            }
            inStream.close();
        }
        else
        {
            std::cerr << "Problem reading stone " << idx+1 << " from file " << stoneFileName << "." << std::endl;
        }

    }

}
示例#3
0
/** open the file stream for printing */
void AbstractFileMonitor::openStream(void)
{
    
    RbFileManager f = RbFileManager(working_file_name);
    f.createDirectoryForFile();
        
    // open the stream to the file
    if ( append == true )
    {
        out_stream.open( working_file_name.c_str(), std::fstream::in | std::fstream::out | std::fstream::app);
    }
    else
    {
        out_stream.open( working_file_name.c_str(), std::fstream::out);
        out_stream.close();
        out_stream.open( working_file_name.c_str(), std::fstream::in | std::fstream::out);
    }
    
//    out_stream.close();
    
}
示例#4
0
/**
 * This method simply writes a character data object into a file in Fasta format.
 *
 * \param[in]   fileName    The name of the file into which the objects is to be written.
 * \param[in]   data        The character data object which is written out.
 */
void FastaWriter::writeData(std::string const &fileName, const AbstractHomologousDiscreteCharacterData &data)
{
    
    // the filestream object
    std::fstream outStream;
    
    RbFileManager f = RbFileManager(fileName);
    f.createDirectoryForFile();
    
    // open the stream to the file
    outStream.open( fileName.c_str(), std::fstream::out );
    
    const std::vector<Taxon> &taxa = data.getTaxa();
    for (std::vector<Taxon>::const_iterator it = taxa.begin();  it != taxa.end(); ++it)
    {

        if ( !data.isTaxonExcluded( it->getName() ) )
        {

            const AbstractDiscreteTaxonData &taxon = data.getTaxonData( it->getName() );

            outStream << ">" << it->getName() << std::endl;

            size_t nChars = taxon.getNumberOfCharacters();
            for (size_t i = 0; i < nChars; ++i)
            {
                if ( !data.isCharacterExcluded( i ) )
                {
                    const CharacterState &c = taxon.getCharacter( i );
                    outStream << c.getStringValue();
                }
            }
            outStream << std::endl;
        }
    }
    
    // close the stream
    outStream.close();
}
void PowerPosteriorAnalysis::runStone(size_t idx, size_t gen)
{
    
    // create the directory if necessary
    RbFileManager fm = RbFileManager(filename);
    std::string stoneFileName = fm.getFileNameWithoutExtension() + "_stone_" + idx + "." + fm.getFileExtension();

    RbFileManager f = RbFileManager(fm.getFilePath(), stoneFileName);
    f.createDirectoryForFile();
    
    std::fstream outStream;
    outStream.open( f.getFullFileName().c_str(), std::fstream::out);
    outStream << "state\t" << "power\t" << "likelihood" << std::endl;
    
    // reset the counters for the move schedules
    sampler->reset();
    
//    if ( sampler->getCurrentGeneration() == 0 )
//    {
//    }
    
    /* Reset the monitors */
    //    for (size_t i=0; i<replicates; ++i)
    //    {
    //        for (size_t j=0; i<runs[i].getMonitors().size(); i++)
    //        {
    //            runs[i].getMonitors()[j].reset( kIterations);
    //        }
    //    }
    
    // reset the stopping rules
//    for (size_t i=0; i<rules.size(); ++i)
//    {
//        rules[i].runStarted();
//    }

    
    size_t burnin = size_t( ceil( 0.25*gen ) );
    
    size_t printInterval = size_t( round( fmax(1,gen/20.0) ) );
    size_t digits = size_t( ceil( log10( powers.size() ) ) );
    
    /* Run the chain */
    if ( processActive )
    {
        std::cout << "Step ";
        for (size_t d = size_t( ceil( log10( idx+1.1 ) ) ); d < digits; d++ )
        {
            std::cout << " ";
        }
        std::cout << (idx+1) << " / " << powers.size();
        std::cout << "\t\t";
    }
    
    // set the power of this sampler
    sampler->setLikelihoodHeat( powers[idx] );
    sampler->setStoneIndex( idx );
    
    
    // Monitor
    sampler->startMonitors(gen);
    sampler->monitor(0);
    
    double p = powers[idx];
    for (size_t k=1; k<=gen; k++)
    {
        
        if ( processActive )
        {
            if ( k % printInterval == 0 )
            {
                std::cout << "**";
                std::cout.flush();
            }
        }
        
        sampler->nextCycle( true );

        // Monitor
        sampler->monitor(k);
        
        // sample the likelihood
        if ( k > burnin && k % sampleFreq == 0 )
        {
            // compute the joint likelihood
            double likelihood = sampler->getModelLnProbability();
            outStream << k << "\t" << p << "\t" << likelihood << std::endl;
        }
            
    }
    
    if ( processActive )
    {
        std::cout << std::endl;
    }
    
    outStream.close();
    
    
}