annotate SMART/Java/Python/ncList/FindOverlapsWithSeveralIntervalsIndex.py @ 65:4ab9c86890ba draft

Minor change
author m-zytnicki
date Wed, 04 Nov 2015 03:41:26 -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-2011
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 random, os, time, MySQLdb
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 from optparse import OptionParser
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.TranscriptWriter import TranscriptWriter
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.misc.Progress import Progress
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 from SMART.Java.Python.misc.UnlimitedProgress import UnlimitedProgress
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 class FindOverlapsWithSeveralIntervalsIndex(object):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 def __init__(self, verbosity):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 self.verbosity = verbosity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44 randomNumber = random.randint(0, 10000)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 self.dbName = "smartdb"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 if "SMARTTMPPATH" in os.environ:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 self.dbName = os.join(os.environ["SMARTTMPPATH"], self.dbName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 self.db = MySQLdb.connect(db = self.dbName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 self.tableName = "table_%s" % (randomNumber)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 self.nbQueries = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 self.nbRefs = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 self.nbOverlaps = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 def __del__(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 cursor = self.db.cursor()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 cursor.execute("DROP TABLE IF EXISTS %s" % (self.tableName))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 def setReferenceFile(self, fileName, format):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 cursor = self.db.cursor()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 cursor.execute("CREATE TABLE %s (start INT, end INT)" % (self.tableName))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 cursor.execute("CREATE INDEX index_%s ON %s (start, end)" % (self.tableName, self.tableName))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 chooser = ParserChooser(self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 chooser.findFormat(format)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 parser = chooser.getParser(fileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 progress = UnlimitedProgress(1000, "Reading references", self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 for transcript in parser.getIterator():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 start = transcript.getStart()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 end = transcript.getEnd()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 cursor = self.db.cursor()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 cursor.execute("INSERT INTO %s (start, end) VALUES (%d, %d)" % (self.tableName, start, end))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 self.nbRefs += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 progress.inc()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 self.db.commit()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 progress.done()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 def setQueryFile(self, fileName, format):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 chooser = ParserChooser(self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 chooser.findFormat(format)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 self.queryParser = chooser.getParser(fileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 self.nbQueries = self.queryParser.getNbTranscripts()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 def setOutputFile(self, fileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 self.writer = TranscriptWriter(fileName, "gff3", self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 def compare(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 progress = Progress(self.nbQueries, "Reading queries", self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 startTime = time.time()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 for queryTranscript in self.queryParser.getIterator():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 queryStart = queryTranscript.getStart()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 queryEnd = queryTranscript.getEnd()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 command = "SELECT 1 FROM %s WHERE start <= %d and end >= %d" % (self.tableName, queryEnd, queryStart)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 cursor = self.db.cursor()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 cursor.execute(command)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 overlap = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 line = cursor.fetchone()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 while line:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 overlap = True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 line = cursor.fetchone()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 if overlap:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 self.writer.addTranscript(queryTranscript)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 self.nbOverlaps += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 progress.inc()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 progress.done()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 endTime = time.time()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 self.timeSpent = endTime - startTime
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 def displayResults(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 print "# queries: %d" % (self.nbQueries)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 print "# refs: %d" % (self.nbRefs)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 print "# overlaps: %d" % (self.nbOverlaps)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 print "time: %.2gs" % (self.timeSpent)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 def run(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 self.compare()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 self.displayResults()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 if __name__ == "__main__":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 description = "Find Overlaps With Several Intervals Using Indices v1.0.1: Use MySQL to compare intervals. [Category: Personal]"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 parser = OptionParser(description = description)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 parser.add_option("-i", "--input1", dest="inputFileName1", action="store", type="string", help="query input file [compulsory] [format: file in transcript format given by -f]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 parser.add_option("-f", "--format1", dest="format1", action="store", type="string", help="format of previous file [compulsory] [format: transcript file format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125 parser.add_option("-j", "--input2", dest="inputFileName2", action="store", type="string", help="reference input file [compulsory] [format: file in transcript format given by -g]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 parser.add_option("-g", "--format2", dest="format2", action="store", type="string", help="format of previous file [compulsory] [format: transcript file format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 parser.add_option("-o", "--output", dest="outputFileName", action="store", type="string", help="output file [format: output file in GFF3 format]")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128 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
129 (options, args) = parser.parse_args()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 fowsii = FindOverlapsWithSeveralIntervalsIndex(options.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 fowsii.setQueryFile(options.inputFileName1, options.format1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133 fowsii.setReferenceFile(options.inputFileName2, options.format2)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 fowsii.setOutputFile(options.outputFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 fowsii.run()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137