Get trees in big files without reading the lot into memory.
P4 Tree objects are a little obese, and large tree files will flood your RAM. This class addresses that problem by reading in the file as text, and then creating Tree objects only on demand. If the trees are not saved then there should not be such a problem with memory.
Instantiate with a file name. It can handle mcmc output from p4, Beast, and MrBayes, and phylip format.
This can handle tree descriptions with line breaks. However, it does not know about nexus-style ‘commenting-out’ [ie with square brackets, like this]. Also, it is not particularly robust with regard to being case-insensitive. So while the usual way of reading in tree files via the read() command will handle nexus tree lines that start with tReE or trEe, TreeFileLite cannot, due to lazy programming. So unless your file conforms to the expectations of TreeFileLite, it would be best to use read().
To decrease bloat, it is not loaded by default when you start up p4. To access it, you need to do:
from p4.treefilelite import TreeFileLite
The only method is getTree(), although you can get the tLines if you want.
Eg to just get a few Tree objects:
from p4.treefilelite import TreeFileLite tfl = TreeFileLite('mcmc_trees_0.nex') for i in [23, 45, 67]: t = tfl.getTree(i) t.draw()
or, to write some trees, as text (not as Tree objects), to a new file:
from p4.treefilelite import TreeFileLite tfl = TreeFileLite('myBigFile.nex') f = open('mySmallerFile.nex', 'w') f.write(tfl.header) for i in range(24000,25000): f.write('tree %s\n' % tfl.tLines[i]) f.write('end;') f.close()