-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSSM.py
194 lines (154 loc) · 6.2 KB
/
PSSM.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
'''
PSSM (Position Specific Scoring Matrix)
1. The multiple aligned DNA sequences is produced in 2 ways:
a. Reading the prepared multiple aligned DNA sequences from file called PSSMData.txt
where first line is for the values of t sequences, and n nucleotides.
b. Generate t sequences where one sequence of length n is formed of random order of nucleotides.
2. Print the multiple aligned DNA sequences.
3. Apply PSSM on the multiple aligned DNA sequences.
4. Print the PSSM matrix
5. Let user enter a new sequence of length n, then print the probability of new entered sequence
joining the rest of the multiple aligned DNA sequences.
'''
import random
import math
def randomSeqsGenterator(t,n):
seqs = []
for _ in range(t):
seq = ""
for _ in range(n):
seq += random.choice('ACGT')
seqs.append(seq)
return seqs
def prepareSequences():
print("\nPlease Enter your choice for choosing multiple aligned DNA sequences:\n")
print("1-Reading the prepared multiple aligned DNA sequences from file called PSSMData.txt where first line is for the values of t sequences, and n nucleotides.")
print("2-Generate t sequences where one sequence of length n is formed of random order of nucleotides.\n")
choice = 0
t = 0
n = 0
seqs = []
try:
choice = int(input("Enter: \n"))
except ValueError:
print("Error, you are only allowed to enter two numbers 1 or 2.")
return False
if(choice < 1 or choice > 2):
print("Error, you are only allowed to enter two numbers 1 or 2.")
return False
elif(choice == 1):
try:
myfile = open("PSSMData.txt")
line = myfile.readline()
line = line.split(" ")
t = line[0]
n = line[1]
seqs = []
line = myfile.readline()
while(line):
seqs.append(line.rstrip())
line = myfile.readline()
t = len(seqs)
n = len(seqs[0])
for i in seqs:
temp = len(i)
if(temp != n):
print("Error: all sequences must be of same length, no insertions or deletions allowed.")
return False
return seqs
except IndexError:
print("Error, wrong file format.")
return False
except FileNotFoundError:
print("Error, wrong file format.")
return False
else:
try:
t = int(input("Please, enter the t number for your sequences : \n"))
n = int(input("Please, enter the n length number of all sequences : \n"))
seqs = randomSeqsGenterator(t, n)
except ValueError:
print("Error, you are only allowed to enter numbers integers.")
return False
return seqs
def PSSM(seqs):
t = len(seqs)
n = len(seqs[0])
array = []
mergedSequences = ''.join(seqs)
totalA = (mergedSequences.count("A") + mergedSequences.count("a")) / len(mergedSequences)
totalC = (mergedSequences.count("C") + mergedSequences.count("c")) / len(mergedSequences)
totalG = (mergedSequences.count("G") + mergedSequences.count("g")) / len(mergedSequences)
totalT = (mergedSequences.count("T") + mergedSequences.count("t")) / len(mergedSequences)
for i in range(4):
temp = []
for j in range(n):
temp.append(0)
array.append(temp)
for i in range(n):
A = 0.0
C = 0.0
G = 0.0
T = 0.0
for j in range(t):
if(seqs[j][i] == "a" or seqs[j][i] == "A"):
A += 1
elif(seqs[j][i] == "c" or seqs[j][i] == "C"):
C += 1
elif(seqs[j][i] == "g" or seqs[j][i] == "G"):
G += 1
elif(seqs[j][i] == "t" or seqs[j][i] == "T"):
T += 1
array[0][i] = A/t/totalA
array[1][i] = C/t/totalC
array[2][i] = G/t/totalG
array[3][i] = T/t/totalT
if(array[0][i] != 0):
array[0][i] = round(math.log(array[0][i], 2),2)
if(array[1][i] != 0):
array[1][i] = round(math.log(array[1][i], 2),2)
if(array[2][i] != 0):
array[2][i] = round(math.log(array[2][i], 2),2)
if(array[3][i] != 0):
array[3][i] = round(math.log(array[3][i], 2),2)
return array
def calcProb(matrix,seq):
prob = 0.0
if(len(seq) != len(matrix[0])):
print("\nError, your enetered sequence must be of the same length of the PSSM sequences length = " + str(len(matrix[0])) + "\n")
return "Error"
for i in range(len(seq)):
if(seq[i] == "A" or seq[i] == "a"):
prob += matrix[0][i]
if(seq[i] == "C" or seq[i] == "c"):
prob += matrix[1][i]
if(seq[i] == "G" or seq[i] == "g"):
prob += matrix[2][i]
if(seq[i] == "T" or seq[i] == "t"):
prob += matrix[3][i]
return round(prob,2)
def main():
seqs = prepareSequences()
matrix = []
if(seqs):
print("\nThe multiple aligned DNA sequences:\n")
for i in seqs:
print(i)
matrix = PSSM(seqs)
print("\nThe PSSM matrix:\n")
print('A: ',matrix[0])
print('C: ',matrix[1])
print('G: ',matrix[2])
print('T: ',matrix[3])
seq = input("\nEnter the sequence you want to calculate its probability : \n")
probability = calcProb(matrix,seq)
if(probability != "Error"):
print("\nThe probability of new entered sequence joining the rest of the multiple aligned DNA sequences is : \n")
print(probability)
if(probability >= 0):
print("\nThis sequence is a similar/identical residue match.\n")
else:
print("\nThis sequence is a non-conserved residue match.\n")
#################################################################################################################################
# Main Goes Here #
main()