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

Change the repository level.
author yufei-luo
date Fri, 18 Jan 2013 04:54:14 -0500
parents
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-2012
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 import os
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 from optparse import OptionParser, OptionGroup
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 from commons.core.parsing.ParserChooser import ParserChooser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 from commons.core.writer.Gff3Writer import Gff3Writer
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 from SMART.Java.Python.structure.Transcript import Transcript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 from SMART.Java.Python.ncList.NCListFilePickle import NCListFileUnpickle
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 from SMART.Java.Python.ncList.FileSorter import FileSorter
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 from SMART.Java.Python.misc.Progress import Progress
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 from SMART.Java.Python.misc import Utils
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 class GetUpDownStream(object):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44 def __init__(self, verbosity = 0):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 self.verbosity = verbosity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 self.inputReader = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 self.outputWriter = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 self.nbRead = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 self.nbWritten = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 self.nbMerges = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 self.splittedFileNames = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 def __del__(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 for fileName in self.splittedFileNames.values():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 os.remove(fileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57 def setInputFile(self, fileName, format):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 parserChooser = ParserChooser(self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 parserChooser.findFormat(format, "transcript")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 self.parser = parserChooser.getParser(fileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 self.sortedFileName = "%s_sorted.pkl" % (os.path.splitext(fileName)[0])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 def setOutputFile(self, fileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 self.outputWriter = Gff3Writer(fileName, self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 def setDistances(self, up, down):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 self.upDistance = up
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 self.downDistance = down
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 def _sortFile(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 fs = FileSorter(self.parser, self.verbosity-4)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 fs.perChromosome(True)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 fs.setOutputFileName(self.sortedFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 fs.sort()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 self.splittedFileNames = fs.getOutputFileNames()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 self.nbElementsPerChromosome = fs.getNbElementsPerChromosome()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 self.nbRead = fs.getNbElements()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 def _write(self, start, end, reference, after):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 if start > end:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 return
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 transcript = Transcript()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 transcript.setChromosome(reference.getChromosome())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 transcript.setStart(start)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 transcript.setEnd(end)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 transcript.setDirection("+")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 transcript.setName("%s_%s" % ("up" if Utils.xor(reference.getDirection() == 1, after) else "down", reference.getName()))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 self.outputWriter.addTranscript(transcript)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 def _getFlanking(self, chromosome):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 progress = Progress(self.nbElementsPerChromosome[chromosome], "Analyzing chromosome %s" % (chromosome), self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 parser = NCListFileUnpickle(self.splittedFileNames[chromosome], self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 previous = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 for transcript in parser.getIterator():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 progress.inc()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 transcript.removeExons()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 if previous == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 distance = self.upDistance if transcript.getDirection() == 1 else self.downDistance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 start = max(1, transcript.getStart() - distance)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 self._write(start, transcript.getStart()-1, transcript, False)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 previous = transcript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 continue
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 if previous.include(transcript):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 continue
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 if transcript.overlapWith(previous):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 previous = transcript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 continue
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 distancePrevious = self.downDistance if previous.getDirection() == 1 else self.upDistance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 distanceCurrent = self.upDistance if transcript.getDirection() == 1 else self.downDistance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 distance = transcript.getDistance(previous)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 if distancePrevious + distanceCurrent == 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 previous = transcript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 continue
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 if distance >= distancePrevious + distanceCurrent:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 endPrevious = previous.getEnd() + distancePrevious
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 startCurrent = transcript.getStart() - distanceCurrent
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 middle = previous.getEnd() + int((distance-1) * float(distancePrevious) / (distancePrevious + distanceCurrent))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 endPrevious = middle
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 startCurrent = middle+1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 self._write(previous.getEnd() + 1, endPrevious, previous, True)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 self._write(startCurrent, transcript.getStart() - 1, transcript, False)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 previous = transcript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 distance = self.downDistance if previous.getDirection() == 1 else self.upDistance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125 self._write(previous.getEnd() + 1, previous.getEnd() + distance, previous, True)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 progress.done()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128 def run(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 self._sortFile()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 for chromosome in sorted(self.nbElementsPerChromosome.keys()):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 self._getFlanking(chromosome)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 self.outputWriter.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 if __name__ == "__main__":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136 # parse command line
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137 description = "Get Up and Down Stream v1.0.0: Get the flanking regions of an annotation. [Category: Data Modification]"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 parser = OptionParser(description = description)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 parser.add_option("-i", "--input", dest="inputFileName", action="store", type="string", help="input file [compulsory] [format: file in mapping format given by -f]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 parser.add_option("-f", "--format", dest="format", action="store", type="string", help="format of the file [compulsory] [format: mapping file format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 parser.add_option("-o", "--output", dest="outputFileName", action="store", type="string", help="output file [compulsory] [format: output file in GFF3 format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143 parser.add_option("-u", "--up", dest="up", action="store", default=0, type="int", help="the upstream distance [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144 parser.add_option("-d", "--down", dest="down", action="store", default=0, type="int", help="the downstream distance [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145 parser.add_option("-v", "--verbosity", dest="verbosity", action="store", default=1, type="int", help="trace level [default: 1] [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146 (options, args) = parser.parse_args()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148 guds = GetUpDownStream(options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149 guds.setInputFile(options.inputFileName, options.format)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150 guds.setOutputFile(options.outputFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151 guds.setDistances(options.up, options.down)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152 guds.run()