-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChunkManager1.py
87 lines (77 loc) · 2.72 KB
/
ChunkManager1.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
import random
import ExtraModules
# Object that effectively manages RAM by addding bases to the file as soon as chunk is detected
# dnaBaseStr ---> our DNA encoded string
# currLenOfDNA ---> present length of DNA encoded
# chunkIndex ---> present chunkIndex
# fileID ---> represents ID of the file
# fileFinal ---> file Object for writing the final output
# startIndex ---> actual DNA encoded string is from startIndex to end of dnaBaseStr
# maxRamCapacity ---> as soon as dnaBaseStr length is more than maxRamCapacity it empties strings and adjust startIndex accordingly
class ChunkManager(object):
dnaBaseStr = []
currLenOfDNA = 0
chunkIndex = 0
fileID = ''
fileFinal = None
startIndex = 0
maxRAMCapacity = 500000
def addBase(self,base1):
self.dnaBaseStr.append(base1)
self.currLenOfDNA = self.currLenOfDNA + 1
if self.currLenOfDNA == 100 :
self.updateManager()
def addString(self,string1):
for base in string1:
self.addBase(base)
def updateManager(self):
currChunk = ''.join(self.dnaBaseStr[self.startIndex:])
if (self.chunkIndex)%2 != 0 :
currChunk = ExtraModules.reverseComplement(currChunk)
i3 = ExtraModules.intToBase3(self.chunkIndex,12)
P = int(self.fileID[0]) + int(i3[0]) + int(i3[2]) + int(i3[4]) + int(i3[6]) + int(i3[8]) + int(i3[10])
P = P % 3
indexingInfo = self.fileID + i3 + str(P)
encodedIndexInfo = ExtraModules.encodeSTR(indexingInfo,currChunk[-1])
if currChunk[0] == 'A' :
startBase = 'T'
elif currChunk[0] == 'T' :
startBase = 'A'
else :
randomNum = random.random()
if randomNum > 0.5:
startBase = 'A'
else:
startBase = 'T'
if encodedIndexInfo[-1] == 'C' :
endBase = 'G'
elif encodedIndexInfo[-1] == 'G' :
endBase = 'C'
else :
randomNum = random.random()
if randomNum > 0.5 :
endBase = 'C'
else:
endBase = 'G'
currChunk = startBase + currChunk + encodedIndexInfo + endBase
self.chunkIndex = self.chunkIndex + 1
self.startIndex = self.startIndex + 25
self.currLenOfDNA = 75
if self.startIndex >= self.maxRAMCapacity :
self.dnaBaseStr = self.dnaBaseStr[self.startIndex:]
self.startIndex = 0
self.fileFinal.write(unicode(currChunk+'\n'))
def close(self):
if self.currLenOfDNA > 0:
self.updateManager()
if self.fileFinal is not None:
self.fileFinal.flush()
self.fileFinal.close()
def __init__(self,fileFinal1,fileID1):
self.fileFinal = fileFinal1
self.fileID = fileID1
self.dnaBaseStr = []
self.currLenOfDNA = 0
self.chunkIndex = 0
self.startIndex = 0
self.maxRAMCapacity = 500000