annotate sRbowtie.py @ 0:64064dccdb11 draft

Imported from capsule None
author drosofff
date Mon, 03 Nov 2014 10:24:38 -0500
parents
children b50d7228b678
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
1 #!/usr/bin/env python
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
2 # small RNA oriented bowtie wrapper
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
3 # version 1.5 17-7-2014: arg parser implementation
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
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}>
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
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
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
6 # Christophe Antoniewski <drosofff@gmail.com>
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
7
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
8 import sys, os, subprocess, tempfile, shutil, argparse
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
9
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
10 def Parser():
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
11 the_parser = argparse.ArgumentParser(description="bowtie wrapper for small fasta reads")
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
12 the_parser.add_argument('--input', action="store", type=str, help="input fasta file")
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
13 the_parser.add_argument('--method', action="store", type=str, help="RNA, unique, multiple, k_option, n_option, a_option")
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
14 the_parser.add_argument('--v-mismatches', dest="v_mismatches", action="store", type=str, help="number of mismatches allowed for the alignments")
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
15 the_parser.add_argument('--output-format', dest="output_format", action="store", type=str, help="tabular, sam, bam")
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
16 the_parser.add_argument('--output', action="store", type=str, help="output file path")
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
17 the_parser.add_argument('--index-from', dest="index_from", action="store", type=str, help="indexed or history")
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
18 the_parser.add_argument('--index-source', dest="index_source", action="store", type=str, help="file path to the index source")
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
19 the_parser.add_argument('--aligned', action="store", type=str, help="aligned read file path, maybe None")
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
20 the_parser.add_argument('--unaligned', action="store", type=str, help="unaligned read file path, maybe None")
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
21 the_parser.add_argument('--num-threads', dest="num_threads", action="store", type=str, help="number of bowtie threads")
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
22 args = the_parser.parse_args()
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
23 return args
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
24
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
25 def stop_err( msg ):
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
26 sys.stderr.write( '%s\n' % msg )
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
27 sys.exit()
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
28
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
29 def bowtieCommandLiner (alignment_method="RNA", v_mis="1", out_type="tabular", aligned="None", unaligned="None", input="path", index="path", output="path", pslots="4"):
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
30 if alignment_method=="RNA":
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
31 x = "-v %s -M 1 --best --strata -p %s --norc --suppress 6,7,8" % (v_mis, pslots)
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
32 elif alignment_method=="unique":
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
33 x = "-v %s -m 1 -p %s --suppress 6,7,8" % (v_mis, pslots)
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
34 elif alignment_method=="multiple":
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
35 x = "-v %s -M 1 --best --strata -p %s --suppress 6,7,8" % (v_mis, pslots)
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
36 elif alignment_method=="k_option":
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
37 x = "-v %s -k 1 --best -p %s --suppress 6,7,8" % (v_mis, pslots)
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
38 elif alignment_method=="n_option":
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
39 x = "-n %s -M 1 --best -p %s --suppress 6,7,8" % (v_mis, pslots)
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
40 elif alignment_method=="a_option":
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
41 x = "-v %s -a --best -p %s --suppress 6,7,8" % (v_mis, pslots)
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
42 if aligned == "None" and unaligned == "None": fasta_command = ""
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
43 elif aligned != "None" and unaligned == "None": fasta_command= " --al %s" % aligned
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
44 elif aligned == "None" and unaligned != "None": fasta_command = " --un %s" % unaligned
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
45 else: fasta_command = " --al %s --un %s" % (aligned, unaligned)
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
46 x = x + fasta_command
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
47 if out_type == "tabular":
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
48 return "bowtie %s %s -f %s > %s" % (x, index, input, output)
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
49 elif out_type=="sam":
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
50 return "bowtie %s -S %s -f %s > %s" % (x, index, input, output)
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
51 elif out_type=="bam":
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
52 return "bowtie %s -S %s -f %s |samtools view -bS - > %s" % (x, index, input, output)
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
53
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
54 def bowtie_squash(fasta):
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
55 tmp_index_dir = tempfile.mkdtemp() # make temp directory for bowtie indexes
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
56 ref_file = tempfile.NamedTemporaryFile( dir=tmp_index_dir )
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
57 ref_file_name = ref_file.name
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
58 ref_file.close() # by default, delete the temporary file, but ref_file.name is now stored in ref_file_name
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
59 os.symlink( fasta, ref_file_name ) # symlink between the fasta source file and the deleted ref_file name
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
60 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)
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
61 try:
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
62 FNULL = open(os.devnull, 'w')
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
63 tmp = tempfile.NamedTemporaryFile( dir=tmp_index_dir ).name # a path string for a temp file in tmp_index_dir. Just a string
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
64 tmp_stderr = open( tmp, 'wb' ) # creates and open a file handler pointing to the temp file
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
65 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
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
66 returncode = proc.wait()
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
67 tmp_stderr.close()
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
68 FNULL.close()
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
69 sys.stdout.write(cmd1 + "\n")
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
70 except Exception, e:
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
71 # clean up temp dir
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
72 if os.path.exists( tmp_index_dir ):
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
73 shutil.rmtree( tmp_index_dir )
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
74 stop_err( 'Error indexing reference sequence\n' + str( e ) )
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
75 # no Cleaning if no Exception, tmp_index_dir has to be cleaned after bowtie_alignment()
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
76 index_full_path = os.path.join(tmp_index_dir, ref_file_name) # bowtie fashion path without extention
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
77 return tmp_index_dir, index_full_path
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
78
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
79 def bowtie_alignment(command_line, flyPreIndexed=''):
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
80 # make temp directory just for stderr
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
81 tmp_index_dir = tempfile.mkdtemp()
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
82 tmp = tempfile.NamedTemporaryFile( dir=tmp_index_dir ).name
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
83 tmp_stderr = open( tmp, 'wb' )
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
84 # conditional statement for sorted bam generation viewable in Trackster
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
85 if "samtools" in command_line:
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
86 target_file = command_line.split()[-1] # recover the final output file name
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
87 path_to_unsortedBam = os.path.join(tmp_index_dir, "unsorted.bam")
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
88 path_to_sortedBam = os.path.join(tmp_index_dir, "unsorted.bam.sorted")
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
89 first_command_line = " ".join(command_line.split()[:-3]) + " -o " + path_to_unsortedBam + " - "
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
90 # 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 -
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
91 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
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
92 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
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
93 returncode = p.wait()
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
94 sys.stdout.write("%s\n" % first_command_line + str(returncode))
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
95 p = subprocess.Popen(args=second_command_line, cwd=tmp_index_dir, shell=True, stderr=tmp_stderr.fileno())
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
96 returncode = p.wait()
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
97 sys.stdout.write("\n%s\n" % second_command_line + str(returncode))
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
98 if os.path.isfile(path_to_sortedBam + ".bam"):
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
99 shutil.copy2(path_to_sortedBam + ".bam", target_file)
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
100 else:
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
101 p = subprocess.Popen(args=command_line, shell=True, stderr=tmp_stderr.fileno())
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
102 returncode = p.wait()
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
103 sys.stdout.write(command_line + "\n")
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
104 tmp_stderr.close()
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
105 ## cleaning if the index was created in the fly
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
106 if os.path.exists( flyPreIndexed ):
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
107 shutil.rmtree( flyPreIndexed )
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
108 # cleaning tmp files and directories
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
109 if os.path.exists( tmp_index_dir ):
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
110 shutil.rmtree( tmp_index_dir )
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
111 return
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
112
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
113 def __main__():
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
114 args = Parser()
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
115 F = open (args.output, "w")
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
116 if args.index_from == "history":
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
117 tmp_dir, index_path = bowtie_squash(args.index_source)
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
118 else:
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
119 tmp_dir, index_path = "dummy/dymmy", args.index_source
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
120 command_line = bowtieCommandLiner(args.method, args.v_mismatches, args.output_format, args.aligned, args.unaligned, args.input, index_path, args.output, args.num_threads)
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
121 bowtie_alignment(command_line, flyPreIndexed=tmp_dir)
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
122 F.close()
64064dccdb11 Imported from capsule None
drosofff
parents:
diff changeset
123 if __name__=="__main__": __main__()