annotate SMART/Java/Python/toolLauncher/RnaFoldLauncher.py @ 38:2c0c0a89fad7

Uploaded
author m-zytnicki
date Thu, 02 May 2013 09:56:47 -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 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
2 # Copyright INRA-URGI 2009-2010
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
3 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
4 # This software is governed by the CeCILL license under French law and
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
5 # abiding by the rules of distribution of free software. You can use,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
6 # modify and/ or redistribute the software under the terms of the CeCILL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
7 # license as circulated by CEA, CNRS and INRIA at the following URL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
8 # "http://www.cecill.info".
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
9 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
10 # As a counterpart to the access to the source code and rights to copy,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
11 # modify and redistribute granted by the license, users are provided only
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
12 # with a limited warranty and the software's author, the holder of the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
13 # economic rights, and the successive licensors have only limited
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
14 # liability.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
15 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
16 # In this respect, the user's attention is drawn to the risks associated
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
17 # with loading, using, modifying and/or developing or reproducing the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
18 # software by the user in light of its specific status of free software,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
19 # that may mean that it is complicated to manipulate, and that also
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
20 # therefore means that it is reserved for developers and experienced
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
21 # professionals having in-depth computer knowledge. Users are therefore
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
22 # encouraged to load and test the software's suitability as regards their
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
23 # requirements in conditions enabling the security of their systems and/or
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
24 # data to be ensured and, more generally, to use and operate it in the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
25 # same conditions as regards security.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
26 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
27 # The fact that you are presently reading this means that you have had
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
28 # knowledge of the CeCILL license and that you accept its terms.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
29 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
30 import os
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
31 import sys
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 import random
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 import subprocess
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 from SMART.Java.Python.structure.TranscriptList import TranscriptList
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 commons.core.parsing.FastaParser import FastaParser
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 RnaFoldStructure(object):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 A structure to store the output of RNAFold
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 @ivar name: the name of the sequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44 @type name: string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 @ivar sequence: the sequence (with gaps)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 @type sequence: string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 @ivar structure: the bracket structure
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 @type structure: string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 @ivar energy: the energy of the fold
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 @type energy: float
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 @ivar interactions: the interactions inside the structure
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 @ivar interactions: the interactions inside the structure
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 def __init__(self, name, sequence, structure, energy):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57 Initialize the structure
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 @param name the name of the sequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 @type name: string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 @param sequence: the sequence (with gaps)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 @type sequence: string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 @param structure: the bracket structure
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 @type structure: string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 @param energy: the energy of the fold
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 @type energy: float
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 self.name = name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 self.sequence = sequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 self.structure = structure
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 self.energy = energy
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 self.interactions = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 def analyze(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 Analyze the output, assign the interactions
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 if len(self.sequence) != len(self.structure):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 sys.exit("Sizes of sequence and structure differ ('%s' and '%s')!\n" % (self.sequence, self.structure))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 stack = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 self.interactions = [None for i in range(len(self.sequence))]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 for i in range(len(self.sequence)):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 if self.structure[i] == "(":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 stack.append(i)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 elif self.structure[i] == ")":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 if not stack:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 sys.exit("Something wrong in the interaction line '%s'!\n" % (self.structure))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 otherI = stack.pop()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 self.interactions[i] = otherI
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 self.interactions[otherI] = i
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 if stack:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 sys.exit("Something wrong in the interaction line '%s'!\n" % (self.structure))
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 getNbBulges(self, start = None, end = None):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 Get the number of insertions in a given range (in number of letters)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 @param start: where to start the count
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 @type start: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 @param end: where to end the co
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 @type end: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 if start == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 start = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 if end == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 end = len(self.sequence)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 previousInt = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 nbBulges = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 inRegion = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 for i in range(len(self.sequence)):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 if i == start:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 inRegion = True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 if i > end:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 return nbBulges
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 if inRegion:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 if self.interactions[i] == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 nbBulges += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 elif previousInt != None and abs(self.interactions[i] - previousInt) != 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 nbBulges += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 previousInt = self.interactions[i]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 return nbBulges
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 def getStar(self, start = None, end = None):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 Get the supposed miRNA*
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 @param start: where to start the count
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128 @type start: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 @param end: where to end the co
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 @type end: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 if start == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133 start = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 if end == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 end = len(self.sequence)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136 minStar = 1000000
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137 maxStar = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138 inRegion = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 for i in range(len(self.sequence)):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 if i == start:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 inRegion = True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 if i > end:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143 return (minStar, maxStar)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144 if inRegion:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145 if self.interactions[i] != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146 minStar = min(minStar, self.interactions[i])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147 maxStar = max(maxStar, self.interactions[i])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148 return (minStar, maxStar)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152 class RnaFoldLauncher(object):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
154 Start and parse a RNAFold instance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
155 @ivar inputTranscriptList: a list of transcripts
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
156 @type inputTranscriptList: class L{TranscriptList<TranscriptList>}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
157 @ivar genomeFileParser: a parser to the genome file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
158 @type genomeFileParser: class L{SequenceListParser<SequenceListParser>}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
159 @ivar bothStrands: whether folding is done on both strands
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
160 @type bothStrands: bool
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
161 @ivar fivePrimeExtension: extension towards the 5' end
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
162 @type fivePrimeExtension: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
163 @ivar threePrimeExtension: extension towards the 3' end
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
164 @type threePrimeExtension: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
165 @ivar inputTranscriptList: the input list of transcripts
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
166 @type inputTranscriptList: class L{TranscriptList<TranscriptList>}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
167 @ivar outputTranscriptList: the output list of transcripts
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
168 @type outputTranscriptList: class L{TranscriptList<TranscriptList>}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
169 @ivar tmpInputFileName: the name of the temporary input file for RNAFold
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
170 @type tmpInputFileName: string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
171 @ivar tmpOutputFileName: the name of the temporary output file for RNAFold
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
172 @type tmpOutputFileName: string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
173 @ivar verbosity: verbosity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
174 @type verbosity: int [default: 0]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
175 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
176
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
177 def __init__(self, verbosity = 0):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
178 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
179 Constructor
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
180 @param verbosity: verbosity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
181 @type verbosity: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
182 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
183 self.verbosity = verbosity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
184 self.transcriptNames = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
185 randomNumber = random.randint(0, 100000)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
186 self.bothStrands = True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
187 self.tmpInputFileName = "tmpInput_%d.fas" % (randomNumber)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
188 self.tmpOutputFileName = "tmpOutput_%d.fas" % (randomNumber)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
189 self.outputTranscriptList = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
190 self.fivePrimeExtension = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
191 self.threePrimeExtension = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
192
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
193
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
194 def __del__(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
195 for file in (self.tmpInputFileName, self.tmpOutputFileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
196 if os.path.isfile(file):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
197 os.remove(file)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
198
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
199
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
200 def setTranscriptList(self, inputTranscriptList):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
201 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
202 Set the list of transcripts
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
203 @ivar inputTranscriptList: a list of transcripts
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
204 @type inputTranscriptList: class L{TranscriptList<TranscriptList>}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
205 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
206 self.inputTranscriptList = inputTranscriptList
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
207
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
208
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
209 def setExtensions(self, fivePrime, threePrime):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
210 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
211 Set extension sizes
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
212 @ivar fivePrime: extension towards the 5' end
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
213 @type fivePrime: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
214 @ivar threePrime: extension towards the 3' end
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
215 @type threePrime: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
216 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
217 self.fivePrimeExtension = fivePrime
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
218 self.threePrimeExtension = threePrime
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
219
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
220
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
221 def setNbStrands(self, nbStrands):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
222 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
223 Set number of strands
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
224 @ivar nbStrands: if 2, the work is done on both strands
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
225 @type nbStrands: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
226 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
227 self.nbStrands = nbStrands
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
228
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
229
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
230 def setGenomeFile(self, fileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
231 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
232 Set the genome file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
233 @ivar genomeFileName: the multi-FASTA file containing the genome
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
234 @type genomeFileName: a string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
235 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
236 self.genomeFileParser = FastaParser(fileName, self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
237
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
238
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
239 def writeInputFile(self, transcript, reverse, fivePrimeExtension, threePrimeExtension):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
240 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
241 Write the RNAFold input file, containing the sequence corresponding to the transcript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
242 @ivar transcript: a transcript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
243 @type transcript: class L{Transcript<Transcript>}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
244 @ivar reverse: invert the extensions
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
245 @type reverse: bool
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
246 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
247 transcriptCopy = Transcript(transcript)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
248 transcriptCopy.removeExons()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
249 if not reverse:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
250 transcriptCopy.extendStart(fivePrimeExtension)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
251 transcriptCopy.extendEnd(threePrimeExtension)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
252 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
253 transcriptCopy.extendStart(threePrimeExtension)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
254 transcriptCopy.extendEnd(fivePrimeExtension)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
255 sequence = transcriptCopy.extractSequence(self.genomeFileParser)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
256 handle = open(self.tmpInputFileName, "w")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
257 handle.write(">%s\n%s\n" % (sequence.getName().replace(":", "_").replace(".", "_"), sequence.getSequence()))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
258 handle.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
259
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
260
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
261 def startRnaFold(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
262 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
263 Start RNAFold
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
264 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
265 command = "RNAfold < %s > %s" % (self.tmpInputFileName, self.tmpOutputFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
266 if self.verbosity > 100:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
267 print "Launching command '%s'" % (command)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
268 status = subprocess.call(command, shell=True)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
269 if status != 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
270 raise Exception("Problem with RNAFold! Input file is %s, status is: %s" % (self.tmpInputFileName, status))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
271
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
272
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
273 def parseRnaFoldOutput(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
274 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
275 Parse an output file of RNAFold
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
276 @return: an RnaFoldStructure
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
277 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
278 lines = open(self.tmpOutputFileName).readlines()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
279 if len(lines) != 3:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
280 raise Exception("Problem in RNAfold output! '%s'" % (lines))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
281 name = lines[0].strip()[1:].split()[0]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
282 sequence = lines[1].strip()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
283 structure = lines[2].strip().split()[0]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
284 energy = float(lines[2].strip().split(" ", 1)[1][1:-1].strip())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
285 if self.verbosity > 100:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
286 print "Getting sequence %s, structure %s" % (sequence, structure)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
287 return RnaFoldStructure(name, sequence, structure, energy)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
288
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
289
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
290 def analyzeRnaFoldOutput(self, transcript, rnaFoldOutput, reverse, fivePrimeExtension, threePrimeExtension):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
291 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
292 Analyze the RNAFold
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
293 @ivar transcript: a transcript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
294 @type transcript: class L{Transcript<Transcript>}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
295 @ivar rnaFoldOutput: the output of RNAFold
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
296 @type rnaFoldOutput: class L{RnaFoldStructure<RnaFoldStructure>}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
297 @ivar reverse: invert the extensions
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
298 @type reverse: bool
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
299 @return: a t-uple of energy, number of insertions, number of bulges, strand
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
300 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
301 rnaFoldOutput.analyze()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
302 transcriptSize = transcript.end - transcript.start + 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
303 start = fivePrimeExtension if not reverse else threePrimeExtension
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
304 end = start + transcriptSize
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
305 energy = rnaFoldOutput.energy
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
306 nbBulges = rnaFoldOutput.getNbBulges(start, end)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
307 (minStar, maxStar) = rnaFoldOutput.getStar(start, end)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
308 minStar += transcript.start - start
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
309 maxStar += transcript.start - start
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
310 if self.verbosity > 100:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
311 print "Getting structure with energy %d, nbBulges %d, miRna* %d-%d, strand %s" % (energy, nbBulges, minStar, maxStar, "-" if reverse else "+")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
312 return (energy, nbBulges, minStar, maxStar, reverse)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
313
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
314
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
315 def fold(self, transcript):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
316 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
317 Fold a transcript (in each strand)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
318 @ivar transcript: a transcript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
319 @type transcript: class L{Transcript<Transcript>}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
320 @return: a t-uple of energy, number of insertions, number of bulges, strand
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
321 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
322 results = [None] * self.nbStrands
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
323 strands = [False, True] if self.nbStrands == 2 else [False]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
324 minNbBulges = 1000000
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
325 for i, reverse in enumerate(strands):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
326 self.writeInputFile(transcript, reverse, self.fivePrimeExtension, self.threePrimeExtension)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
327 self.startRnaFold()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
328 output = self.parseRnaFoldOutput()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
329 results[i] = self.analyzeRnaFoldOutput(transcript, output, reverse, self.fivePrimeExtension, self.threePrimeExtension)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
330 minNbBulges = min(minNbBulges, results[i][1])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
331 for result in results:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
332 if result[1] == minNbBulges:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
333 return result
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
334 return None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
335
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
336
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
337 def refold(self, transcript):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
338 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
339 Fold a transcript, knowing where the miRNA starts and end
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
340 @ivar transcript: a transcript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
341 @type transcript: class L{Transcript<Transcript>}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
342 @return: the energy
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
343 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
344 miStar = transcript.getTagValue("miRnaStar")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
345 startMiStar = int(miStar.split("-")[0])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
346 endMiStart = int(miStar.split("-")[1])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
347 fivePrimeExtension = max(0, transcript.start - startMiStar) + 5
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
348 threePrimeExtension = max(0, endMiStart - transcript.end) + 5
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
349 self.writeInputFile(transcript, False, fivePrimeExtension, threePrimeExtension)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
350 self.startRnaFold()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
351 output = self.parseRnaFoldOutput()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
352 result = self.analyzeRnaFoldOutput(transcript, output, False, fivePrimeExtension, threePrimeExtension)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
353 return result[0]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
354
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
355
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
356 def computeResults(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
357 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
358 Fold all and fill an output transcript list with the values
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
359 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
360 progress = Progress(self.inputTranscriptList.getNbTranscripts(), "Handling transcripts", self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
361 self.outputTranscriptList = TranscriptList()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
362 for transcript in self.inputTranscriptList.getIterator():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
363 result = self.fold(transcript)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
364 transcript.setTagValue("nbBulges", result[1])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
365 transcript.setTagValue("miRnaStar", "%d-%d" % (result[2], result[3]))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
366 transcript.setTagValue("miRNAstrand", result[4])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
367 transcript.setTagValue("energy", self.refold(transcript))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
368 self.outputTranscriptList.addTranscript(transcript)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
369 progress.inc()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
370 progress.done()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
371
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
372
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
373 def getResults(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
374 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
375 Get an output transcript list with the values
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
376 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
377 if self.outputTranscriptList == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
378 self.computeResults()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
379 return self.outputTranscriptList