-
Notifications
You must be signed in to change notification settings - Fork 15
/
bowtie2_protocol.py
executable file
·58 lines (48 loc) · 1.6 KB
/
bowtie2_protocol.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
#!/usr/bin/python
import sys
from subprocess import call
print "\nUsage: bowtie2_protocol.py ListOfReads Reference Threads [noreduce/reduce]\n"
try:
data = sys.argv[1]
except:
data = raw_input("List of reads: ")
try:
ref = sys.argv[2]
except:
ref = raw_input("FASTA reference file: ")
try:
threads = sys.argv[3]
except:
threads = raw_input("Number of threads: ")
try:
reduce = sys.argv[4]
except:
reduce = raw_input("Reduce or not reduce: ")
files = open(data).readlines()
l_files = []
for f in range(0,(len(files)/2)):
l_files.append([files[f*2][:-1],files[(f*2)+1][:-1]])
ref_pref = ref.split(".")
ref_pref = ref_pref[:-1]
ref_pref = ".".join(ref_pref)
try:
open(ref_pref+".1.bt2")
open(ref_pref+".2.bt2")
open(ref_pref+".3.bt2")
open(ref_pref+".4.bt2")
open(ref_pref+".rev.1.bt2")
open(ref_pref+".rev.2.bt2")
except:
call("bowtie2-build %s %s" % (ref, ref_pref), shell=True)
for pair in l_files:
name = pair[0]
name = name.split(".")
name = name[0][:-2]
call("bowtie2 --very-sensitive -p %s -x %s -1 %s -2 %s | samtools view -bS -> %s_fastq.bam" % (threads, ref_pref, pair[0], pair[1], name), shell=True)
call("samtools sort -T aln.sorted %s_fastq.bam -o %s_sort.bam" % (name, name), shell=True)
call("samtools index %s_sort.bam" % (name), shell=True)
call("rm %s_fastq.bam" % (name), shell=True)
call("samtools flagstat %s_sort.bam > %s_sort.flagstat" % (name, name), shell=True)
if reduce == "reduce":
call("reduce_bam.py %s_sort.bam && rm %s_sort.bam" % (name, name), shell=True)
call("rm %s_sort.bam.bai" % (name), shell=True)