annotate SMART/Java/Python/compareOverlapping.py @ 46:169d364ddd91

Uploaded
author m-zytnicki
date Mon, 30 Sep 2013 03:19:26 -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 """Compare overlap of two transcript lists"""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 import sys
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 import os
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 SMART.Java.Python.misc import Utils
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 from SMART.Java.Python.structure.TranscriptContainer import TranscriptContainer
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 from commons.core.writer.TranscriptWriter import TranscriptWriter
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 from SMART.Java.Python.structure.TranscriptListsComparator import TranscriptListsComparator
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 from SMART.Java.Python.misc.RPlotter import RPlotter
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
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 class CompareOverlapping(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):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 self._options = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 def setAttributesFromCmdLine(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 description = "Compare Overlapping v1.0.3: Get the data which overlap with a reference set. [Category: Data Comparison]"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 parser = OptionParser(description = description)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 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
53 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
54 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
55 parser.add_option("-g", "--format2", dest="format2", action="store", type="string", help="format of file 2 [compulsory] [format: transcript file format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 parser.add_option("-o", "--output", dest="output", 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
57 parser.add_option("-S", "--start1", dest="start1", action="store", default=None, type="int", help="only consider the n first nucleotides of the transcripts in file 1 (do not use it with -U) [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 parser.add_option("-s", "--start2", dest="start2", action="store", default=None, type="int", help="only consider the n first nucleotides of the transcripts in file 2 (do not use it with -u) [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 parser.add_option("-U", "--end1", dest="end1", action="store", default=None, type="int", help="only consider the n last nucleotides of the transcripts in file 1 (do not use it with -S) [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 parser.add_option("-u", "--end2", dest="end2", action="store", default=None, type="int", help="only consider the n last nucleotides of the transcripts in file 2 (do not use it with -s) [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 parser.add_option("-t", "--intron", dest="introns", action="store_true", default=False, help="also report introns [format: bool] [default: false]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 parser.add_option("-E", "--5primeExtension1", dest="fivePrime1", action="store", default=None, type="int", help="extension towards 5' in file 1 [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 parser.add_option("-e", "--5primeExtension2", dest="fivePrime2", action="store", default=None, type="int", help="extension towards 5' in file 2 [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 parser.add_option("-N", "--3primeExtension1", dest="threePrime1", action="store", default=None, type="int", help="extension towards 3' in file 1 [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 parser.add_option("-n", "--3primeExtension2", dest="threePrime2", action="store", default=None, type="int", help="extension towards 3' in file 2 [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 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
67 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
68 parser.add_option("-d", "--distance", dest="distance", action="store", default=None, type="int", help="accept some distance between query and reference [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 parser.add_option("-k", "--included", dest="included", action="store_true", default=False, help="keep only elements from file 1 which are included in an element of file 2 [format: bool] [default: false]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 parser.add_option("-K", "--including", dest="including", action="store_true", default=False, help="keep only elements from file 2 which are included in an element of file 1 [format: bool] [default: false]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 parser.add_option("-m", "--minOverlap", dest="minOverlap", action="store", default=1, type="int", help="minimum number of nucleotides overlapping to declare an overlap [format: int] [default: 1]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 parser.add_option("-p", "--pcOverlap", dest="pcOverlap", action="store", default=None, type="int", help="minimum percentage of nucleotides to overlap to declare an overlap [format: int]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 parser.add_option("-O", "--notOverlapping", dest="notOverlapping", action="store_true", default=False, help="also output not overlapping data [format: bool] [default: false]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 parser.add_option("-x", "--exclude", dest="exclude", action="store_true", default=False, help="invert the match [format: bool] [default: false]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 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
76 parser.add_option("-l", "--log", dest="log", action="store_true", default=False, help="write a log file [format: bool] [default: false]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 (self._options, args) = parser.parse_args()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 def run(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 logHandle = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 if self._options.log:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 logHandle = open(self._options.output, "w")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 transcriptContainer1 = TranscriptContainer(self._options.inputFileName1, self._options.format1, self._options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 transcriptContainer2 = TranscriptContainer(self._options.inputFileName2, self._options.format2, self._options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 writer = TranscriptWriter(self._options.output, "gff3", self._options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 transcriptListComparator = TranscriptListsComparator(logHandle, self._options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 transcriptListComparator.restrictToStart(transcriptListComparator.QUERY, self._options.start1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 transcriptListComparator.restrictToStart(transcriptListComparator.REFERENCE, self._options.start2)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 transcriptListComparator.restrictToEnd(transcriptListComparator.QUERY, self._options.end1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 transcriptListComparator.restrictToEnd(transcriptListComparator.REFERENCE, self._options.end2)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 transcriptListComparator.extendFivePrime(transcriptListComparator.QUERY, self._options.fivePrime1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 transcriptListComparator.extendFivePrime(transcriptListComparator.REFERENCE, self._options.fivePrime2)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 transcriptListComparator.extendThreePrime(transcriptListComparator.QUERY, self._options.threePrime1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 transcriptListComparator.extendThreePrime(transcriptListComparator.REFERENCE, self._options.threePrime2)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 transcriptListComparator.acceptIntrons(transcriptListComparator.QUERY, self._options.introns)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 transcriptListComparator.acceptIntrons(transcriptListComparator.REFERENCE, self._options.introns)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 transcriptListComparator.getAntisenseOnly(self._options.antisense)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 transcriptListComparator.getColinearOnly(self._options.colinear)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 transcriptListComparator.getInvert(self._options.exclude)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 transcriptListComparator.setMaxDistance(self._options.distance)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 transcriptListComparator.setMinOverlap(self._options.minOverlap)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 transcriptListComparator.setPcOverlap(self._options.pcOverlap)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 transcriptListComparator.setIncludedOnly(self._options.included)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 transcriptListComparator.setIncludingOnly(self._options.including)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 transcriptListComparator.includeNotOverlapping(self._options.notOverlapping)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 transcriptListComparator.computeOdds(True)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 transcriptListComparator.setInputTranscriptContainer(transcriptListComparator.QUERY, transcriptContainer1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 transcriptListComparator.setInputTranscriptContainer(transcriptListComparator.REFERENCE, transcriptContainer2)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 transcriptListComparator.setOutputWriter(writer)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 transcriptListComparator.compareTranscriptList()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 if self._options.log:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 logHandle.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 if not self._options.exclude:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 odds = transcriptListComparator.getOdds()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 if self._options.verbosity > 0 and odds:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 print "min/avg/med/max transcripts: %d/%.2f/%.1f/%d" % Utils.getMinAvgMedMax(odds)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 if __name__ == "__main__":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 icompareOverlapping = CompareOverlapping()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125 icompareOverlapping.setAttributesFromCmdLine()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 icompareOverlapping.run()