annotate commons/core/seq/BioseqDB.py @ 27:4e7ee5683ef1

Deleted selected files
author m-zytnicki
date Mon, 29 Apr 2013 03:30:11 -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 # Copyright INRA (Institut National de la Recherche Agronomique)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
2 # http://www.inra.fr
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
3 # http://urgi.versailles.inra.fr
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
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 re
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 from commons.core.seq.Bioseq import Bioseq
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 from commons.core.stat.Stat import Stat
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 ## Handle a collection of a Bioseq (header-sequence)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 class BioseqDB( 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, name="" ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 self.idx = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44 self.idx_renamed = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 self.db = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 self.name = name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 if name != "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 faFile = open( name )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 self.read( faFile )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 faFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 self.mean_seq_lgth = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 self.stat = Stat()
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 ## Equal operator
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57 def __eq__( self, o ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 selfSize = self.getSize()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 if selfSize != o.getSize():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 return False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 nbEqualInstances = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 for i in self.db:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 atLeastOneIsEqual = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 for j in o.db:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 if i == j:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 atLeastOneIsEqual = True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 continue
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 if atLeastOneIsEqual:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 nbEqualInstances += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 if nbEqualInstances == selfSize:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 return True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 return False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 ## Change the name of the BioseqDB
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 # @param name the BioseqDB name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 def setName(self, name):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 self.name = name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 ## Record each sequence of the input file as a list of Bioseq instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 # @param faFileHandler handler of a fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 def read( self, faFileHandler ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 while True:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 seq = Bioseq()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 seq.read( faFileHandler )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 if seq.sequence == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 self.add( seq )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 ## Write all Bioseq of BioseqDB in a formatted fasta file (60 character long)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 # @param faFileHandler file handler of a fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 def write( self, faFileHandler ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 for bs in self.db:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 bs.writeABioseqInAFastaFile( faFileHandler )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 ## Write all Bioseq of BioseqDB in a formatted fasta file (60 character long)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 # @param outFaFileName file name of fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 # @param mode 'write' or 'append'
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 def save( self, outFaFileName, mode="w" ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 outFaFile = open( outFaFileName, mode )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 self.write( outFaFile )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 outFaFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 ## Read a formatted fasta file and load it in the BioseqDB instance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 # @param inFaFileName file name of fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 def load(self, inFaFileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 fichier = open(inFaFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 self.read(fichier)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 fichier.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 ## Reverse each sequence of the collection
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128 def reverse( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 for bs in self.db:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 bs.reverse()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133 ## Turn each sequence into its complement
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 def complement( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136 for bs in self.db:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137 bs.complement()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 ## Reverse and complement each sequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 def reverseComplement( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143 for bs in self.db:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144 bs.reverseComplement()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147 ## Set the collection from a list of Bioseq instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149 def setData( self, lBioseqs ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150 for i in lBioseqs:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151 self.add( i )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
154 ## Initialization of each attribute of the collection
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
155 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
156 def reset( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
157 self.db = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
158 self.idx = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
159 self.name = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
160 self.mean_seq_lgth = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
161 self.stat.reset()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
162
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
163
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
164 ## Remove all the gap of the sequences of the collection
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
165 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
166 def cleanGap(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
167 for iBioSeq in self.db:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
168 iBioSeq.cleanGap()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
169
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
170
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
171 ## Add a Bioseq instance and update the attributes
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
172 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
173 # @param bs a Bioseq instance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
174 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
175 def add( self, bs ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
176 if self.idx.has_key( bs.header ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
177 sys.stderr.write( "ERROR: two sequences with same header '%s'\n" % ( bs.header ) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
178 sys.exit(1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
179 self.db.append( bs )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
180 self.idx[ bs.header ] = len(self.db) - 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
181 self.idx_renamed[ bs.header.replace("::","-").replace(":","-").replace(",","-").replace(" ","_") ] = len(self.db) - 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
182
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
183
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
184 ## Give the Bioseq instance corresponding to specified index
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
185 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
186 # @return a Bioseq instance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
187 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
188 def __getitem__(self,index):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
189 if index < len(self.db):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
190 return self.db[index]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
191
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
192
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
193 ## Give the number of sequences in the bank
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
194 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
195 # @return an integer
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
196 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
197 def getSize( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
198 return len( self.db )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
199
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
200
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
201 ## Give the cumulative sequence length in the bank
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
202 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
203 # @return an integer
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
204 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
205 def getLength( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
206 cumLength = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
207 for iBioseq in self.db:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
208 cumLength += iBioseq.getLength()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
209
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
210 return cumLength
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
211
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
212
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
213 ## Return the length of a given sequence via its header
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
214 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
215 # @return an integer
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
216 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
217 def getSeqLength( self, header ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
218 return self.fetch(header).getLength()
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 ## Return a list with the sequence headers
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
222 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
223 def getHeaderList( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
224 lHeaders = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
225 for bs in self.db:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
226 lHeaders.append( bs.header )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
227 return lHeaders
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 ## Return a list with the sequences
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
231 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
232 def getSequencesList( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
233 lSeqs = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
234 for bs in self.db:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
235 lSeqs.append( bs.getSequence() )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
236 return lSeqs
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 ## Give the Bioseq instance of the BioseqDB specified by its header
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
240 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
241 # @warning name of this method not appropriate getBioseqByHeader is proposed
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
242 # @param header string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
243 # @return a Bioseq instance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
244 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
245 def fetch( self, header ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
246 return self.db[self.idx[header]]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
247
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
248
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
249 ## Give the Bioseq instance of the BioseqDB specified by its renamed header
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
250 # In renamed header "::", ":", "," character are been replaced by "-" and " " by "_"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
251 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
252 # @param renamedHeader string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
253 # @return a Bioseq instance
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
254 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
255 def getBioseqByRenamedHeader( self, renamedHeader ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
256 return self.db[self.idx_renamed[renamedHeader]]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
257
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
258
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
259 ## Count the number of times the given nucleotide is present in the bank.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
260 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
261 # @param nt character (nt or aa)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
262 # @return an integer
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
263 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
264 def countNt( self, nt ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
265 total = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
266 for iBioseq in self.db:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
267 total+= iBioseq.countNt( nt )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
268 return total
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
269
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
270
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
271 ## Count the number of times each nucleotide (A,T,G,C,N) is present in the bank.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
272 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
273 # @return a dictionary with nucleotide as key and an integer as values
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
274 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
275 def countAllNt( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
276 dNt2Count = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
277 for nt in ["A","T","G","C","N"]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
278 dNt2Count[ nt ] = self.countNt( nt )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
279 return dNt2Count
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
280
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
281
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
282 ## Extract a sub BioseqDB of specified size which beginning at specified start
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
283 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
284 # @param start integer index of first included Bioseq
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
285 # @param size integer size of expected BioseqDB
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
286 # @return a BioseqDB
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
287 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
288 def extractPart(self, start, size):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
289 iShorterBioseqDB = BioseqDB()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
290 for iBioseq in self.db[start:(start + size)]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
291 iShorterBioseqDB.add(iBioseq)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
292 return iShorterBioseqDB
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
293
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
294
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
295 ## Extract a sub BioseqDB with the specified number of best length Bioseq
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
296 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
297 # @param numBioseq integer the number of Bioseq searched
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
298 # @return a BioseqDB
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
299 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
300 def bestLength(self, numBioseq):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
301 length_list = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
302 numseq = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
303 for each_seq in self.db:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
304 if each_seq.sequence == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
305 l=0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
306 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
307 l = each_seq.getLength()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
308 length_list.append(l)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
309 numseq = numseq + 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
310
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
311 length_list.sort()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
312 size = len(length_list)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
313 if numBioseq < size:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
314 len_min = length_list[size-numBioseq]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
315 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
316 len_min = length_list[0]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
317
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
318 numseq = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
319 nbsave = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
320 bestSeqs = BioseqDB()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
321 bestSeqs.setName(self.name)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
322 for each_seq in self.db:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
323 if each_seq.sequence == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
324 l=0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
325 else :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
326 l = each_seq.getLength()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
327 numseq = numseq + 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
328 if l >= len_min:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
329 bestSeqs.add(each_seq)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
330 nbsave = nbsave + 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
331 if nbsave == numBioseq :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
332 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
333 return bestSeqs
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
334
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
335
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
336 ## Extract a sub BioseqDB from a file with Bioseq header containing the specified pattern
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
337 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
338 # @param pattern regular expression of wished Bioseq header
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
339 # @param inFileName name of fasta file in which we want extract the BioseqDB
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
340 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
341 def extractPatternOfFile(self, pattern, inFileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
342 if pattern=="" :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
343 return
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
344 srch=re.compile(pattern)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
345 file_db=open(inFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
346 numseq=0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
347 nbsave=0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
348 while 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
349 seq=Bioseq()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
350 seq.read(file_db)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
351 if seq.sequence==None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
352 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
353 numseq+=1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
354 m=srch.search(seq.header)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
355 if m:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
356 self.add(seq)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
357 nbsave+=1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
358 file_db.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
359
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
360
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
361 ## Extract a sub BioseqDB from the instance with all Bioseq header containing the specified pattern
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
362 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
363 # @param pattern regular expression of wished Bioseq header
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
364 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
365 # @return a BioseqDB
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
366 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
367 def getByPattern(self,pattern):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
368 if pattern=="" :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
369 return
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
370 iBioseqDB=BioseqDB()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
371 srch=re.compile(pattern)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
372 for iBioseq in self.db:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
373 if srch.search(iBioseq.header):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
374 iBioseqDB.add(iBioseq)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
375 return iBioseqDB
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
376
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
377
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
378 ## Extract a sub BioseqDB from the instance with all Bioseq header not containing the specified pattern
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
379 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
380 # @param pattern regular expression of not wished Bioseq header
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
381 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
382 # @return a BioseqDB
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
383 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
384 def getDiffFromPattern(self,pattern):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
385 if pattern=="" :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
386 return
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
387 iBioseqDB=BioseqDB()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
388 srch=re.compile(pattern)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
389 for iBioseq in self.db:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
390 if not srch.search(iBioseq.header):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
391 iBioseqDB.add(iBioseq)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
392 return iBioseqDB
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
393
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
394 #TODO: to run several times to remove all concerned sequences when big data. How to fix it ?
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
395 ## Remove from the instance all Bioseq which header contains the specified pattern
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
396 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
397 # @param pattern regular expression of not wished Bioseq header
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
398 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
399 def rmByPattern(self,pattern):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
400 if pattern=="" :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
401 return
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
402 srch=re.compile(pattern)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
403 for seq in self.db:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
404 if srch.search(seq.header):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
405 self.db.remove(seq)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
406
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
407
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
408 ## Copy a part from another BioseqDB in the BioseqDB if Bioseq have got header containing the specified pattern
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
409 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
410 # @warning this method is called extractPattern in pyRepet.seq.BioseqDB
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
411 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
412 # @param pattern regular expression of wished Bioseq header
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
413 # @param sourceBioseqDB the BioseqDB from which we want extract Bioseq
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
414 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
415 def addBioseqFromABioseqDBIfHeaderContainPattern(self, pattern, sourceBioseqDB):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
416 if pattern=="" :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
417 return
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
418 srch=re.compile(pattern)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
419 for seq in sourceBioseqDB.db:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
420 m=srch.search(seq.header)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
421 if m:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
422 self.add(seq)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
423
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
424
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
425 ## Up-case the sequence characters in all sequences
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
426 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
427 def upCase( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
428 for bs in self.db:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
429 bs.upCase()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
430
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
431
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
432 ## Split each gapped Bioseq in a list and store all in a dictionary
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
433 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
434 # @return a dict, keys are bioseq headers, values are list of Map instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
435 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
436 def getDictOfLMapsWithoutGaps( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
437 dSeq2Maps = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
438
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
439 for bs in self.db:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
440 dSeq2Maps[ bs.header ] = bs.getLMapWhithoutGap()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
441
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
442 return dSeq2Maps
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
443
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
444 ## Give the list of the sequence length in the bank
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
445 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
446 # @return an list
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
447 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
448 def getListOfSequencesLength( self ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
449 lLength = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
450 for iBioseq in self.db:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
451 lLength.append(iBioseq.getLength())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
452
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
453 return lLength
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
454
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
455 ## Return sequence length for a list of sequence header
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
456 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
457 def getSeqLengthByListOfName( self, lHeaderName ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
458 lseqLength=[]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
459 for headerName in lHeaderName:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
460 lseqLength.append(self.getSeqLength( headerName ))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
461 return lseqLength