forked from alfredkam/corRna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBootStrap.py
executable file
·92 lines (89 loc) · 2.25 KB
/
BootStrap.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
#! /usr/bin/env python
import sys
import re
import commands
import shutil
import getopt
import os, glob, sys
import os.path
import random, math
from multiprocessing import Process, Manager, Array, Value
from decimal import Decimal
import time
from math import *
import Pearson
DEBUG = False
class bootstrap:
def __init__(self, wildtype, depth):
self.wild = wildtype
self.depth = depth
if DEBUG:
print "to grab correlation"
self.corr = Pearson.Correlation(self.wild)
self.boot = Manager().list()
if DEBUG:
print "done grab correlation"
def start(self):
if DEBUG:
print "ready to start"
self.startSampling()
def getStanding(self,*args):
if len(args) == 1:
seq = args[0]
depth = self.detectDepth(seq)
if depth == 0:
return "---"
sampling = self.boot[depth]
corr = self.corr.getCorr(seq)
sampling = self.boot[int(depth)-1]
sampling.append(corr)
sampling.sort()
return float(float(sampling.index(corr))/float(len(sampling)))
elif len(args) == 2:
depth = self.detectDepth(args[0])
if depth == 0:
return "---"
sampling = self.boot[int(depth)-1]
sampling.append(args[1])
sampling.sort()
return float(float(sampling.index(args[1]))/float(len(sampling)))
def detectDepth(self,seq):
counter = 0
for i in range(len(self.wild)):
if(self.wild[i] != seq[i]):
counter+=1
return counter
def startSampling(self):
proc = []
for i in range(1,int(self.depth)+1):
if DEBUG:
print i
p = Process(target=self.generate, args=(i,))
p.start()
proc.append(p)
for p in proc:
p.join()
def generate(self, depth):
kSequence = []
for i in range(1000):
kSequence.append(self.computeSeq(depth))
self.boot.insert(int(depth)-1,kSequence)
def computeSeq(self,depth):
seq = self.sequenceRandomizer(self.wild, depth)
corr = self.corr.getCorr(seq)
return corr
def sequenceRandomizer(self, wildtype, n):
random.seed()
for i in range(n):
x = random.randint(1,len(wildtype)-1)
wildtype = self.substring(wildtype,x,self.ranRNA(wildtype[x]))
return wildtype
def substring(self, stri, posi, ch):
return stri[:posi]+ch+stri[posi+1:]
def ranRNA(self,c):
random.seed()
x = random.randint(0,3)
rna = ["A","C","G","U"]
if(rna[x] == c):
return self.ranRNA(c)
return rna[x]