-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfasta-dinucleotide-shuffle_typeEF.py
More file actions
executable file
·251 lines (221 loc) · 5.68 KB
/
Copy pathfasta-dinucleotide-shuffle_typeEF.py
File metadata and controls
executable file
·251 lines (221 loc) · 5.68 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/local/bin/python
import sys, string, random
import sequence_met
#
# turn on psyco to speed up by 3X
#
if __name__=='__main__':
try:
import psyco
#psyco.log()
psyco.full()
psyco_found = True
except ImportError:
# psyco_found = False
pass
# print >> sys.stderr, "psyco_found", psyco_found
# altschulEriksonDinuclShuffle.py
# P. Clote, Oct 2003
def computeCountAndLists(s):
#Initialize lists and mono- and dinucleotide dictionaries
List = {} #List is a dictionary of lists
List['A'] = []; List['C'] = [];
List['G'] = []; List['T'] = [];
# FIXME: is this ok?
List['N'] = []
List['E'] = []
List['F'] = []
nuclList = ["A","C","G","T","N","E","F"]
s = s.upper()
#s = s.replace("U","T")
nuclCnt = {} #empty dictionary
dinuclCnt = {} #empty dictionary
for x in nuclList:
nuclCnt[x]=0
dinuclCnt[x]={}
for y in nuclList:
dinuclCnt[x][y]=0
#Compute count and lists
nuclCnt[s[0]] = 1
nuclTotal = 1
dinuclTotal = 0
for i in range(len(s)-1):
x = s[i]; y = s[i+1]
List[x].append( y )
nuclCnt[y] += 1; nuclTotal += 1
dinuclCnt[x][y] += 1; dinuclTotal += 1
assert (nuclTotal==len(s))
assert (dinuclTotal==len(s)-1)
return nuclCnt,dinuclCnt,List
def chooseEdge(x,dinuclCnt):
z = random.random()
denom=dinuclCnt[x]['A']+dinuclCnt[x]['C']+dinuclCnt[x]['G']+dinuclCnt[x]['T']+dinuclCnt[x]['N']+dinuclCnt[x]['E']+dinuclCnt[x]['F']
numerator = dinuclCnt[x]['A']
if z < float(numerator)/float(denom):
dinuclCnt[x]['A'] -= 1
return 'A'
numerator += dinuclCnt[x]['C']
if z < float(numerator)/float(denom):
dinuclCnt[x]['C'] -= 1
return 'C'
numerator += dinuclCnt[x]['G']
if z < float(numerator)/float(denom):
dinuclCnt[x]['G'] -= 1
return 'G'
numerator += dinuclCnt[x]['T']
if z < float(numerator)/float(denom):
dinuclCnt[x]['T'] -= 1
return 'T'
numerator += dinuclCnt[x]['E']
if z < float(numerator)/float(denom):
dinuclCnt[x]['E'] -= 1
return 'E'
numerator += dinuclCnt[x]['F']
if z < float(numerator)/float(denom):
dinuclCnt[x]['F'] -= 1
return 'F'
dinuclCnt[x]['N'] -= 1
return 'N'
def connectedToLast(edgeList,nuclList,lastCh):
D = {}
for x in nuclList: D[x]=0
for edge in edgeList:
a = edge[0]; b = edge[1]
if b==lastCh: D[a]=1
for i in range(3):
for edge in edgeList:
a = edge[0]; b = edge[1]
if D[b]==1: D[a]=1
ok = 0
for x in nuclList:
if x!=lastCh and D[x]==0: return 0
return 1
def eulerian(s):
nuclCnt,dinuclCnt,List = computeCountAndLists(s)
#compute nucleotides appearing in s
nuclList = []
for x in ["A","C","G","T","N","E","F"]:
if x in s: nuclList.append(x)
#create dinucleotide shuffle L
firstCh = s[0] #start with first letter of s
lastCh = s[-1]
edgeList = []
for x in nuclList:
if x!= lastCh: edgeList.append( [x,chooseEdge(x,dinuclCnt)] )
ok = connectedToLast(edgeList,nuclList,lastCh)
return ok,edgeList,nuclList,lastCh
def shuffleEdgeList(L):
n = len(L); barrier = n
for i in range(n-1):
z = int(random.random() * barrier)
tmp = L[z]
L[z]= L[barrier-1]
L[barrier-1] = tmp
barrier -= 1
return L
def dinuclShuffle(s):
if len(s)==0:
return s
ok = 0
while not ok:
ok,edgeList,nuclList,lastCh = eulerian(s)
nuclCnt,dinuclCnt,List = computeCountAndLists(s)
#remove last edges from each vertex list, shuffle, then add back
#the removed edges at end of vertex lists.
for [x,y] in edgeList: List[x].remove(y)
for x in nuclList: shuffleEdgeList(List[x])
for [x,y] in edgeList: List[x].append(y)
#construct the eulerian path
L = [s[0]]; prevCh = s[0]
for i in range(len(s)-2):
ch = List[prevCh][0]
L.append( ch )
del List[prevCh][0]
prevCh = ch
L.append(s[-1])
t = string.join(L,"")
return t
def main():
#
# defaults
#
file_name = None
seed = 1
copies = 1
#print "running"
#
# get command line arguments
#
usage = """USAGE: %s [options]
-f <filename> file name (required)
-t <tag> added to shuffled sequence names
-s <seed> random seed; default: %d
-c <n> make <n> shuffled copies of each sequence; default: %d
-h print this usage message
""" % (sys.argv[0], seed, copies)
# no arguments: print usagei
if len(sys.argv) == 1:
#print "arg too short"
print >> sys.stderr, usage; sys.exit(1)
tag = ""
# parse command line
#print "lenthsys",len(sys.argv)
i = 1
#print i
while i < len(sys.argv):
arg = sys.argv[i]
#print i, arg
if (arg == "-f"):
i += 1
try:
file_name = sys.argv[i]
#print "filename", file_name
except:
print >> sys.stderr, usage; sys.exit(1)
elif (arg == "-t"):
i += 1
try:
tag = sys.argv[i]
#print "tag",tag
except:
print >> sys.stderr, usage; sys.exit(1)
elif (arg == "-s"):
i += 1
try:
seed = string.atoi(sys.argv[i])
#print "seed",seed
except:
print >> sys.stderr, usage; sys.exit(1)
elif (arg == "-c"):
i += 1
try:
copies = string.atoi(sys.argv[i])
#print "copies",copies
except:
print >> sys.stderr, usage; sys.exit(1)
elif (arg == "-h"):
print >> sys.stderr, usage; sys.exit(1)
#print "help"
else:
print >> sys.stderr, "Unknown command line argument: " + arg
sys.exit(1)
i += 1
# check that required arguments given
if (file_name == None):
print >> sys.stderr, usage; sys.exit(1)
random.seed(seed)
# read sequences
seqs = sequence_met.readFASTA(file_name,'Methylation DNA')
for s in seqs:
str = s.getString()
#FIXME altschul can't handle ambigs
name = s.getName()
#print >> sys.stderr, ">%s" % name
for i in range(copies):
shuffledSeq = dinuclShuffle(str)
if (copies == 1):
print >> sys.stdout, ">%s\n%s" % (name+tag, shuffledSeq)
else:
print >> sys.stdout, ">%s_%d\n%s" % (name+tag, i, shuffledSeq)
if __name__ == '__main__':
main()