annotate SMART/Java/Python/clusterize.py @ 6:769e306b7933

Change the repository level.
author yufei-luo
date Fri, 18 Jan 2013 04:54:14 -0500
parents
children 94ab73e8a190
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
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 import os
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
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 class Clusterize(object):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 def __init__(self, verbosity):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 self.normalize = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 self.presorted = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 self.distance = 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 self.colinear = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 self.nbWritten = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 self.nbMerges = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 self.verbosity = verbosity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 self.splittedFileNames = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 def __del__(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 for fileName in self.splittedFileNames.values():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57 os.remove(fileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 def setInputFile(self, fileName, format):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 parserChooser = ParserChooser(self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 parserChooser.findFormat(format)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 self.parser = parserChooser.getParser(fileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 self.sortedFileName = "%s_sorted.pkl" % (os.path.splitext(fileName)[0])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 def setOutputFileName(self, fileName, format="gff3", title="S-MART", feature="transcript", featurePart="exon"):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 writerChooser = WriterChooser()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 writerChooser.findFormat(format)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 self.writer = writerChooser.getWriter(fileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 self.writer.setTitle(title)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 self.writer.setFeature(feature)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 self.writer.setFeaturePart(featurePart)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 def setDistance(self, distance):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 self.distance = distance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 def setColinear(self, colinear):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 self.colinear = colinear
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 def setNormalize(self, normalize):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 self.normalize = normalize
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 def setPresorted(self, presorted):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 self.presorted = presorted
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 def _sortFile(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 fs = FileSorter(self.parser, self.verbosity-4)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 fs.perChromosome(True)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 fs.setPresorted(self.presorted)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 fs.setOutputFileName(self.sortedFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 fs.sort()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 self.splittedFileNames = fs.getOutputFileNames()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 self.nbElementsPerChromosome = fs.getNbElementsPerChromosome()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 self.nbElements = fs.getNbElements()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 def _iterate(self, chromosome):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 progress = Progress(self.nbElementsPerChromosome[chromosome], "Checking chromosome %s" % (chromosome), self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 transcripts = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 parser = NCListFileUnpickle(self.splittedFileNames[chromosome], self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 for newTranscript in parser.getIterator():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 newTranscripts = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 for oldTranscript in transcripts:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 if self._checkOverlap(newTranscript, oldTranscript):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 self._merge(newTranscript, oldTranscript)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 elif self._checkPassed(newTranscript, oldTranscript):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 self._write(oldTranscript)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 newTranscripts.append(oldTranscript)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 newTranscripts.append(newTranscript)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 transcripts = newTranscripts
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 progress.inc()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 for transcript in transcripts:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 self._write(transcript)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 progress.done()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 def _merge(self, transcript1, transcript2):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 self.nbMerges += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 transcript2.setDirection(transcript1.getDirection())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 transcript1.merge(transcript2)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 def _write(self, transcript):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 self.nbWritten += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 self.writer.addTranscript(transcript)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 def _checkOverlap(self, transcript1, transcript2):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125 if self.colinear and transcript1.getDirection() != transcript2.getDirection():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 return False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 if transcript1.getDistance(transcript2) > self.distance:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128 return False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 return True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 def _checkPassed(self, transcript1, transcript2):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 return (transcript1.getDistance(transcript2) > self.distance)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 def run(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 self._sortFile()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136 for chromosome in sorted(self.splittedFileNames.keys()):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137 self._iterate(chromosome)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138 self.writer.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 if self.verbosity > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 print "# input: %d" % (self.nbElements)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 print "# written: %d (%d%% overlaps)" % (self.nbWritten, 0 if (self.nbElements == 0) else ((float(self.nbWritten) / self.nbElements) * 100))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 print "# merges: %d" % (self.nbMerges)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145 if __name__ == "__main__":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146 description = "Clusterize v1.0.3: clusterize the data which overlap. [Category: Merge]"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148 parser = OptionParser(description = description)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149 parser.add_option("-i", "--input", dest="inputFileName", action="store", type="string", help="input file [compulsory] [format: file in transcript format given by -f]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150 parser.add_option("-f", "--format", dest="format", action="store", type="string", help="format of file [format: transcript file format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151 parser.add_option("-o", "--output", dest="outputFileName", action="store", type="string", help="output file [compulsory] [format: output file in transcript format given by -u]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152 parser.add_option("-u", "--outputFormat", dest="outputFormat", action="store", default="gff", type="string", help="output file format [format: transcript file format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153 parser.add_option("-c", "--colinear", dest="colinear", action="store_true", default=False, help="merge colinear transcripts only [format: bool] [default: false]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
154 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]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
155 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]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
156 parser.add_option("-v", "--verbosity", dest="verbosity", action="store", default=1, type="int", help="trace level [format: int] [default: 1]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
157 (options, args) = parser.parse_args()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
158
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
159 c = Clusterize(options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
160 c.setInputFile(options.inputFileName, options.format)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
161 c.setOutputFileName(options.outputFileName, options.outputFormat)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
162 c.setColinear(options.colinear)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
163 c.setDistance(options.distance)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
164 c.setNormalize(options.normalize)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
165 c.run()