Mercurial > repos > yufei-luo > s_mart
comparison SMART/Java/Python/structure/TranscriptList.py @ 6:769e306b7933
Change the repository level.
author | yufei-luo |
---|---|
date | Fri, 18 Jan 2013 04:54:14 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
5:ea3082881bf8 | 6:769e306b7933 |
---|---|
1 # | |
2 # Copyright INRA-URGI 2009-2010 | |
3 # | |
4 # This software is governed by the CeCILL license under French law and | |
5 # abiding by the rules of distribution of free software. You can use, | |
6 # modify and/ or redistribute the software under the terms of the CeCILL | |
7 # license as circulated by CEA, CNRS and INRIA at the following URL | |
8 # "http://www.cecill.info". | |
9 # | |
10 # As a counterpart to the access to the source code and rights to copy, | |
11 # modify and redistribute granted by the license, users are provided only | |
12 # with a limited warranty and the software's author, the holder of the | |
13 # economic rights, and the successive licensors have only limited | |
14 # liability. | |
15 # | |
16 # In this respect, the user's attention is drawn to the risks associated | |
17 # with loading, using, modifying and/or developing or reproducing the | |
18 # software by the user in light of its specific status of free software, | |
19 # that may mean that it is complicated to manipulate, and that also | |
20 # therefore means that it is reserved for developers and experienced | |
21 # professionals having in-depth computer knowledge. Users are therefore | |
22 # encouraged to load and test the software's suitability as regards their | |
23 # requirements in conditions enabling the security of their systems and/or | |
24 # data to be ensured and, more generally, to use and operate it in the | |
25 # same conditions as regards security. | |
26 # | |
27 # The fact that you are presently reading this means that you have had | |
28 # knowledge of the CeCILL license and that you accept its terms. | |
29 # | |
30 from SMART.Java.Python.structure.Transcript import Transcript | |
31 from SMART.Java.Python.mySql.MySqlTable import MySqlTable | |
32 from SMART.Java.Python.structure.Interval import Interval | |
33 from SMART.Java.Python.misc.Progress import Progress | |
34 | |
35 | |
36 class TranscriptList(object): | |
37 """A class that codes for a list of transcript""" | |
38 | |
39 def __init__(self, verbosity = 0): | |
40 self.transcripts = dict() | |
41 self.longestTranscript = 0 | |
42 self.verbosity = verbosity | |
43 | |
44 | |
45 def getTranscript(self, chromosome, index): | |
46 return self.transcripts[chromosome][index] | |
47 | |
48 | |
49 def getChromosomes(self): | |
50 return self.transcripts.keys() | |
51 | |
52 | |
53 def getTranscriptsOnChromosome(self, chromosome): | |
54 if chromosome not in self.transcripts: | |
55 return [] | |
56 return self.transcripts[chromosome] | |
57 | |
58 | |
59 def addTranscript(self, transcript): | |
60 if transcript.getChromosome() in self.transcripts: | |
61 self.transcripts[transcript.getChromosome()].append(transcript) | |
62 else: | |
63 self.transcripts[transcript.getChromosome()] = [transcript] | |
64 self.longestTranscript = max(self.longestTranscript, transcript.getEnd() - transcript.getStart()) | |
65 | |
66 | |
67 def removeTranscript(self, chromosome, i): | |
68 del self.transcripts[chromosome][i] | |
69 | |
70 | |
71 def removeAll(self): | |
72 self.transcripts = {} | |
73 | |
74 | |
75 def getNbTranscripts(self): | |
76 nbTranscripts = 0 | |
77 for chromosome in self.transcripts: | |
78 nbTranscripts += len(self.transcripts[chromosome]) | |
79 return nbTranscripts | |
80 | |
81 | |
82 def getSize(self): | |
83 size = 0 | |
84 for chromosome in self.transcripts: | |
85 for transcript in self.transcripts[chromosome]: | |
86 size += transcript.getSize() | |
87 return size | |
88 | |
89 | |
90 def sort(self): | |
91 for chromosome in self.transcripts: | |
92 self.transcripts[chromosome].sort(lambda x, y: x.getStart() - y.getStart()) | |
93 | |
94 | |
95 def removeOverlapWith(self, transcriptList): | |
96 transcriptList.sort() | |
97 for chromosome in self.transcripts: | |
98 progress = Progress(len(self.transcripts[chromosome]), "Handling chromosome %s" % (chromosome), self.verbosity) | |
99 for thisTranscriptId in range(len(self.transcripts[chromosome])): | |
100 progress.inc() | |
101 for thatTranscriptId in range(len(transcriptList.transcripts[chromosome])): | |
102 if self.transcripts[chromosome][thisTranscriptId].overlapWith(transcriptList.transcripts[chromosome][thatTranscriptId]): | |
103 self.transcripts[chromosome][thisTranscriptId] = None | |
104 break | |
105 if self.transcripts[chromosome][thisTranscriptId].getEnd() > transcriptList.transcripts[chromosome][thatTranscriptId]: | |
106 break | |
107 self.transcripts[chromosome] = [transcript for transcript in self.transcripts[chromosome] if transcript != None] | |
108 progress.done() | |
109 | |
110 | |
111 def removeOverlapWithExon(self, transcriptList): | |
112 transcriptList.sort() | |
113 for chromosome in self.transcripts: | |
114 progress = Progress(len(self.transcripts[chromosome]), "Handling chromosome %s" % (chromosome), self.verbosity) | |
115 for thisTranscriptId in range(len(self.transcripts[chromosome])): | |
116 progress.inc() | |
117 for thatTranscriptId in range(len(transcriptList.transcripts[chromosome])): | |
118 if self.transcripts[chromosome][thisTranscriptId].overlapWithExon(transcriptList.transcripts[chromosome][thatTranscriptId]): | |
119 self.transcripts[chromosome][thisTranscriptId] = None | |
120 break | |
121 if self.transcripts[chromosome][thisTranscriptId].getEnd() > transcriptList.transcripts[chromosome][thatTranscriptId]: | |
122 break | |
123 self.transcripts[chromosome] = [transcript for transcript in self.transcripts[chromosome] if transcript != None] | |
124 progress.done() | |
125 | |
126 | |
127 def setDefaultTagValue(self, name, value): | |
128 for transcript in self.getIterator(): | |
129 transcript.setTag(name, value) | |
130 | |
131 | |
132 def storeDatabase(self, mySqlConnection): | |
133 transcriptsTable = MySqlTable("TmpTranscriptsTable", mySqlConnection) | |
134 transcriptsTable.create(Transcript.getSqlVariables(), Transcript.getSqlTypes()) | |
135 intervalsVariables = Interval.getSqlVariables() | |
136 intervalsVariables.append("idTranscript") | |
137 intervalsTypes = Interval.getSqlTypes() | |
138 intervalsTypes["idTranscript"] = "int" | |
139 intervalsTable = MySqlTable("TmpIntervalsTable", mySqlConnection) | |
140 intervalsTable.create(intervalsVariables, intervalsTypes) | |
141 for chromosome in self.transcripts: | |
142 for transcript in self.transcripts[chromosome]: | |
143 idTranscript = transcriptsTable.addLine(transcript.getSqlValues()) | |
144 for exon in transcript.getExons(): | |
145 intervalValues = exon.getSqlValues() | |
146 intervalValues["idTranscript"] = idTranscript | |
147 intervalsTable.addLine(intervalValues) | |
148 | |
149 | |
150 def getIterator(self): | |
151 chromosomes = self.transcripts.keys() | |
152 currentChromosome = 0 | |
153 currentTranscript = 0 | |
154 while True: | |
155 if currentChromosome >= len(chromosomes): | |
156 return | |
157 elif currentTranscript >= len(self.transcripts[chromosomes[currentChromosome]]): | |
158 currentTranscript = 0 | |
159 currentChromosome += 1 | |
160 elif self.transcripts[chromosomes[currentChromosome]][currentTranscript] == None: | |
161 currentTranscript += 1 | |
162 else: | |
163 yield self.transcripts[chromosomes[currentChromosome]][currentTranscript] | |
164 currentTranscript += 1 | |
165 | |
166 | |
167 def __str__(self): | |
168 string = "" | |
169 for transcript in self.getIterator(): | |
170 string += str(transcript) | |
171 return string | |
172 |