#!/usr/bin/python from PIPRes.util.client_import import * from PIPRes.cipres_types import * if __name__ == '__main__': _LOG = cipresGetLogger('pipres.service_impl.client.read_nexus') try: flagIndex = sys.argv.index('-filename') except ValueError: sys.exit('Specify a NEXUS file to read with the arguments:\n\t-filename ') if flagIndex == len(sys.argv) - 1: sys.exit('Expecting a filename after the -filename argument') fileName = os.path.abspath(sys.argv[flagIndex + 1]) sys.argv.pop(flagIndex + 1) sys.argv.pop(flagIndex) registryWrapper = cipresClientInit(sys.argv) nexusReader = registryWrapper.getServiceOrExit(CipresIDL_api1.ReadNexus) try: import time startTime = time.time() blocksRead = nexusReader.readNexusFile(fileName, -1) endTime = time.time() assert(len(blocksRead) >= 3) #should report taxa, trees, characters (in that order) at least nTaxaBlocks = blocksRead[CipresIDL_api1.ReadNexus.NEXUS_TAXA_BLOCK_INDEX] if nTaxaBlocks > 0: print nTaxaBlocks, 'taxa block(s)' for i in range(nTaxaBlocks): taxa = nexusReader.getTaxa(i) print 'Taxa from taxa block', i+1,':\n',taxa nTreesBlocks = blocksRead[CipresIDL_api1.ReadNexus.NEXUS_TREES_BLOCK_INDEX] if nTreesBlocks : print nTreesBlocks, 'trees block(s)' for i in range(nTreesBlocks): trees = nexusReader.getTrees(i) print 'Tree from Trees block', i+1,':\n' for i in trees: ct = CipresTree(i) print 'Tree', ct.m_name, 'with leaves:', ct.m_leafSet print str(ct) nCharsBlocks = blocksRead[CipresIDL_api1.ReadNexus.NEXUS_CHARACTERS_BLOCK_INDEX] if nCharsBlocks: print nCharsBlocks, 'characters block(s)' for i in range(nCharsBlocks): characters = nexusReader.getCharacters(i) print 'Matrix from characters block', i+1,':\n', characters print 'The NEXUS reader required', endTime - startTime , 'seconds to read', fileName finally: nexusReader.remove() nexusReader = None