forked from clairedubin/thermotolerance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdxy_calc.py
81 lines (52 loc) · 2.11 KB
/
dxy_calc.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
import os
import pandas as pd
import numpy as np
from Bio import SeqIO
import csv
import sys
#USAGE:
#python3 dxy_calc.py alignment_directory output_file.csv
#alignment_directory contains one Scer population aligned with a Spar population, one file per gene
# dxy function for one gene
good_nucs = ['A', 'T', 'C', 'G', 'a', 't', 'c', 'g']
def dxy(spar_seqs, scer_seqs):
dxy = 0
spar_strain_count, scer_strain_count = len(spar_seqs), len(scer_seqs)
try:
for spar_seq in spar_seqs:
for scer_seq in scer_seqs:
for i in range(len(spar_seq)):
if scer_seq[i] != spar_seq[i] and scer_seq[i] in good_nucs and spar_seq[i] in good_nucs:
dxy += 1
dxy = dxy / (spar_strain_count * scer_strain_count)
return dxy / len(spar_seqs[0])
except:
return 'NaN'
#filter out bad alignments
def separate_strains(file, gap_max=0.1, spar_strain_count_min=8, scer_strain_count_min=0): #.7*724
spar_seqs, scer_seqs = [], []
for record in SeqIO.parse(file, 'fasta'):
if (str(record.seq).count('-') + str(record.seq).count('N') + str(record.seq).count('n')) / len(record.seq) > gap_max:
continue
if '_Sp_' in record.description:
spar_seqs += [record.seq]
elif 'ref(S288c)' not in record.description:
scer_seqs += [record.seq]
if len(scer_seqs) < scer_strain_count_min or len(spar_seqs) < spar_strain_count_min:
return None, None
return spar_seqs, scer_seqs
directory = sys.argv[1]
output = sys.argv[2]
if __name__ == '__main__':
for file in os.listdir(directory):
if "muscle_afa" not in file:
continue
gene = file.split('.')[0].split('_')[-1]
spar_seqs, scer_seqs = separate_strains(directory+"/"+file)
if spar_seqs and scer_seqs:
dxy_val = dxy(spar_seqs, scer_seqs)
with open(output, 'a') as f:
writer = csv.writer(f)
writer.writerow([directory, gene, dxy_val, len(spar_seqs), len(scer_seqs)])
f.close()
print(gene, dxy_val)