-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_to_virtual_genomes.py
212 lines (163 loc) · 9.05 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
#/usr/bin/python3
import time
import subprocess
import argparse
import yaml
import pickle
import pandas as pd
import time
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, tmp_file_location, name+'.fa'),
# 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):
print(get_time(), 'Start cleaning rawreads...')
read_name = raw_read.split('/')[-1].split('.')[-2]
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
subprocess.call('fastq-dump {} -O {}'
.format(raw_read, tmp_file_location),
shell=True)
# Filter out low quality reads by Trimmomatic
subprocess.call('java -jar {} SE -phred33 '
'{} {} ILLUMINACLIP:{}:2:30:10 '
'LEADING:3 TRAILING:3 SLIDINGWINDOW:4:15 MINLEN:16'
.format(trimmomatic, tmp_file_location+'/'+read_name+'.fastq', read_name+'.clean.fastq', riboseq_adapters), shell=True)
# Map clean reads to ribosome sequence by bowtie
subprocess.call('bowtie -p {} -norc --un {} {} {} > {}.map_to_rRNA.sam'
.format(thread, tmp_file_location+'/'+without_rrna_reads, tmp_file_location+'/'+ribo_name, read_name+'.clean.fastq', ribo_name),
shell=True)
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, 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, 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:')
# 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('bowtie -p {} -norc {} {} > {}'
# .format(thread, tmp_file_location+'/'+genome_name+'.fa', cleanreads, tmp_file_location+'/all_bam/'+read_name+'.sam'))
# subprocess.call('bowtie -p {} -norc {} {} > {}'
# .format(thread, tmp_file_location+'/'+genome_name+'.fa', cleanreads, tmp_file_location+'/all_bam/'+read_name+'.sam'), shell=True)
# subprocess.call('samtools view {} > {}'.format(tmp_file_location+'/all_bam/'+read_name+'.sam',tmp_file_location+'/all_bam/'+read_name+'_accepted_hits.bam'),shell=True)
print(get_time(), 'Finished mapping')
print(get_time(), 'Start analysing...')
print('rename bam file')
a=tophat_result+'/accepted_hits.bam'
print(a)
# subprocess.call('mv {} {}'.format(tophat_result+'/accepted_hits.bam',tophat_result+'/'+read_name+'_accepted_hits.bam'), shell=True)
# subprocess.call('mv {} {}'.format(tophat_result+'/'+read_name+'_accepted_hits.bam', tmp_file_location+'/all_bam'), shell=True)
print('-'*100)
# subprocess.call('samtools {} {}'.format(tmp_file_location+'/all_bam/total.bam',tmp_file_location+'/all_bam/*_accepted_hits.bam'), shell=True)
# subprocess.call(
# 'bedtools bamtobed -bed12 -i {}/{}_tophat_result/accepted_hits.bam > {}/{}_tophat_result/bamtobed_result.bed'
# .format(tmp_file_location, read_name, tmp_file_location, read_name),
# shell=True)
# subprocess.call(
# 'bedtools merge -i {}/{}_tophat_result/bamtobed_result.bed -c 1 -o count > {}/{}_merge_result'
# .format(tmp_file_location, read_name, tmp_file_location, read_name),
# shell=True)
def find_reads_on_junction(tmp_file_location):
result = pd.DataFrame(columns=['a', 'b', 'c', 'd'])
junction_file = tmp_file_location+'/junction'
merge_result_file = tmp_file_location+'/merge_result'
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'))
for i in junction:
if merge_result.loc[(merge_result.b < i) & (i < merge_result.c)].empty:
pass
else:
print(merge_result.loc[(merge_result.b < i) & (i < merge_result.c)])
result = result.append(merge_result.loc[(merge_result.b < i) & (i < merge_result.c)])
reads_jun.append(i)
#print(result)
try:
pickle.dump(result, open('RCRJ_result', 'wb'))
pickle.dump(reads_jun, open('reads_jun', 'wb'))
except:
print('Error while dumping RCRJ_result')
result.to_csv(tmp_file_location+'/junction_result', sep='\t', header=0, index=False)
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 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)
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']
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
p = Pool(len(raw_reads))
for raw_read in raw_reads:
print(raw_read)
p.apply_async(deal_raw_data, args=(genome, raw_read, ribosome, thread, trimmomatic,riboseq_adapters,tmp_file_location,genome_fasta))
p.close()
p.join()
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)
find_reads_on_junction(tmp_file_location)
remove_tmp_file()
if __name__ == '__main__':
main()