annotate SMART/DiffExpAnal/compareOverlapping_parallel.py @ 34:529e3e6a0954

Deleted selected files
author m-zytnicki
date Tue, 30 Apr 2013 14:35:27 -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 #This program is a wrapp for CompareOverlapping.py.
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
3 import optparse, os, sys, subprocess, tempfile, shutil, tarfile, glob
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
4 import os, struct, time, random
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
5 from optparse import OptionParser
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
6 from commons.core.parsing.ParserChooser import ParserChooser
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
7 from commons.core.writer.Gff3Writer import Gff3Writer
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
8 from SMART.Java.Python.CompareOverlapping import CompareOverlapping
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
9 from SMART.Java.Python.structure.Transcript import Transcript
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
10 from SMART.Java.Python.structure.Interval import Interval
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
11 from SMART.Java.Python.ncList.NCList import NCList
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
12 from SMART.Java.Python.ncList.NCListCursor import NCListCursor
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
13 from SMART.Java.Python.ncList.NCListFilePickle import NCListFilePickle, NCListFileUnpickle
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
14 from SMART.Java.Python.ncList.FileSorter import FileSorter
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
15 from SMART.Java.Python.misc.Progress import Progress
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
16 from SMART.Java.Python.misc.UnlimitedProgress import UnlimitedProgress
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
17 from SMART.Java.Python.misc import Utils
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
18
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
19
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
20
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
21 def stop_err( msg ):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
22 sys.stderr.write( "%s\n" % msg )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
23 sys.exit()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
24
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
25 def toTar(tarFileName, overlapOutputNames):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
26 dir = os.path.dirname(tarFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
27 tfile = tarfile.open(tarFileName + ".tmp.tar", "w")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
28 currentPath = os.getcwd()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
29 os.chdir(dir)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
30 for file in overlapOutputNames:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
31 relativeFileName = os.path.basename(file)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
32 tfile.add(relativeFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
33 os.system("mv %s %s" % (tarFileName + ".tmp.tar", tarFileName))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
34 tfile.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
35 os.chdir(currentPath)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
36
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
37 def __main__():
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
38 description = "Compare Overlapping wrapp script: Get the a list of data which overlap with a reference set. [Category: Data Comparison]"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
39 parser = OptionParser(description = description)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
40 parser.add_option("-i", "--input1", dest="inputFileName1", action="store", type="string", help="input file 1 (for annotation) [compulsory] [format: file in transcript format given by -f]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
41 parser.add_option("-f", "--format1", dest="format1", action="store", type="string", help="format of file 1 [compulsory] [format: transcript file format]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
42 parser.add_option("", "--inputTxt", dest="inputTxt", action="store", type="string", help="input, a txt file for a list of input reads files. Should identify all reads files format, given by -g [compulsory]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
43 #parser.add_option("-j", "--input2", dest="inputFileName2", action="store", default="inputRead", type="string", help="input file 2 [compulsory] [format: file in transcript format given by -g]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
44 parser.add_option("-g", "--format2", dest="format2", action="store", type="string", help="format of file 2 [compulsory] [format: transcript file format]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
45 #parser.add_option("-o", "--output", dest="output", action="store", default=None, type="string", help="output file [compulsory] [format: output file in GFF3 format]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
46 parser.add_option("-S", "--start1", dest="start1", action="store", default=None, type="int", help="only consider the n first nucleotides of the transcripts in file 1 (do not use it with -U) [format: int]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
47 parser.add_option("-s", "--start2", dest="start2", action="store", default=None, type="int", help="only consider the n first nucleotides of the transcripts in file 2 (do not use it with -u) [format: int]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
48 parser.add_option("-U", "--end1", dest="end1", action="store", default=None, type="int", help="only consider the n last nucleotides of the transcripts in file 1 (do not use it with -S) [format: int]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
49 parser.add_option("-u", "--end2", dest="end2", action="store", default=None, type="int", help="only consider the n last nucleotides of the transcripts in file 2 (do not use it with -s) [format: int]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
50 parser.add_option("-t", "--intron", dest="introns", action="store_true", default=False, help="also report introns [format: bool] [default: false]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
51 parser.add_option("-E", "--5primeExtension1", dest="fivePrime1", action="store", default=None, type="int", help="extension towards 5' in file 1 [format: int]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
52 parser.add_option("-e", "--5primeExtension2", dest="fivePrime2", action="store", default=None, type="int", help="extension towards 5' in file 2 [format: int]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
53 parser.add_option("-N", "--3primeExtension1", dest="threePrime1", action="store", default=None, type="int", help="extension towards 3' in file 1 [format: int]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
54 parser.add_option("-n", "--3primeExtension2", dest="threePrime2", action="store", default=None, type="int", help="extension towards 3' in file 2 [format: int]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
55 parser.add_option("-c", "--colinear", dest="colinear", action="store_true", default=False, help="colinear only [format: bool] [default: false]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
56 parser.add_option("-a", "--antisense", dest="antisense", action="store_true", default=False, help="antisense only [format: bool] [default: false]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
57 parser.add_option("-d", "--distance", dest="distance", action="store", default=None, type="int", help="accept some distance between query and reference [format: int]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
58 parser.add_option("-k", "--included", dest="included", action="store_true", default=False, help="keep only elements from file 1 which are included in an element of file 2 [format: bool] [default: false]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
59 parser.add_option("-K", "--including", dest="including", action="store_true", default=False, help="keep only elements from file 2 which are included in an element of file 1 [format: bool] [default: false]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
60 parser.add_option("-m", "--minOverlap", dest="minOverlap", action="store", default=None, type="int", help="minimum number of nucleotides overlapping to declare an overlap [format: int] [default: 1]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
61 parser.add_option("-p", "--pcOverlap", dest="pcOverlap", action="store", default=None, type="int", help="minimum percentage of nucleotides to overlap to declare an overlap [format: int]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
62 parser.add_option("-O", "--notOverlapping", dest="notOverlapping", action="store_true", default=False, help="also output not overlapping data [format: bool] [default: false]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
63 parser.add_option("-x", "--exclude", dest="exclude", action="store_true", default=False, help="invert the match [format: bool] [default: false]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
64 parser.add_option("-v", "--verbosity", dest="verbosity", action="store", default=1, type="int", help="trace level [format: int]")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
65 parser.add_option('', '--tar', dest='outputTar', default=None, help='output all SAM results in a tar file.' )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
66 parser.add_option( '', '--outTxt', dest='outTxtFile', help='The output list of results files on txt format.[compulsory]' )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
67 (options, args) = parser.parse_args()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
68
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
69
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
70 #Parse the input txt file and read a list of BAM files.
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
71 file = open(options.inputTxt, "r")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
72 lines = file.readlines()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
73 inputFileNames = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
74 overlapOutputNames = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
75 outputName = options.outTxtFile
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
76 resDirName = os.path.dirname(outputName) + "/"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
77 #Write output txt file and define all output sam file names.
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
78 out = open(outputName, "w")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
79 for line in lines:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
80 tab = line.split()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
81 inputFileNames.append(tab[1])
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
82 overlapOutName = resDirName + tab[0] + '_overlapOut_%s.gff3' % random.randrange(0, 10000)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
83 overlapOutputNames.append(overlapOutName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
84 out.write(tab[0] + '\t' + overlapOutName + '\n')
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
85 file.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
86 out.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
87
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
88 #construction the commandes for each input file
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
89 cmds = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
90 for i in range(len(inputFileNames)):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
91 absFile = sys.argv[0]
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
92 absDir = os.path.dirname(absFile)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
93 parentDir = os.path.abspath(os.path.join(absDir, os.path.pardir))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
94 cmd = "python %s/Java/Python/CompareOverlappingSmallQuery.py " % parentDir
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
95 opts = "-i %s -f %s -j %s -g %s -o %s " % (options.inputFileName1, options.format1, inputFileNames[i], options.format2, overlapOutputNames[i])
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
96 #if options.start1 != None:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
97 # opts += "-S %s " % options.start1
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
98 #if options.start2 != None:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
99 # opts += "-s %s " % options.start2
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
100 #if options.end1 != None:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
101 # opts += "-U %s " % options.end1
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
102 #if options.end2 != None:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
103 # opts += "-u %s " % options.end2
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
104 #if options.fivePrime1 != None:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
105 # opts += "-E %s " % options.fivePrime1
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
106 #if options.fivePrime2 != None:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
107 # opts += "-e %s " % options.fivePrime2
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
108 #if options.threePrime1 != None:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
109 # opts += "-N %s " % options.threePrime1
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
110 #if options.threePrime2 != None:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
111 # opts += "-n %s " % options.threePrime2
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
112 #if options.colinear:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
113 # opts += "-c "
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
114 #if options.antisense:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
115 # opts +="-a "
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
116 #if options.included:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
117 # opts += "-k "
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
118 #if options.including:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
119 # opts += "-K "
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
120 #if options.pcOverlap != None:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
121 # opts += "-p %s " % options.pcOverlap
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
122 if options.notOverlapping:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
123 opts += "-O "
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
124 if options.exclude:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
125 opts += "-x "
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
126 if options.distance != None:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
127 opts += "-d %s " % options.distance
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
128 #if options.minOverlap != None:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
129 # opts += "-m %s " % options.minOverlap
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
130 cmd += opts
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
131 cmds.append(cmd)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
132
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
133
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
134 print "les commandes sont %s \n" % cmds
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
135
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
136 tmp_files = []
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
137 for i in range(len(cmds)):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
138 try:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
139 tmp_out = tempfile.NamedTemporaryFile().name
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
140 tmp_files.append(tmp_out)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
141 tmp_stdout = open( tmp_out, 'wb' )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
142 tmp_err = tempfile.NamedTemporaryFile().name
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
143 tmp_files.append(tmp_err)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
144 tmp_stderr = open( tmp_err, 'wb' )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
145 proc = subprocess.Popen( args=cmds[i], shell=True, cwd=".", stdout=tmp_stdout, stderr=tmp_stderr )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
146 returncode = proc.wait()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
147 tmp_stderr.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
148 # get stderr, allowing for case where it's very large
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
149 tmp_stderr = open( tmp_err, 'rb' )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
150 stderr = ''
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
151 buffsize = 1048576
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
152 try:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
153 while True:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
154 stderr += tmp_stderr.read( buffsize )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
155 if not stderr or len( stderr ) % buffsize != 0:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
156 break
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
157 except OverflowError:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
158 pass
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
159 tmp_stdout.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
160 tmp_stderr.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
161 if returncode != 0:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
162 raise Exception, stderr
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
163 except Exception, e:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
164 stop_err( 'Error in :\n' + str( e ) )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
165
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
166 if options.outputTar != None:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
167 toTar(options.outputTar, overlapOutputNames)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
168
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
169 for tmp_file in tmp_files:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
170 os.remove(tmp_file)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
171
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
172
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
173 if __name__=="__main__": __main__()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
174
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
175