/** Execute function */
RevPtr<RevVariable> Func_readBranchLengthTrees::execute( void ) {
    
    // get the information from the arguments for reading the file
    const RlString& fn = static_cast<const RlString&>( args[0].getVariable()->getRevObject() );
    
    // get the global instance of the NCL reader and clear warnings from its warnings buffer
    RevBayesCore::NclReader reader = RevBayesCore::NclReader();
    
    ModelVector<BranchLengthTree> *trees = new ModelVector<BranchLengthTree>();
    std::vector<RevBayesCore::BranchLengthTree*> tmp = *(reader.readBranchLengthTrees( fn.getValue() ));
    for (std::vector<RevBayesCore::BranchLengthTree*>::iterator t = tmp.begin(); t != tmp.end(); ++t) 
    {
        trees->push_back( BranchLengthTree(*t) );
    }
    
    return new RevVariable( trees );
}
Exemple #2
0
/** Execute function */
RevPtr<Variable> Func_range::execute( void ) {
    
    int f = static_cast<const Integer &>( args[0].getVariable()->getRevObject() ).getValue();
    int l = static_cast<const Integer &>( args[1].getVariable()->getRevObject() ).getValue();
    
    ModelVector<Integer> *range = new ModelVector<Integer>();
    if (f < l) {
        for ( int i = f; i <= l; i++ )
            range->push_back( Integer(i) );
    }
    else {
        for ( int i = f; i >= l; i-- )
            range->push_back( Integer(i) );
    }
    
    return new Variable( range );
}
Exemple #3
0
/* Map calls to member methods */
RevPtr<Variable> RlAtlas::executeMethod(std::string const &name, const std::vector<Argument> &args) {
    
    
    if (name == "nAreas")
    {
        return new Variable(new Natural((int)this->dagNode->getValue().getNumAreas())) ;
    }
    else if (name == "nEpochs")
    {
        return new Variable(new Natural((int)this->dagNode->getValue().getNumEpochs())) ;
    }
    else if (name == "names")
    {
        ModelVector<RlString> *n = new ModelVector<RlString>();
        const std::vector<std::vector<RevBayesCore::GeographicArea*> >& areas = this->dagNode->getValue().getAreas();
        for (size_t i = 0; i < areas[0].size(); ++i)
        {
            std::string name = areas[0][i]->getName();
            n->push_back( RlString( name ) );
        }
        return new Variable( n );
    }
    return ModelObject<RevBayesCore::TimeAtlas>::executeMethod( name, args );
}
/** Execute function */
RevPtr<RevVariable> Func_readCharacterDataUniversal::execute( void ) {
    
    // get the information from the arguments for reading the file
    const std::string& fn = static_cast<const RlString&>( args[0].getVariable()->getRevObject() ).getValue();
    bool returnAsVector = static_cast<const RlBoolean&>( args[1].getVariable()->getRevObject() ).getValue();
    
    // check that the file/path name has been correctly specified
    RevBayesCore::RbFileManager myFileManager( fn );
    if ( !myFileManager.testFile() && !myFileManager.testDirectory() )
        {
        std::string errorStr = "";
        formatError(myFileManager, errorStr);
        throw RbException("Could not find file or path with name \"" + fn + "\"");
        }
        
    // set up a vector of strings containing the name or names of the files to be read
    std::vector<std::string> vectorOfFileNames;
    if ( myFileManager.isDirectory() )
        {
        myFileManager.setStringWithNamesOfFilesInDirectory(vectorOfFileNames);
        }
    else 
        {
        vectorOfFileNames.push_back( myFileManager.getFullFileName() );
        }
    
    // get the global instance of the NCL reader and clear warnings from its warnings buffer
    RevBayesCore::NclReader reader = RevBayesCore::NclReader();
    
    // the vector of matrices;
    ModelVector<AbstractDiscreteCharacterData> *m = new ModelVector<AbstractDiscreteCharacterData>();
    
    // the return value
    RevObject* retVal = NULL;
    
    // Set up a map with the file name to be read as the key and the file type as the value. Note that we may not
    // read all of the files in the string called "vectorOfFileNames" because some of them may not be in a format
    // that can be read.
    size_t numFilesRead = 0;
    for (std::vector<std::string>::iterator p = vectorOfFileNames.begin(); p != vectorOfFileNames.end(); p++)
        {
        bool isInterleaved = false;
        std::string myFileType = "unknown";
        std::string dType = "unknown";
        if (reader.isNexusFile(*p) == true)
            {
            myFileType = "nexus";
            }
        else if (reader.isPhylipFile(*p, dType, isInterleaved) == true)
            {
            myFileType = "phylip";
            }
        else if (reader.isFastaFile(*p, dType) == true)
            {
            myFileType = "fasta";
            }
        
        int numMatricesReadForThisFile=0;
        if (myFileType != "unknown")
            {
            std::string suffix = "|" + dType;
            if ( myFileType == "phylip" )
                {
                if (isInterleaved == true)
                    {
                    suffix += "|interleaved";
                    }
                else
                    {
                    suffix += "|noninterleaved";
                    }
                }
            else if ( myFileType == "fasta" )
                {
                suffix += "|noninterleaved";
                }
            else
                {
                suffix += "|unknown";
                }
            myFileType += suffix;
            
            // read the content of the file now
            std::vector<RevBayesCore::AbstractCharacterData*> m_i = reader.readMatrices( *p, myFileType );
            for (std::vector<RevBayesCore::AbstractCharacterData*>::iterator it = m_i.begin(); it != m_i.end(); it++)
                {
                dType = (*it)->getDatatype();

                // Assume success; correct below if failure
                numMatricesReadForThisFile++;
                
                if ( dType == "DNA" )
                    {
                    RevBayesCore::DiscreteCharacterData<RevBayesCore::DnaState> *coreM = static_cast<RevBayesCore::DiscreteCharacterData<RevBayesCore::DnaState> *>( *it );
                    DiscreteCharacterData<DnaState> mDNA = DiscreteCharacterData<DnaState>( coreM );
                    m->push_back( mDNA );
                    }
                else if ( dType == "RNA" )
                    {
                    RevBayesCore::DiscreteCharacterData<RevBayesCore::RnaState> *coreM = static_cast<RevBayesCore::DiscreteCharacterData<RevBayesCore::RnaState> *>( *it );
                    DiscreteCharacterData<RnaState> mRNA = DiscreteCharacterData<RnaState>( coreM );
                    m->push_back( mRNA );
                    }
                else if ( dType == "Protein" )
                    {
                    RevBayesCore::DiscreteCharacterData<RevBayesCore::AminoAcidState> *coreM = static_cast<RevBayesCore::DiscreteCharacterData<RevBayesCore::AminoAcidState> *>( *it );
                    DiscreteCharacterData<AminoAcidState> mAA = DiscreteCharacterData<AminoAcidState>( coreM );
                    m->push_back( mAA );
                    }
                else if ( dType == "Standard" )
                    {
                    RevBayesCore::DiscreteCharacterData<RevBayesCore::StandardState> *coreM = static_cast<RevBayesCore::DiscreteCharacterData<RevBayesCore::StandardState> *>( *it );
                    DiscreteCharacterData<StandardState> mSS = DiscreteCharacterData<StandardState>( coreM );
                    m->push_back( mSS );
                    }
                else if ( dType == "Continuous" )
                    {
                    RevBayesCore::ContinuousCharacterData *coreM = static_cast<RevBayesCore::ContinuousCharacterData *>( *it );
                    ContinuousCharacterData mCC = ContinuousCharacterData (coreM );
                    m->push_back( mCC );
                    }
                else
                    {
                    numMatricesReadForThisFile--;
                    throw RbException("Unknown data type \"" + dType + "\".");
                    }
                }
            }
        else
            {
            reader.addWarning("Unknown file type");
            }
        
        if (numMatricesReadForThisFile > 0)
            {
            numFilesRead++;
            }
        }
    
    // print summary of results of file reading to the user
    if (myFileManager.isDirectory() == true)
        {
        std::stringstream o2;
        if ( numFilesRead == 0 )
            {
            o2 << "Failed to read any files from directory '" << fn << "'";
            }
        else if ( numFilesRead == 1 )
            {
            if ( m->size() == 1 )
                {
                o2 << "Successfully read one file with one character matrix from directory '" << fn << "'";
                }
            else
                {
                o2 << "Successfully read one file with " << m->size() << " character matrices from directory '" << fn << "'";
                }
            }
        else
            {
            o2 << "Successfully read " << numFilesRead << " files with " << m->size() << " character matrices from directory '" << fn << "'";
            }
        RBOUT(o2.str());
        std::set<std::string> myWarnings = reader.getWarnings();
        if ( vectorOfFileNames.size() - numFilesRead > 0 && myWarnings.size() > 0 )
            {
            std::stringstream o3;
            if (vectorOfFileNames.size() - numFilesRead == 1)
                {
                o3 << "Did not read a file for the following ";
                }
            else
                {
                o3 << "Did not read " << vectorOfFileNames.size() - numFilesRead << " files for the following ";
                }
            
            if (myWarnings.size() == 1)
                {
                o3 << "reason:";
                }
            else
                {
                o3 << "reasons:";
                }
            RBOUT(o3.str());
            for (std::set<std::string>::iterator it = myWarnings.begin(); it != myWarnings.end(); it++)
                {
                RBOUT("* "+(*it));
                }
            }

        // set the return value
        retVal = m;
        }
    else
        {
        if (m->size() == 1)
            {
            RBOUT("Successfully read one character matrix from file '" + fn + "'");

            // set the return value
            if ( returnAsVector == false )
                {
                retVal = new AbstractDiscreteCharacterData( (*m)[0] );
                delete m;
                }
            else
                {
                retVal = m;
                }
            }
        else if (m->size() > 1)
            {
            std::stringstream o3;
            o3 << "Successfully read " << m->size() << " character matrices from file '" << fn << "'";
            RBOUT(o3.str());
            
            // set the return value
            retVal = m;
            }
        else
            {
            std::set<std::string> myWarnings = reader.getWarnings();
            if ( myWarnings.size() > 0 )
                {
                std::stringstream o3;
                o3 << "Error reading file '" << fn << "'";
                RBOUT(o3.str());
                for (std::set<std::string>::iterator it = myWarnings.begin(); it != myWarnings.end(); it++)
                    {
                    RBOUT("Error:   " + (*it));
                    }
                }
            }
        }
    return new RevVariable( retVal );
}
Exemple #5
0
/* Map calls to member methods */
RevPtr<RevVariable> RlAtlas::executeMethod(std::string const &name, const std::vector<Argument> &args, bool &found)
{
    
    if (name == "nAreas")
    {
        found = true;
        
        return new RevVariable(new Natural((int)this->dagNode->getValue().getNumAreas())) ;
    }
    else if (name == "nEpochs")
    {
        found = true;
        
        return new RevVariable(new Natural((int)this->dagNode->getValue().getNumEpochs())) ;
    }
    else if (name == "names")
    {
        found = true;
        
        ModelVector<RlString> *n = new ModelVector<RlString>();
        const std::vector<std::vector<RevBayesCore::GeographicArea*> >& areas = this->dagNode->getValue().getAreas();
        for (size_t i = 0; i < areas[0].size(); ++i)
        {
            std::string name = areas[0][i]->getName();
            n->push_back( name );
        }
        return new RevVariable( n );
    }
    else if (name == "epochTimes")
    {
        found = true;
        ModelVector<RealPos> *n = new ModelVector<RealPos>( this->dagNode->getValue().getEpochs() );
        return new RevVariable( n );
    }
    else if (name == "getValues")
    {
        found = true;
        
        // get the member with give index
        std::string value = static_cast<const RlString &>( args[0].getVariable()->getRevObject() ).getValue();
        std::vector<std::vector<RevBayesCore::GeographicArea*> > areas = this->dagNode->getValue().getAreas();        
        ModelVector<ModelVector<ModelVector<Real > > > *f = new ModelVector<ModelVector<ModelVector<Real > > >();
        for (size_t i = 0; i < areas.size(); i++)
        {
            RevBayesCore::RbVector<RevBayesCore::RbVector<double> > v;
            for (size_t j = 0; j < areas[i].size(); j++)
            {
                if (value == "dispersal")
                    v.push_back(areas[i][j]->getDispersalValues());
                else if (value == "dispersal-upper")
                    v.push_back(areas[i][j]->getDispersalValues());
                else if (value == "extinction")
                    v.push_back(areas[i][j]->getExtinctionValues());
                else if (value == "latlon")
                    v.push_back(areas[i][j]->getLatlon());
                else if (value == "altitude")
                    v.push_back( RevBayesCore::RbVector<double>(1, areas[i][j]->getAltitude()) );
                else if (value == "size")
                    v.push_back( RevBayesCore::RbVector<double>(1, areas[i][j]->getSize()) );
            }
            f->push_back( ModelVector<ModelVector<Real> >(v) );
        }
        return new RevVariable(f);
    }
    
    return ModelObject<RevBayesCore::TimeAtlas>::executeMethod( name, args, found );
}
/* Map calls to member methods */
RevPtr<RevVariable> AbstractCharacterData::executeCharacterDataMethod(std::string const &name, const std::vector<Argument> &args, bool &found)
{
    if (name == "chartype")
    {
        found = true;
        
        return new RevVariable( new RlString( charDataObject->getDatatype() ) );
    }
    else if (name == "excludeTaxa")
    {
        found = true;
        
        const RevObject& argument = args[0].getVariable()->getRevObject();
        if ( argument.isType( RlString::getClassTypeSpec() ) )
        {
            const std::string &n = static_cast<const RlString&>( argument ).getValue();
            // remember that we internally store the character indeces from 0 to n-1
            // but externally represent it as 1 to n
            charDataObject->excludeTaxon( n );
        }
        else if ( argument.isType( ModelVector<RlString>::getClassTypeSpec() ) )
        {
            const ModelVector<RlString>& x = static_cast<const ModelVector<RlString>&>( argument );
            RevBayesCore::AbstractCharacterData &v = *charDataObject;
            for ( size_t i=0; i<x.size(); i++ )
            {
                v.excludeTaxon( x[i] );
            }
        }
        return NULL;
    }
    else if (name == "includeTaxa")
    {
        found = true;
        
        const RevObject& argument = args[0].getVariable()->getRevObject();
        if ( argument.isType( RlString::getClassTypeSpec() ) )
        {
            const std::string &n = static_cast<const RlString&>( argument ).getValue();
            // remember that we internally store the character indeces from 0 to n-1
            // but externally represent it as 1 to n
            charDataObject->includeTaxon( n );
        }
        else if ( argument.isType( ModelVector<RlString>::getClassTypeSpec() ) )
        {
            const ModelVector<RlString>& x = static_cast<const ModelVector<RlString>&>( argument );
            RevBayesCore::AbstractCharacterData &v = *charDataObject;
            for ( size_t i=0; i<x.size(); i++ )
            {
                // remember that we internally store the character indeces from 0 to n-1
                // but externally represent it as 1 to n
                v.includeTaxon( x[i] );
            }
        }
        
        return NULL;
    }
    else if (name == "isSequenceMissing")
    {
        found = true;
        
        const RevObject& argument = args[0].getVariable()->getRevObject();
        const std::string &n = static_cast<const RlString&>( argument ).getValue();
        
        bool tf = charDataObject->isSequenceMissing( n );

        return new RevVariable( new RlBoolean(tf) );
    }
    else if (name == "names")
    {
        found = true;
        
        ModelVector<RlString> *n = new ModelVector<RlString>();
        for (size_t i = 0; i < charDataObject->getNumberOfTaxa(); ++i)
        {
            n->push_back( charDataObject->getTaxonNameWithIndex( i ) );
        }
        
        return new RevVariable( n );
    }
    else if (name == "ntaxa") 
    {
        found = true;
        
        int n = (int)charDataObject->getNumberOfTaxa();
        
        return new RevVariable( new Natural(n) );
    }
    else if (name == "percentageMissing")
    {
        found = true;
        
        const RevObject& argument = args[0].getVariable()->getRevObject();
        const std::string &n = static_cast<const RlString&>( argument ).getValue();
        
        double p = charDataObject->getPercentageMissing( n );
        
        return new RevVariable( new Probability(p) );
    }
    else if (name == "size") 
    {
        found = true;
        
        int n = (int)charDataObject->getNumberOfTaxa();
        
        return new RevVariable( new Natural(n) );
    }
    else if (name == "removeTaxa" )
    {
        found = true;
        
        const RevObject& argument = args[0].getVariable()->getRevObject();
        if ( argument.isType( RlString::getClassTypeSpec() ) )
        {
            std::string n = std::string( static_cast<const RlString&>( argument ).getValue() );
            charDataObject->excludeTaxon( n );
        }
        else if ( argument.isType( ModelVector<RlString>::getClassTypeSpec() ) )
        {
            const ModelVector<RlString>& x = static_cast<const ModelVector<RlString>&>( argument );
            RevBayesCore::AbstractCharacterData &v = *charDataObject;
            for ( size_t i=0; i<x.size(); i++ )
            {
                std::string n = std::string( static_cast<const RlString&>( x[i] ).getValue() );
                v.excludeTaxon( n );
            }
        }
        return NULL;
    }
    else if (name == "setTaxonName")
    {
        found = true;
        
        const RevObject& current = args[0].getVariable()->getRevObject();
        if ( current.isType( RlString::getClassTypeSpec() ) )
        {
            std::string n = std::string( static_cast<const RlString&>( current ).getValue() );
            const RevObject& newName = args[1].getVariable()->getRevObject();
            if ( newName.isType( RlString::getClassTypeSpec() ) )
            {
                std::string name = std::string( static_cast<const RlString&>( newName ).getValue() );
                charDataObject->setTaxonName( n ,name );
               // std::cout << "new name: "<< dagNode->getValue().getTaxonData( n ).getTaxonName() << std::endl;
            }
        }
        return NULL;
    }
    else if (name == "show")
    {
        found = true;
        
        charDataObject->show(std::cout);
        
        return NULL;
    }
    
    
    // not found a matching method
    found = false;
    return NULL;
}
Exemple #7
0
/* Map calls to member methods */
RevPtr<RevVariable> RlDistanceMatrix::executeMethod(std::string const &name, const std::vector<Argument> &args, bool &found)
{
    
    if (name == "matrix")
    {
        found = true;

        return new RevVariable(new MatrixReal((RevBayesCore::MatrixReal) (this->dagNode->getValue().getMatrix() ) ) ) ;
    }
    else if (name == "names")
    {
        found = true;
		
		std::vector<std::string> names = this->dagNode->getValue().getNames();
        ModelVector<RlString> *n = new ModelVector<RlString>();
        for (size_t i = 0; i < names.size(); ++i)
        {
            n->push_back( names[i] );
        }
        return new RevVariable( n );
    }
	else if ( name == "size" )
	{
		found = true;
		
		// return a new RevVariable with the size of this container
		return RevPtr<RevVariable>( new RevVariable( new Natural( size() ), "" ) );
	}
	else if ( name == "getElement" )
	{
		
		if ( args.size() > 1 && args[0].getVariable()->getRevObject().isType( Natural::getClassTypeSpec() ) && args[1].getVariable()->getRevObject().isType( Natural::getClassTypeSpec() ) )
		{
			found = true;
			// get the member with given indices
			const Natural& i = static_cast<const Natural&>( args[0].getVariable()->getRevObject() );
			const Natural& j = static_cast<const Natural&>( args[1].getVariable()->getRevObject() );

			if ( size() < (size_t)(i.getValue()) )
			{
				throw RbException("Index i out of bounds in getElement");
			}
			if ( size() < (size_t)(j.getValue()) )
			{
				throw RbException("Index j out of bounds in getElement");
			}

			double element = static_cast< RevBayesCore::DistanceMatrix& >( this->dagNode->getValue() ).getElement(size_t(i.getValue()) - 1, size_t(j.getValue()) - 1);
			
			return new RevVariable( new Real( element ) );

		}

	}
	else if ( name == "setElement" )
	{
		
		if ( args.size() > 2 && args[0].getVariable()->getRevObject().isType( Natural::getClassTypeSpec() ) && args[1].getVariable()->getRevObject().isType( Natural::getClassTypeSpec() ) && args[2].getVariable()->getRevObject().isType( Real::getClassTypeSpec() ) )
		{
			found = true;
			// get the member with given indices
			const Natural& i = static_cast<const Natural&>( args[0].getVariable()->getRevObject() );
			const Natural& j = static_cast<const Natural&>( args[1].getVariable()->getRevObject() );
			const Real& v = static_cast<const Real&>( args[2].getVariable()->getRevObject() );

			if ( size() < (size_t)(i.getValue()) )
			{
				throw RbException("Index i out of bounds in getElement");
			}
			if ( size() < (size_t)(j.getValue()) )
			{
				throw RbException("Index j out of bounds in getElement");
			}
			
			static_cast< RevBayesCore::DistanceMatrix& >( this->dagNode->getValue() ).getElement(size_t(i.getValue()) - 1, size_t(j.getValue()) - 1) = double(v.getValue() );
			
		}
		return NULL;
		
	}

	
    return ModelObject<RevBayesCore::DistanceMatrix>::executeMethod( name, args, found );
}