-
Notifications
You must be signed in to change notification settings - Fork 0
/
ka_ks.py
87 lines (64 loc) · 1.83 KB
/
ka_ks.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
from Bio import SeqIO
import re
import sys
import subprocess
# Parse protein sequences from fasta file (PEP)
filinPEP = sys.argv[1]
dataPEP = SeqIO.parse(filinPEP,"fasta")
PEP = {}
for record in dataPEP :
# Retrieve gene name
m = re.search(r'gene:(\S*)\s+transcript:(\S*)\s+', record.description)
gene = m.group(1)
isoform = m.group(2)
#print("gene : " + gene + ", isoform : " + isoform)
l = len(record.seq)
# If gene is not in dictionnary ==> store it
if gene not in PEP:
PEP[isoform] = [record.seq, gene, l]
# If gene already in dictionnay ==> keep the record with the longest sequence
else:
if l > PEP[isoform][2]:
PEP[isoform] = [record.seq, gene, l]
# Parse DNA sequences from fasta file (CDS)
filinCDS = sys.argv[2]
dataCDS = SeqIO.parse(filinCDS,"fasta")
CDS = {}
for record in dataCDS :
# Retrieve gene name
m = re.search(r'gene:(\S*)\s+', record.description)
gene = m.group(1)
#print("gene : " + gene)
l = len(record.seq)
# If gene is not in dictionnary ==> store it
if gene not in CDS:
CDS[gene] = [record.seq, l]
# If gene already in dictionnay ==> keep the record with the longest sequence
else:
if l > CDS[gene][1]:
CDS[gene] = [record.seq, l]
# Create couple from same family and calculate Ka/ks
filinFam = open(sys.argv[3], 'r')
filinFam.readline()
i = 0
li = []
for gene in filinFam :
name = gene.split()[0]
family = gene.split()[1]
# If different family / 1st line ==> create new list
if family != i or family == 0 :
li = []
i = family
# If same family ==> calcultate ka/ks
else :
# Make pairs with other gens already in list
for gen in li :
# Create temporary files
print(name, gen)
print(PEP[name][0], PEP[gen][0])
print(CDS[PEP[name][1]][0], CDS[PEP[name][1]][0])
# Run PAML
# subprocess.run("... > test.nt")
# Add to list
li.append(name)
# young and nelson 2000