annotate SMART/Java/Python/clusterize.py @ 24:452e051f6562

Deleted selected files
author m-zytnicki
date Mon, 29 Apr 2013 03:28:24 -0400
parents 94ab73e8a190
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1 #! /usr/bin/env python
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
2 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
3 # Copyright INRA-URGI 2009-2010
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
4 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
5 # This software is governed by the CeCILL license under French law and
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
6 # abiding by the rules of distribution of free software. You can use,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
7 # modify and/ or redistribute the software under the terms of the CeCILL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
8 # license as circulated by CEA, CNRS and INRIA at the following URL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
9 # "http://www.cecill.info".
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
10 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
11 # As a counterpart to the access to the source code and rights to copy,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
12 # modify and redistribute granted by the license, users are provided only
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
13 # with a limited warranty and the software's author, the holder of the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
14 # economic rights, and the successive licensors have only limited
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
15 # liability.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
16 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
17 # In this respect, the user's attention is drawn to the risks associated
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
18 # with loading, using, modifying and/or developing or reproducing the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
19 # software by the user in light of its specific status of free software,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
20 # that may mean that it is complicated to manipulate, and that also
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
21 # therefore means that it is reserved for developers and experienced
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
22 # professionals having in-depth computer knowledge. Users are therefore
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
23 # encouraged to load and test the software's suitability as regards their
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
24 # requirements in conditions enabling the security of their systems and/or
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
25 # data to be ensured and, more generally, to use and operate it in the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
26 # same conditions as regards security.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
27 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
28 # The fact that you are presently reading this means that you have had
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
29 # knowledge of the CeCILL license and that you accept its terms.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
30 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
31 from commons.core.writer.WriterChooser import WriterChooser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 """Clusterize a set of transcripts"""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33
18
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
34 import os, os.path, random
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 from optparse import OptionParser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 from commons.core.parsing.ParserChooser import ParserChooser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 from commons.core.writer.Gff3Writer import Gff3Writer
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 from SMART.Java.Python.structure.Transcript import Transcript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 from SMART.Java.Python.ncList.NCListFilePickle import NCListFileUnpickle
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 from SMART.Java.Python.ncList.FileSorter import FileSorter
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 from SMART.Java.Python.misc.Progress import Progress
18
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
42 from SMART.Java.Python.misc.UnlimitedProgress import UnlimitedProgress
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44 class Clusterize(object):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45
18
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
46 def __init__(self, verbosity):
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
47 self.normalize = False
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
48 self.presorted = False
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
49 self.distance = 1
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
50 self.colinear = False
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
51 self.nbWritten = 0
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
52 self.nbMerges = 0
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
53 self.verbosity = verbosity
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
54 self.splittedFileNames = {}
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
55
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
56 def __del__(self):
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
57 for fileName in self.splittedFileNames.values():
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
58 os.remove(fileName)
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59
18
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
60 def setInputFile(self, fileName, format):
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
61 parserChooser = ParserChooser(self.verbosity)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
62 parserChooser.findFormat(format)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
63 self.parser = parserChooser.getParser(fileName)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
64 self.sortedFileName = "%s_sorted_%d.pkl" % (os.path.splitext(fileName)[0], random.randint(1, 100000))
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
65 if "SMARTTMPPATH" in os.environ:
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
66 self.sortedFileName = os.path.join(os.environ["SMARTTMPPATH"], os.path.basename(self.sortedFileName))
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67
18
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
68 def setOutputFileName(self, fileName, format="gff3", title="S-MART", feature="transcript", featurePart="exon"):
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
69 writerChooser = WriterChooser()
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
70 writerChooser.findFormat(format)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
71 self.writer = writerChooser.getWriter(fileName)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
72 self.writer.setTitle(title)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
73 self.writer.setFeature(feature)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
74 self.writer.setFeaturePart(featurePart)
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75
18
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
76 def setDistance(self, distance):
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
77 self.distance = distance
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78
18
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
79 def setColinear(self, colinear):
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
80 self.colinear = colinear
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81
18
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
82 def setNormalize(self, normalize):
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
83 self.normalize = normalize
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
84
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
85 def setPresorted(self, presorted):
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
86 self.presorted = presorted
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87
18
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
88 def _sortFile(self):
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
89 if self.presorted:
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
90 return
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
91 fs = FileSorter(self.parser, self.verbosity-4)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
92 fs.perChromosome(True)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
93 fs.setPresorted(self.presorted)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
94 fs.setOutputFileName(self.sortedFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
95 fs.sort()
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
96 self.splittedFileNames = fs.getOutputFileNames()
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
97 self.nbElementsPerChromosome = fs.getNbElementsPerChromosome()
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
98 self.nbElements = fs.getNbElements()
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
99
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
100 def _iterate(self, chromosome):
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
101 if chromosome == None:
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
102 progress = UnlimitedProgress(10000, "Reading input file", self.verbosity)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
103 parser = self.parser
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
104 else:
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
105 progress = Progress(self.nbElementsPerChromosome[chromosome], "Checking chromosome %s" % (chromosome), self.verbosity)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
106 parser = NCListFileUnpickle(self.splittedFileNames[chromosome], self.verbosity)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
107 transcripts = []
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
108 self.nbElements = 0
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
109 for newTranscript in parser.getIterator():
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
110 newTranscripts = []
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
111 if newTranscript.__class__.__name__ == "Mapping":
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
112 newTranscript = newTranscript.getTranscript()
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
113 for oldTranscript in transcripts:
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
114 if self._checkOverlap(newTranscript, oldTranscript):
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
115 self._merge(newTranscript, oldTranscript)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
116 elif self._checkPassed(newTranscript, oldTranscript):
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
117 self._write(oldTranscript)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
118 else:
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
119 newTranscripts.append(oldTranscript)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
120 newTranscripts.append(newTranscript)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
121 transcripts = newTranscripts
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
122 self.nbElements += 1
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
123 progress.inc()
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
124 for transcript in transcripts:
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
125 self._write(transcript)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
126 progress.done()
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127
18
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
128 def _merge(self, transcript1, transcript2):
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
129 self.nbMerges += 1
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
130 transcript2.setDirection(transcript1.getDirection())
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
131 transcript1.merge(transcript2)
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132
18
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
133 def _write(self, transcript):
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
134 self.nbWritten += 1
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
135 self.writer.addTranscript(transcript)
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136
18
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
137 def _checkOverlap(self, transcript1, transcript2):
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
138 if transcript1.getChromosome() != transcript2.getChromosome():
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
139 return False
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
140 if self.colinear and transcript1.getDirection() != transcript2.getDirection():
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
141 return False
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
142 if transcript1.getDistance(transcript2) > self.distance:
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
143 return False
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
144 return True
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145
18
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
146 def _checkPassed(self, transcript1, transcript2):
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
147 return ((transcript1.getChromosome() != transcript2.getChromosome()) or (transcript1.getDistance(transcript2) > self.distance))
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148
18
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
149 def run(self):
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
150 self._sortFile()
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
151 if self.presorted:
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
152 self._iterate(None)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
153 else:
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
154 for chromosome in sorted(self.splittedFileNames.keys()):
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
155 self._iterate(chromosome)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
156 self.writer.close()
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
157 if self.verbosity > 0:
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
158 print "# input: %d" % (self.nbElements)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
159 print "# written: %d (%d%% overlaps)" % (self.nbWritten, 0 if (self.nbElements == 0) else ((float(self.nbWritten) / self.nbElements) * 100))
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
160 print "# merges: %d" % (self.nbMerges)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
161
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
162
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
163 if __name__ == "__main__":
18
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
164 description = "Clusterize v1.0.3: clusterize the data which overlap. [Category: Merge]"
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
165
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
166 parser = OptionParser(description = description)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
167 parser.add_option("-i", "--input", dest="inputFileName", action="store", type="string", help="input file [compulsory] [format: file in transcript format given by -f]")
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
168 parser.add_option("-f", "--format", dest="format", action="store", type="string", help="format of file [format: transcript file format]")
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
169 parser.add_option("-o", "--output", dest="outputFileName", action="store", type="string", help="output file [compulsory] [format: output file in transcript format given by -u]")
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
170 parser.add_option("-u", "--outputFormat", dest="outputFormat", action="store", default="gff", type="string", help="output file format [format: transcript file format]")
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
171 parser.add_option("-c", "--colinear", dest="colinear", action="store_true", default=False, help="merge colinear transcripts only [format: bool] [default: false]")
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
172 parser.add_option("-d", "--distance", dest="distance", action="store", default=0, type="int", help="max. distance between two transcripts to be merged [format: int] [default: 0]")
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
173 parser.add_option("-n", "--normalize", dest="normalize", action="store_true", default=False, help="normalize the number of reads per cluster by the number of mappings per read [format: bool] [default: false]")
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
174 parser.add_option("-s", "--sorted", dest="sorted", action="store_true", default=False, help="input is already sorted [format: bool] [default: false]")
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
175 parser.add_option("-v", "--verbosity", dest="verbosity", action="store", default=1, type="int", help="trace level [format: int] [default: 1]")
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
176 (options, args) = parser.parse_args()
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
177
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
178 c = Clusterize(options.verbosity)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
179 c.setInputFile(options.inputFileName, options.format)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
180 c.setOutputFileName(options.outputFileName, options.outputFormat)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
181 c.setColinear(options.colinear)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
182 c.setDistance(options.distance)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
183 c.setNormalize(options.normalize)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
184 c.setPresorted(options.sorted)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
185 c.run()