-
Notifications
You must be signed in to change notification settings - Fork 0
/
getZScoresOfPhage.py
147 lines (130 loc) · 5.02 KB
/
getZScoresOfPhage.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
import numpy as np
from os import listdir
import json
import sys
def main():
if len(sys.argv) != 3:
mainThing("C:\\Users\\Ray\\Documents\\GitHub\\pallid_pallas\\phageFastas", 6)
# if the correct arguments aren't given, exit with a helpful message
sys.exit('USAGE: folder of fastas with badname file removed \n \
size of pals, \n')
# assign variables from the arguments
else:
folder= sys.argv[1]
n = int(sys.argv[2])
# run the program you designed
mainThing(folder, n)
#Take in folder, output list of fasta names
def listOfFastasInFolder(folderLocation):
fastas = [folderLocation +"\\" + f for f in listdir(folderLocation)]
fastas.remove(folderLocation +"\\" + "badNames.txt")
return fastas
#This takes the four nucleotides (nucs) and converts them to integerrs so that ultimately, all palindromes can be found and defined as integers.
def palToInt(seq, nucs):
int1 = 0
for i in range(0, len(seq)//2):
int1 = int1 + 4**i *nucs.index(seq[i])
return int1
#n is length of a sequence. This converts integer palindrome representations back into strings
def intToPal(int1, nucs, n):
seq = ""
for i in range(n//2):
num = int1//4**(n//2 - i - 1)
int1 = int1 - num*4**(n//2 - i - 1)
seq = seq + nucs[num]
otherHalf = ""
for i in seq:
otherHalf = otherHalf + nucs[3-nucs.index(i)]
return seq[::-1] + otherHalf
#inputs a .fasta file for a single genome and outputs the genome sequence as a string
def getSequenceFromFasta(file1):
with open(file1, 'r') as f:
line = f.readline()
while line[0]=='>':
line=f.readline()
sequence = line.replace("\n", "")
for line in f:
sequence = sequence + line.replace("\n", "")
if 'N' in sequence:
return ""
return sequence.replace("\t", '')
#tests if an input kmer is a palindrome and outputs the appropriate truth value
def isPal(seq, nucs):
n = len(seq)
if n%2 == 1:
return False
for i in range(n//2):
if nucs.index(seq[i]) + nucs.index(seq[n - i - 1]) != 3:
return False
return True
#takes in a genome as a string and outputs the number of each palindrome
def palNucCount(seq, n, nucs):
counts = np.zeros(4)
palCount = np.zeros(4**(n//2))
for i in range(n - 1):
counts[nucs.index(seq[i])] = counts[nucs.index(seq[i])] + 1
for i in range(n , len(seq) + 1):
counts[nucs.index(seq[i-1])] = counts[nucs.index(seq[i-1])] + 1
ntide = seq[i - n: i]
if isPal(ntide, nucs):
palCount[palToInt(ntide, nucs)] = palCount[palToInt(ntide, nucs)] + 1
return (counts, palCount)
def tud(nucCount, palCount, nucs, n):
totalNucs = np.sum(nucCount)
tud = np.zeros(4**(n//2))
for i in range(4**(n//2)):
pal = intToPal(i, nucs, n)
prob = np.product([nucCount[nucs.index(j)] for j in pal]/totalNucs)
tud[i] = palCount[i]/(prob*(totalNucs - n + 1))
return tud
def zScores(tuds):
mean = np.mean(tuds)
sigma = np.std(tuds)
return (tuds-mean)/sigma
#==============================================================================
# for i in range(len(tuds[0])):
# sigma = np.std([tuds[j][i] for j in range(len(tuds))])
# for j in range(len(tuds)):
# tuds[j][i] = (tuds[j][i] - 1)/sigma
# return tuds
#==============================================================================
#This runs the code properly in order, finding Z scores for palindrome use
#Finds scores for each palindrome for all genomes, using fastas downloaded from phages DB
def mainThing(folder, n):
#KEEP THIS ORDERING
badFastas = []
nucs = ['A', 'C', 'G', 'T']
fastas = listOfFastasInFolder(folder)
#fastas = ["C:\Users\Ray\Documents\GitHub\pallid_pallas\phageFastas\Yahalom-B3.fasta"]
allTUD = []
gc = []
for i in range(len(fastas)):
fasta = fastas[i]
print fasta
dna = getSequenceFromFasta(fasta)
if dna == "":
badFastas.append(fasta)
else:
data = palNucCount(dna, n, nucs)
gc.append((data[0][1]+ data[0][2])/sum(data[0]))
tuds = tud(data[0], data[1], nucs, n)
allTUD.append(list(tuds))
z = zScores(np.array(allTUD))
print(badFastas)
for bad in badFastas:
fastas.remove(bad)
nicePhageNames = [f1.replace(folder + "\\", "").replace(".fasta", "") for f1 in fastas]
with open("zscores"+ str(n)+".txt", "w") as f:
json.dump([list(a) for a in z], f)
with open("phages"+ str(n)+".txt", "w") as f:
json.dump(nicePhageNames, f)
with open("pals"+ str(n)+".txt", "w") as f:
json.dump([intToPal(int1, nucs, n) for int1 in range(4**(n//2))], f)
with open("tuds"+ str(n)+".txt", "w") as f:
json.dump(allTUD, f)
with open("gc"+ str(n)+".txt", "w") as f:
json.dump(gc, f)
# these two lines automatically run the main function if the script
# is called from the commend line
if __name__ == '__main__':
main()