comparison sRbowtieCascade.py @ 1:4c7013211739 draft

Uploaded
author drosofff
date Sun, 22 Jun 2014 13:09:46 -0400
parents
children
comparison
equal deleted inserted replaced
0:6f25e1dac784 1:4c7013211739
1 #!/usr/bin/env python
2 # small RNA oriented bowtie wrapper in cascade for small RNA data set genome annotation
3 # version 0.9 13-6-2014
4 # Usage sRbowtie_cascade.py see Parser() for valid arguments
5 # Christophe Antoniewski <drosofff@gmail.com>
6
7 import sys, os, subprocess, tempfile, shutil, argparse
8 from collections import defaultdict
9
10 def Parser():
11 the_parser = argparse.ArgumentParser()
12 the_parser.add_argument('--output', action="store", type=str, help="output file")
13 the_parser.add_argument('--num-threads', dest="num_threads", action="store", type=str, help="number of bowtie threads")
14 the_parser.add_argument('--mismatch', action="store", type=str, help="number of mismatches allowed")
15 the_parser.add_argument('--indexing-flags', dest="indexing_flags", nargs='+', help="whether the index should be generated or not by bowtie-buid")
16 the_parser.add_argument('--index',nargs='+', help="paths to indexed or fasta references")
17 the_parser.add_argument('--indexName',nargs='+', help="Names of the indexes")
18 the_parser.add_argument('--input',nargs='+', help="paths to multiple input files")
19 the_parser.add_argument('--label',nargs='+', help="labels of multiple input files")
20 args = the_parser.parse_args()
21 return args
22
23 def stop_err( msg ):
24 sys.stderr.write( '%s\n' % msg )
25 sys.exit()
26
27 def bowtie_squash(fasta):
28 tmp_index_dir = tempfile.mkdtemp() # make temp directory for bowtie indexes
29 ref_file = tempfile.NamedTemporaryFile( dir=tmp_index_dir )
30 ref_file_name = ref_file.name
31 ref_file.close() # by default, delete the temporary file, but ref_file.name is now stored in ref_file_name
32 os.symlink( fasta, ref_file_name ) # symlink between the fasta source file and the deleted ref_file name
33 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)
34 try:
35 FNULL = open(os.devnull, 'w')
36 tmp = tempfile.NamedTemporaryFile( dir=tmp_index_dir ).name # a path string for a temp file in tmp_index_dir. Just a string
37 tmp_stderr = open( tmp, 'wb' ) # creates and open a file handler pointing to the temp file
38 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
39 returncode = proc.wait()
40 tmp_stderr.close()
41 FNULL.close()
42 sys.stdout.write(cmd1 + "\n")
43 except Exception, e:
44 # clean up temp dir
45 if os.path.exists( tmp_index_dir ):
46 shutil.rmtree( tmp_index_dir )
47 stop_err( 'Error indexing reference sequence\n' + str( e ) )
48 # no Cleaning if no Exception, tmp_index_dir has to be cleaned after bowtie_alignment()
49 index_full_path = os.path.join(tmp_index_dir, ref_file_name) # bowtie fashion path without extention
50 return index_full_path
51
52 def make_working_dir():
53 working_dir = tempfile.mkdtemp()
54 return working_dir
55
56 def Clean_TempDir(directory):
57 if os.path.exists( directory ):
58 shutil.rmtree( directory )
59 return
60
61 def bowtie_alignment(command_line="None", working_dir = ""):
62 FNULL = open(os.devnull, 'w')
63 p = subprocess.Popen(args=command_line, cwd=working_dir, shell=True, stderr=FNULL, stdout=FNULL)
64 returncode = p.wait()
65 sys.stdout.write("%s\n" % command_line)
66 FNULL.close()
67 #p = subprocess.Popen(["wc", "-l", "%s/al.fasta"%working_dir], cwd=working_dir, stdout=subprocess.PIPE)
68 #aligned = p.communicate()[0].split()[0]
69 aligned = 0
70 F = open ("%s/al.fasta" % working_dir, "r")
71 for line in F:
72 aligned += 1
73 F.close()
74 sys.stdout.write("Aligned: %s\n" % aligned)
75 return aligned/2
76
77 def CommandLiner (v_mis="1", pslots="12", index="dum/my", input="dum/my", working_dir=""):
78 return "bowtie -v %s -k 1 --best -p %s --al %s/al.fasta --un %s/unal.fasta --suppress 1,2,3,4,5,6,7,8 %s -f %s" % (v_mis, pslots, working_dir, working_dir, index, input)
79
80 def __main__():
81 args = Parser()
82 ## first we make all indexes available. They can be already available or be squashed by bowtie-build
83 ## we keep them in a list that alternates indexPath and "toClear" or "DoNotDelete"
84 BowtieIndexList = []
85 for indexing_flags, bowtiePath in zip (args.indexing_flags, args.index):
86 if indexing_flags == "history":
87 BowtieIndexList.append ( bowtie_squash (bowtiePath) )
88 BowtieIndexList.append ( "toClear" )
89 else:
90 BowtieIndexList.append ( bowtiePath )
91 BowtieIndexList.append ( "DoNotDelete")
92 ###### temporary Indexes are generated. They must be deleted at the end (after removing file name in the temp path)
93 ResultDict = defaultdict(list)
94 for label, input in zip(args.label, args.input): ## the main cascade, iterating over samples and bowtie indexes
95 workingDir = make_working_dir()
96 cmd = CommandLiner (v_mis=args.mismatch, pslots=args.num_threads, index=BowtieIndexList[0], input=input, working_dir=workingDir)
97 ResultDict[label].append( bowtie_alignment(command_line=cmd, working_dir = workingDir) ) # first step of the cascade
98 if len(BowtieIndexList) > 2: # is there a second step to perform ?
99 os.rename("%s/al.fasta"%workingDir, "%s/toAlign.fasta"%workingDir) ## end of first step. the aligned reads are the input of the next step
100 cmd = CommandLiner (v_mis=args.mismatch, pslots=args.num_threads, index=BowtieIndexList[2], input="%s/toAlign.fasta"%workingDir, working_dir=workingDir)
101 ResultDict[label].append( bowtie_alignment(command_line=cmd, working_dir = workingDir) )## second step of the cascade
102 if len(BowtieIndexList) > 4: ## remaining steps
103 for BowtieIndexPath in BowtieIndexList[4::2]:
104 os.rename("%s/unal.fasta"%workingDir, "%s/toAlign.fasta"%workingDir)
105 cmd = CommandLiner (v_mis=args.mismatch, pslots=args.num_threads, index=BowtieIndexPath, input="%s/toAlign.fasta"%workingDir, working_dir=workingDir)
106 ResultDict[label].append( bowtie_alignment(command_line=cmd, working_dir = workingDir) )
107 Fun = open("%s/unal.fasta"%workingDir, "r") ## to finish, compute the number of unmatched reads
108 n = 0
109 for line in Fun:
110 n += 1
111 ResultDict[label].append(n/2)
112 Fun.close()
113 Clean_TempDir (workingDir) # clean the sample working directory
114 ## cleaning
115 for IndexPath, IndexFlag in zip(BowtieIndexList[::2], BowtieIndexList[1::2]):
116 if IndexFlag == "toClear":
117 Clean_TempDir ("/".join(IndexPath.split("/")[:-1]))
118 ## end of cleaning
119
120
121
122 F = open (args.output, "w")
123 print >> F, "alignment reference\t%s" % "\t".join(args.label)
124 for i, reference in enumerate(args.indexName):
125 F.write ("%s" % reference)
126 for sample in args.label:
127 F.write ("\t%s" % "{:,}".format(ResultDict[sample][i]) )
128 print >> F
129 F.write ("Remaining Unmatched")
130 for sample in args.label:
131 F.write ("\t%s" % "{:,}".format(ResultDict[sample][-1]) )
132 print >> F
133
134 F.close()
135
136 if __name__=="__main__": __main__()