-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmakeHyde.py
More file actions
executable file
·253 lines (218 loc) · 6.66 KB
/
makeHyde.py
File metadata and controls
executable file
·253 lines (218 loc) · 6.66 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/usr/bin/python
import re
import sys
import os
import getopt
import operator
import collections
import copy
def main():
params = parseArgs()
if params.phylip:
#Get sequences as dict of lists
seqs = readPhylip(params.phylip)
else:
print("No input provided.")
sys.exit(1)
pop_assign = dict()
#parse popmap file for dictionary of sample assignments
if params.popmap:
print("Parsing popmap file...")
pop_assign = parsePopmap(params.popmap)
else:
print("ERROR: Popmap file must be provided.")
sys.exit(1)
if seqs and pop_assign:
#Remove samples from pop_assign that do not have data
pop_assign = cleanPopmap(pop_assign, seqs.keys())
#Make dict of dicts that splits by population, only retaining pops/samples from the popmap.
#Get unique pop names using one of the worst python lines ever written
pops = dict()
for k in set(pop_assign.values()):
pops[k] = dict()
#Remove pops listed as excluded
if params.exclude:
print("Excluding populations:", ", ".join(params.exclude))
for exc in params.exclude:
if exc in pops:
del pops[exc]
if params.include:
print("Only keeping populations:", ", ".join(params.include))
for pop in list(pops):
if pop not in params.include:
del pops[pop]
#make sure we didn't throw out all populations...
if len(list(pops)) < 1:
print("Oops! No populations remaining. Check that popmap sample names match those in your data file, or that selections using --include or --exclude are correct! :)")
sys.exit(1)
alen = getSeqLen(seqs)
inum = 0
for assigned in pop_assign:
if pop_assign[assigned] in pops:
pops[pop_assign[assigned]][assigned] = seqs[assigned]
inum+=1
seqs.clear()
#Make 2D list to remove columns failing the globalN filter
bad_columns = list() #list of column numbers to delete
#For each pop dict, make 2D list to remove columns failing popN filter
print("Found",alen,"nucleotide columns in the dataset!")
columns = [[]for i in range(alen)] #2D array of global data
for pop, data in pops.items():
for sample, sequence in data.items():
for i, nuc in enumerate(sequence):
columns[i].append(nuc)
#Write new ordered output and phylip
print("Writing outputs...")
phy = params.out + ".phy"
omap = params.out + ".map"
pfh = open(phy, "w")
mfh = open(omap, "w")
header = str(inum) + "\t" + str(alen) + "\n"
pfh.write(header)
for pop in sorted(pops):
for ind, data in pops[pop].items():
indline = str(ind) + "\t" + "".join(data) + "\n"
pfh.write(indline)
mapline = str(ind) + "\t" + str(pop) + "\n"
mfh.write(mapline)
pfh.close()
mfh.close()
print("Done!\n")
#Goes through a dict of sequences and get the alignment length
def getSeqLen(aln):
length = None
for key in aln:
if not length:
length = len(aln[key])
else:
if length != len(aln[key]):
print("getSeqLen: Alignment contains sequences of multiple lengths.")
return(length)
#function reads a tab-delimited popmap file and return dictionary of assignments
def parsePopmap(popmap):
ret = dict()
if os.path.exists(popmap):
with open(popmap, 'r') as fh:
try:
contig = ""
seq = ""
for line in fh:
line = line.strip()
if not line:
continue
else:
stuff = line.split()
ret[stuff[0]] = stuff[1]
return(ret)
except IOError:
print("Could not read file ",pairs)
sys.exit(1)
finally:
fh.close()
else:
raise FileNotFoundError("File %s not found!"%popmap)
#Function to remove samples from a popmap dict, given a list of valid samples (e.g. those to retain)
def cleanPopmap(popmap, names):
ret = copy.deepcopy(popmap)
to_remove = list()
for ind in popmap:
if ind not in names:
to_remove.append(ind)
for rem in sorted(to_remove, reverse=True):
del ret[rem]
return(ret)
#Function to read a phylip file. Returns dict (key=sample) of lists (sequences divided by site)
def readPhylip(phy):
if os.path.exists(phy):
with open(phy, 'r') as fh:
try:
num=0
ret = dict()
for line in fh:
line = line.strip()
if not line:
continue
num += 1
if num == 1:
continue
arr = line.split()
ret[arr[0]] = list(arr[1])
return(ret)
except IOError:
print("Could not read file ",fas)
sys.exit(1)
finally:
fh.close()
else:
raise FileNotFoundError("File %s not found!"%fas)
#Object to parse command-line arguments
class parseArgs():
def __init__(self):
#Define options
try:
options, remainder = getopt.getopt(sys.argv[1:], 'p:i:ho:X:I:', \
["input=","phylip=","phy=","out=","popmap=","maxN=",
"popN=","exclude=","include="])
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.phylip=None
self.popmap=None
self.out="out"
self.exclude = list()
self.include = list()
#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 ('i', 'phylip', 'input','phy'):
self.phylip = arg
elif opt in ('p', 'popmap'):
self.popmap = arg
elif opt in ('h', 'help'):
pass
elif opt in ('o','out'):
self.out = arg
elif opt in ('X', 'exclude'):
self.exclude = arg.split(",")
elif opt in ('I','include'):
self.include = arg.split(",")
else:
assert False, "Unhandled option %r"%opt
#Check manditory options are set
if not self.phylip :
self.display_help("Error: Missing required alignment file (--input)")
if not self.popmap:
self.display_help("Error: Missing required popmap file (-p, --popmap)")
if self.include and self.exclude:
self.display_help("Don't use both --include and --exclude.")
def display_help(self, message=None):
if message is not None:
print ("\n",message)
print ("\nmakeHyde.py\n")
print ("Contact:Tyler K. Chafin, University of Arkansas,tkchafin@uark.edu")
print ("\nUsage: ", sys.argv[0], "-i /path/to/phylip -i /path/to/popmap\n")
print ("Description: Making inputs for HyDe and filtering populations for inclusion/exclusion")
print("""
Arguments:
INPUT FILES [REQUIRED]
-i,--input : Input file as PHYLIP
-p,--popmap : Tab-delimited population map
PARAMETERS [OPTIONAL]
-o,--out : Output file name <default = out.nex>
-X,--exclude: List of pops to exclude (format: -x "Pop1,Pop2,Sample4...")
-I,--include: List of pops to include (removing all others)
-h,--help : Displays help menu
""")
sys.exit()
#Call main function
if __name__ == '__main__':
main()