-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbsseq_sim.py
More file actions
183 lines (165 loc) · 4.36 KB
/
bsseq_sim.py
File metadata and controls
183 lines (165 loc) · 4.36 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
import numpy as np
import math
import random
import sys
#==============================Variable setting===================================
readsname = sys.argv[1]
conversion_ratio=1
methylation_ratio=0.75
readsnum=5000000
readlength=100
#================================================================================
#==============================Get Genome fasta dictory=========================
chr=''
ans={}
pos=0
mark=False
s=''
length=0
l=[]
chr_order=[]
with open('/data/dsun/ref/humanigenome/hg19.fa') as f:
for line in f:
temp = line.strip()
if temp[0]=='>':
if chr!='':
ans[chr]=s
ll=len(s)
length+=ll
l.append(ll)
s=''
chr= temp[1:]
chr_order.append(chr)
continue
s+=temp
ans[chr]=s
ll=len(s)
l.append(ll)
length+=ll
#print(ans['chr3'][43483166:43483246].upper())
#print(ans['chr3'][43483379:43483459].upper())
#sys.exit()
#=========================================================================
'''
def getcg(read):
num=0
if random.random()>0.5:
for i in range(1,len(read)):
if ('G'==read[i] or 'g'==read[i]) and ('C'==read[i-1] or 'c'==read[i-1]):
num+=1
else:
for i in range(1,len(read)):
if ('C'==read[i] or 'c'==read[i]) and ('G'==read[i-1] or 'g'==read[i-1]):
num+=1
return num
'''
def fake(fl,read):
base=['A','T','C','G']
l = len(read)
if l>=fl:
return read,0,0
front = int((fl - l)/2)
end = fl-l-front
r=read
for i in range(front):
pos = random.randint(0,3)
r=base[pos]+r
for i in range(end):
pos = random.randint(0,3)
r=r+base[pos]
#print(r,front,end,l)
return r,front,end
def reverse(read):
dic={'A':'T','T':'A','C':'G','G':'C','N':'N'}
r=''
# print('r')
for rr in read:
r=dic[rr.upper()]+r
return r
def bisulfite(conv_r,meth_r,read):
r=''
l = len(read)
for i in range(1,l-1):
base=read[i].upper()
if base=='C':
c = random.random()
if c<conv_r:
if read[i+1].upper()=='G':
m = random.random()
if m>meth_r:
base='T'
else:
base='T'
r=r+base
return r
#time1=time.time()
num=[]
zero=0
finalbed=[]
for i in range(readsnum):
firstpart = random.randint(5,100)
pos1 = random.randint(0,length)
pos2 = random.randint(0,length)
chr1=0
chr2=0
#f=0
#t=0
while pos1>l[chr1]:
pos1-=l[chr1]
chr1+=1
#readlength=length
start1=pos1-1
end1=pos1+firstpart
if pos1<1:continue
if pos1>l[chr1]:continue
secondpart = random.randint(5,100)
while pos2>l[chr2]:
pos2-=l[chr2]
chr2+=1
start2=pos2
end2=pos2+secondpart
if pos2<1: continue
if pos2>l[chr2]:continue
read=ans[chr_order[chr1]][start1:end1]+ans[chr_order[chr2]][start2:end2]
r=read
a1=random.random()
a2=random.random()
if a1>0.5:
r=reverse(read)#Get reads from +/- strand
r = bisulfite(conversion_ratio,methylation_ratio,r)
if a2>0.5:
r=reverse(r)#PCR +/-
if end2-readlength>start2: continue
restlength = readlength - (end2-start2)
start1 = end1 - restlength
else:
if start1+readlength<end1: continue
restlength = readlength - (end1 - start1)
end2 = start2 + restlength
#r,f,t = fake(fake_length,r)
r=r[:readlength]
quality = 'E'*readlength
# print(readlength)
#print(a1,a2)
#print(read)
print('@'+'NS500669:73:HG3HFBGX2:1:11101:'+str(i)+':'+readsname+' 1:N:0:GCCAAT')
print(r)
print('+')
print(quality)
finalbed.append(chr_order[chr1]+'\t'+str(start1+1)+'\t'+str(end1)+'\t'+'@'+'NS500669:73:HG3HFBGX2:1:11101:'+str(i)+':'+readsname+' 1:N:0:GCCAAT'+'\t'+chr_order[chr2]+'\t'+str(start2)+'\t'+str(end2-1)+'\n')
with open(readsname+'_simulation.bed','w') as f:
f.writelines(finalbed)
#print(time.time()-time1)
#g=getcg(read)
#num.append(g)
#if g==0: zero+=1
#print(zero/float(100000000))
#print((100000000-zero)/float(100000000))
#import seaborn as sns
#from matplotlib import pyplot as plt
#import numpy as np
#n=np.array_split(num,10)
#for nn in n:
# sns.distplot(nn)
#plt.savefig('125.pdf')
##