-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandom_Functions.py
61 lines (41 loc) · 2.42 KB
/
Random_Functions.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
import random,math
class ERV_Randoms(object):
def __init__(self, numHostGen, hostMutationRate, rateNewCopy):
self.numHostGen = numHostGen
self.hostMutationRate = hostMutationRate
self.rateNewCopy = rateNewCopy
random.seed()
# Function that calculates the waiting time for the Transposon model
def ERV_Time_Transposon(self, tip_list):
randNum = random.uniform(0,1)
waiting_time = -math.log(randNum)/(self.rateNewCopy*(len(tip_list)))
return waiting_time
# Function that calculates the waiting time for the Master model
def ERV_Time_Master(self):
randNum = random.uniform(0,1)
waiting_time = -math.log(randNum)/self.rateNewCopy
return waiting_time
# Function that randomly choose which copy will generate the new copy
def choose_seq(self, tip_list):
seq = random.choice(tip_list) # It will random choose which copy will generate the new copy
while seq == None:
seq = random.choice(tip_list)
return seq
# Function that randomly choose which sequence (ERV copy) in the phylogenetic tree will be replaced.
def choose_SeqToReplace(self, seq1, seq2, tip_list, total_N, max_N):
prob = float(total_N-1)/(max_N-1) # This probability increases as it increases the number of tips in the phylogenetic tree
seqToReplace = None
randN = random.random()
if randN < prob: # if random number is less than prob I replace the sequence in the phylogenetic tree
seqToReplace = random.choice(tip_list) # Randomly chooses a Sequence in the list Tips to be replaced
# The copy to be selected has to be different from seq1 and seq2. This makes easier to to simulate trees
# because I will know that seq1 is more closely related to seq2
while seqToReplace == seq1 or seqToReplace == seq2:
seqToReplace = random.choice(tip_list)
return seqToReplace
# Function that will return the Final Time that the last element was generated
def final_Time(self, totalTime, waiting_time):
totalTime -= waiting_time # I subtract the value in waiting_time because in my main code, I first sum to check if totalTime will be bigger than number os host generations
timeFinal = self.numHostGen - totalTime
timeFinal = timeFinal * self.hostMutationRate # Final time in substitution per site
return timeFinal