annotate SMART/Java/Python/mergeTranscriptLists.py @ 55:2ac71607aa60

Uploaded
author m-zytnicki
date Fri, 10 Jan 2014 08:59:23 -0500
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 """Merge elements of two transcript lists with some condition"""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 import os, random, shutil, glob
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 from optparse import OptionParser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 from commons.core.parsing.SequenceListParser import SequenceListParser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 from commons.core.parsing.BedParser import BedParser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 from commons.core.parsing.GffParser import GffParser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 from commons.core.writer.TranscriptWriter import TranscriptWriter
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 from SMART.Java.Python.structure.TranscriptListsComparator import TranscriptListsComparator
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 from SMART.Java.Python.structure.TranscriptContainer import TranscriptContainer
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 from SMART.Java.Python.misc.RPlotter import RPlotter
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 from SMART.Java.Python.misc.Progress import Progress
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 class MergeLists(object):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 def __init__(self, verbosity):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 self.verbosity = verbosity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 self.seed = random.randint(0, 100000)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 self.aggregation = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 self.normalization = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 self.distance = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 self.antisense = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 self.colinear = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 self.fileNames = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57 self.formats = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 self.tmpFileNames = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 self.logHandle = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 # def __del__(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 # for fileNameRoot in self.tmpFileNames:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 # for fileName in glob.glob("%s*" % (fileNameRoot)):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 # os.remove(fileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 # if self.logHandle != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 # self.logHandle.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 # self.logHandle = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 def setLogFileName(self, fileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 self.logHandle = open(fileName, "w")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 def setInputFileName(self, fileName, format, id):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 self.fileNames[id] = fileName
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 self.formats[id] = format
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 def setOutputFileName(self, fileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 self.outputFileName = fileName
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 def setAggregate(self, aggregation):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 self.aggregation = aggregation
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 def setNormalization(self, normalization):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 self.normalization = normalization
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 def setDistance(self, distance):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 self.distance = distance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 def setAntisense(self, antisense):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 self.antisense = antisense
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 def setColinear(self, colinear):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 self.colinear = colinear
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 def createTmpFileName(self, root):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 fileName = "tmp_%s_%d.gff3" % (root, self.seed)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 self.tmpFileNames.append(fileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 return fileName
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 def selfMerge(self, fileName, format, outputFileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 transcriptListComparator = TranscriptListsComparator(self.logHandle, self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 transcriptListComparator.getColinearOnly(True)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 transcriptListComparator.setNormalization(self.normalization)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 transcriptContainer = TranscriptContainer(fileName, format, self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 writer = TranscriptWriter(outputFileName, "gff3", self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 transcriptListComparator.setInputTranscriptContainer(transcriptListComparator.QUERY, transcriptContainer)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 transcriptListComparator.setOutputWriter(writer)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 transcriptListComparator.compareTranscriptListSelfMerge()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 def keepOverlapping(self, fileNames, formats, outputFileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 transcriptListComparator = TranscriptListsComparator(self.logHandle, self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 transcriptListComparator.getAntisenseOnly(self.antisense)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 transcriptListComparator.getColinearOnly(self.colinear)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 for i in (0, 1):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 transcriptContainer = TranscriptContainer(fileNames[i], formats[i], self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 transcriptListComparator.setInputTranscriptContainer(i, transcriptContainer)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 transcriptListComparator.aggregate(self.aggregation)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 transcriptListComparator.setNormalization(self.normalization)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 transcriptListComparator.setMaxDistance(self.distance)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 writer = TranscriptWriter(outputFileName, "gff3", self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 transcriptListComparator.setOutputWriter(writer)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 transcriptListComparator.compareTranscriptList()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 def mergeFiles(self, fileName1, fileName2, outputFileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 outputFile = open(outputFileName, "w")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125 shutil.copyfileobj(open(fileName1, "r"), outputFile)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 shutil.copyfileobj(open(fileName2, "r"), outputFile)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 outputFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 def run(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 selectedFileQuery = self.createTmpFileName("query")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 self.keepOverlapping({0: self.fileNames[0], 1: self.fileNames[0]}, {0: "gff3", 1: "gff3"}, selectedFileQuery)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 mergeFileTarget = self.createTmpFileName("target")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133 self.selfMerge(self.fileNames[1], self.formats[1], mergeFileTarget)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 if not self.aggregation:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 overlapFile = self.createTmpFileName("overlap")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136 self.keepOverlapping({0: mergeFileTarget, 1: selectedFileQuery}, {0: "gff3", 1: "gff3"}, overlapFile)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137 mergeFileTarget = overlapFile
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138 mergeFileMerged = self.createTmpFileName("merged")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 self.mergeFiles(mergeFileTarget, selectedFileQuery, mergeFileMerged)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 self.selfMerge(mergeFileMerged, "gff3", self.outputFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144 if __name__ == "__main__":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146 # parse command line
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147 description = "Merge Lists v1.0.3: Merge the elements of two lists of genomic coordinates. [Category: Merge]"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149 parser = OptionParser(description = description)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150 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
151 parser.add_option("-f", "--format1", dest="format1", action="store", type="string", help="format of file 1 [compulsory] [format: transcript file format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152 parser.add_option("-j", "--input2", dest="inputFileName2", action="store", default=None, 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
153 parser.add_option("-g", "--format2", dest="format2", action="store", default=None, type="string", help="format of file 2 [compulsory] [format: file in transcript format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
154 parser.add_option("-o", "--output", dest="outputFileName", action="store", default=None, type="string", help="output file [compulsory] [format: output file in GFF3 format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
155 parser.add_option("-k", "--all", dest="all", action="store_true", default=False, help="print all the transcripts, not only those overlapping [format: bool] [default: false]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
156 parser.add_option("-d", "--distance", dest="distance", action="store", default=0, type="int", help="max. distance between two transcripts [format: int] [default: 0]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
157 parser.add_option("-a", "--antisense", dest="antisense", action="store_true", default=False, help="antisense only [format: bool] [default: false]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
158 parser.add_option("-c", "--colinear", dest="colinear", action="store_true", default=False, help="colinear only [format: bool] [default: false]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
159 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
160 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
161 (options, args) = parser.parse_args()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
162
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
163 # ml = MergeLists(logHandle, options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
164
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
165 ml = MergeLists(0)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
166 ml.setInputFileName(options.inputFileName1, options.format1, 0)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
167 ml.setInputFileName(options.inputFileName2, options.format2, 1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
168 ml.setOutputFileName(options.outputFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
169 ml.setAntisense(options.antisense)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
170 ml.setColinear(options.colinear)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
171 ml.setAggregate(options.all)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
172 ml.setNormalization(options.normalize)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
173 ml.setDistance(options.distance)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
174 ml.run()