annotate SMART/DiffExpAnal/bam_to_sam_parallel_unSQL.py @ 31:0ab839023fe4

Uploaded
author m-zytnicki
date Tue, 30 Apr 2013 14:33:21 -0400
parents 94ab73e8a190
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
1 #!/usr/bin/env python
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
2 """
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
3 Converts BAM data to sorted SAM data.
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
4 usage: bam_to_sam.py [options]
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
5 --input1: SAM file to be converted
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
6 --output1: output dataset in bam format
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
7 """
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
8
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
9 import optparse, os, sys, subprocess, tempfile, shutil, tarfile, random
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
10 from commons.core.launcher.Launcher import Launcher
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
11 from commons.core.sql.TableJobAdaptatorFactory import TableJobAdaptatorFactory
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
12 #from galaxy import eggs
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
13 #import pkg_resources; pkg_resources.require( "bx-python" )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
14 #from bx.cookbook import doc_optparse
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
15 #from galaxy import util
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
16
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
17 def stop_err( msg ):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
18 sys.stderr.write( '%s\n' % msg )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
19 sys.exit()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
20
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
21 def toTar(tarFileName, samOutputNames):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
22 dir = os.path.dirname(tarFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
23 tfile = tarfile.open(tarFileName + ".tmp.tar", "w")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
24 currentPath = os.getcwd()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
25 os.chdir(dir)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
26 for file in samOutputNames:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
27 relativeFileName = os.path.basename(file)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
28 tfile.add(relativeFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
29 os.system("mv %s %s" % (tarFileName + ".tmp.tar", tarFileName))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
30 tfile.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
31 os.chdir(currentPath)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
32
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
33 def _map(iLauncher, cmd, cmdStart, cmdFinish ):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
34 lCmds = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
35 lCmds.extend(cmd)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
36 lCmdStart = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
37 lCmdStart.extend(cmdStart)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
38 lCmdFinish = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
39 lCmdFinish.extend(cmdFinish)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
40 return(iLauncher.prepareCommands_withoutIndentation(lCmds, lCmdStart, lCmdFinish))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
41
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
42 def _createSamToolsViewCmd(iLauncher, inputFile, tmp_sorted_aligns_file_name, header):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
43 lArgs = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
44 lArgs.append("-o %s" % inputFile)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
45 lArgs.append("%s" % tmp_sorted_aligns_file_name)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
46 if header:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
47 lArgs.append("-h")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
48 return iLauncher.getSystemCommand("samtools view", lArgs)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
49
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
50 def _createSamToolsSortCmd(iLauncher, inputFile, tmp_sorted_aligns_file_base):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
51 lArgs = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
52 lArgs.append("%s" % inputFile)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
53 lArgs.append("%s" % tmp_sorted_aligns_file_base)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
54 return iLauncher.getSystemCommand("samtools sort", lArgs)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
55
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
56 def __main__():
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
57 #Parse Command Line
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
58 parser = optparse.OptionParser()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
59 parser.add_option('-t', '--tar', dest='outputTar', default=None, help='output all SAM results in a tar file.' )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
60 parser.add_option( '', '--input1', dest='input1', help='The input list of BAM datasets on txt format.' )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
61 #parser.add_option( '', '--input1', dest='input1', help='The input BAM dataset' )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
62 parser.add_option( '', '--output1', dest='output1', help='The output list of SAM datasets on txt format.' )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
63 #parser.add_option( '', '--output1', dest='output1', help='The output SAM dataset' )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
64 parser.add_option( '', '--header', dest='header', action='store_true', default=False, help='Write SAM Header' )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
65 ( options, args ) = parser.parse_args()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
66
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
67
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
68 #Parse the input txt file and read a list of BAM files.
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
69 file = open(options.input1, "r")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
70 lines = file.readlines()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
71 inputFileNames = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
72 samOutputNames = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
73 outputName = options.output1
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
74 resDirName = os.path.dirname(outputName) + '/'
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
75 #Write output txt file and define all output sam file names.
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
76 out = open(outputName, "w")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
77 for line in lines:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
78 tab = line.split()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
79 inputFileNames.append(tab[1])
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
80 samOutName = resDirName + tab[0] + '_samOutput_%s.sam' % random.randrange(0, 10000)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
81 samOutputNames.append(samOutName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
82 out.write(tab[0] + '\t' + samOutName + '\n')
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
83 file.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
84 out.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
85
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
86 # output version # of tool
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
87 try:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
88 tmp_files = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
89 tmp = tempfile.NamedTemporaryFile().name
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
90 tmp_files.append(tmp)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
91 tmp_stdout = open( tmp, 'wb' )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
92 proc = subprocess.Popen( args='samtools 2>&1', shell=True, stdout=tmp_stdout )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
93 tmp_stdout.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
94 returncode = proc.wait()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
95 stdout = None
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
96 for line in open( tmp_stdout.name, 'rb' ):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
97 if line.lower().find( 'version' ) >= 0:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
98 stdout = line.strip()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
99 break
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
100 if stdout:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
101 sys.stdout.write( 'Samtools %s\n' % stdout )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
102 else:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
103 raise Exception
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
104 except:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
105 sys.stdout.write( 'Could not determine Samtools version\n' )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
106
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
107 tmp_dirs = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
108 acronym = "bam_to_sam"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
109 jobdb = TableJobAdaptatorFactory.createJobInstance()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
110 iLauncher = Launcher(jobdb, os.getcwd(), "", "", os.getcwd(), os.getcwd(), "jobs", "", acronym, acronym, False, True)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
111 lCmdsTuples = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
112 for i in range(len(inputFileNames)): #Construct the lines commands
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
113 if os.path.getsize( inputFileNames[i] ) == 0:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
114 raise Exception, 'Initial input txt file is empty.'
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
115 tmp_dir = tempfile.mkdtemp(dir="%s" % os.getcwd())
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
116 tmp_dirs.append(tmp_dir)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
117 tmp_sorted_aligns_file = tempfile.NamedTemporaryFile( dir=tmp_dir )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
118 tmp_sorted_aligns_file_base = tmp_sorted_aligns_file.name
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
119 tmp_sorted_aligns_file_name = '%s.bam' % tmp_sorted_aligns_file.name
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
120 tmp_files.append(tmp_sorted_aligns_file_name)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
121 tmp_sorted_aligns_file.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
122
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
123 inputFile = inputFileNames[i]
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
124 outputFile = samOutputNames[i]
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
125 cmd2Launch = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
126 cmd2Launch.append(_createSamToolsSortCmd(iLauncher, inputFile, tmp_sorted_aligns_file_base))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
127 cmd2Launch.append(_createSamToolsViewCmd(iLauncher, outputFile, tmp_sorted_aligns_file_name, options.header))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
128 cmdStart = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
129 cmdFinish = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
130 lCmdsTuples.append(_map(iLauncher, cmd2Launch, cmdStart, cmdFinish))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
131
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
132 iLauncher.runLauncherForMultipleJobs(acronym, lCmdsTuples, True)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
133
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
134 if options.outputTar != None:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
135 toTar(options.outputTar, samOutputNames)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
136 #clean up temp files
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
137 for tmp_dir in tmp_dirs:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
138 if os.path.exists( tmp_dir ):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
139 shutil.rmtree( tmp_dir )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
140 #print tmp_files
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
141 #for tmp in tmp_files:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
142 # os.remove(tmp)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
143
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
144
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
145 if __name__=="__main__": __main__()