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