annotate SMART/Java/Python/mergeSlidingWindowsClusters.py @ 16:6135c3075bc5

Deleted selected files
author m-zytnicki
date Mon, 22 Apr 2013 11:09:41 -0400
parents 769e306b7933
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 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 Merge sliding windows of two different clusterings
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 import sys
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 import re
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 import os
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 from optparse import OptionParser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 from SMART.Java.Python.structure.TranscriptContainer import TranscriptContainer
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 from commons.core.writer.Gff3Writer import Gff3Writer
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 from SMART.Java.Python.structure.Transcript import Transcript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44 class MergeSlidingWindowsClusters(object):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 Merge the ouptput of several sets of sliding windows
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 def __init__(self, verbosity = 0):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 self.verbosity = verbosity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 self.inputs = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 self.outputData = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 self.nbData = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 self.nbWrittenData = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 self.chromosomes = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 self.writer = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 def __del__(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 if self.writer != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 self.writer.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 def addInput(self, fileName, fileFormat):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 self.inputs.append(TranscriptContainer(fileName, fileFormat, self.verbosity))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 self.chromosomes = list(set(self.chromosomes).union(set(self.inputs[-1].getChromosomes())))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 def setOutput(self, fileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 self.writer = Gff3Writer(fileName, self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 def readInput(self, i, chromosome):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 progress = Progress(self.inputs[i].getNbTranscripts(), "Reading file #%d -- chromosome %s" % (i+1, chromosome), self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 for transcript in self.inputs[i].getIterator():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 progress.inc()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 if chromosome != transcript.getChromosome(): continue
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 start = transcript.getStart()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 end = transcript.getEnd()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 direction = transcript.getDirection()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 tags = transcript.tags
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 if chromosome not in self.outputData:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 self.outputData[chromosome] = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 if direction not in self.outputData[chromosome]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 self.outputData[chromosome][direction] = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 if start not in self.outputData[chromosome][direction]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 self.outputData[chromosome][direction][start] = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 if end in self.outputData[chromosome][direction][start]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 ends = self.outputData[chromosome][direction][start].keys()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 if ends[0] != end:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 sys.exit("Error! Two regions starting at %d end are not consistent (%d and %d) in %s on strand %d" % (start, end, ends[0], chromosome, direction))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 self.outputData[chromosome][direction][start][end].update(tags)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 self.outputData[chromosome][direction][start][end] = tags
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 self.nbData += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 progress.done()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 def writeOutput(self, chromosome):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 progress = Progress(self.nbData - self.nbWrittenData, "Writing output for chromosome %s" % (chromosome), self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 for direction in self.outputData[chromosome]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 for start in self.outputData[chromosome][direction]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 for end in self.outputData[chromosome][direction][start]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 transcript = Transcript()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 transcript.setChromosome(chromosome)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 transcript.setStart(start)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 transcript.setEnd(end)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 transcript.setDirection(direction)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 transcript.tags = self.outputData[chromosome][direction][start][end]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 transcript.setName("region_%d" % (self.nbWrittenData + 1))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 tags = transcript.getTagNames()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 for tag in tags:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 if tag.startswith("Name_") or tag.startswith("ID_"):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 del transcript.tags[tag]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 self.nbWrittenData += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 self.writer.addTranscript(transcript)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 progress.inc()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 self.writer.write()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 progress.done()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 self.outputData = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 def merge(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 for chromosome in self.chromosomes:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 for i, input in enumerate(self.inputs):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 self.readInput(i, chromosome)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 self.writeOutput(chromosome)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 self.writer.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 if __name__ == "__main__":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128 # parse command line
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 description = "Merge Sliding Windows Clusters v1.0.2: Merge two files containing the results of a sliding windows clustering. [Category: Sliding Windows]"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 parser = OptionParser(description = description)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 parser.add_option("-i", "--input1", dest="inputFileName1", action="store", type="string", help="input file 1 [compulsory] [format: file in transcript format given by -f]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133 parser.add_option("-f", "--inputFormat1", dest="inputFormat1", action="store", type="string", help="format of the input file 1 [compulsory] [format: transcript file format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 parser.add_option("-j", "--input2", dest="inputFileName2", action="store", type="string", help="input file 2 [compulsory] [format: file in transcript format given by -g]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 parser.add_option("-g", "--inputFormat2", dest="inputFormat2", action="store", type="string", help="format of the input file 2 [compulsory] [format: transcript file format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136 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
137 parser.add_option("-v", "--verbosity", dest="verbosity", action="store", default=1, type="int", help="trace level [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138 (options, args) = parser.parse_args()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 merger = MergeSlidingWindowsClusters(options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 merger.addInput(options.inputFileName1, options.inputFormat1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 merger.addInput(options.inputFileName2, options.inputFormat2)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143 merger.setOutput(options.outputFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144 merger.merge()