0
|
1 #!/usr/bin/env python
|
|
2 # small RNA oriented bowtie wrapper
|
|
3 # version 1.5 17-7-2014: arg parser implementation
|
|
4 # Usage sRbowtie.py <1 input_fasta_file> <2 alignment method> <3 -v mismatches> <4 out_type> <5 buildIndexIfHistory> <6 fasta/bowtie index> <7 bowtie output> <8 ali_fasta> <9 unali_fasta> <10 --num-threads \${GALAXY_SLOTS:-4}>
|
|
5 # current rev: for bowtie __norc, move from --supress 2,6,7,8 to --supress 6,7,8. Future Parser must be updated to take into account this standardisation
|
|
6 # Christophe Antoniewski <drosofff@gmail.com>
|
|
7
|
|
8 import sys
|
|
9 import os
|
|
10 import subprocess
|
|
11 import tempfile
|
|
12 import shutil
|
|
13 import argparse
|
|
14
|
|
15
|
|
16 def Parser():
|
|
17 the_parser = argparse.ArgumentParser(
|
|
18 description="bowtie wrapper for small fasta reads")
|
|
19 the_parser.add_argument(
|
|
20 '--input', action="store", type=str, help="input file")
|
|
21 the_parser.add_argument(
|
|
22 '--input-format', dest="input_format", action="store", type=str, help="fasta or fastq")
|
|
23 the_parser.add_argument('--method', action="store", type=str,
|
|
24 help="RNA, unique, multiple, k_option, n_option, a_option")
|
|
25 the_parser.add_argument('--v-mismatches', dest="v_mismatches", action="store",
|
|
26 type=str, help="number of mismatches allowed for the alignments")
|
|
27 the_parser.add_argument(
|
|
28 '--output-format', dest="output_format", action="store", type=str, help="tabular, sam, bam")
|
|
29 the_parser.add_argument(
|
|
30 '--output', action="store", type=str, help="output file path")
|
|
31 the_parser.add_argument(
|
|
32 '--index-from', dest="index_from", action="store", type=str, help="indexed or history")
|
|
33 the_parser.add_argument('--index-source', dest="index_source",
|
|
34 action="store", type=str, help="file path to the index source")
|
|
35 the_parser.add_argument(
|
|
36 '--aligned', action="store", type=str, help="aligned read file path, maybe None")
|
|
37 the_parser.add_argument('--unaligned', action="store",
|
|
38 type=str, help="unaligned read file path, maybe None")
|
|
39 the_parser.add_argument('--num-threads', dest="num_threads",
|
|
40 action="store", type=str, help="number of bowtie threads")
|
|
41 args = the_parser.parse_args()
|
|
42 return args
|
|
43
|
|
44
|
|
45 def stop_err(msg):
|
|
46 sys.stderr.write('%s\n' % msg)
|
|
47 sys.exit()
|
|
48
|
|
49
|
|
50 def bowtieCommandLiner(alignment_method="RNA", v_mis="1", out_type="tabular",
|
|
51 aligned="None", unaligned="None", input_format="fasta", input="path",
|
|
52 index="path", output="path", pslots="4"):
|
|
53 if input_format == "fasta":
|
|
54 input_format = "-f"
|
|
55 elif (input_format == "fastq") or (input_format == "fastqsanger"):
|
|
56 input_format = "-q"
|
|
57 else:
|
|
58 raise Exception('input format must be one of fasta or fastq')
|
|
59 if alignment_method == "RNA":
|
|
60 x = "-v %s -M 1 --best --strata -p %s --norc --suppress 6,7,8" % (
|
|
61 v_mis, pslots)
|
|
62 elif alignment_method == "unique":
|
|
63 x = "-v %s -m 1 -p %s --suppress 6,7,8" % (v_mis, pslots)
|
|
64 elif alignment_method == "multiple":
|
|
65 x = "-v %s -M 1 --best --strata -p %s --suppress 6,7,8" % (
|
|
66 v_mis, pslots)
|
|
67 elif alignment_method == "k_option":
|
|
68 x = "-v %s -k 1 --best -p %s --suppress 6,7,8" % (v_mis, pslots)
|
|
69 elif alignment_method == "n_option":
|
|
70 x = "-n %s -M 1 --best -p %s --suppress 6,7,8" % (v_mis, pslots)
|
|
71 elif alignment_method == "a_option":
|
|
72 x = "-v %s -a --best -p %s --suppress 6,7,8" % (v_mis, pslots)
|
|
73 if aligned == "None" and unaligned == "None":
|
|
74 fasta_command = ""
|
|
75 elif aligned != "None" and unaligned == "None":
|
|
76 fasta_command = " --al %s" % aligned
|
|
77 elif aligned == "None" and unaligned != "None":
|
|
78 fasta_command = " --un %s" % unaligned
|
|
79 else:
|
|
80 fasta_command = " --al %s --un %s" % (aligned, unaligned)
|
|
81 x = x + fasta_command
|
|
82 if out_type == "tabular":
|
|
83 return "bowtie %s %s %s %s > %s" % (x, index, input_format, input, output)
|
|
84 elif out_type == "sam":
|
|
85 return "bowtie %s -S %s %s %s > %s" % (x, index, input_format, input, output)
|
|
86 elif out_type == "bam":
|
|
87 return "bowtie %s -S %s %s %s |samtools view -bS - > %s" % (
|
|
88 x, index, input_format, input, output)
|
|
89
|
|
90
|
|
91 def bowtie_squash(fasta):
|
|
92 # make temp directory for bowtie indexes
|
|
93 tmp_index_dir = tempfile.mkdtemp()
|
|
94 ref_file = tempfile.NamedTemporaryFile(dir=tmp_index_dir)
|
|
95 ref_file_name = ref_file.name
|
|
96 # by default, delete the temporary file, but ref_file.name is now stored
|
|
97 # in ref_file_name
|
|
98 ref_file.close()
|
|
99 # symlink between the fasta source file and the deleted ref_file name
|
|
100 os.symlink(fasta, ref_file_name)
|
|
101 # bowtie command line, which will work after changing dir
|
|
102 # (cwd=tmp_index_dir)
|
|
103 cmd1 = 'bowtie-build -f %s %s' % (ref_file_name, ref_file_name)
|
|
104 try:
|
|
105 FNULL = open(os.devnull, 'w')
|
|
106 # a path string for a temp file in tmp_index_dir. Just a string
|
|
107 tmp = tempfile.NamedTemporaryFile(dir=tmp_index_dir).name
|
|
108 # creates and open a file handler pointing to the temp file
|
|
109 tmp_stderr = open(tmp, 'wb')
|
|
110 # both stderr and stdout of bowtie-build are redirected in dev/null
|
|
111 proc = subprocess.Popen(
|
|
112 args=cmd1, shell=True, cwd=tmp_index_dir, stderr=FNULL, stdout=FNULL)
|
|
113 returncode = proc.wait()
|
|
114 tmp_stderr.close()
|
|
115 FNULL.close()
|
|
116 sys.stdout.write(cmd1 + "\n")
|
|
117 except Exception as e:
|
|
118 # clean up temp dir
|
|
119 if os.path.exists(tmp_index_dir):
|
|
120 shutil.rmtree(tmp_index_dir)
|
|
121 stop_err('Error indexing reference sequence\n' + str(e))
|
|
122 # no Cleaning if no Exception, tmp_index_dir has to be cleaned after
|
|
123 # bowtie_alignment()
|
|
124 # bowtie fashion path without extention
|
|
125 index_full_path = os.path.join(tmp_index_dir, ref_file_name)
|
|
126 return tmp_index_dir, index_full_path
|
|
127
|
|
128
|
|
129 def bowtie_alignment(command_line, flyPreIndexed=''):
|
|
130 # make temp directory just for stderr
|
|
131 tmp_index_dir = tempfile.mkdtemp()
|
|
132 tmp = tempfile.NamedTemporaryFile(dir=tmp_index_dir).name
|
|
133 tmp_stderr = open(tmp, 'wb')
|
|
134 # conditional statement for sorted bam generation viewable in Trackster
|
|
135 if "samtools" in command_line:
|
|
136 # recover the final output file name
|
|
137 target_file = command_line.split()[-1]
|
|
138 path_to_unsortedBam = os.path.join(tmp_index_dir, "unsorted.bam")
|
|
139 path_to_sortedBam = os.path.join(tmp_index_dir, "unsorted.bam.sorted")
|
|
140 first_command_line = " ".join(
|
|
141 command_line.split()[:-3]) + " -o " + path_to_unsortedBam + " - "
|
|
142 # example: bowtie -v 0 -M 1 --best --strata -p 12 --suppress 6,7,8 -S
|
|
143 # /home/galaxy/galaxy-dist/bowtie/Dmel/dmel-all-chromosome-r5.49 -f
|
|
144 # /home/galaxy/galaxy-dist/database/files/003/dataset_3460.dat
|
|
145 # |samtools view -bS -o /tmp/tmp_PgMT0/unsorted.bam -
|
|
146 # generates an "unsorted.bam.sorted.bam file", NOT an
|
|
147 # "unsorted.bam.sorted" file
|
|
148 second_command_line = "samtools sort %s %s" % (
|
|
149 path_to_unsortedBam, path_to_sortedBam)
|
|
150 # fileno() method return the file descriptor number of tmp_stderr
|
|
151 p = subprocess.Popen(
|
|
152 args=first_command_line, cwd=tmp_index_dir, shell=True, stderr=tmp_stderr.fileno())
|
|
153 returncode = p.wait()
|
|
154 sys.stdout.write("%s\n" % first_command_line + str(returncode))
|
|
155 p = subprocess.Popen(
|
|
156 args=second_command_line, cwd=tmp_index_dir, shell=True, stderr=tmp_stderr.fileno())
|
|
157 returncode = p.wait()
|
|
158 sys.stdout.write("\n%s\n" % second_command_line + str(returncode))
|
|
159 if os.path.isfile(path_to_sortedBam + ".bam"):
|
|
160 shutil.copy2(path_to_sortedBam + ".bam", target_file)
|
|
161 else:
|
|
162 p = subprocess.Popen(
|
|
163 args=command_line, shell=True, stderr=tmp_stderr.fileno())
|
|
164 returncode = p.wait()
|
|
165 sys.stdout.write(command_line + "\n")
|
|
166 tmp_stderr.close()
|
|
167 # cleaning if the index was created in the fly
|
|
168 if os.path.exists(flyPreIndexed):
|
|
169 shutil.rmtree(flyPreIndexed)
|
|
170 # cleaning tmp files and directories
|
|
171 if os.path.exists(tmp_index_dir):
|
|
172 shutil.rmtree(tmp_index_dir)
|
|
173 return
|
|
174
|
|
175
|
|
176 def __main__():
|
|
177 args = Parser()
|
|
178 F = open(args.output, "w")
|
|
179 if args.index_from == "history":
|
|
180 tmp_dir, index_path = bowtie_squash(args.index_source)
|
|
181 else:
|
|
182 tmp_dir, index_path = "dummy/dymmy", args.index_source
|
|
183 command_line = bowtieCommandLiner(args.method, args.v_mismatches, args.output_format,
|
|
184 args.aligned, args.unaligned, args.input_format, args.input,
|
|
185 index_path, args.output, args.num_threads)
|
|
186 bowtie_alignment(command_line, flyPreIndexed=tmp_dir)
|
|
187 F.close()
|
|
188 if __name__ == "__main__":
|
|
189 __main__()
|