-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup_refs.py
286 lines (227 loc) · 8.91 KB
/
setup_refs.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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/env python
"""
"""
import sys, argparse, os, glob, subprocess
import ConfigParser
from scipy.cluster.hierarchy import linkage
from scipy.cluster.hierarchy import fcluster
from scipy.spatial.distance import squareform
__version__= '0.1'
__date__= '12Feb2014'
__author__ = 'ulf.schaefer@phe.gov.uk'
# ---------------------------------------------------------------
def parse_args():
sDescription = 'version %s, date %s, author %s' %(__version__, __date__, __author__)
oParser = argparse.ArgumentParser(description=sDescription)
oParser.add_argument('-f', '--folder',
metavar='FILE',
dest='folder',
required=True,
help='REQUIRED: Folder that contains a set of fasta files with one reference genome each.')
oParser.add_argument('-n', '--name',
metavar='FILE',
dest='name',
required=True,
help='REQUIRED: Unique name for this set of references. Case insensitive. [e.g. salmonella]')
oParser.add_argument('-c', '--config',
metavar='FILE',
dest='config',
required=True,
help='REQUIRED: Configuration file. Usually config/config.cnf.')
oArgs = oParser.parse_args()
return oArgs, oParser
# ---------------------------------------------------------------
def main():
oArgs, oParser = parse_args()
oConf = ConfigParser.RawConfigParser()
sFolder = os.path.abspath(oArgs.folder)
if os.path.exists(sFolder) == False:
stdout_write("ERROR: %s folder not found\nexiting ..." % sFolder)
sys.exit()
oArgs.name = oArgs.name.lower()
sConfFile = oArgs.config
oConf.read(sConfFile)
try:
oConf.add_section('group_folders')
except ConfigParser.DuplicateSectionError:
pass
oConf.set('group_folders', oArgs.name, sFolder)
aFileEndings = ["fa", "fna", "fas", "fasta"]
aFileList = []
for sFileEnd in aFileEndings:
aFileList += glob.glob(os.path.join(sFolder, "*." + sFileEnd))
aFileList += glob.glob(os.path.join(sFolder, "*." + sFileEnd + ".gz"))
stdout_write("%i sequence files found." % len(aFileList))
gZipped = False
aKmerLists = []
for sFile in aFileList:
k = sFile.rfind(".")
sKmerList = sFile[:k] + "_kmers.txt"
if os.path.exists(sKmerList) != True:
gZipped = False
if sFile.endswith(".gz"):
gZipped = True
p = subprocess.Popen("gunzip " + sFile, shell=True, stdin=None, stdout=None, stderr=None, close_fds=True)
stdout_write("gunzipping %s ..." % sFile)
p.wait()
sFile = sFile[:-3]
sCmd = "bin/kmer_refset_process %i %s > %s" % (18, sFile, sKmerList)
p = subprocess.Popen(sCmd, shell=True, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
stdout_write("Calculating kmer list for %s ..." % sFile)
p.wait()
aKmerLists.append(sKmerList)
if gZipped == True:
p = subprocess.Popen("gzip " + sFile, shell=True, stdin=None, stdout=None, stderr=None, close_fds=True)
stdout_write("zipping %s ..." % sFile)
p.wait()
else:
stdout_write("%s - kmer list found. skipping creation." % sKmerList)
aKmerLists.append(sKmerList)
stdout_write("%i kmer lists made or found." % len(aKmerLists))
sSimMatFile = "config%s%s_simmat.tsv" % (os.sep, oArgs.name)
if os.path.exists(sSimMatFile) == True:
(c, r) = get_mat_dims(sSimMatFile)
if c == len(aKmerLists) + 1 and c==r:
stdout_write("found similarity matrix for reference group %s, skipping creation ..." % oArgs.name)
else:
stdout_write("creating similarity matrix for reference group %s ..." % oArgs.name)
create_sim_matrix(aKmerLists, sSimMatFile)
else:
stdout_write("creating similarity matrix for reference group %s ..." % oArgs.name)
create_sim_matrix(aKmerLists, sSimMatFile)
stdout_write("Created sim mat file: %s" % sSimMatFile)
try:
oConf.add_section('matrices')
except ConfigParser.DuplicateSectionError:
pass
oConf.set('matrices', oArgs.name, sSimMatFile)
d3Cl = cluster_group(sSimMatFile, 3)
d3Cen = get_centroids(d3Cl, sSimMatFile)
try:
oConf.add_section('%s_centroids' % oArgs.name)
except ConfigParser.DuplicateSectionError:
pass
for k in d3Cen.keys():
oConf.set('%s_centroids' % oArgs.name, str(k), d3Cen[k])
if len(aFileList) > 40:
d40Cl = cluster_group(sSimMatFile, 40)
d40Cen = get_centroids(d40Cl, sSimMatFile)
try:
oConf.add_section('%s_refset' % oArgs.name)
except ConfigParser.DuplicateSectionError:
pass
for k in d40Cen.keys():
oConf.set('%s_refset' % oArgs.name, str(k), d40Cen[k])
else:
aGenomes = [os.path.basename(s).replace("_kmers.txt", "") for s in aKmerLists]
try:
oConf.add_section('%s_refset' % oArgs.name)
except ConfigParser.DuplicateSectionError:
pass
for i in range(1, len(aGenomes)+1):
oConf.set('%s_refset' % oArgs.name, str(i),aGenomes[i-1])
fCnf = open(sConfFile, 'w')
oConf.write(fCnf)
fCnf.close()
return
# end of main ---------------------------------------------------
def stdout_write(s):
sys.stdout.write("%s\n" % s)
sys.stdout.flush()
return
# ---------------------------------------------------------------
def create_sim_matrix(aFiles, sSimMat):
fOut = open(sSimMat, 'w')
iNofFiles = len(aFiles)
aNames = [os.path.basename(x).replace("_kmers.txt", "") for x in aFiles]
for s in aNames:
fOut.write("\t%s" % (s))
fOut.write("\n")
d = {}
for i in range(0, iNofFiles):
fOut.write(aNames[i])
for j in range(0, iNofFiles):
if i==j:
fOut.write("\t1.0")
else:
flSim = 0.0
k1 = str(i) + "-" + str(j)
k2 = str(j) + "-" + str(i)
try:
fOut.write("\t%f" % (d[k2]))
except KeyError:
sCmd = "bin/kmer_jaccard_index %s %s" % (aFiles[i], aFiles[j])
p = subprocess.Popen(sCmd, shell=True, stdin=None, stdout=subprocess.PIPE, close_fds=True)
# get output from subprocess
sOutLine = p.stdout.readline()
flSim = float(sOutLine.split("\t")[0])
fOut.write("\t%f" % (flSim))
d[k1] = flSim
fOut.write("\n")
fOut.close()
return
# ---------------------------------------------------------------
def get_mat_dims(sFile):
f = open(sFile, 'r')
a = []
for s in f:
a.append(len(s.split("\t")))
f.close()
c = -1
if a.count(a[0]) == len(a):
c = a[0]
r = len(a)
return (c, r)
# ---------------------------------------------------------------
def cluster_group(sFile, iClusters):
f = open(sFile, 'r')
a = []
aLines = []
for s in f:
aLines.append(s.strip())
f.close()
aNames = aLines[0].strip().split("\t")
for sL in aLines[1:]:
a.append([1.0-float(x) for x in sL.split("\t")[1:]])
linkage_matrix = linkage(squareform(a), method='average')
cluster = fcluster(linkage_matrix, float(iClusters), criterion='maxclust')
dClus = {}
for x in range(0, len(cluster)):
clusnum = cluster[x]
try:
dClus[clusnum].append(aNames[x])
except KeyError:
dClus[clusnum] = [aNames[x]]
return dClus
# ---------------------------------------------------------------
def get_centroids(dClusters, sSimMatFile):
fMat = open(sSimMatFile, 'r')
aaSimMatrix = []
for sLine in fMat:
aaSimMatrix.append([x.strip() for x in sLine.split("\t")])
fMat.close()
aNames = aaSimMatrix[0][1:]
dSimMatrix = {}
for aLine in aaSimMatrix[1:]:
sName = aLine[0]
dSimMatrix[sName] = {}
for i in range(1, len(aLine)):
dSimMatrix[sName][aNames[i-1]] = float(aLine[i])
dCentroids = {}
for k in dClusters.keys():
if len(dClusters[k]) <= 2:
dCentroids[k] = dClusters[k][0]
else:
dAvgDists = {}
for x in dClusters[k]:
flAvgDist = 0.0
for y in dClusters[k]:
flAvgDist += dSimMatrix[x][y]
flAvgDist /= float(len(dClusters[k]))
dAvgDists[flAvgDist] = x
flMin = max(dAvgDists.keys())
dCentroids[k] = dAvgDists[flMin]
return dCentroids
# ---------------------------------------------------------------
if __name__=='__main__':
main()