I am working with the biopthon Phylo package. I have an outside data source (API) from which I pull down trees as newick strings. I am attempting to construct biopython phylo trees from these strings using the
My goal is to do so without having to write the newick strings to a file (since parse takes a file handle). I am attempting to use
io.StringIO as a replacement, and simply passing an instance of that to
Phylo.parse(). It is not working, and returning an empty tree list. Does anyone know if this is due to biopython not supporting StringIO instances as file handles? Is there some otherway to create biopython trees from a string directly?
Here is my code
trees_file = io.StringIO() # this is the 'file handle' for analysis in analyses: # get the string, I know this works uni_tree = unicode(analysis.taxonomy_as_newick()) trees_file.write(uni_tree) trees = list(Phylo.parse(trees_file, 'newick'))
At this point trees is an empty list. If I print the contents of
trees_file to the console, I get the expected value (a bunch of newick trees). Any thoughts?