Mercurial > repos > drosofff > ged_bowtie
comparison sRbowtie_wrapper.py @ 1:eed2a141eb0c draft
Uploaded
author | drosofff |
---|---|
date | Sun, 11 May 2014 18:18:25 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:05ae11c21834 | 1:eed2a141eb0c |
---|---|
1 #!/usr/bin/env python | |
2 # generic bowtie wrapper for bowtie, small RNA oriented | |
3 # version 1 17-1-2014 | |
4 # Usage sRbowtie_wrapper.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> | |
5 | |
6 import sys, re, os, subprocess, shlex, tempfile, shutil | |
7 | |
8 def stop_err( msg ): | |
9 sys.stderr.write( '%s\n' % msg ) | |
10 sys.exit() | |
11 | |
12 def bowtieCommandLiner (alignment_method, v_mis, out_type, aligned, unaligned, input, index, output): | |
13 if alignment_method=="RNA": | |
14 x = "-v %s -M 1 --best --strata -p 12 --norc --suppress 2,6,7,8" % v_mis | |
15 elif alignment_method=="unique": | |
16 x = "-v %s -m 1 -p 12 --suppress 6,7,8" % v_mis | |
17 elif alignment_method=="multiple": | |
18 x = "-v %s -M 1 --best --strata -p 12 --suppress 6,7,8" % v_mis | |
19 elif alignment_method=="k_option": | |
20 x = "-v %s -k 1 --best -p 12 --suppress 6,7,8" % v_mis | |
21 elif alignment_method=="n_option": | |
22 x = "-n %s -M 1 --best -p 12 --suppress 6,7,8" % v_mis | |
23 elif alignment_method=="a_option": | |
24 x = "-v %s -a --best -p 12 --suppress 6,7,8" % v_mis | |
25 if aligned == "None" and unaligned == "None": fasta_command = "" | |
26 elif aligned != "None" and unaligned == "None": fasta_command= " --al %s" % aligned | |
27 elif aligned == "None" and unaligned != "None": fasta_command = " --un %s" % unaligned | |
28 else: fasta_command = " --al %s --un %s" % (aligned, unaligned) | |
29 x = x + fasta_command | |
30 if out_type == "tabular": | |
31 return "bowtie %s %s -f %s > %s" % (x, index, input, output) | |
32 elif out_type=="sam": | |
33 return "bowtie %s -S %s -f %s > %s" % (x, index, input, output) | |
34 elif out_type=="bam": | |
35 return "bowtie %s -S %s -f %s |samtools view -bS - > %s" % (x, index, input, output) | |
36 | |
37 def bowtie_squash(fasta): | |
38 # make temp directory for placement of indices and copy reference file there if necessary | |
39 tmp_index_dir = tempfile.mkdtemp() | |
40 ref_file = tempfile.NamedTemporaryFile( dir=tmp_index_dir ) | |
41 ref_file_name = ref_file.name | |
42 ref_file.close() # by default, this action delete the temporary file ! | |
43 os.symlink( fasta, ref_file_name ) # now there is a symlink between the fasta source file and the deleted ref_file name | |
44 cmd1 = 'bowtie-build -f %s %s' % (ref_file_name, ref_file_name ) # this will work with the bowtie command line but we have to change the working dir ! | |
45 try: | |
46 FNULL = open(os.devnull, 'w') | |
47 tmp = tempfile.NamedTemporaryFile( dir=tmp_index_dir ).name # a full path temp file to tmp_index_dir is created. just a string | |
48 tmp_stderr = open( tmp, 'wb' ) # creates and open the file handler | |
49 proc = subprocess.Popen( args=cmd1, shell=True, cwd=tmp_index_dir, stderr=FNULL, stdout=FNULL ) # fileno() method return the file descriptor number of tmp_stderr / stderr=tmp_stderr.fileno() | |
50 # here I played a while a finish by redirecting everythin in dev/null. Clean later tmp_stderr calls | |
51 returncode = proc.wait() | |
52 tmp_stderr.close() | |
53 FNULL.close() | |
54 sys.stdout.write(cmd1 + "\n") | |
55 except Exception, e: | |
56 # clean up temp dir | |
57 if os.path.exists( tmp_index_dir ): | |
58 shutil.rmtree( tmp_index_dir ) | |
59 stop_err( 'Error indexing reference sequence\n' + str( e ) ) | |
60 # no Cleaning if no Exception, to be cleaned later after bowtie alignment | |
61 index_full_path = os.path.join(tmp_index_dir, ref_file_name) # bowtie fashion path (without extention) ... | |
62 return tmp_index_dir, index_full_path | |
63 | |
64 def bowtie_alignment(command_line, flyPreIndexed=''): | |
65 # make temp directory just for stderr | |
66 tmp_index_dir = tempfile.mkdtemp() | |
67 tmp = tempfile.NamedTemporaryFile( dir=tmp_index_dir ).name | |
68 tmp_stderr = open( tmp, 'wb' ) | |
69 # conditional statement for sorted bam generation viewable in Trackster | |
70 if "samtools" in command_line: | |
71 target_file = command_line.split()[-1] # recover the final output file name | |
72 path_to_unsortedBam = os.path.join(tmp_index_dir, "unsorted.bam") | |
73 path_to_sortedBam = os.path.join(tmp_index_dir, "unsorted.bam.sorted") | |
74 first_command_line = " ".join(command_line.split()[:-3]) + " -o " + path_to_unsortedBam + " - " # 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 - | |
75 second_command_line = "samtools sort %s %s" % (path_to_unsortedBam, path_to_sortedBam) # Be carreful : this indeed will generate an unsorted.bam.sorted.bam file, NOT a unsorted.bam.sorted file | |
76 p = subprocess.Popen(args=first_command_line, cwd=tmp_index_dir, shell=True, stderr=tmp_stderr.fileno()) | |
77 returncode = p.wait() | |
78 sys.stdout.write("%s\n" % first_command_line + str(returncode)) | |
79 p = subprocess.Popen(args=second_command_line, cwd=tmp_index_dir, shell=True, stderr=tmp_stderr.fileno()) | |
80 returncode = p.wait() | |
81 sys.stdout.write("\n%s\n" % second_command_line + str(returncode)) | |
82 if os.path.isfile(path_to_sortedBam + ".bam"): | |
83 shutil.copy2(path_to_sortedBam + ".bam", target_file) | |
84 else: | |
85 p = subprocess.Popen(args=command_line, shell=True, stderr=tmp_stderr.fileno()) | |
86 returncode = p.wait() | |
87 sys.stdout.write(command_line + "\n") | |
88 tmp_stderr.close() | |
89 ## cleaning if the index was created in the fly | |
90 if os.path.exists( flyPreIndexed ): | |
91 shutil.rmtree( flyPreIndexed ) | |
92 # cleaning tmp files and directories | |
93 if os.path.exists( tmp_index_dir ): | |
94 shutil.rmtree( tmp_index_dir ) | |
95 return | |
96 | |
97 def __main__(): | |
98 F = open (sys.argv[-3], "w") | |
99 if sys.argv[5] == "history": | |
100 tmp_dir, index_path = bowtie_squash(sys.argv[6]) | |
101 else: | |
102 tmp_dir, index_path = "dummy/dymmy", sys.argv[6] | |
103 command_line = bowtieCommandLiner(sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[-2], sys.argv[-1], sys.argv[1], index_path, sys.argv[7]) | |
104 bowtie_alignment(command_line, flyPreIndexed=tmp_dir) | |
105 F.close() | |
106 if __name__=="__main__": __main__() |