How to pass variable filename to python ete?

361 views Asked by At

I am trying load newick string from a file using following code:

filename = templist[1].lower().replace(" ","") + ".nwk"
tt = Tree(filename, format=1)

but getting following error:

 tt = Tree(filename, format=1) # Loads a tree structure from a newick string. The returned variable tt is the root node for the tree.
  File "/python2.7/dist-packages/ete2-2.2.1072-py2.7.egg/ete2/coretype/tree.py", line 225, in __init__
    read_newick(newick, root_node = self, format=format)
  File "/python2.7/dist-packages/ete2-2.2.1072-py2.7.egg/ete2/parser/newick.py", line 237, in read_newick
    'Unexisting tree file or Malformed newick tree structure.'
ete2.parser.newick.NewickError: Unexisting tree file or Malformed newick tree structure.

I have verified the file format, and it is Ok. I think I am passing the variable in wrong way, can you please guide me to pass filename as variable.

When I pass the filename as string without using variable it is working fine, but I need to use variable value to load tree.

1

There are 1 answers

4
Anand S Kumar On BEST ANSWER

Are you sure the format=1 is correct?

According to documentation -

for named internal nodes we will use format 1

Are you sure your newick tree has named internal nodes? If not, try without passing any value to the format argument.


Also, please make sure that the filename is either absolute path to the file, or if its relative path , for example a.nwk , then make sure the file exists in the directory from which you are running the python script.