-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.py
189 lines (154 loc) · 6.46 KB
/
generate.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
from random import random
import numpy as np
import os
def generate_sequences(sequence_count, sequence_length):
"""
@param sequence_count: number of sequences to be generated
@param sequence_length: length of each seqeunce
@return: sequences which is a list of strings of A, C, G, and Ts
"""
sequences = [] #list of sequences
sequence_array = np.random.rand(sequence_count, sequence_length) #np array of random number [0,1) of shape (sc, sl)
for i in range(sequence_array.shape[0]):
sequence_string = ""
for j in range(sequence_array.shape[1]):
if sequence_array[i][j] < 0.25:
sequence_string += "A"
elif sequence_array[i][j] < 0.5:
sequence_string += "C"
elif sequence_array[i][j] < 0.75:
sequence_string += "G"
else:
sequence_string += "T"
sequences.append(sequence_string)
return sequences
def generate_binding_sites(sequence_count, sequence_length, motif_length):
"""
@param sequence_count: number of sequences to be generated
@param sequence_length: length of each seqeunce
@param motif_length: length of each motif
@return: np array of integers of size sequence count
"""
return np.random.randint(0, sequence_length-motif_length-1, size=sequence_count)
def generate_motif(ICPC, ML):
"""
@param ICPC: information count per column
@ML: motif length
@SC: sequence count
Used the algorithm given in the appendix of the mini project PDF
@return: a list of lists with float as the element
"""
motif = []
icpcDict = {1 : .8105, 1.5 : .9245, 2 : 1} #probabilities of each ICPC
#select preferred nucleotide
for i in range(0, ML):
randNum = random()
p = icpcDict[ICPC]
otherP = (1-p)/3
preffered = ""
second = ""
third = ""
fourth = ""
if (randNum < .25):
preffered = "A"
second = "C"
third = "G"
fourth = "T"
elif (randNum < .50):
preffered = "C"
second = "A"
third = "G"
fourth = "T"
elif (randNum < .75):
preffered = "G"
second = "A"
third = "C"
fourth = "T"
else:
preffered = "T"
second = "A"
third = "G"
fourth = "C"
seq = {"A" : 0, "C" : 0, "G" : 0, "T" : 0}
seq[preffered] = p
seq[second] = round(otherP, 9)
seq[third] = round(otherP, 9)
seq[fourth] = round(otherP, 9)
seqAdd = [seq["A"], seq["C"], seq["G"], seq["T"]]
motif.append(seqAdd)
return motif
def plant_sites(sequences, sites, motif):
"""
@param sequences: a list of strings, each string is a sequence to be planted
@sites: an np array of integers
@motif: a list of lists with integers as elements (PWM)
@return: a list of sequences with the motifs planted
"""
nuc_dict = {0: 'A', 1:'C', 2:'G', 3:'T'}
planted_sequences = []
for i, (sequence, site) in enumerate(zip(sequences,sites)):
seqlist = list(sequence)
for i, nuc in enumerate(motif):
seqlist[i+site] = nuc_dict[np.random.choice(np.arange(4), 1, p=nuc)[0]]
planted_sequences.append(''.join(seqlist))
return planted_sequences
def convert_motif(ML, motif, file):
"""
@param ML: integer, motif length
@param motif: a list of lists with integers as elements
@param file: a string containing relative path to the file
"""
with open(file, 'w') as f:
f.write('>MOTIF1\t' + str(ML))
for i in range(len(motif)):
f.write('\n')
for j in range(len(motif[i])):
f.write('{:<12}'.format(str(motif[i][j])))
f.write('\t')
f.write('\n<')
if __name__ == "__main__":
sl = 500 #default value for sequence length
directories = ['default', 'ICPC_1', 'ICPC_1.5', 'ML_6', 'ML_7', 'SC_5', 'SC_20']
#dict that maps each directory with tuple of parameters (ICPC, ML, SC)
directory_dict = {'default': (2, 8, 10),
'ICPC_1': (1, 8, 10),
'ICPC_1.5': (1.5, 8, 10),
'ML_6': (2, 6, 10),
'ML_7': (2, 7, 10),
'SC_5': (2, 8, 5),
'SC_20': (2, 8, 20)
}
dirname = os.path.dirname(__file__) #path of generate.py
#generate 10 sequences and binding sites for directories
for directory in directories:
for i in range(1, 11):
#join for relative path to file
sequence_file = os.path.join(dirname, 'dataset/'+ directory + '/' + str(i) + '/sequences.fasta')
site_file = os.path.join(dirname, 'dataset/'+ directory + '/' + str(i) + '/sites.txt')
motif_length_file = os.path.join(dirname, 'dataset/'+ directory + '/' + str(i) + '/motiflength.txt')
motif_file = os.path.join(dirname, 'dataset/'+ directory + '/' + str(i) + '/motif.txt')
#generate
mySequences = generate_sequences(directory_dict[directory][2], sl)
mySites = generate_binding_sites(directory_dict[directory][2], sl, directory_dict[directory][1])
myMotifLength = directory_dict[directory][1]
myMotif = generate_motif(directory_dict[directory][0], directory_dict[directory][1])
plantedSequences = plant_sites(mySequences, mySites, myMotif)
#open files
fasta_file = open(sequence_file, "w")
sites = open(site_file, "w")
motif_length = open(motif_length_file, "w")
motif = open(motif_file, "w")
#write into files
for j in range(len(plantedSequences)):
if j == len(plantedSequences)-1:
fasta_file.write(">" + "Sequence " + str(j+1) + "\n" + plantedSequences[j])
sites.write(str(mySites[j]))
else:
fasta_file.write(">" + "Sequence " + str(j+1) + "\n" + plantedSequences[j] + "\n")
sites.write(str(mySites[j]) + "\n")
motif_length.write(str(myMotifLength))
convert_motif(directory_dict[directory][1], myMotif, motif_file)
#close files
fasta_file.close()
sites.close()
motif_length.close()