-
Notifications
You must be signed in to change notification settings - Fork 0
/
thomtools.py
578 lines (543 loc) · 20.8 KB
/
thomtools.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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#!/usr/bin/python
#
# thomtools.py by Thom Nelson
#
# Useful functions for performing coalescent simulations, generating sequence data,
# and performing high-throughput analyses of sequence data.
#
import random
import sys
import os
import re
###
### DEFINE FUNCTIONS TO RUN SIMULATIONS
###
def run_ms(nchroms, segsites, mu):
# THIS FUNCTION IS SIMPLY A WRAPPER FOR RUNNING MS IN A PYTHON SCRIPT.
# MS MUST BE IN THE PATH FOR THIS ONE TO WORK.
ms = "ms {nsams} 1 -s {segs} -T -p 3 > ms.tmp"
cmnd = ms.format(nsams = nchroms, segs = segsites)
os.system(cmnd)
ms_in = open("ms.tmp", "r")
cmdline = ms_in.readline().strip('\n')
rseed = ms_in.readline().strip('\n')
ms_in.readline().strip('\n'); ms_in.readline().strip('\n')
newick = ms_in.readline().strip('\n')
nsegs = ms_in.readline().strip('\n')
positions = ms_in.readline().strip('\n').split(' '); positions = positions[1:-1]
pos = []
for position in positions:
if "." in position:
pos.append(position)
haps = []
for hap in ms_in:
hap = hap.strip('\n')
hap = list(hap)
haps.append(hap)
# Remove cutsite mutations if called for
segsites = int(segsites)
if mu == False:
newpos = []
for i in range(len(pos)):
test = int(float(pos[i]) * 1000)
if test < 496 or test > 504:
newpos.append(pos[i])
else:
segsites -= 1
for j in range(len(haps)):
del haps[j][i]
pos = newpos
nsegs = "segsites: %s"%(segsites)
for hap in range(len(haps)):
haps[hap] = ''.join(haps[hap])
ms = {'command':cmdline , 'random seed':rseed , 'tree':newick , \
'segregating sites':str(nsegs) , 'positions':pos, 'haplotypes':haps}
os.system('rm ms.tmp')
return ms
def ms2halflotype(ms):
# THIS FUNCTION TAKES HAPLOTYPES GENERATED WITH MS AND BREAKS THEM INTO TWO SETS OF ALLELES.
# THIS IS TO SIMULATED HAPLOTYPE DATA GENERATED WITH RAD, WHERE TWO ADJACENT HAPLOTYPES
# ARE SEQUENCED AND MUST BE PHASED.
# THE OUTPUT IS A 'HALFOTYPES' CLASS, WHICH IS A DUMB NAME BUT CONTAINS ALL THE INFO NEEDED
# TO MATCH ALLELES TO INDIVIDUALS AND GENERATE INPUT FOR PHASE.
unique = []; halflos1 = []; halflos2 = []; indivs_bi = []; indivs_m = []; nalleles = 0
hapLen = len(ms['haplotypes'][1])
sitesBefore = 0
for pos in ms['positions']:
pos = float(pos)
if pos < 0.5:
sitesBefore += 1
cutsite = sitesBefore
multi1 = {}
translate1 = {}
multi1n = 0
multi2 = {}
translate2 = {}
multi2n = 0
# populate lists of alleles present and dictionaries to relate biallelic halflotypes
# to their corresponding value in the 'multiallelic' genotype
for allele in ms['haplotypes']:
halflos = [allele[:cutsite],allele[cutsite:]]
halflos1.append(halflos[0]); halflos2.append(halflos[1])
if halflos[0] not in multi1:
multi1n += 1
multi1[halflos[0]] = multi1n
translate1[multi1n] = halflos[0]
if halflos[1] not in multi2:
multi2n += 1
multi2[halflos[1]] = multi2n
translate2[multi2n] = halflos[1]
if allele not in unique:
unique.append(allele)
nalleles += 1
# populate indivs_* lists, which will be used to print out genotype inputs to PHASE
i = 0
while i < nalleles:
geno1a_bi = halflos1[i] ; geno1b_bi = halflos1[i+1]
geno2a_bi = halflos2[i] ; geno2b_bi = halflos2[i+1]
geno1a_m = multi1[geno1a_bi] ; geno1b_m = multi1[geno1b_bi]
geno2a_m = multi2[geno2a_bi] ; geno2b_m = multi2[geno2b_bi]
indivs_bi.append([[geno1a_bi,geno2a_bi],[geno1b_bi, geno2b_bi]])
indivs_m.append([[geno1a_m,geno2a_m], [geno1b_m, geno2b_m]])
i += 2
# rescale positions so that RAD locus is 1 kb
positions = ms['positions']
for position in positions:
position = float(position) * 1000
# define 'unique' with true haplotypes and unique 'multiallelic' translations
unique = {'true':unique, 'translate1':translate1, 'translate2':translate2}
biallelic = {'nindiv':nsam, 'nloci':str(hapLen),'positions':positions,'genotypes':indivs_bi}
multiallelic = {'nindiv':nsam,'nalleles1':multi1n, 'nalleles2':multi2n , 'nloci':'2', 'positions':['0','1'],'genotypes':indivs_m}
halflotypes = {'unique':unique,'biallelic':biallelic,'multiallelic':multiallelic}
return halflotypes
def generatePHASEinp(halflotypes, outfile_biallelic, outfile_multiallelic):
# TAKE A 'HALFLOTYPES' OBJECT AND GENERATE A PHASE INPUT FILE
bi = halflotypes['biallelic']
multi = halflotypes['multiallelic']
# generate 'biallelic' output file
bi_out = open(outfile_biallelic, 'w')
indivs = bi['nindiv']+'\n' ; bi_out.write(indivs)
nloci = bi['nloci']+'\n' ; bi_out.write(nloci)
positions = bi['positions']
for i in range(len(positions)):
if float(positions[i]) > 0 and float(positions[i]) < 1:
positions[i] = str(float(positions[i]) * 1000)
else:
positions[i] = str(positions[i])
positions = 'P '+' '.join(positions)+'\n' ; bi_out.write(positions)
segline = 'S'*int(bi['nloci'])+'\n' ; bi_out.write(segline)
nindivs = 0
for indiv in bi['genotypes']:
nindivs += 1
indivlabel = 'Sample_'+str(nindivs)+'\n'
line1 = indiv[0][0]+indiv[0][1]+'\n'
line2 = indiv[1][0]+indiv[1][1]+'\n'
bi_out.write(indivlabel)
bi_out.write(line1)
bi_out.write(line2)
bi_out.close()
# generate 'multiallelic' output file
m_out = open(outfile_multiallelic, 'w')
indivs = multi['nindiv']+'\n' ; m_out.write(indivs)
nloci = multi['nloci']+'\n' ; m_out.write(nloci)
positions = 'P '+' '.join(multi['positions'])+'\n' ; m_out.write(positions)
segline = 'M'*2+'\n' ; m_out.write(segline)
nindivs = 0
for indiv in multi['genotypes']:
nindivs += 1
indivlabel = 'Sample_'+str(nindivs)+'\n'
line1 = str(indiv[0][0])+' '+str(indiv[0][1])+'\n'
line2 = str(indiv[1][0])+' '+str(indiv[1][1])+'\n'
m_out.write(indivlabel)
m_out.write(line1)
m_out.write(line2)
m_out.close()
def runPHASE(inp,out,d, l, executable='phase'):
# RUN PHASE, DUMP INTO A TEMPORARY FILE, AND COLLECT OUTPUT INTO A VARIABLE
cmnd = "{executable} -d{d} -l{l} -T {inp} {out} 1> PHASE.stdout 2> PHASE.stderr"
PHASE = cmnd.format(executable = executable, d = d, l = l, inp = inp, out = out)
os.system(PHASE)
result = open(out, 'r')
haplos = []
for hap in result:
hap = hap.strip('\n')
haplos.append(hap)
return haplos
###
### analyze PHASE output:
### 1) % correct best-guess haplos: compare to unique, ignore () or []
### 2) mean uncertainty: mean(len(haplo) - (segsites + (segsites - 1) / 2))
### 3) % correct @ indiv level: How many indivs have correctly called haplos?
### 4)
###
def sumPHASE(bi_out, m_out, halflotypes, cutsite):
# CREATE A SUMMARY OF PHASE RESULTS FROM SIMULATED DATA
resultS = [] ; resultM = []
phaseS = open(bi_out,'r') ; phaseM = open(m_out,'r')
# collect phaseS output
alleles = 0 ; indivs = 0
guessesS = [] # collect n. phasing guesses per individual
indiv = [] # tmp holder for incoming alleles before addition of tuple to result
for allele in phaseS:
allele = allele.strip(' \n').split(' ')
alleles += 1
indiv.append(allele)
if alleles % 2 == 0:
resultS.append(indiv)
guess = 0
for site in allele:
if '(' in site:
guess += 1
guessesS.append(guess)
indiv = []
indivs += 1
meanGuessesS = float(sum(guessesS)) / float(len(guessesS))
# collect phaseM output
alleles = 0 ; indivs = 0; guessesM = []
indiv = [] # tmp holder for incoming alleles before addition of tuple to result
for allele in phaseM:
allele = allele.strip(' \n').split(' ')
alleles += 1
indiv.append(allele)
if alleles % 2 == 0:
resultM.append(indiv)
guess = 0
for site in allele:
if '(' in site:
guess += 1
guessesM.append(guess)
indiv = []
indivs += 1
meanGuessesM = float(sum(guessesM)) / float(len(guessesM))
# Compare phase output to original haplotypes
trueS = halflotypes['biallelic'] ; trueM = halflotypes['multiallelic']
# Because there are only two phasing 'options' per individual,
# confirm that first estimated haplotype matches either true haplotype
# if not, phasing is incorrect.
# First, remove ambiguities '()' and '[]' from loci
trueHaplosS = trueS['genotypes']
for indiv in range(len(trueHaplosS)):
for halflo in range(len(trueHaplosS[indiv])):
full = ''.join(trueHaplosS[indiv][halflo])
trueHaplosS[indiv][halflo] = full
correctS = 0
uniqueS = []
for indiv in range(len(resultS)):
for haplo in range(len(resultS[indiv])):
for locus in range(len(resultS[indiv][haplo])):
rmAmbig = re.sub('[\(\)\[\]]','',resultS[indiv][haplo][locus])
resultS[indiv][haplo][locus] = rmAmbig
haplotype = ''.join(resultS[indiv][haplo])
if haplotype not in uniqueS:
uniqueS.append(haplotype)
resultS[indiv][haplo] = haplotype
estimated = resultS[indiv]
if estimated[0] in trueHaplosS[indiv]:
correctS += 1
trueHaplosM = trueM['genotypes']
for indiv in range(len(trueHaplosM)):
for haplo in range(len(trueHaplosM[indiv])):
full = trueHaplosM[indiv][haplo]
for half in range(len(full)):
full[half] = str(full[half])
trueHaplosM[indiv][haplo] = ''.join(full)
correctM = 0
uniqueM = []
for indiv in range(len(resultM)):
for haplo in range(len(resultM[indiv])):
full = ''.join(resultM[indiv][haplo])
rmAmbig = re.sub('[\(\)\[\]]','',full)
resultM[indiv][haplo] = rmAmbig
if rmAmbig not in uniqueM:
uniqueM.append(rmAmbig)
estimated = resultM[indiv]
if estimated[0] in trueHaplosM[indiv]:
correctM += 1
# collect results:
# chrom, coord, n. indiv, segsites, halflotype lengths, n. unique haplotypes, n unique halflotypes 1 and 2,
# n est. haplotypes S and M, mean guessed phasings S and M, n. correct indivs S and M
chrom = cutsite[0]
coord = int(cutsite[1]) + 2
lenHalf1 = 0
for i in halflotypes['biallelic']['positions']:
if float(i) < 500:
lenHalf1 += 1
lenHalf2 = int(segsites) - lenHalf1
n_unique = len(halflotypes['unique'])
n_unique1 = halflotypes['multiallelic']['nalleles1']
n_unique2 = halflotypes['multiallelic']['nalleles2']
results = [chrom, coord, int(nsam), int(segsites), lenHalf1, lenHalf2, n_unique, n_unique1, n_unique2, len(uniqueS), len(uniqueM),meanGuessesS,meanGuessesM,correctS,correctM]
return results
def SAMheaders(nsam, SAMheader, out):
for i in range(int(nsam)):
fname = out+"/Sample_"+str(i+1)+".sam"
cmnd = "cp {source} {dest}"
SAMcopy = cmnd.format(source = SAMheader, dest = fname)
os.system(SAMcopy)
def ms2sam(ms, coverage, nsam, fa, cutsite, out):
ms = ms
intpos = []
# convert position strings into integers between 0-1000 and check if any hit cutsite
for position in ms['positions']:
position = int(float(position) * 1000)
if position == 1000:
position = 999
intpos.append(position)
# get sequence from stickleback genome
chrom = cutsite[0] ; cutstart = int(cutsite[1]) ; cutend = int(cutsite[2]) ; cutlen = int(cutsite[3])
midcoord = cutstart + (cutlen / 2)
locusStart = midcoord - 500 ; locusEnd = midcoord + 499
# Run samtools faidx to grab reference sequence
cmnd = "samtools faidx {fasta} {chrom}:{locusStart}-{locusEnd} > tmp.fa"
getAncestral = cmnd.format(fasta = fa, chrom = chrom, locusStart = str(locusStart), locusEnd = str(locusEnd))
os.system(getAncestral)
# Mutate ancestral sequence
ancIN = open("tmp.fa", "r")
ancIN.readline()
ancestral = []
for line in ancIN:
line = line.strip('\n')
ancestral.append(line)
ancIN.close()
os.system("rm tmp.fa")
ancestral = ''.join(ancestral)
ancestral = list(ancestral)
ancSites = []
derSites = []
for i in intpos:
anc = ancestral[i]
ancSites.append(anc)
derived = re.sub(anc, "" ,"ATCG") ; derived = random.sample(list(derived),1)[0]
derSites.append(derived)
mutations = [ancSites, derSites]
# Generate mutated haplotypes and dump into SAM files
haploCount = 0
sampleCount = 0
sample = []
Samples = []
nsites = len(intpos)
for haplotype in ms['haplotypes']:
allele = ancestral
AncDir = list(haplotype)
for site in range(len(AncDir)):
AncDir[site] = int(AncDir[site])
for i in range(nsites):
position = intpos[i]
state = AncDir[i]
base = mutations[state][i]
allele[position] = base
allele = ''.join(allele)
haploCount += 1
if haploCount % 2 == 1:
sample.append(allele)
else:
sample.append(allele)
Samples.append(sample)
sample = []
sampleCount += 1
rad1 = allele[0:502]
rad2 = allele[498:]
# Fake SAM fields
image = 1000 ; x = 1000 ; y = 1000
flag = ['16','0'] ; mapq = '38' ; cigar = '502M' ; rnext = '*' ; pnext = '0' ; tlen = '0' ; qual = 'I'*502
for i in range(sampleCount):
SAM = open(out+"/Sample_"+str(i+1)+".sam", "a")
haplo1 = Samples[i][0] ; haplo2 = Samples[i][1]
radA1 = haplo1[0:502] ; radA2 = haplo2[0:502]
radB1 = haplo1[498:] ; radB2 = haplo2[498:]
halfsite1 = [radA1,radA2] ; halfsite2 = [radB1,radB2]
halfsites = [halfsite1,halfsite2]
coordA = locusStart
coordB = locusStart + 498
coords = [coordA,coordB]
# Generate fake SAM fields
for locus in range(2):
coord = str(coords[locus])
strand = flag[locus]
for allele in range(2):
sequence = halfsites[locus][allele]
for j in range(coverage):
flowcell = "1_%s_%s_%s_1"%(image,x,y)
entry = flowcell+"\t"+strand+"\t"+chrom+"\t"+coord+"\t"+mapq+"\t"+cigar+"\t"+rnext+"\t"+pnext+"\t"+tlen+"\t"+sequence+"\t"+qual+"\n"
SAM.write(entry)
image += 1 ; x += 1 ; y += 1
SAM.close()
def readPopMap(popmap):
# READ A POPMAP USED IN A STACKS ANALYSIS
map = {}
popOrder = []
popmapIN = open(popmap, 'r')
for line in popmapIN:
line = line.strip('\n').split('\t')
pop = line[1]
if pop in map:
map[pop].append(line[0])
else:
map[pop] = [line[0]]
popOrder.append(pop)
popmapIN.close()
return [map,popOrder]
def writeSubseq(file, fformat, subseqLen, taxa, outfile, popmap):
# TRIM AN ALIGNMENT FILE TO A SPECIFIED LENGTH, OUTPUTTING A PHYLIP FILE
popOrder = popmap[1]
popmap = popmap[0]
npops = len(popOrder)
sampleN = len(taxa)
aln = open(file, 'r')
entries = {}
if fformat == "phylip":
header = aln.readline() ; header = header.strip('\n').split('\t')
seqlen = header[-1]
n = header[-2]
for entry in aln:
entry = entry.strip('\n').split(' ')
name = entry[0]
seq = entry[-1]
subseq = seq[0:subseqLen]
entries[name] = subseq
elif fformat == "fasta":
l = 1
name = ""
for line in aln:
if l % 2 == 1:
name = line.strip('\n') ; name = name[1:]
else:
seq = line.strip('\n')
subseq = seq[0:subseqLen]
entries[name] = subseq
aln.close()
out = open(outfile, 'w')
out.write(" %s\t%s\n"%(sampleN, subseqLen))
for sample in taxa:
seq = entries[sample]
namelen = len(sample)
gaplen = 10 - namelen ; gaplen = " "*gaplen
out.write("%s%s%s\n"%(sample,gaplen,seq))
out.close()
def revComp(seq):
# REVERSE COMPLEMENT A DNA SEQUENCE.
# REPLACE YOUR 'T's WITH 'U's LATER, RNASEEKERS!
comp = {'A':'T','T':'A','C':'G','G':'C','-':'-','N':'N','/':'/'}
seq = list(seq)
seqlen = len(seq)
seqnew = []
for i in range(seqlen):
pos = seqlen - (i+1)
base = seq[pos]
seqnew.append(comp[base])
return "".join(seqnew)
### PARSE GFF
def parseGFF(gffpath, featuretype, scaffolds):
# this function reads a gff from the specified path
# and extracts records of features given in 'featuretype'
# 'featuretype' is a string and can be a regular expression
# defining multiple features (e.g. 'CDS|gene').
# featuretype = 'all' is translated to featuretype = '*'
# Result is a list of dictionaries
import re
import gzip
if len(scaffolds) == 0:
scaffolds = ['all']
if type(scaffolds) is str:
scaffolds = [scaffolds]
sys.stderr.write("Extracting genomic features from %s\n"%(gffpath))
is_compressed = False
if ".gz" in gffpath:
is_compressed = True
gff = gzip.open(gffpath,'rt')
else:
gff = open(gffpath,'r')
if featuretype == 'all':
featuretype = '*'
result = []
features = re.compile(featuretype)
nfeatures = 0
for feature in gff:
feature = feature.strip().split()
if "#" in feature[0]:
continue
if feature[0] not in scaffolds and 'all' not in scaffolds:
continue
featuretest = feature[2]
match = features.search(featuretest)
if not match:
continue
nfeatures += 1
if nfeatures % 1000 == 0:
sys.stderr.write(" %s features extracted...\r"%(nfeatures))
result.append({'scaffold':feature[0],'featuretype':feature[2],'start':feature[3],'stop':feature[4],'strand':feature[6],'phase':feature[7],'attributes':feature[8]})
sys.stderr.write(" %s features extracted from GFF. \n"%(nfeatures))
return(result)
def readVCFregion(vcfpath, scaffold, start, stop, SNPsOnly, biallelicOnly):
# this function reads through a tabix-indexed vcf.gz
# and provides a list of dictionaries
# SNPsOnly is logical: Only record SNPs?
# biallelicOnly is logical: only kep biallelic sites?
import subprocess
import re
results = []
if len(start) > 0:
region = scaffold+":"+str(start)+"-"+str(stop)
else:
region = scaffold
cmd_line = ("tabix",vcfpath,region)
entries = subprocess.Popen(cmd_line, stdout = subprocess.PIPE)
output = entries.communicate()[0].decode("utf-8")
for site in output.split('\n'):
site = site.split()
nfields = len(site)
if nfields == 0:
continue
scaffold = site[0]
position = site[1]
ID = site[2]
ref = site[3]
alt = site[4]
qual = site[5]
FILTER = site[6]
INFO = site[7]
FORMAT = site[8]
indel = re.compile('[ATGCN]{2,}')
multiallelic = re.compile(',')
if re.search(indel,ref) or re.search(indel,alt):
if SNPsOnly:
continue
if biallelicOnly and re.search(multiallelic,alt):
continue
GTs = []
genofields = range(9,nfields)
for i in genofields:
gt = site[i].split(":")[0]
GTs.append(gt)
results.append({'CHROM':scaffold,'POS':position,'ID':ID,'REF':ref,'ALT':alt,'QUAL':qual,'FILTER':FILTER,'INFO':INFO,'FORMAT':FORMAT,'GTs':GTs})
return(results)
def AFstats(GTs, sep, missingChar):
from math import pow, fsum
# this function takes a list of genotypes
# and calculates E[heterozygosity] as
# 1 - sum(E[homozygosities])
AFs = {}
nalleles = 0
Ohets = 0
for gt in GTs:
gt = gt.split(sep)
if gt[0] != gt[1]:
Ohets += 1
for allele in gt:
if allele == missingChar:
continue
if allele not in AFs:
AFs[allele] = 1
else:
AFs[allele] += 1
nalleles += 1
Ehomos = []
maf = 1.0
for allele in AFs:
p = float(AFs[allele]) / nalleles
if p < maf:
maf = p
Ehom = pow(p,2)
Ehomos.append(Ehom)
het = 1 - fsum(Ehomos)
return({'Ehet':het,'Ohet':float(Ohets)/(float(nalleles)/2),'MAF':maf})