#include "phycas/phycas.h" #if defined(ALL_BIND_USING_CPPS_IN_ONE_FILE) # define INCLUDED_FROM_OTHER_BIND_USING_FILE # if defined(_MSC_VER) //#warning not allowed in VC7 # include "./cipres_nexus_reader.cpp" # include "ncl/characters/nxs_characters_block.cpp" # include "ncl/command/nxs_command_output.cpp" # else # warning using macro to include cipres_nexus_reader.cpp TEMPORARY HACK! # include "./cipres_nexus_reader.cpp" # warning using macro to include nxs_charcters_block.cpp TEMPORARY HACK! # include "ncl/characters/nxs_characters_block.cpp" # warning using macro to include nxs_command_output.cpp TEMPORARY HACK! # include "ncl/command/nxs_command_output.cpp" # endif #endif #include "cipres_services/read_nexus/read_nexus_i.hpp" #include "cipres_services/read_nexus/cipres_nexus_reader.hpp" #include "ncl/misc/nxs_file_path.hpp" #include "phycas/trees/trees_manager.hpp" #include "phycas/characters/characters_manager.hpp" #include "phycas/taxa/taxa_manager.hpp" #include "ncl/nxs_token.hpp" #include "CipresCommlib/CipresAssert.hpp" #include using std::vector; using std::map; using std::string; // TAO_IDL - Generated from // be/be_visitor_interface/interface_is.cpp:52 // Implementation skeleton constructor CipresIDL_api1_ReadNexus_i::CipresIDL_api1_ReadNexus_i (const bool verbose) :verboseMode(verbose) { } // Implementation skeleton destructor CipresIDL_api1_ReadNexus_i::~CipresIDL_api1_ReadNexus_i (void) { } CipresIDL_api1_ReadNexus_i::CipresNexusReaderShPtr CipresIDL_api1_ReadNexus_i::newNexusReader ( CORBA::Short blocksToRead) const { const int blocksToReadBits = static_cast(blocksToRead); return CipresNexusReaderShPtr(new CipresNexusReader(blocksToReadBits)); } // called after a file or string is read ::CipresIDL_api1::PhyloReader::NumBlockReadSequence * CipresIDL_api1_ReadNexus_i::postNexusReading( CipresNexusReaderShPtr mostRecentNexusReader, CORBA::Short blocksToRead) { const int blocksToReadBits = static_cast(blocksToRead); nexusReader = mostRecentNexusReader; //store in a persistent variable after successful read ::CipresIDL_api1::PhyloReader::NumBlockReadSequence_var t = new ::CipresIDL_api1::PhyloReader::NumBlockReadSequence(); t->length(3); boost::shared_ptr taxaMgr = nexusReader->GetTaxaManager(); boost::shared_ptr treesMgr = nexusReader->GetTreesManager(); boost::shared_ptr charMgr = nexusReader->GetCharactersManager(); t[CipresIDL_api1::PhyloReader::NEXUS_TAXA_BLOCK_INDEX] = CORBA::Short((blocksToReadBits & (int) CipresIDL_api1::PhyloReader::NEXUS_TAXA_BLOCK_BIT) && taxaMgr && taxaMgr->GetSize() > 0 ? 1 : 0); t[CipresIDL_api1::PhyloReader::NEXUS_TREES_BLOCK_INDEX] = CORBA::Short((blocksToReadBits & (int) CipresIDL_api1::PhyloReader::NEXUS_TREES_BLOCK_BIT) && treesMgr && treesMgr->GetSize() > 0 ? 1 : 0); t[CipresIDL_api1::PhyloReader::NEXUS_CHARACTERS_BLOCK_INDEX] = CORBA::Short((blocksToReadBits & (int) CipresIDL_api1::PhyloReader::NEXUS_CHARACTERS_BLOCK_BIT) && charMgr && charMgr->GetSize() > 0 ? charMgr->GetNumMatrices() : 0); return t._retn(); } ::CipresIDL_api1::PhyloReader::NumBlockReadSequence * CipresIDL_api1_ReadNexus_i::readNexusFile ( const char * filePath, CORBA::Short blocksToRead ) ACE_THROW_SPEC ((CORBA::SystemException, CipresIDL_api1::NexusException)) { try { CipresNexusReaderShPtr tempNexusReader = newNexusReader(blocksToRead); NxsInFilePath infilePath(filePath); tempNexusReader->ReadNxsFilePath(infilePath); return postNexusReading(tempNexusReader, blocksToRead); } catch (const NxsException & x) { throw CipresIDL_api1::NexusException(x.msg.c_str(), filePath, x.pos, x.line); } catch (const std::bad_alloc &) { throw CORBA::NO_MEMORY(); } } ::CipresIDL_api1::PhyloReader::NumBlockReadSequence * CipresIDL_api1_ReadNexus_i::readStringAsNexus ( const char * nexusContent, CORBA::Short blocksToRead ) ACE_THROW_SPEC ((CORBA::SystemException, CipresIDL_api1::NexusException)) { try { CipresNexusReaderShPtr tempNexusReader = newNexusReader(blocksToRead); const std::string s(nexusContent); //adding a new const char * constructor to NxsToken, would eliminate this copy. tempNexusReader->ReadStringAsNexus(s); return postNexusReading(tempNexusReader, blocksToRead); } catch (const NxsException & x) { std::cerr << " NxsException caught"<< std::endl; throw CipresIDL_api1::NexusException(x.msg.c_str(), "", x.pos, x.line); } catch (const std::bad_alloc &) { throw CORBA::NO_MEMORY(); } catch (...) { const char * msg = "uncaught exception in readStringAsNexus"; std::cerr << msg << std::endl; throw CipresIDL_api1::NexusException(msg, "", -1, -1); } } // TAO_IDL - Generated from // be/be_visitor_operation/operation_is.cpp:57 ::CipresIDL_api1::DataMatrix * CipresIDL_api1_ReadNexus_i::getCharacters ( CORBA::Short charactersBlockIndex ) ACE_THROW_SPEC (( CORBA::SystemException )) { if (charactersBlockIndex != 0) { CIPRES_RELEASE_ASSERT("Multiple characters blocks are currently not supported"); } if (!nexusReader) { CIPRES_RELEASE_ASSERT("readNexusFile must be called before getCharacters()"); } if (charactersBlockIndex >= 0) { typedef boost::scoped_ptr DiscMatrixScPtr; DiscMatrixScPtr discMatrix(createNativeDiscreteMatrix(*nexusReader, (unsigned int) charactersBlockIndex)); ::CipresIDL_api1::DataMatrix * toReturn = discMatrix->_retn(); return toReturn; } ::CipresIDL_api1::DataMatrix_var cipresMat = new ::CipresIDL_api1::DataMatrix(); return cipresMat._retn(); } // TAO_IDL - Generated from // be/be_visitor_operation/operation_is.cpp:57 ::CipresIDL_api1::TaxonInfoSeq * CipresIDL_api1_ReadNexus_i::getTaxa ( CORBA::Short taxaBlockIndex ) ACE_THROW_SPEC (( CORBA::SystemException )) { ::CipresIDL_api1::TaxonInfoSeq_var t = new ::CipresIDL_api1::TaxonInfoSeq(); if (taxaBlockIndex != 0) { CIPRES_RELEASE_ASSERT("Multiple taxa blocks are currently not supported"); } if (taxaBlockIndex >= 0) { boost::shared_ptr taxaMgr = nexusReader->GetTaxaManager(); //@should get the taxa block # taxaBlockIndex here, now we can only handle one so we ignore the arg. const unsigned nTaxa = (taxaMgr ? taxaMgr->GetSize(): 0); t->length(nTaxa); if (nTaxa > 0) { const VecString labels = taxaMgr->GetAllLabels(); unsigned ind = 0; for (VecString::const_iterator labIt = labels.begin(); labIt != labels.end(); ++labIt, ++ind) t[ind] = CORBA::string_dup(labIt->c_str()); } } return t._retn(); } // TAO_IDL - Generated from // be/be_visitor_operation/operation_is.cpp:57 ::CipresIDL_api1::TreeSeq * CipresIDL_api1_ReadNexus_i::getTrees ( CORBA::Short treesBlockIndex ) ACE_THROW_SPEC (( CORBA::SystemException )) { ::CipresIDL_api1::TreeSeq_var toReturn = new ::CipresIDL_api1::TreeSeq(); if (treesBlockIndex != 0) { CIPRES_RELEASE_ASSERT("Multiple trees blocks are currently not supported"); } if (treesBlockIndex >= 0) { const VecOfTreeDescriptions & allTrees = nexusReader->GetTrees(); toReturn->length(allTrees.size()); unsigned currTreeIndex = 0; for (VecOfTreeDescriptions::const_iterator trIt = allTrees.begin(); trIt != allTrees.end(); ++trIt, ++currTreeIndex) { toReturn[currTreeIndex] = CipresIDL_api1::Tree(); CipresIDL_api1::Tree & t = toReturn[currTreeIndex]; string newNewick = incrementTreesTaxaIndices(trIt->newick); //cipres indices start at 1, ncl's start at 0 newNewick.append(";"); t.m_newick = CORBA::string_dup(newNewick.c_str()); t.m_name = CORBA::string_dup(trIt->name.c_str()); t.m_score.noScore(0.0); t.m_leafSet.length(trIt->leafSet.size()); unsigned ind = 0; for (NxsIndexSet::const_iterator leafIt = trIt->leafSet.begin(); leafIt != trIt->leafSet.end(); ++leafIt, ++ind) t.m_leafSet[ind] = 1 + *leafIt; //cipres indices start at 1, ncl's start at 0 //SetCorbaLong::const_iterator indIt = indices.begin(); //for (UInt k = 0; indIt != indices.end(); ++k, ++indIt) //t.m_leafSet[k] = *indIt; } } return toReturn._retn(); } // TAO_IDL - Generated from // be/be_visitor_operation/operation_is.cpp:57 char * CipresIDL_api1_ReadNexus_i::getUIXml ( ) ACE_THROW_SPEC (( CORBA::SystemException )) { return CORBA::string_dup(""); // no configuration } // TAO_IDL - Generated from // be/be_visitor_operation/operation_is.cpp:57 CORBA::Boolean CipresIDL_api1_ReadNexus_i::execute ( const char * command, CORBA::String_out display ) ACE_THROW_SPEC (( CORBA::SystemException )) { return true; //no configuration. Maybe we should return false? } // TAO_IDL - Generated from // be/be_visitor_operation/operation_is.cpp:57 /* void CipresIDL_api1_ReadNexus_i::remove ( ) ACE_THROW_SPEC (( CORBA::SystemException )) { // Add your implementation here //mth, need to talk to tl about what to do here. } */