import sys # Parse the command line. if (len(sys.argv) != 2): print "USAGE: read-tree.py " sys.exit(1) treeFilename = sys.argv[1] ############################################################################ # Split up a tree string into two subtree strings. Assumes that the # string starts and ends with parentheses. def splitTreeString(treeString): # Remove the leading and trailing parentheses. treeString = treeString[1:-1] # Search for the middle comma. numParens = 0 for index in range(0, len(treeString)): thisChar = treeString[index] if (thisChar == "("): numParens += 1 elif (thisChar == ")"): numParens -= 1 elif ((thisChar == ",") and (numParens == 0)): leftString = treeString[:index] rightString = treeString[index+1:] return((leftString, rightString)) # If we reach here, then the string is misformatted. print "Can't parse string %s." % treeString ############################################################################ def readTree (treeString): # Remove leading and trailing whitespace. treeString = treeString.strip() # If the string doesn't start with a parenthesis, this is the base case. if (treeString[0] != "("): return(treeString) else: # If there is a leading parenthesis, split and recurse. (leftString, rightString) = splitTreeString(treeString) return([readTree(leftString), readTree(rightString)]) ############################################################################ def countLeaves (myTree): if (isinstance(myTree, str)): return 1 else: return(countLeaves(myTree[0]) + countLeaves(myTree[1])) ############################################################################ # MAIN PROCEDURE ############################################################################ # Get the tree as a string. treeFile = open(treeFilename, "r") treeString = treeFile.readline() treeFile.close() # Initialize an empty tree. myTree = readTree(treeString) # Count the leaves. print "Read %d species from %s." % (countLeaves(myTree), treeFilename)