/
mig2lamarc
executable file
·333 lines (297 loc) · 12.7 KB
/
mig2lamarc
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/usr/bin/env python
#
# converts a migrate file into ima
# mig2ima treestring infile outfile
#
from __future__ import print_function
from __future__ import division
#from __future__ import unicode_literals
import sys
import migread as mr
def help():
print( "Syntax: mig2other <-ima | -mist | -bpp | -lamarc > migrateinputfile convertedfile <guidetree>")
print( "Default is conversion for ima or using the -ima option")
print( " -mist converts the input for the program MIST (Chung and Hey 2017)")
print( " -bpp converts the input for the program BPP (Yang and Rannala)")
print( " -lamarc converts the input for the program LAMARC (Kuhner)")
sys.exit()
def chunk(in_string,num_chunks):
chunk_size = len(in_string)//num_chunks
if len(in_string) % num_chunks: chunk_size += 1
iterator = iter(in_string)
for _ in range(num_chunks):
accumulator = list()
for _ in range(chunk_size):
try: accumulator.append(next(iterator))
except StopIteration: break
yield ''.join(accumulator)
def fprintf(outfile, format, *args):
outfile.write(format % args)
def convert2ima(outfile, popnum, missing, tree, loci, populations):
fprintf(outfile,"Simulated data generated with migtree and migdata [IMA style]\n")
fprintf(outfile,"# assumed that migrate infile is --modern\n")
fprintf(outfile,"# the population tree may needs to be fixed\n")
fprintf(outfile,"%li\n",popnum-missing)
for pop in range(popnum-missing):
fprintf(outfile,"pop%li ",pop)
fprintf(outfile,"\n")
fprintf(outfile,"%s\n",tree)
fprintf(outfile,"%li\n",loci)
for locus in range(loci):
fprintf(outfile,"Locus%li ",locus)
for pop in range(popnum-missing):
fprintf(outfile,"%li ",len(populations[pop]))
seq = populations[pop][0][1].translate(None, '!@#$0123456789\n ')
s = list(chunk(seq,loci))
fprintf(outfile,"%li H 1\n",len(s[locus]))
for pop in range(popnum-missing):
for ind in range(len(populations[pop])):
fprintf (outfile, "%-10.10s", populations[pop][ind][0])
seq = populations[pop][ind][1].translate(None, '!@#$0123456789\n ')
s = list(chunk(seq,loci))
fprintf (outfile,"%s\n",s[locus])
fprintf (outfile,"\n")
def convert2mist(outfile, popnum, missing, loci, populations):
#fprintf(outfile,"# MIST data generated from a migrate input file using mig2ima\n")
#fprintf(outfile,"# assumed that migrate infile is --modern\n")
#fprintf(outfile,"# the population tree may needs to be fixed\n")
total = 0
for pop in range(popnum-missing):
total += len(populations[pop])
fprintf(outfile,"%li %li\n", total, loci)
for locus in range(loci):
seq = populations[pop][0][1].translate(None, '!@#$0123456789\n ')
s = list(chunk(seq,loci))
fprintf(outfile,"%li H\n",len(s[locus]))
for pop in range(popnum-missing):
for ind in range(len(populations[pop])):
fprintf (outfile, "%li %li ",pop, ind)
seq = populations[pop][ind][1].translate(None, '!@#$0123456789\n ')
s = list(chunk(seq,loci))
fprintf (outfile,"%s\n",s[locus])
fprintf (outfile,"\n")
def convert2bpp(outfile, imap, popnum, missing, loci, populations):
# over all loci
for locus in range(loci):
# over all populations first to find number of individuals
numind = 0
for pop in range(popnum-missing):
numind += len(populations[pop])
fprintf(outfile,"%li ",numind)
seq = populations[0][0][1].translate(None, '!@#$0123456789\n ')
s = list(chunk(seq,loci))
fprintf(outfile,"%li\n\n",len(s[locus]))
#over all populations to print individuals for locus
for pop in range(popnum-missing):
# over all individuals in population pop
for ind in range(len(populations[pop])):
fprintf (outfile, "%s^%li ", populations[pop][ind][0].strip(), pop)
seq = populations[pop][ind][1].translate(None, '!@#$0123456789\n ')
s = list(chunk(seq,loci))
fprintf (outfile,"%s\n",s[locus])
fprintf (outfile,"\n")
for pop in range(popnum-missing):
fprintf(imap,"%li %li\n",pop,pop)
def bpp_cntl(cntl,seqfile,imapfile,outfile,mcmcfile):
a = """
seed = 123
seqfile = {0}
Imapfile = {1}
outfile = {2}
mcmcfile = {3}
speciesdelimitation = 0 * fixed species tree
* speciesdelimitation = 1 0 2 * speciesdelimitation algorithm0(e)
* speciesdelimitation = 1 1 2 1 * speciesdelimitation algorithm1(a m)
speciestree = 0 * species tree fixed
* speciestree = 1 0.4 0.2 0.9 * speciestree pSlider ExpandRatio ShrinkRatio
* speciesmodelprior = 1 * 0: uniform LH; 1:uniform rooted trees; 2: uniformSLH; 3: uniformSRooted
species&tree = 2 A B
20 20
(A, B);
usedata = 1 * 0: no data (prior); 1:seq like
nloci = 10 * 1000 * number of data sets in seqfile
cleandata = 0 * remove sites with ambiguity data (1:yes, 0:no)?
thetaprior = 3 0.4 # invgamma(a, b) for theta
tauprior = 3 0.2 # invgamma(a, b) for root tau & Dirichlet(a) for other tau's
* locusrate = 0 2.0 # (0: No variation, 1: estimate, 2: from file) & a_Dirichlet (if 1)
* heredity = 0 4 4 # (0: No variation, 1: estimate, 2: from file) & a_gamma b_gamma (if 1)
* sequenceerror = 0 0 0 0 0 : 0.05 1 # sequencing errors: gamma(a, b) prior
finetune = 1: 1 0.002 0.01 0.01 0.02 0.005 1.0 # finetune for GBtj, GBspr, theta, tau, mix, locusrate, seqerr
print = 1 0 0 0 * MCMC samples, locusrate, heredityscalars Genetrees
burnin = 4000
sampfreq = 2
nsample = 200000
""".format(seqfile,imapfile,outfile,mcmcfile)
fprintf(cntl,"%s",a)
def convert2lamarc(outfile, inputfile, popnum, missing, tree, loci, populations):
fprintf(outfile,'<?xml version="1.0" ?>\n<!--\ndata converted from migrate data format to lamarc\n')
fprintf(outfile," assumed that migrate infile is {double hyphen}modern\n")
fprintf(outfile," works for two populations sampled\n-->\n")
fprintf(outfile,"%s\n",lamarc_xml_header())
fprintf(outfile,"<data>")
popnames = map(str,range(1,popnum+1))
for locus in range(loci):
fprintf(outfile,'<region name="from %s %li">',inputfile,locus)
fprintf(outfile,'<spacing>')
fprintf(outfile,'<block name="segment 1 of %s %li" />\n',inputfile,locus)
fprintf(outfile,'</spacing>')
for pop in range(popnum-missing):
fprintf(outfile,'<population name="%s">\n',popnames[pop])
for ind in range(len(populations[pop])):
seq = populations[pop][ind][1].translate(None, '!@#$0123456789\n ')
s = list(chunk(seq,loci))
indname = populations[pop][ind][0]
fprintf(outfile,'<individual name="%s">\n',indname.strip())
fprintf(outfile,'<sample name="%s_0">\n',indname.strip())
fprintf(outfile,'<datablock type="DNA"> %s </datablock>\n',s[locus])
fprintf(outfile,'</sample>\n </individual>\n')
fprintf(outfile,'</population>\n')
fprintf(outfile,'</region>\n')
fprintf(outfile,'</data>\n')
fprintf(outfile,'</lamarc>\n')
fprintf (outfile,"\n")
def lamarc_xml_header():
return '''
<lamarc version="2.1.10">
<chains>
<replicates>1</replicates>
<bayesian-analysis>Yes</bayesian-analysis>
<heating>
<adaptive>false</adaptive>
<temperatures> 1</temperatures>
<swap-interval>10</swap-interval>
</heating>
<strategy>
<resimulating>0.3125</resimulating>
<tree-size>0.0625</tree-size>
<haplotyping>0</haplotyping>
<trait-arranger>0</trait-arranger>
<epoch-size>0.3125</epoch-size>
<bayesian>0.3125</bayesian>
</strategy>
<initial>
<number>10</number>
<samples>500</samples>
<discard>1000</discard>
<interval>100</interval>
</initial>
<final>
<number>2</number>
<samples>100000</samples>
<discard>1000</discard>
<interval>100</interval>
</final>
</chains>
<format>
<convert-output-to-eliminate-zero> Yes </convert-output-to-eliminate-zero>
<!-- The tag below documents the seed used for this run. -->
<!-- It is ignored if you use this file as lamarc input -->
<seed-from-system-clock>1516940797</seed-from-system-clock>
<verbosity>normal</verbosity>
<progress-reports>none</progress-reports>
<results-file>outfile.txt</results-file>
<use-in-summary>false</use-in-summary>
<in-summary-file>insumfile.xml</in-summary-file>
<use-out-summary>false</use-out-summary>
<out-summary-file>outsumfile.xml</out-summary-file>
<use-curvefiles>true</use-curvefiles>
<curvefile-prefix>curvefile</curvefile-prefix>
<use-reclocfile>false</use-reclocfile>
<reclocfile-prefix>reclocfile</reclocfile-prefix>
<use-tracefile>true</use-tracefile>
<tracefile-prefix>tracefile</tracefile-prefix>
<use-newicktreefile>false</use-newicktreefile>
<newicktreefile-prefix>newick</newicktreefile-prefix>
<out-xml-file>menusettings_infile.xml</out-xml-file>
<xml-report-file>report.xml</xml-report-file>
<profile-prefix>profile</profile-prefix>
</format>
<forces>
<divergence-migration>
<start-values> 0 50.000000 0 50.000000 0 0 0 0 0 </start-values>
<method> USER USER USER USER USER USER USER USER USER </method>
<max-events> 10000 </max-events>
<profiles> None None None None None None None None None </profiles>
<constraints> Invalid Unconstrained Invalid Unconstrained Invalid Invalid Invalid Invalid Invalid </constraints>
<prior type="linear">
<paramindex> default </paramindex>
<lower> 0.0 </lower>
<upper> 100.0 </upper>
</prior>
</divergence-migration>
<divergence>
<prior type="linear">
<paramindex> default </paramindex>
<lower> 0.0 </lower>
<upper> 0.2 </upper>
</prior>
<method> USER </method>
<start-values> 0.002000 </start-values>
<population-tree>
<epoch-boundary>
<new-populations> 1 2 </new-populations>
<ancestor> Parent_1 </ancestor>
</epoch-boundary>
</population-tree>
</divergence>
</forces>
'''
if __name__ == '__main__':
convertima = True #default convert to ima
convertbpp = False
convertmist = False
convertlamarc = False
if "-h" in sys.argv or "--help" in sys.argv:
help()
if "-ima" in sys.argv: # convert to ima input file
convertima=True
ii = sys.argv.index('-ima')
sys.argv.pop(ii)
if "-mist" in sys.argv: # convert to mist input file
convertmist=True
convertima=False
ii = sys.argv.index('-mist')
sys.argv.pop(ii)
if "-bpp" in sys.argv: # convert to mist input file
convertbpp=True
convertima=False
ii = sys.argv.index('-bpp')
sys.argv.pop(ii)
seqfile = sys.argv[2]
if "-lamarc" in sys.argv: # convert to ima input file
convertlamarc=True
convertima=False
ii = sys.argv.index('-lamarc')
sys.argv.pop(ii)
print(sys.argv)
inputfile = sys.argv[1]
outfile = open(sys.argv[2],'w')
if convertbpp:
imap = open(sys.argv[2]+".imap","w")
imapfile=sys.argv[2]+".imap"
outfilet = sys.argv[2]+".out"
mcmcfile = sys.argv[2]+".mcmc"
cntlfile = sys.argv[2]+".cntl"
cntl = open(cntlfile,'w')
data = mr.reader(inputfile)
if len(sys.argv) > 3:
tree = sys.argv[3]
else:
tree = "(0,1):2"
populations, title = mr.split_migrate(data)
popnum, loci = mr.get_header(data)
# how many empty populations
numinds = [len(populations[pop]) for pop in range(popnum)]
#print numinds
missing = len(filter(lambda x: x==0,numinds))
#print missing
#conversion to ima
if convertima:
convert2ima(outfile, popnum, missing, tree, loci, populations)
elif convertmist: #convert to mist
convert2mist(outfile, popnum, missing, loci, populations)
elif convertbpp: #convert to bpp
convert2bpp(outfile, imap, popnum, missing, loci, populations)
bpp_cntl(cntl, seqfile,imapfile, outfilet,mcmcfile)
else:
convert2lamarc(outfile, inputfile, popnum, missing, tree, loci, populations)