-
Notifications
You must be signed in to change notification settings - Fork 4
/
map_to_virtual_genomes.py
280 lines (227 loc) · 11.6 KB
/
map_to_virtual_genomes.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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#/usr/bin/python3
import time
import subprocess
import argparse
import yaml
import pickle
import pandas as pd
import time
import os
from multiprocessing import Pool
# make index of genome and ribo-seq reads
def make_index(thread, genome, ribosome, tmp_file_location, genome_fasta, name):
ribo_name = str(ribosome).split('/')[-1].split('.')[0]
genomename = str(genome).split('/')[-1].split('.')[0]
ribo_name = str(ribosome).split('/')[-1].split('.')[0]
print('STAR --runThreadN {} --runMode genomeGenerate --genomeDir {} --genomeFastaFiles {}'.format(thread,tmp_file_location+'/',genome))
subprocess.call('./requiredSoft/STAR --runThreadN {} --runMode genomeGenerate --genomeDir {} --genomeFastaFiles {}'.format(thread,tmp_file_location+'/',genome),shell=True)
subprocess.call('bowtie-build --threads {} {} {}/{}'
.format(thread, ribosome, tmp_file_location, ribo_name),
shell=True, stdout=False)
subprocess.call('bowtie-build --threads {} {} {}/{}'.format(thread, genome_fasta, tmp_file_location,genome_fasta.split('/')[-1]),shell=True)
print('bowtie-build --threads {} {} {}/{}'.format(thread, genome_fasta, tmp_file_location, genome_fasta.split('/')[-1]))
print('-' * 100)
print(get_time(), 'Make index successfully!')
print('-' * 100)
def deal_raw_data(genome, raw_read, ribosome, thread, trimmomatic, riboseq_adapters, tmp_file_location,genome_fasta,ribotype):
print(get_time(), 'Start cleaning rawreads...')
if ribotype == 'sra':
read_name = raw_read.split('/')[-1].split('.')[-2]
elif ribotype == 'fastq.gz':
read_name = raw_read.split('/')[-1].split('.')[-3]
ribo_name = str(ribosome).split('/')[-1].split('.')[0]
without_rrna_reads = read_name+'.clean.without.rRNA.fastq'
unmaped_reads = read_name+'.clean.without.rRNA.unmapped.fastq'
print(get_time(), 'Loading reads form:', raw_read)
print('-' * 100)
# Transform sra to fastq format
if ribotype == 'sra':
subprocess.call('fastq-dump {} -O {}'
.format(raw_read,
tmp_file_location),
shell=True)
else:
pass
# Filter out low quality reads by Trimmomatic
if ribotype == 'sra':
subprocess.call('java -jar {} SE -threads {} -phred33 '
'{} {} ILLUMINACLIP:{}:2:30:10 '
'LEADING:3 TRAILING:3 SLIDINGWINDOW:4:15 MINLEN:16'
.format(trimmomatic,thread,
tmp_file_location+'/'+read_name+'.fastq',
tmp_file_location+'/'+read_name+'.clean.fastq',
riboseq_adapters),
shell=True)
elif ribotype == 'fastq.gz':
subprocess.call('java -jar {} SE -threads {} -phred33 '
'{} {} ILLUMINACLIP:{}:2:30:10 '
'LEADING:3 TRAILING:3 SLIDINGWINDOW:4:15 MINLEN:16'
.format(trimmomatic,thread,
raw_read,
tmp_file_location+'/'+read_name+'.clean.fastq',
riboseq_adapters),
shell=True)
# Map clean reads to ribosome sequence by bowtie
print('remove rRNA')
subprocess.call('bowtie -p {} -norc --un {} {} {} > {}.map_to_rRNA.sam'
.format(thread, tmp_file_location+'/'+without_rrna_reads,
tmp_file_location+'/'+ribo_name,
tmp_file_location+'/'+read_name+'.clean.fastq',
tmp_file_location+'/'+ribo_name),
shell=True)
print('remove liner sequence')
print('bowtie -p {} -norc --un {} {} {} > {}.map_to_genome.sam'.format(thread,
tmp_file_location+'/'+unmaped_reads,
tmp_file_location+'/'+genome_fasta.split('/')[-1],
tmp_file_location+'/'+without_rrna_reads,
tmp_file_location+'/'+ribo_name))
subprocess.call('bowtie -p {} -norc --un {} {} {} > {}.map_to_genome.sam'.format(thread,
tmp_file_location+'/'+unmaped_reads,
tmp_file_location+'/'+genome_fasta.split('/')[-1],
tmp_file_location+'/'+without_rrna_reads,
tmp_file_location+'/'+ribo_name),shell=True)
print('-' * 100)
print(get_time(), 'Finished clean process.')
print('-' * 100)
global cleanreads
cleanreads = tmp_file_location+'/'+unmaped_reads
genome_name = str(genome).split('/')[-1].split('.')[0]
print(get_time(), 'Start mapping...')
print('command:')
print('./requiredSoft/STAR --runThreadN {} --outSAMtype BAM SortedByCoordinate --alignIntronMax 10 --genomeDir {} --readFilesIn {} --outFileNamePrefix {}'
.format(thread,tmp_file_location+'/',
tmp_file_location+'/'+unmaped_reads,tmp_file_location+'/all_bam/'+read_name))
# Path to tophat2 result:
tophat_result = tmp_file_location+'/'+read_name+'_tophat_result'
subprocess.call('./requiredSoft/STAR --runThreadN {} --outSAMtype BAM SortedByCoordinate --alignIntronMax 10 --genomeDir {} --readFilesIn {} --outFileNamePrefix {}'
.format(thread,tmp_file_location+'/',
tmp_file_location+'/'+unmaped_reads,
tmp_file_location+'/all_bam/'+read_name),
shell=True)
print(get_time(), 'Finished mapping')
print('-'*100)
print(get_time(), 'Start analysing...')
def find_reads_on_junction(tmp_file_location,merge_result_name):
jun_name_dic_pickle = pickle.load(open(tmp_file_location+'/junction_name_dic','rb'))
result = pd.DataFrame(columns=['a', 'b', 'c', 'd'])
junction_file = tmp_file_location+'/junction'
merge_result_file = tmp_file_location+'/'+merge_result_name
reads_jun = []
merge_result = pd.read_csv(merge_result_file, sep='\t', low_memory=True, header=None)
merge_result.columns = ['a', 'b', 'c', 'd']
junction = pickle.load(open(junction_file, 'rb'))
circ_id = []
for i in junction:
if merge_result.loc[(merge_result.b < i) & (i < merge_result.c)].empty:
pass
else:
result = result.append(merge_result.loc[(merge_result.b < i) & (i < merge_result.c)])
reads_jun.append(i)
tmp_id = jun_name_dic_pickle[i]
circ_id.append(tmp_id)
print('dump data...')
try:
pickle.dump(circ_id, open(tmp_file_location+'/'+merge_result_name+'.trans_circ_id', 'wb'))
pickle.dump(result, open(tmp_file_location+'/'+merge_result_name+'.RCRJ_result', 'wb'))
pickle.dump(reads_jun, open(tmp_file_location+'/'+merge_result_name+'.reads_jun', 'wb'))
except:
print('Error while dumping RCRJ_result')
result.to_csv(tmp_file_location+'/'+merge_result_name+'.RCRJ_result.csv', sep='\t', header=0, index=False)
print('find_reads_on_junction finashed. ')
def remove_tmp_file():
subprocess.call('mkdir -p reads', shell=True)
subprocess.call('mv *.clean.without.rRNA.fastq ./reads', shell=True)
subprocess.call('mkdir -p result', shell=True)
subprocess.call('rm *.fastq', shell=True)
def get_time():
now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
strnow = '[{tim}]'.format(tim=now)
return strnow
def filter_and_map_reads():
start = time.time()
make_index()
deal_raw_data()
remove_tmp_file()
stop = time.time()
print('-' * 100)
print(get_time(), 'Finished all pipline')
hours = int(int((stop - start) / 60) / 60)
print(get_time(), 'Totally run', hours, 'hours')
print('-' * 100)
def bamtobed(bamfile,tmp_file_location):
subprocess.call('bedtools bamtobed -bed12 -i {} > {}.bam2bedresult.bed'.
format(tmp_file_location+'/all_bam/'+bamfile,
tmp_file_location+'/'+bamfile),
shell=True)
subprocess.call('bedtools merge -i {} -c 1 -o count > {}'.
format(tmp_file_location+'/'+bamfile+'.bam2bedresult.bed',
tmp_file_location+'/'+bamfile+'.merge_result'),
shell=True)
def main():
parse = argparse.ArgumentParser(description='This script helps to clean reads and map to genome')
parse.add_argument('-y', dest="yaml", required=True)
args = parse.parse_args()
yamlfile = args.yaml
file = open(yamlfile)
fileload = yaml.load(file, Loader=yaml.FullLoader)
raw_reads = fileload['raw_reads']
thread = fileload['thread']
ribosome = fileload['ribosome_fasta']
trimmomatic = fileload['trimmomatic_jar']
riboseq_adapters = fileload['riboseq_adapters']
tmp_file_location = fileload['tmp_file_location']
genome_fasta = fileload['genome_fasta']
name = fileload['genome_name']
merge = fileload['merge']
ribotype = fileload['ribotype']
genome = '{}/{}.fa'.format(tmp_file_location, name)
subprocess.call('mkdir -p {}'.format(tmp_file_location+'/all_bam'),
shell=True)
make_index(thread,
genome,
ribosome,
tmp_file_location,
genome_fasta,
name)
# use multiprocess to deal raw reads
for raw_read in raw_reads:
deal_raw_data(genome,raw_read,ribosome,thread,trimmomatic,riboseq_adapters,tmp_file_location,genome_fasta,ribotype)
if merge == 'T':
print('-'*20)
print('merge result...')
print('-'*20)
subprocess.call('samtools merge -f {} {}'.format(tmp_file_location+'/all_bam/total.bam',
tmp_file_location+'/all_bam/*Aligned.sortedByCoord.out.bam'),
shell=True)
subprocess.call('bedtools bamtobed -bed12 -i {} > {}/bamtobed_result.bed'.format(tmp_file_location+'/all_bam/total.bam',
tmp_file_location),
shell=True)
subprocess.call('bedtools merge -i {}/bamtobed_result.bed -c 1 -o count > {}/merge_result'.format(tmp_file_location,
tmp_file_location),
shell=True)
else:
print('-'*20)
print('not merge...')
print('-'*20)
result_bam_list = list(filter(lambda x:x[-4:]=='.bam',
os.listdir(tmp_file_location+'/all_bam')))
print('bam_list:', result_bam_list)
p2 = Pool(len(result_bam_list))
for bamfile in result_bam_list:
p2.apply_async(bamtobed,args=(bamfile,tmp_file_location))
p2.close()
p2.join()
if merge == 'T':
print('analysis junction reads...')
merge_result_name = 'merge_result'
find_reads_on_junction(tmp_file_location,merge_result_name)
else:
print('analysis junction reads...')
merge_files = list(filter(lambda x:x[-12:] == 'merge_result', os.listdir(tmp_file_location)))
p3 = Pool(thread)
for merge_result_name in merge_files:
p3.apply_async(find_reads_on_junction,args=(tmp_file_location, merge_result_name))
p3.close()
p3.join()
if __name__ == '__main__':
main()