Mercurial > repos > drosofff > bowtie_for_smallrna
comparison sRbowtie/sRbowtie.py @ 0:324c3142ca0f draft
Uploaded
author | drosofff |
---|---|
date | Mon, 19 May 2014 17:33:06 -0400 |
parents | |
children | 303baef87cb7 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:324c3142ca0f |
---|---|
1 #!/usr/bin/env python | |
2 # small RNA oriented bowtie wrapper | |
3 # version 1 19-5-2014 | |
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 # To Do: | |
6 # implement number of bowtie processes as a Galaxy env variable | |
7 # implement an arg parser | |
8 # Christophe Antoniewski <drosofff@gmail.com> | |
9 | |
10 import sys, os, subprocess, tempfile, shutil | |
11 | |
12 def stop_err( msg ): | |
13 sys.stderr.write( '%s\n' % msg ) | |
14 sys.exit() | |
15 | |
16 def bowtieCommandLiner (alignment_method, v_mis, out_type, aligned, unaligned, input, index, output, pslots="12"): | |
17 if alignment_method=="RNA": | |
18 x = "-v %s -M 1 --best --strata -p %s --norc --suppress 2,6,7,8" % (v_mis, pslots) | |
19 elif alignment_method=="unique": | |
20 x = "-v %s -m 1 -p %s --suppress 6,7,8" % (v_mis, pslots) | |
21 elif alignment_method=="multiple": | |
22 x = "-v %s -M 1 --best --strata -p %s --suppress 6,7,8" % (v_mis, pslots) | |
23 elif alignment_method=="k_option": | |
24 x = "-v %s -k 1 --best -p %s --suppress 6,7,8" % (v_mis, pslots) | |
25 elif alignment_method=="n_option": | |
26 x = "-n %s -M 1 --best -p %s --suppress 6,7,8" % (v_mis, pslots) | |
27 elif alignment_method=="a_option": | |
28 x = "-v %s -a --best -p %s --suppress 6,7,8" % (v_mis, pslots) | |
29 if aligned == "None" and unaligned == "None": fasta_command = "" | |
30 elif aligned != "None" and unaligned == "None": fasta_command= " --al %s" % aligned | |
31 elif aligned == "None" and unaligned != "None": fasta_command = " --un %s" % unaligned | |
32 else: fasta_command = " --al %s --un %s" % (aligned, unaligned) | |
33 x = x + fasta_command | |
34 if out_type == "tabular": | |
35 return "bowtie %s %s -f %s > %s" % (x, index, input, output) | |
36 elif out_type=="sam": | |
37 return "bowtie %s -S %s -f %s > %s" % (x, index, input, output) | |
38 elif out_type=="bam": | |
39 return "bowtie %s -S %s -f %s |samtools view -bS - > %s" % (x, index, input, output) | |
40 | |
41 def bowtie_squash(fasta): | |
42 tmp_index_dir = tempfile.mkdtemp() # make temp directory for bowtie indexes | |
43 ref_file = tempfile.NamedTemporaryFile( dir=tmp_index_dir ) | |
44 ref_file_name = ref_file.name | |
45 ref_file.close() # by default, delete the temporary file, but ref_file.name is now stored in ref_file_name | |
46 os.symlink( fasta, ref_file_name ) # symlink between the fasta source file and the deleted ref_file name | |
47 cmd1 = 'bowtie-build -f %s %s' % (ref_file_name, ref_file_name ) # bowtie command line, which will work after changing dir (cwd=tmp_index_dir) | |
48 try: | |
49 FNULL = open(os.devnull, 'w') | |
50 tmp = tempfile.NamedTemporaryFile( dir=tmp_index_dir ).name # a path string for a temp file in tmp_index_dir. Just a string | |
51 tmp_stderr = open( tmp, 'wb' ) # creates and open a file handler pointing to the temp file | |
52 proc = subprocess.Popen( args=cmd1, shell=True, cwd=tmp_index_dir, stderr=FNULL, stdout=FNULL ) # both stderr and stdout of bowtie-build are redirected in dev/null | |
53 returncode = proc.wait() | |
54 tmp_stderr.close() | |
55 FNULL.close() | |
56 sys.stdout.write(cmd1 + "\n") | |
57 except Exception, e: | |
58 # clean up temp dir | |
59 if os.path.exists( tmp_index_dir ): | |
60 shutil.rmtree( tmp_index_dir ) | |
61 stop_err( 'Error indexing reference sequence\n' + str( e ) ) | |
62 # no Cleaning if no Exception, tmp_index_dir has to be cleaned after bowtie_alignment() | |
63 index_full_path = os.path.join(tmp_index_dir, ref_file_name) # bowtie fashion path without extention | |
64 return tmp_index_dir, index_full_path | |
65 | |
66 def bowtie_alignment(command_line, flyPreIndexed=''): | |
67 # make temp directory just for stderr | |
68 tmp_index_dir = tempfile.mkdtemp() | |
69 tmp = tempfile.NamedTemporaryFile( dir=tmp_index_dir ).name | |
70 tmp_stderr = open( tmp, 'wb' ) | |
71 # conditional statement for sorted bam generation viewable in Trackster | |
72 if "samtools" in command_line: | |
73 target_file = command_line.split()[-1] # recover the final output file name | |
74 path_to_unsortedBam = os.path.join(tmp_index_dir, "unsorted.bam") | |
75 path_to_sortedBam = os.path.join(tmp_index_dir, "unsorted.bam.sorted") | |
76 first_command_line = " ".join(command_line.split()[:-3]) + " -o " + path_to_unsortedBam + " - " | |
77 # example: bowtie -v 0 -M 1 --best --strata -p 12 --suppress 6,7,8 -S /home/galaxy/galaxy-dist/bowtie/Dmel/dmel-all-chromosome-r5.49 -f /home/galaxy/galaxy-dist/database/files/003/dataset_3460.dat |samtools view -bS -o /tmp/tmp_PgMT0/unsorted.bam - | |
78 second_command_line = "samtools sort %s %s" % (path_to_unsortedBam, path_to_sortedBam) # generates an "unsorted.bam.sorted.bam file", NOT an "unsorted.bam.sorted" file | |
79 p = subprocess.Popen(args=first_command_line, cwd=tmp_index_dir, shell=True, stderr=tmp_stderr.fileno()) # fileno() method return the file descriptor number of tmp_stderr | |
80 returncode = p.wait() | |
81 sys.stdout.write("%s\n" % first_command_line + str(returncode)) | |
82 p = subprocess.Popen(args=second_command_line, cwd=tmp_index_dir, shell=True, stderr=tmp_stderr.fileno()) | |
83 returncode = p.wait() | |
84 sys.stdout.write("\n%s\n" % second_command_line + str(returncode)) | |
85 if os.path.isfile(path_to_sortedBam + ".bam"): | |
86 shutil.copy2(path_to_sortedBam + ".bam", target_file) | |
87 else: | |
88 p = subprocess.Popen(args=command_line, shell=True, stderr=tmp_stderr.fileno()) | |
89 returncode = p.wait() | |
90 sys.stdout.write(command_line + "\n") | |
91 tmp_stderr.close() | |
92 ## cleaning if the index was created in the fly | |
93 if os.path.exists( flyPreIndexed ): | |
94 shutil.rmtree( flyPreIndexed ) | |
95 # cleaning tmp files and directories | |
96 if os.path.exists( tmp_index_dir ): | |
97 shutil.rmtree( tmp_index_dir ) | |
98 return | |
99 | |
100 def __main__(): | |
101 F = open (sys.argv[7], "w") | |
102 if sys.argv[5] == "history": | |
103 tmp_dir, index_path = bowtie_squash(sys.argv[6]) | |
104 else: | |
105 tmp_dir, index_path = "dummy/dymmy", sys.argv[6] | |
106 command_line = bowtieCommandLiner(sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[8], sys.argv[9], sys.argv[1], index_path, sys.argv[7], sys.argv[10]) | |
107 bowtie_alignment(command_line, flyPreIndexed=tmp_dir) | |
108 F.close() | |
109 if __name__=="__main__": __main__() |