📄 taxon1.cpp
字号:
TGCMap::const_iterator gci( m_gcStorage.find( gc_id ) ); if( gci != m_gcStorage.end() ) { gc_name_out.assign( gci->second ); return true; } else { SetLastError( "ERROR: GetGCName(): Unknown genetic code" ); return false; }}//---------------------------------------------// Get taxonomic rank name by rank id///boolCTaxon1::GetRankName(short rank_id, string& rank_name_out ){ SetLastError( NULL ); const char* pchName = m_plCache->GetRankName( rank_id ); if( pchName ) { rank_name_out.assign( pchName ); return true; } else { SetLastError( "ERROR: GetRankName(): Rank not found" ); return false; }}//---------------------------------------------// Get taxonomic division name by division id///boolCTaxon1::GetDivisionName(short div_id, string& div_name_out ){ SetLastError( NULL ); const char* pchName = m_plCache->GetDivisionName( div_id ); if( pchName ) { div_name_out.assign( pchName ); return true; } else { SetLastError( "ERROR: GetDivisionName(): Division not found" ); return false; }}//---------------------------------------------// Get taxonomic name class (scientific name, common name, etc.) by id///boolCTaxon1::GetNameClass(short nameclass_id, string& name_class_out ){ SetLastError( NULL ); const char* pchName = m_plCache->GetNameClassName( nameclass_id ); if( pchName ) { name_class_out.assign( pchName ); return true; } else { SetLastError( "ERROR: GetNameClass(): Name class not found" ); return false; }}intCTaxon1::Join(int taxid1, int taxid2){ int tax_id = 0; CTaxon1Node *pNode1, *pNode2; SetLastError(NULL); if( m_plCache->LookupAndAdd( taxid1, &pNode1 ) && pNode1 && m_plCache->LookupAndAdd( taxid2, &pNode2 ) && pNode2 ) { CRef< ITreeIterator > pIt( GetTreeIterator() ); pIt->GoNode( pNode1 ); pIt->GoAncestor( pNode2 ); tax_id = pIt->GetNode()->GetTaxId(); } return tax_id;}intCTaxon1::GetAllNames(int tax_id, TNameList& lNames, bool unique){ int count(0); SetLastError(NULL); CTaxon1_req req; CTaxon1_resp resp; req.SetGetorgnames( tax_id ); if( SendRequest( req, resp ) ) { if( resp.IsGetorgnames() ) { // Correct response, return object const list< CRef< CTaxon1_name > >& lNm = resp.GetGetorgnames(); // Fill in the list for( list< CRef< CTaxon1_name > >::const_iterator i = lNm.begin(); i != lNm.end(); ++i, ++count ) if( !unique ) { lNames.push_back( (*i)->GetOname() ); } else { lNames.push_back( ((*i)->IsSetUname() && !(*i)->GetUname().empty()) ? (*i)->GetUname() : (*i)->GetOname() ); } } else { // Internal: wrong respond type SetLastError( "Response type is not Getorgnames" ); return 0; } } return count;}/*--------------------------------------------- * Find organism name in the string (for PDB mostly) * Returns: nimber of tax_ids found * NOTE: * 1. orgname is substring of search_str which matches organism name * (return parameter). * 2. Ids consists of tax_ids. Caller is responsible to free this memory */ // int getTaxId4Str(const char* search_str, char** orgname, intPtr *Ids_out);boolCTaxon1::IsAlive(void){ SetLastError(NULL); if( m_pServer ) { if( !m_pOut || !m_pOut->InGoodState() ) SetLastError( "Output stream is not in good state" ); else if( !m_pIn || !m_pIn->InGoodState() ) SetLastError( "Input stream is not in good state" ); else return true; } else { SetLastError( "Not connected to Taxonomy service" ); } return false;}boolCTaxon1::GetTaxId4GI(int gi, int& tax_id_out ){ SetLastError(NULL); CTaxon1_req req; CTaxon1_resp resp; req.SetId4gi( gi ); if( SendRequest( req, resp ) ) { if( resp.IsId4gi() ) { // Correct response, return object tax_id_out = resp.GetId4gi(); return true; } else { // Internal: wrong respond type SetLastError( "Response type is not Id4gi" ); } } return false;}boolCTaxon1::GetBlastName(int tax_id, string& blast_name_out ){ CTaxon1Node* pNode = 0; SetLastError(NULL); if( m_plCache->LookupAndAdd( tax_id, &pNode ) && pNode ) { while( !pNode->IsRoot() ) { if( !pNode->GetBlastName().empty() ) { blast_name_out.assign( pNode->GetBlastName() ); return true; } pNode = pNode->GetParent(); } blast_name_out.erase(); return true; } return false;}boolCTaxon1::SendRequest( CTaxon1_req& req, CTaxon1_resp& resp ){ unsigned nIterCount( 0 ); unsigned fail_flags( 0 ); if( !m_pServer ) { SetLastError( "Service is not initialized" ); return false; } SetLastError( NULL ); do { bool bNeedReconnect( false ); try { *m_pOut << req; m_pOut->Flush(); if( m_pOut->InGoodState() ) { try { *m_pIn >> resp; if( m_pIn->InGoodState() ) { if( resp.IsError() ) { // Process error here string err; resp.GetError().GetErrorText( err ); SetLastError( err.c_str() ); return false; } else return true; } } catch( exception& e ) { SetLastError( e.what() ); } fail_flags = m_pIn->GetFailFlags(); bNeedReconnect = (fail_flags & ( CObjectIStream::eEOF |CObjectIStream::eReadError |CObjectIStream::eOverflow |CObjectIStream::eFail |CObjectIStream::eNotOpen ) ? true : false); } else { m_pOut->ThrowError((CObjectOStream::EFailFlags) m_pOut->GetFailFlags(), "Output stream is in bad state"); } } catch( exception& e ) { SetLastError( e.what() ); fail_flags = m_pOut->GetFailFlags(); bNeedReconnect = (fail_flags & ( CObjectOStream::eEOF |CObjectOStream::eWriteError |CObjectOStream::eOverflow |CObjectOStream::eFail |CObjectOStream::eNotOpen ) ? true : false); } if( !bNeedReconnect ) break; // Reconnect the service if( nIterCount < m_nReconnectAttempts ) { delete m_pOut; delete m_pIn; delete m_pServer; m_pOut = NULL; m_pIn = NULL; m_pServer = NULL; try { auto_ptr<CObjectOStream> pOut; auto_ptr<CObjectIStream> pIn; auto_ptr<CConn_ServiceStream> pServer( new CConn_ServiceStream(m_pchService, fSERV_Any, 0, 0, m_timeout) ); pOut.reset( CObjectOStream::Open(m_eDataFormat, *pServer) ); pIn.reset( CObjectIStream::Open(m_eDataFormat, *pServer) ); m_pServer = pServer.release(); m_pIn = pIn.release(); m_pOut = pOut.release(); } catch( exception& e ) { SetLastError( e.what() ); } } else { // No more attempts left break; } } while( nIterCount++ < m_nReconnectAttempts ); return false;}voidCTaxon1::SetLastError( const char* pchErr ){ if( pchErr ) m_sLastError.assign( pchErr ); else m_sLastError.erase();}static void s_StoreResidueTaxid( CTreeIterator* pIt, CTaxon1::TTaxIdList& lTo ){ CTaxon1Node* pNode = static_cast<CTaxon1Node*>( pIt->GetNode() ); if( !pNode->IsJoinTerminal() ) { lTo.push_back( pNode->GetTaxId() ); } if( pIt->GoChild() ) { do { s_StoreResidueTaxid( pIt, lTo ); } while( pIt->GoSibling() ); pIt->GoParent(); }}//--------------------------------------------------// This function constructs minimal common tree from the gived tax id// set (ids_in) treated as tree's leaves. It then returns a residue of // this tree node set and the given tax id set in ids_out.// Returns: false if some error// true if Ok///typedef vector<CTaxon1Node*> TTaxNodeLineage;boolCTaxon1::GetPopsetJoin( const TTaxIdList& ids_in, TTaxIdList& ids_out ){ SetLastError(NULL); if( ids_in.size() > 0 ) { map< int, CTaxon1Node* > nodeMap; CTaxon1Node *pParent = 0, *pNode = 0, *pNewParent = 0; CTreeCont tPartTree; // Partial tree CTreeIterator* pIt = tPartTree.GetIterator(); TTaxNodeLineage vLin; // Build the partial tree bool bHasSiblings; vLin.reserve( 256 ); for( TTaxIdList::const_iterator ci = ids_in.begin(); ci != ids_in.end(); ++ci ) { map< int, CTaxon1Node* >::iterator nmi = nodeMap.find( *ci ); if( nmi == nodeMap.end() ) { if( m_plCache->LookupAndAdd( *ci, &pNode ) ) { if( !tPartTree.GetRoot() ) { pNewParent = new CTaxon1Node ( *static_cast<const CTaxon1Node*> (m_plCache->GetTree().GetRoot()) ); tPartTree.SetRoot( pNewParent ); nodeMap.insert( map< int,CTaxon1Node* >::value_type (pNewParent->GetTaxId(), pNewParent) ); } if( pNode ) { vLin.clear(); pParent = pNode->GetParent(); pNode = new CTaxon1Node( *pNode ); pNode->SetJoinTerminal(); vLin.push_back( pNode ); while( pParent && ((nmi=nodeMap.find(pParent->GetTaxId())) == nodeMap.end()) ) { pNode = new CTaxon1Node( *pParent ); vLin.push_back( pNode ); pParent = pParent->GetParent(); } if( !pParent ) { pIt->GoRoot(); } else { pIt->GoNode( nmi->second ); } for( TTaxNodeLineage::reverse_iterator i = vLin.rbegin(); i != vLin.rend(); ++i ) { pNode = *i; nodeMap.insert( map< int,CTaxon1Node* >::value_type ( pNode->GetTaxId(), pNode ) ); pIt->AddChild( pNode ); pIt->GoNode( pNode ); } } } else { // Error while adding - ignore invalid tax_ids continue; //return false; } } else { // Node is already here nmi->second->SetJoinTerminal(); } } // Partial tree is build, make a residue pIt->GoRoot(); bHasSiblings = true; if( pIt->GoChild() ) { while( !pIt->GoSibling() ) { pNode = static_cast<CTaxon1Node*>( pIt->GetNode() ); if( pNode->IsJoinTerminal() || !pIt->GoChild() ) { bHasSiblings = false; break; } } if( bHasSiblings ) { pIt->GoParent(); } s_StoreResidueTaxid( pIt, ids_out ); } } return true;}//-----------------------------------// Tree-related functionsboolCTaxon1::LoadSubtreeEx( int tax_id, int levels, const ITaxon1Node** ppNode ){ CTaxon1Node* pNode = 0; SetLastError(NULL); if( ppNode ) { *ppNode = pNode; } if( m_plCache->LookupAndAdd( tax_id, &pNode ) && pNode ) { if( ppNode ) { *ppNode = pNode; } if( pNode->IsSubtreeLoaded() ) { return true; } if( levels == 0 ) { return true; } CTaxon1_req req; CTaxon1_resp resp; if( levels < 0 ) { tax_id = -tax_id; } req.SetTaxachildren( tax_id ); if( SendRequest( req, resp ) ) { if( resp.IsTaxachildren() ) { // Correct response, return object list< CRef< CTaxon1_name > >& lNm = resp.SetTaxachildren(); // Fill in the list CTreeIterator* pIt = m_plCache->GetTree().GetIterator(); pIt->GoNode( pNode ); for( list< CRef< CTaxon1_name > >::const_iterator i = lNm.begin(); i != lNm.end(); ++i ) { if( (*i)->GetCde() == 0 ) { // Change parent node if( m_plCache->LookupAndAdd( (*i)->GetTaxid(), &pNode ) && pNode ) { pIt->GoNode( pNode ); } else { // Invalid parent specified SetLastError( ("Invalid parent taxid " + NStr::IntToString((*i)->GetTaxid()) ).c_str() ); return false; } } else { // Add node to the partial tree if( !m_plCache->Lookup((*i)->GetTaxid(), &pNode) ) { pNode = new CTaxon1Node(*i); m_plCache->SetIndexEntry(pNode->GetTaxId(), pNode); pIt->AddChild( pNode ); } } pNode->SetSubtreeLoaded( pNode->IsSubtreeLoaded() || (levels < 0) ); } return true; } else { // Internal: wrong respond type SetLastError( "Response type is not Taxachildren" ); return false; } } } return false;}CRef< ITreeIterator >CTaxon1::GetTreeIterator( CTaxon1::EIteratorMode mode ){ CRef< ITreeIterator > pIt; CTreeConstIterator* pIter = m_plCache->GetTree().GetConstIterator(); switch( mode ) { default: case eIteratorMode_FullTree: pIt.Reset( new CFullTreeConstIterator( pIter ) ); break; case eIteratorMode_LeavesBranches: pIt.Reset( new CTreeLeavesBranchesIterator( pIter ) ); break; case eIteratorMode_Best: pIt.Reset( new CTreeBestIterator( pIter ) ); break; case eIteratorMode_Blast: pIt.Reset( new CTreeBlastIterator( pIter ) ); break; } SetLastError(NULL); return pIt;}CRef< ITreeIterator >CTaxon1::GetTreeIterator( int tax_id, CTaxon1::EIteratorMode mode ){ CRef< ITreeIterator > pIt; CTaxon1Node* pData = 0; SetLastError(NULL); if( m_plCache->LookupAndAdd( tax_id, &pData ) ) { pIt = GetTreeIterator( mode ); if( !pIt->GoNode( pData ) ) { SetLastError( "Iterator in this mode cannot point to the node with" " this tax id" ); pIt.Reset( NULL ); } } return pIt;}boolCTaxon1::GetNodeProperty( int tax_id, const string& prop_name, string& prop_val ){ SetLastError(NULL); CTaxon1_req req; CTaxon1_resp resp; CRef<CTaxon1_info> pProp( new CTaxon1_info() ); CDiagAutoPrefix( "Taxon1::GetNodeProperty" ); if( !prop_name.empty() ) { pProp->SetIval1( tax_id ); pProp->SetIval2( -1 ); // Get string property by name pProp->SetSval( prop_name );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -