-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipeline
More file actions
276 lines (260 loc) · 11.6 KB
/
pipeline
File metadata and controls
276 lines (260 loc) · 11.6 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 15 10:35:37 2020
@author: Suha Naser-Khdour
"""
from Bio import SeqIO
from Bio import AlignIO
from Bio.Nexus import Nexus
import math
import itertools as ite
import numpy as np
import os
import glob
import pandas as pd
import shutil
from random import sample
import linecache
from itertools import permutations
import re
def extract_subalignment(aln_in,aln_out):
nex = Nexus.Nexus()
nex.read(aln_in)
aln = AlignIO.read(open(aln_in), "nexus")
for k,v in nex.taxsets.items():
sequences = []
for rec in aln:
if rec.name in v:
sequences.append(rec)
SeqIO.write(sequences, os.path.splitext(aln_in)[0]+k+'.nex', "nexus")
def random_lines(n,partition):
'''
Randomly return n% of the partitions
'''
nLines = 0
i = 0
lines = []
with open(partition, 'r') as inf:
for line in inf:
if 'CHARSET' in line:
nLines += 1
lines.append(i)
i += 1
idxs = sample(lines, int(nLines/n))
rLines = [linecache.getline(partition, j) for j in idxs]
with open(partition, 'w') as inf:
inf.writelines('#nexus\n')
inf.writelines('begin SETS;\n')
inf.writelines('[loci]\n')
for i in rLines:
inf.writelines(i)
inf.writelines('end;\n')
return
def nCr(n,r):
f = math.factorial
return f(n) // f(r) // f(n-r)
def Qfinder(iqtree):
condition = False
Q = []
with open(iqtree, 'r') as inf:
for line in inf:
if 'State frequencies: (estimated with maximum likelihood)' in line:
condition = False
if condition and line.strip():
Q.append(line.strip())
if 'Substitution parameters (lower-diagonal) and state frequencies in PAML format (can be used as input for IQ-TREE):' in line:
condition = True
if 'Log-likelihood of the tree:' in line:
end = line.index('(')
logL = float(line[28:end])
if 'Number of free parameters' in line:
FP = float(line[59:])
Q = [i.split(' ') for i in Q]
Q = [i for sublist in Q for i in sublist]
Q = list(map(float, Q))
clade = os.path.splitext(os.path.basename(iqtree))[0]
df = pd.DataFrame([clade, logL, FP]).T
df.columns = ['group','logL','FP']
return [clade,Q], df
def Qdist(dictionary):
no = nCr(len(dictionary),2)+1
p=np.empty([no,3],dtype='U22')
p[0] = np.array(['matrix1','matrix2','dist'])
j = 1
for q in ite.combinations(list(range(len(dictionary))),2): #iterating over all taxa for sites
for i in range(0,len(dictionary[q[0]][1])):
d = np.linalg.norm(dictionary[q[0]][1][i]-dictionary[q[1]][1][i])
p[j] = np.array([dictionary[q[0]][0],dictionary[q[1]][0],d])
j += 1
df =pd.DataFrame(p[1:], columns=p[0])
df['dist'] = pd.to_numeric(df['dist'])
df = df[df.dist == df.dist.min()]
return df.values.tolist()
def merge_two_partitions(partition, merged_partition, Q1, Q2):
with open(merged_partition, 'w') as outf:
outf.writelines('#nexus\n')
outf.writelines("begin SETS;\n")
outf.writelines("[loci]\n")
for i in [Q1,Q2]:
j = os.path.join(rootDir, 'alignment' + i+'.nex')
with open(partition, 'r') as inf, open(merged_partition, 'a') as outf:
for line in inf:
if 'CHARSET' in line:
index = line.index("=") - 1
outf.writelines(line[0:index]+i+' = '+j+':'+line[index+2:-1]+'\n')
with open(merged_partition, 'a') as outf:
outf.writelines('end;\n')
return
def add_partition(merged_partition, original_partition, Q):
with open(merged_partition, 'w') as outf:
outf.writelines('#nexus\n')
outf.writelines("begin SETS;\n")
outf.writelines("[loci]\n")
for i in Q:
j = os.path.join(rootDir, 'alignment' + i+'.nex')
with open(original_partition, 'r') as inf, open(merged_partition, 'a') as outf:
for line in inf:
if 'CHARSET' in line:
index = line.index("=") - 1
outf.writelines(line[0:index]+i+' = '+j+':'+line[index+2:-1]+'\n')
with open(merged_partition, 'a') as outf:
outf.writelines('end;\n')
return
def merge_all_partitions(partition, merged_partition):
with open(merged_partition, 'w') as outf:
outf.writelines('#nexus\n')
outf.writelines("begin SETS;\n")
outf.writelines("[loci]\n")
for j in glob.glob(os.path.join(rootDir, 'alignment*.nex')):
i = os.path.splitext(os.path.basename(j))[0].split('alignment')[1]
with open(partition, 'r') as inf, open(merged_partition, 'a') as outf:
for line in inf:
if 'CHARSET' in line:
index = line.index("=") - 1
outf.writelines(line[0:index]+i+' = '+j+':'+line[index+2:-1]+'\n')
with open(merged_partition, 'a') as outf:
outf.writelines('end;\n')
return
if __name__ == '__main__':
rootDir = '/data' #The root directory where all files will be saved
'''
Estimate a separate Q matrix for each group
'''
for aln in glob.glob(os.path.join(rootDir, 'alignment*.nex')):
partition = os.path.join(rootDir, 'partition.nex')
prefix = os.path.join(rootDir, 'partition' + os.path.splitext(os.path.basename(aln))[0].split('alignment')[1])
runIqtree = " ".join(['iqtree -s', aln, '-spp', partition, '--prefix', prefix, '-nt 35'])
os.system(runIqtree)
for tree in glob.glob(os.path.join(rootDir, 'partition*.treefile')):
aln = os.path.join(rootDir, 'alignment' + os.path.splitext(os.path.basename(tree))[0].split('partition')[1] + '.nex')
bestModel = os.path.splitext(tree)[0]+'.best_model.nex'
prefix = os.path.join(rootDir, 'Q' + os.path.splitext(os.path.basename(tree))[0].split('partition')[1])
runIqtree = " ".join(['iqtree -s', aln, '-spp', bestModel, '-te', tree, '--model-joint GTR20+FO --min-freq 0.001 --prefix', prefix, '-nparam 10 -optfromgiven -nt 35'])
os.system(runIqtree)
results = os.path.join(rootDir, 'results.log')
partition = os.path.join(rootDir,'partition.nex')
'''
Maximum number of matrices: Q1, Q2, ... , Qn
'''
Qdict=[]
df = pd.DataFrame()
for q in glob.glob(os.path.join(rootDir, 'Q*.iqtree')):
Qdict.append(Qfinder(q)[0])
df = df.append(Qfinder(q)[1])
logL = 2*df.FP.sum()-2*df.logL.sum()
with open(results,'w') as outf:
outf.writelines('iteration 1: A separate matrix for each clade\n')
for k in Qdict:
outf.write('{}\n'.format(k))
outf.writelines('AIC for {} separate models is = {}\n'.format(len(df),logL))
outf.writelines('\n')
AIC = logL
AIC_dir = rootDir
Better_AIC_dir = rootDir
'''
All combinations of two matrices: n-1 matrices in total
'''
itr = 2
while True:
AIC = math.inf
for s in ite.combinations(glob.glob(os.path.join(Better_AIC_dir, 'Q*.iqtree')),2):
df = pd.DataFrame()
Qdict=[]
Q1 = os.path.splitext(os.path.basename(s[0]))[0].split('Q')[1]
Q2 = os.path.splitext(os.path.basename(s[1]))[0].split('Q')[1]
if Q1 != Q2:
newDir = os.path.join(rootDir,'Q'+Q1+Q2)
l = [item for sublist in Q1+Q2 for item in sublist]
duplicates = ['Q' + s for s in [''.join(p) for p in permutations(l)]]
for d in [os.path.join(rootDir,d) for d in duplicates]:
if os.path.exists(d):
condition = False
newDir = d
break
if condition:
os.mkdir(newDir)
merged_partition = os.path.join(newDir,'partition.nex')
add_partition(partition, merged_partition, l)
runIqtree = " ".join(['iqtree -spp', merged_partition, '-nt 35'])
os.system(runIqtree)
bestModel = os.path.join(newDir, 'partition.nex.best_model.nex')
tree = os.path.join(newDir, 'partition.nex.treefile')
prefix = os.path.join(newDir, 'Q'+Q1+Q2)
runIqtree = " ".join(['iqtree -spp', bestModel, '-te', tree, '--model-joint GTR20+FO --min-freq 0.001 --prefix', prefix, '-nparam 10 -optfromgiven -nt 35'])
os.system(runIqtree)
for q in glob.glob(os.path.join(Better_AIC_dir, 'Q*.iqtree')):
if not os.path.splitext(os.path.basename(q))[0].split('Q')[1] in [Q1,Q2]:
shutil.copy2(q,newDir)
for q in glob.glob(os.path.join(newDir, 'Q*.iqtree')):
Qdict.append(Qfinder(q)[0])
df = df.append(Qfinder(q)[1])
logL = 2*df.FP.sum()-2*df.logL.sum()
if os.path.basename(Better_AIC_dir).split('Q')[1] in [Q1,Q2]:
with open(results,'a') as outf:
outf.writelines('iteration {}: One matrix for clades {} and {} and a separate matrix for any other clade\n'.format(itr,Q1,Q2))
for k in Qdict:
outf.write('{}\n'.format(k))
outf.writelines('AIC for {} separate models is = {}\n'.format(len(df),logL))
outf.writelines('\n')
itr += 1
else:
l = os.path.basename(Better_AIC_dir).split('Q')[1]
if not os.path.basename(Better_AIC_dir).split('Q')[1] in newDir:
for q in glob.glob(os.path.join(newDir, 'Q*.iqtree')):
if os.path.splitext(os.path.basename(q))[0].split('Q')[1] in l:
os.rename(q,os.path.join(os.path.dirname(q),re.sub('[Q]', 'c', os.path.basename(q))))
shutil.copy2(os.path.join(Better_AIC_dir,os.path.basename(Better_AIC_dir)+'.iqtree'),newDir)
for q in glob.glob(os.path.join(newDir, 'Q*.iqtree')):
Qdict.append(Qfinder(q)[0])
df = df.append(Qfinder(q)[1])
logL = 2*df.FP.sum()-2*df.logL.sum()
if logL < AIC:
AIC = logL
AIC_dir = newDir
if AIC_dir != Better_AIC_dir:
Better_AIC_dir = AIC_dir
else:
break
if len(df) <= 2:
break
'''
One matrix for all clades: Q0
'''
merged_partition = os.path.join(rootDir, 'partition0.nex')
merge_all_partitions(partition, merged_partition)
runIqtree = " ".join(['iqtree -spp', merged_partition, '-nt 35'])
os.system(runIqtree)
bestModel = os.path.join(rootDir,'partition0.nex.best_model.nex')
tree = os.path.join(rootDir,'partition0.nex.treefile')
prefix = os.path.join(rootDir,'Q0')
runIqtree = " ".join(['iqtree -spp', bestModel, '-te', tree, '--model-joint GTR20+FO --min-freq 0.001 --prefix', prefix, '-nparam 10 -optfromgiven -nt 35'])
os.system(runIqtree)
Qdict = Qfinder(os.path.join(rootDir,'q0.iqtree'))[0]
df = Qfinder(os.path.join(rootDir,'q0.iqtree'))[1]
logL = 2*df.FP.sum()-2*df.logL.sum()
with open(results,'a') as outf:
outf.writelines('iteration {}: One matrix for all clades\n'.format(itr))
for k in Qdict:
outf.write('{}\n'.format(k))
outf.writelines('AIC score for 1 model is = {}\n'.format(logL))
outf.writelines('\n')