-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtreeExpansion.py
executable file
·153 lines (129 loc) · 3.52 KB
/
treeExpansion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/python
import re
import sys
import os
import getopt
def main():
params = parseArgs()
popsList = dict()
#parse popmap file for dictionary of sample assignments
if params.popmap:
#print("Parsing popmap file...")
popsList = parsePopmap_alt(params.popmap)
if not params.tree:
print("ERROR: No tree provided.")
sys.exit(1)
newtree = params.tree
for pop in popsList:
replace = ", ".join(popsList[pop])
newtree = newtree.replace(str(pop), str(replace))
print(newtree)
else:
print("ERROR: Popmap file must be provided.")
sys.exit(1)
#function reads a tab-delimited popmap file and return dictionary of assignments
#function returns dict of pops, each pointint to list of taxa
def parsePopmap_alt(popmap):
ret = dict()
with open(popmap, 'r') as fh:
try:
contig = ""
seq = ""
for line in fh:
line = line.strip()
if not line:
continue
else:
stuff = line.split()
if len(stuff)!= 2:
print("Uh oh! Record missing a field: ",stuff)
continue
if stuff[1] not in ret:
l = list()
l.append(stuff[0])
ret[stuff[1]] = l
else:
ret[stuff[1]].append(stuff[0])
return(ret)
except IOError as e:
print("Could not read file %s: %s"%(popmap,e))
sys.exit(1)
except Exception as e:
print("Unexpected error reading file %s: %s"%(popmap,e))
sys.exit(1)
finally:
fh.close()
#function returns first readable line from a file
#good for getting headers etc
def firstLine(f):
with open(f, 'r') as fh:
try:
for line in fh:
line = line.strip()
if not line:
continue
else:
return(line) #returns first real line
except IOError as e:
print("Could not read file %s: %s"%(f,e))
sys.exit(1)
except Exception as e:
print("Unexpected error reading file %s: %s"%(f,e))
sys.exit(1)
finally:
fh.close()
#Object to parse command-line arguments
class parseArgs():
def __init__(self):
#Define options
try:
options, remainder = getopt.getopt(sys.argv[1:], 't:s:p:h', \
["tree=","popmap="])
except getopt.GetoptError as err:
print(err)
self.display_help("\nExiting because getopt returned non-zero exit status.")
#Default values for params
#Input params
self.tree=None
self.popmap=None
#First pass to see if help menu was called
for o, a in options:
if o in ("-h", "-help", "--help"):
self.display_help("Exiting because help menu was called.")
#Second pass to set all args.
for opt, arg_raw in options:
arg = arg_raw.replace(" ","")
arg = arg.strip()
opt = opt.replace("-","")
#print(opt,arg)
if opt in ('t','tree'):
self.tree = firstLine(arg)
elif opt in ('p', 'popmap'):
self.popmap = arg
elif opt in ('h', 'help'):
pass
else:
assert False, "Unhandled option %r"%opt
#Check manditory options are set
if not self.tree:
self.display_help("Error: Missing required tree (--tree or --stree)")
if not self.popmap:
self.display_help("Error: Missing required popmap file (-p, --popmap)")
def display_help(self, message=None):
if message is not None:
print ("\n",message)
print ("\ntreeExpansion.py\n")
print ("Contact:Tyler K. Chafin, University of Arkansas,tkchafin@uark.edu")
print ("Description: Expands Newick tree of clades to include all taxa in a popmap file")
print("""
Arguments:
-p,--popmap : Tab-delimited population map
-t,--tree : Newick tree in a file
or
-s,--stree : Newick tree given as a string
-h,--help : Displays help menu
""")
sys.exit()
#Call main function
if __name__ == '__main__':
main()