annotate commons/core/seq/FastaUtils.py @ 32:3441fe98a2ba

Deleted selected files
author m-zytnicki
date Tue, 30 Apr 2013 14:34:10 -0400
parents 94ab73e8a190
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 os
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 import sys
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 import string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 import math
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 import shutil
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 import re
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 import glob
18
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
39 from operator import itemgetter
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 from commons.core.seq.BioseqDB import BioseqDB
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 from commons.core.seq.Bioseq import Bioseq
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 from commons.core.coord.MapUtils import MapUtils
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 from commons.core.coord.Range import Range
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44 from commons.core.checker.CheckerUtils import CheckerUtils
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 from commons.core.launcher.LauncherUtils import LauncherUtils
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 from commons.core.coord.ConvCoord import ConvCoord
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 from commons.core.parsing.FastaParser import FastaParser
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 ## Static methods for fasta file manipulation
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 class FastaUtils( object ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 ## Count the number of sequences in the input fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 # @param inFile name of the input fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 # @return integer number of sequences in the input fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 @staticmethod
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 def dbSize( inFile ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 nbSeq = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 inFileHandler = open( inFile, "r" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 line = inFileHandler.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 while line:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 if line[0] == ">":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 nbSeq = nbSeq + 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 line = inFileHandler.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 inFileHandler.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 return nbSeq
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 ## Compute the cumulative sequence length in the input fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 # @param inFile handler of the input fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 @staticmethod
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 def dbCumLength( inFile ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 cumLength = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 line = inFile.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 while line:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 if line[0] != ">":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 cumLength += len(string.rstrip(line))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 line = inFile.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 return cumLength
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 ## Return a list with the length of each sequence in the input fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 # @param inFile string name of the input fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 @staticmethod
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 def dbLengths( inFile ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 lLengths = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 inFileHandler = open( inFile, "r" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 currentLength = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 line = inFileHandler.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 while line:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 if line[0] == ">":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 if currentLength != 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 lLengths.append( currentLength )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 currentLength = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 currentLength += len(line[:-1])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 line = inFileHandler.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 lLengths.append( currentLength )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 inFileHandler.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 return lLengths
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 ## Retrieve the sequence headers present in the input fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 # @param inFile string name of the input fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 # @param verbose integer level of verbosity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 # @return list of sequence headers
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 @staticmethod
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 def dbHeaders( inFile, verbose=0 ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 lHeaders = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 inFileHandler = open( inFile, "r" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125 line = inFileHandler.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 while line:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 if line[0] == ">":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128 lHeaders.append( string.rstrip(line[1:]) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 if verbose > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 print string.rstrip(line[1:])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 line = inFileHandler.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 inFileHandler.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 return lHeaders
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137 ## Cut a data bank into chunks according to the input parameters
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138 # If a sequence is shorter than the threshold, it is only renamed (not cut)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 # @param inFileName string name of the input fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 # @param chkLgth string chunk length (in bp, default=200000)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 # @param chkOver string chunk overlap (in bp, default=10000)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143 # @param wordN string N stretch word length (default=11, 0 for no detection)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144 # @param outFilePrefix string prefix of the output files (default=inFileName + '_chunks.fa' and '_chunks.map')
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145 # @param clean boolean remove 'cut' and 'Nstretch' files
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146 # @param verbose integer (default = 0)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148 @staticmethod
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149 def dbChunks( inFileName, chkLgth="200000", chkOver="10000", wordN="11", outFilePrefix="", clean=False, verbose=0 ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150 nbSeq = FastaUtils.dbSize( inFileName )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151 if verbose > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152 print "cut the %i input sequences with cutterDB..." % ( nbSeq )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153 sys.stdout.flush()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
154
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
155 prg = "cutterDB"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
156 cmd = prg
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
157 cmd += " -l %s" % ( chkLgth )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
158 cmd += " -o %s" %( chkOver )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
159 cmd += " -w %s" % ( wordN )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
160 cmd += " %s" % ( inFileName )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
161 returnStatus = os.system( cmd )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
162 if returnStatus != 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
163 msg = "ERROR: '%s' returned '%i'" % ( prg, returnStatus )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
164 sys.stderr.write( "%s\n" % ( msg ) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
165 sys.exit(1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
166
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
167 nbChunks = FastaUtils.dbSize( "%s_cut" % ( inFileName ) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
168 if verbose > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
169 print "done (%i chunks)" % ( nbChunks )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
170 sys.stdout.flush()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
171
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
172 if verbose > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
173 print "rename the headers..."
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
174 sys.stdout.flush()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
175
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
176 if outFilePrefix == "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
177 outFastaName = inFileName + "_chunks.fa"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
178 outMapName = inFileName + "_chunks.map"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
179 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
180 outFastaName = outFilePrefix + ".fa"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
181 outMapName = outFilePrefix + ".map"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
182
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
183 inFile = open( "%s_cut" % ( inFileName ), "r" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
184 line = inFile.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
185
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
186 outFasta = open( outFastaName, "w" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
187 outMap = open( outMapName, "w" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
188
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
189 # read line after line (no need for big RAM) and change the sequence headers
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
190 while line:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
191
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
192 if line[0] == ">":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
193 if verbose > 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
194 print "rename '%s'" % ( line[:-1] ); sys.stdout.flush()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
195 data = line[:-1].split(" ")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
196 seqID = data[0].split(">")[1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
197 newHeader = "chunk%s" % ( str(seqID).zfill( len(str(nbChunks)) ) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
198 oldHeader = data[2]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
199 seqStart = data[4].split("..")[0]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
200 seqEnd = data[4].split("..")[1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
201 outMap.write( "%s\t%s\t%s\t%s\n" % ( newHeader, oldHeader, seqStart, seqEnd ) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
202 outFasta.write( ">%s\n" % ( newHeader ) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
203
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
204 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
205 outFasta.write( line.upper() )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
206
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
207 line = inFile.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
208
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
209 inFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
210 outFasta.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
211 outMap.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
212
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
213 if clean == True:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
214 os.remove(inFileName + "_cut")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
215 os.remove(inFileName + ".Nstretch.map")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
216
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
217
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
218 ## Split the input fasta file in several output files
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
219 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
220 # @param inFile string name of the input fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
221 # @param nbSeqPerBatch integer number of sequences per output file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
222 # @param newDir boolean put the sequences in a new directory called 'batches'
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
223 # @param useSeqHeader boolean use sequence header (only if 'nbSeqPerBatch=1')
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
224 # @param prefix prefix in output file name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
225 # @param verbose integer verbosity level (default = 0)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
226 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
227 @staticmethod
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
228 def dbSplit( inFile, nbSeqPerBatch, newDir, useSeqHeader=False, prefix="batch", verbose=0 ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
229 if not os.path.exists( inFile ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
230 msg = "ERROR: file '%s' doesn't exist" % ( inFile )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
231 sys.stderr.write( "%s\n" % ( msg ) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
232 sys.exit(1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
233
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
234 nbSeq = FastaUtils.dbSize( inFile )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
235
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
236 nbBatches = int( math.ceil( nbSeq / float(nbSeqPerBatch) ) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
237 if verbose > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
238 print "save the %i input sequences into %i batches" % ( nbSeq, nbBatches )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
239 sys.stdout.flush()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
240
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
241 if nbSeqPerBatch > 1 and useSeqHeader:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
242 useSeqHeader = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
243
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
244 if newDir == True:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
245 if os.path.exists( "batches" ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
246 shutil.rmtree( "batches" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
247 os.mkdir( "batches" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
248 os.chdir( "batches" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
249 os.system( "ln -s ../%s ." % ( inFile ) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
250
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
251 inFileHandler = open( inFile, "r" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
252 inFileHandler.seek( 0, 0 )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
253 countBatch = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
254 countSeq = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
255 line = inFileHandler.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
256 while line:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
257 if line == "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
258 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
259 if line[0] == ">":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
260 countSeq += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
261 if nbSeqPerBatch == 1 or countSeq % nbSeqPerBatch == 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
262 if "outFile" in locals():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
263 outFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
264 countBatch += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
265 if nbSeqPerBatch == 1 and useSeqHeader:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
266 outFileName = "%s.fa" % ( line[1:-1].replace(" ","_") )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
267 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
268 outFileName = "%s_%s.fa" % ( prefix, str(countBatch).zfill( len(str(nbBatches)) ) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
269 outFile = open( outFileName, "w" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
270 if verbose > 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
271 print "saving seq '%s' in file '%s'..." % ( line[1:40][:-1], outFileName )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
272 sys.stdout.flush()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
273 outFile.write( line )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
274 line = inFileHandler.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
275 inFileHandler.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
276
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
277 if newDir == True:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
278 os.remove( os.path.basename( inFile ) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
279 os.chdir( ".." )
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 ## Split the input fasta file in several output files
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
283 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
284 # @param inFileName string name of the input fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
285 # @param maxSize integer max cumulative length for each output file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
286 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
287 @staticmethod
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
288 def splitFastaFileInBatches(inFileName, maxSize = 200000):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
289 iBioseqDB = BioseqDB(inFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
290 lHeadersSizeTuples = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
291 for iBioseq in iBioseqDB.db:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
292 lHeadersSizeTuples.append((iBioseq.getHeader(), iBioseq.getLength()))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
293
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
294 lHeadersList = LauncherUtils.createHomogeneousSizeList(lHeadersSizeTuples, maxSize)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
295 os.mkdir("batches")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
296 os.chdir("batches")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
297
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
298 iterator = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
299 for lHeader in lHeadersList :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
300 iterator += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
301 with open("batch_%s.fa" % iterator, 'w') as f :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
302 for header in lHeader :
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
303 iBioseq = iBioseqDB.fetch(header)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
304 iBioseq.write(f)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
305 os.chdir("..")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
306
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
307
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
308 ## Split the input fasta file in several output files according to their cluster identifier
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
309 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
310 # @param inFileName string name of the input fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
311 # @param clusteringMethod string name of the clustering method (Grouper, Recon, Piler, Blastclust)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
312 # @param simplifyHeader boolean simplify the headers
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
313 # @param createDir boolean put the sequences in different directories
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
314 # @param outPrefix string prefix of the output files (default='seqCluster')
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
315 # @param verbose integer (default = 0)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
316 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
317 @staticmethod
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
318 def splitSeqPerCluster( inFileName, clusteringMethod, simplifyHeader, createDir, outPrefix="seqCluster", verbose=0 ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
319 if not os.path.exists( inFileName ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
320 print "ERROR: %s doesn't exist" % ( inFileName )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
321 sys.exit(1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
322
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
323 inFile = open( inFileName, "r" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
324
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
325 line = inFile.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
326 if line:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
327 name = line.split(" ")[0]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
328 if "Cluster" in name:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
329 clusterID = name.split("Cluster")[1].split("Mb")[0]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
330 seqID = name.split("Mb")[1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
331 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
332 clusterID = name.split("Cl")[0].split("Gr")[1] # the notion of 'group' in Grouper corresponds to 'cluster' in Piler, Recon and Blastclust
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
333 if "Q" in name.split("Gr")[0]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
334 seqID = name.split("Gr")[0].split("MbQ")[1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
335 elif "S" in name:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
336 seqID = name.split("Gr")[0].split("MbS")[1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
337 sClusterIDs = set( [ clusterID ] )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
338 if simplifyHeader == True:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
339 header = "%s_Cluster%s_Seq%s" % ( clusteringMethod, clusterID, seqID )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
340 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
341 header = line[1:-1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
342 if createDir == True:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
343 if not os.path.exists( "%s_cluster_%s" % ( inFileName, clusterID ) ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
344 os.mkdir( "%s_cluster_%s" % ( inFileName, clusterID ) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
345 os.chdir( "%s_cluster_%s" % ( inFileName, clusterID ) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
346 outFileName = "%s%s.fa" % ( outPrefix, clusterID )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
347 outFile = open( outFileName, "w" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
348 outFile.write( ">%s\n" % ( header ) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
349 prevClusterID = clusterID
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
350
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
351 line = inFile.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
352 while line:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
353 if line[0] == ">":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
354 name = line.split(" ")[0]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
355 if "Cluster" in name:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
356 clusterID = name.split("Cluster")[1].split("Mb")[0]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
357 seqID = name.split("Mb")[1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
358 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
359 clusterID = name.split("Cl")[0].split("Gr")[1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
360 if "Q" in name.split("Gr")[0]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
361 seqID = name.split("Gr")[0].split("MbQ")[1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
362 elif "S" in name:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
363 seqID = name.split("Gr")[0].split("MbS")[1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
364
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
365 if clusterID != prevClusterID:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
366 outFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
367
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
368 if simplifyHeader == True:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
369 header = "%s_Cluster%s_Seq%s" % ( clusteringMethod, clusterID, seqID )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
370 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
371 header = line[1:-1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
372
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
373 if createDir == True:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
374 os.chdir( ".." )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
375 if not os.path.exists( "%s_cluster_%s" % ( inFileName, clusterID ) ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
376 os.mkdir( "%s_cluster_%s" % ( inFileName, clusterID ) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
377 os.chdir( "%s_cluster_%s" % ( inFileName, clusterID ) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
378
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
379 outFileName = "%s%s.fa" % ( outPrefix, clusterID )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
380 if not os.path.exists( outFileName ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
381 outFile = open( outFileName, "w" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
382 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
383 if clusterID != prevClusterID:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
384 outFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
385 outFile = open( outFileName, "a" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
386 outFile.write( ">%s\n" % ( header ) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
387 prevClusterID = clusterID
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
388 sClusterIDs.add( clusterID )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
389
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
390 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
391 outFile.write( line )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
392
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
393 line = inFile.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
394
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
395 outFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
396 if verbose > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
397 print "number of clusters: %i" % ( len(sClusterIDs) ); sys.stdout.flush()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
398
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
399 if createDir == True:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
400 os.chdir("..")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
401 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
402 print "WARNING: empty input file - no cluster found"; sys.stdout.flush()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
403
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
404
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
405 ## Filter a fasta file in two fasta files using the length of each sequence as a criteron
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
406 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
407 # @param len_min integer length sequence criterion to filter
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
408 # @param inFileName string name of the input fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
409 # @param verbose integer (default = 0)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
410 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
411 @staticmethod
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
412 def dbLengthFilter( len_min, inFileName, verbose=0 ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
413 file_db = open( inFileName, "r" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
414 file_dbInf = open( inFileName+".Inf"+str(len_min), "w" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
415 file_dbSup = open( inFileName+".Sup"+str(len_min), "w" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
416 seq = Bioseq()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
417 numseq = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
418 nbsave = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
419
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
420 seq.read( file_db )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
421 while seq.sequence:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
422 l = seq.getLength()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
423 numseq = numseq + 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
424 if l >= len_min:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
425 seq.write( file_dbSup )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
426 if verbose > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
427 print 'sequence #',numseq,'=',l,'[',seq.header[0:40],'...] Sup !!'
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
428 nbsave=nbsave+1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
429 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
430 seq.write( file_dbInf )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
431 if verbose > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
432 print 'sequence #',numseq,'=',l,'[',seq.header[0:40],'...] Inf !!'
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
433 nbsave=nbsave+1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
434 seq.read( file_db )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
435
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
436 file_db.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
437 file_dbInf.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
438 file_dbSup.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
439 if verbose > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
440 print nbsave,'saved sequences in ',inFileName+".Inf"+str(len_min)," and ", inFileName+".Sup"+str(len_min)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
441
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
442
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
443 ## Extract the longest sequences from a fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
444 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
445 # @param num integer maximum number of sequences in the output file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
446 # @param inFileName string name of the input fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
447 # @param outFileName string name of the output fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
448 # @param minThresh integer minimum length threshold (default=0)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
449 # @param verbose integer (default = 0)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
450 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
451 @staticmethod
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
452 def dbLongestSequences( num, inFileName, outFileName="", verbose=0, minThresh=0 ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
453 bsDB = BioseqDB( inFileName )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
454 if verbose > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
455 print "nb of input sequences: %i" % ( bsDB.getSize() )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
456
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
457 if outFileName == "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
458 outFileName = inFileName + ".best" + str(num)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
459 outFile = open( outFileName, "w" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
460
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
461 if bsDB.getSize()==0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
462 return 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
463
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
464 num = int(num)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
465 if verbose > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
466 print "keep the %i longest sequences" % ( num )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
467 if minThresh > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
468 print "with length > %i bp" % ( minThresh )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
469 sys.stdout.flush()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
470
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
471 # retrieve the length of each input sequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
472 tmpLSeqLgth = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
473 seqNum = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
474 for bs in bsDB.db:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
475 seqNum += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
476 tmpLSeqLgth.append( bs.getLength() )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
477 if verbose > 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
478 print "%d seq %s : %d bp" % ( seqNum, bs.header[0:40], bs.getLength() )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
479 sys.stdout.flush()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
480
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
481 # sort the lengths
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
482 tmpLSeqLgth.sort()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
483 tmpLSeqLgth.reverse()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
484
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
485 # select the longest
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
486 lSeqLgth = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
487 for i in xrange( 0, min(num,len(tmpLSeqLgth)) ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
488 if tmpLSeqLgth[i] >= minThresh:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
489 lSeqLgth.append( tmpLSeqLgth[i] )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
490 if verbose > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
491 print "selected max length: %i" % ( max(lSeqLgth) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
492 print "selected min length: %i" % ( min(lSeqLgth) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
493 sys.stdout.flush()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
494
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
495 # save the longest
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
496 inFile = open( inFileName )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
497 seqNum = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
498 nbSave = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
499 for bs in bsDB.db:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
500 seqNum += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
501 if bs.getLength() >= min(lSeqLgth) and bs.getLength() >= minThresh:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
502 bs.write( outFile )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
503 if verbose > 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
504 print "%d seq %s : saved !" % ( seqNum, bs.header[0:40] )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
505 sys.stdout.flush()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
506 nbSave += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
507 if nbSave == num:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
508 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
509 inFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
510 outFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
511 if verbose > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
512 print nbSave, "saved sequences in ", outFileName
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
513 sys.stdout.flush()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
514
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
515 return 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
516
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
517
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
518 ## Extract all the sequence headers from a fasta file and write them in a new fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
519 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
520 # @param inFileName string name of the input fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
521 # @param outFileName string name of the output file recording the headers (default = inFileName + '.headers')
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
522 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
523 @staticmethod
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
524 def dbExtractSeqHeaders( inFileName, outFileName="" ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
525 lHeaders = FastaUtils.dbHeaders( inFileName )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
526
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
527 if outFileName == "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
528 outFileName = inFileName + ".headers"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
529
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
530 outFile = open( outFileName, "w" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
531 for i in lHeaders:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
532 outFile.write( i + "\n" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
533 outFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
534
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
535 return 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
536
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
537
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
538 ## Extract sequences and their headers selected by a given pattern from a fasta file and write them in a new fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
539 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
540 # @param pattern regular expression to search in headers
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
541 # @param inFileName string name of the input fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
542 # @param outFileName string name of the output file recording the selected bioseq (default = inFileName + '.extracted')
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
543 # @param verbose integer verbosity level (default = 0)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
544 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
545 @staticmethod
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
546 def dbExtractByPattern( pattern, inFileName, outFileName="", verbose=0 ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
547 if pattern == "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
548 return
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
549
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
550 if outFileName == "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
551 outFileName = inFileName + '.extracted'
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
552 outFile = open( outFileName, 'w' )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
553
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
554 patternTosearch = re.compile( pattern )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
555 bioseq = Bioseq()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
556 bioseqNb = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
557 savedBioseqNb = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
558 inFile = open( inFileName, "r" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
559 bioseq.read( inFile )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
560 while bioseq.sequence:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
561 bioseqNb = bioseqNb + 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
562 m = patternTosearch.search( bioseq.header )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
563 if m:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
564 bioseq.write( outFile )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
565 if verbose > 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
566 print 'sequence num',bioseqNb,'matched on',m.group(),'[',bioseq.header[0:40],'...] saved !!'
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
567 savedBioseqNb = savedBioseqNb + 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
568 bioseq.read( inFile )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
569 inFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
570
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
571 outFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
572
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
573 if verbose > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
574 print "%i sequences saved in file '%s'" % ( savedBioseqNb, outFileName )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
575
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
576
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
577 ## Extract sequences and their headers selected by patterns contained in a file, from a fasta file and write them in a new fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
578 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
579 # @param patternFileName string file containing regular expression to search in headers
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
580 # @param inFileName string name of the input fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
581 # @param outFileName string name of the output file recording the selected bioseq (default = inFileName + '.extracted')
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
582 # @param verbose integer verbosity level (default = 0)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
583 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
584 @staticmethod
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
585 def dbExtractByFilePattern( patternFileName, inFileName, outFileName="", verbose=0 ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
586
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
587 if patternFileName == "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
588 print "ERROR: no file of pattern"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
589 sys.exit(1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
590
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
591 bioseq = Bioseq()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
592 bioseqNb = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
593 savedBioseqNb = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
594 lHeaders = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
595
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
596 inFile = open( inFileName, "r" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
597 bioseq.read( inFile )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
598 while bioseq.sequence != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
599 lHeaders.append( bioseq.header )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
600 bioseq.read( inFile )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
601 inFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
602
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
603 lHeadersToKeep = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
604 patternFile = open( patternFileName, "r" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
605 for pattern in patternFile:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
606 if verbose > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
607 print "pattern: ",pattern[:-1]; sys.stdout.flush()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
608
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
609 patternToSearch = re.compile(pattern[:-1])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
610 for h in lHeaders:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
611 if patternToSearch.search(h):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
612 lHeadersToKeep.append(h)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
613 patternFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
614
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
615 if outFileName == "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
616 outFileName = inFileName + ".extracted"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
617 outFile=open( outFileName, "w" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
618
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
619 inFile = open( inFileName, "r" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
620 bioseq.read(inFile)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
621 while bioseq.sequence:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
622 bioseqNb += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
623 if bioseq.header in lHeadersToKeep:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
624 bioseq.write(outFile)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
625 if verbose > 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
626 print 'sequence num',bioseqNb,'[',bioseq.header[0:40],'...] saved !!'; sys.stdout.flush()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
627 savedBioseqNb += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
628 bioseq.read(inFile)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
629 inFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
630
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
631 outFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
632
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
633 if verbose > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
634 print "%i sequences saved in file '%s'" % ( savedBioseqNb, outFileName )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
635
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
636
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
637 ## Extract sequences and their headers not selected by a given pattern from a fasta file and write them in a new fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
638 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
639 # @param pattern regular expression to search in headers
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
640 # @param inFileName string name of the input fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
641 # @param outFileName string name of the output file recording the selected bioseq (default = inFileName + '.extracted')
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
642 # @param verbose integer verbosity level (default = 0)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
643 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
644 @staticmethod
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
645 def dbCleanByPattern( pattern, inFileName, outFileName="", verbose=0 ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
646 if pattern == "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
647 return
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
648
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
649 patternToSearch = re.compile(pattern)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
650
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
651 if outFileName == "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
652 outFileName = inFileName + '.cleaned'
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
653 outFile = open(outFileName,'w')
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
654
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
655 bioseq = Bioseq()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
656 bioseqNb = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
657 savedBioseqNb = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
658 inFile = open(inFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
659 bioseq.read(inFile)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
660 while bioseq.sequence != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
661 bioseqNb += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
662 if not patternToSearch.search(bioseq.header):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
663 bioseq.write(outFile)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
664 if verbose > 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
665 print 'sequence num',bioseqNb,'[',bioseq.header[0:40],'...] saved !!'
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
666 savedBioseqNb += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
667 bioseq.read(inFile)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
668 inFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
669
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
670 outFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
671
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
672 if verbose > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
673 print "%i sequences saved in file '%s'" % ( savedBioseqNb, outFileName )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
674
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
675
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
676 ## Extract sequences and their headers not selected by patterns contained in a file, from a fasta file and write them in a new fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
677 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
678 # @param patternFileName string file containing regular expression to search in headers
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
679 # @param inFileName string name of the input fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
680 # @param outFileName string name of the output file recording the selected bioseq (default = inFileName + '.extracted')
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
681 # @param verbose integer verbosity level (default = 0)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
682 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
683 @staticmethod
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
684 def dbCleanByFilePattern( patternFileName, inFileName, outFileName="", verbose=0 ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
685 if patternFileName == "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
686 print "ERROR: no file of pattern"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
687 sys.exit(1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
688
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
689 bioseq = Bioseq()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
690 bioseqNb = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
691 savedBioseqNb = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
692 lHeaders = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
693 inFile = open( inFileName, "r" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
694 bioseq.read( inFile )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
695 while bioseq.sequence != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
696 bioseqNb += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
697 lHeaders.append( bioseq.header )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
698 bioseq.read( inFile )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
699 inFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
700
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
701 patternFile = open( patternFileName, "r")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
702 lHeadersToRemove = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
703 for pattern in patternFile:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
704 if verbose > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
705 print "pattern: ",pattern[:-1]; sys.stdout.flush()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
706
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
707 patternToSearch = re.compile( pattern[:-1] )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
708 for h in lHeaders:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
709 if patternToSearch.search(h):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
710 lHeadersToRemove.append(h)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
711 patternFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
712
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
713 if outFileName == "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
714 outFileName = inFileName + '.cleaned'
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
715 outFile = open( outFileName, 'w' )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
716
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
717 bioseqNum = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
718 inFile=open( inFileName )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
719 bioseq.read( inFile )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
720 while bioseq.sequence != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
721 bioseqNum += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
722 if bioseq.header not in lHeadersToRemove:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
723 bioseq.write( outFile )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
724 if verbose > 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
725 print 'sequence num',bioseqNum,'/',bioseqNb,'[',bioseq.header[0:40],'...] saved !!'; sys.stdout.flush()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
726 savedBioseqNb += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
727 bioseq.read( inFile )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
728 inFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
729
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
730 outFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
731
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
732 if verbose > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
733 print "%i sequences saved in file '%s'" % ( savedBioseqNb, outFileName )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
734
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
735
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
736 ## Find sequence's ORFs from a fasta file and write them in a tab file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
737 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
738 # @param inFileName string name of the input fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
739 # @param orfMaxNb integer Select orfMaxNb best ORFs
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
740 # @param orfMinLength integer Keep ORFs with length > orfMinLength
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
741 # @param outFileName string name of the output fasta file (default = inFileName + '.orf.map')
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
742 # @param verbose integer verbosity level (default = 0)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
743 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
744 @staticmethod
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
745 def dbORF( inFileName, orfMaxNb = 0, orfMinLength = 0, outFileName = "", verbose=0 ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
746 if outFileName == "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
747 outFileName = inFileName + ".ORF.map"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
748 outFile = open( outFileName, "w" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
749
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
750 bioseq = Bioseq()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
751 bioseqNb = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
752
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
753 inFile = open( inFileName )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
754 bioseq.read( inFile )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
755 while bioseq.sequence != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
756 bioseq.upCase()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
757 bioseqNb += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
758 if verbose > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
759 print 'sequence num',bioseqNb,'=',bioseq.getLength(),'[',bioseq.header[0:40],'...]'
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
760
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
761 orf = bioseq.findORF()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
762 bestOrf = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
763 for i in orf.keys():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
764 orfLen = len(orf[i])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
765 for j in xrange(1, orfLen):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
766 start = orf[i][j-1] + 4
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
767 end = orf[i][j] + 3
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
768 if end - start >= orfMinLength:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
769 bestOrf.append( ( end-start, i+1, start, end ) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
770
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
771 bioseq.reverseComplement()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
772
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
773 orf = bioseq.findORF()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
774 seqLen = bioseq.getLength()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
775 for i in orf.keys():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
776 orfLen = len(orf[i])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
777 for j in xrange(1, orfLen):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
778 start = seqLen - orf[i][j-1] - 3
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
779 end = seqLen - orf[i][j] - 2
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
780 if start - end >= orfMinLength:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
781 bestOrf.append( ( start-end, (i+1)*-1, start, end ) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
782
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
783 bestOrf.sort()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
784 bestOrf.reverse()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
785 bestOrfNb = len(bestOrf)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
786 if orfMaxNb != 0 and orfMaxNb < bestOrfNb:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
787 bestOrfNb = orfMaxNb
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
788 for i in xrange(0, bestOrfNb):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
789 if verbose > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
790 print bestOrf[i]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
791 outFile.write("%s\t%s\t%d\t%d\n"%("ORF|"+str(bestOrf[i][1])+\
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
792 "|"+str(bestOrf[i][0]),bioseq.header,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
793 bestOrf[i][2],bestOrf[i][3]))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
794 bioseq.read( inFile )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
795
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
796 inFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
797 outFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
798
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
799 return 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
800
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
801
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
802 ## Sort sequences by increasing length (write a new file)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
803 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
804 # @param inFileName string name of the input fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
805 # @param outFileName string name of the output fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
806 # @param verbose integer verbosity level
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
807 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
808 @staticmethod
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
809 def sortSequencesByIncreasingLength(inFileName, outFileName, verbose=0):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
810 if verbose > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
811 print "sort sequences by increasing length"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
812 sys.stdout.flush()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
813 if not os.path.exists( inFileName ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
814 print "ERROR: file '%s' doesn't exist" % ( inFileName )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
815 sys.exit(1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
816
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
817 # read each seq one by one
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
818 # save them in distinct temporary files
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
819 # with their length in the name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
820 inFileHandler = open( inFileName, "r" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
821 countSeq = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
822 bs = Bioseq()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
823 bs.read( inFileHandler )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
824 while bs.header:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
825 countSeq += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
826 tmpFile = "%ibp_%inb" % ( bs.getLength(), countSeq )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
827 bs.appendBioseqInFile( tmpFile )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
828 if verbose > 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
829 print "%s (%i bp) saved in '%s'" % ( bs.header, bs.getLength(), tmpFile )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
830 bs.header = ""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
831 bs.sequence = ""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
832 bs.read( inFileHandler )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
833 inFileHandler.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
834
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
835 # sort temporary file names
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
836 # concatenate them into the output file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
837 if os.path.exists( outFileName ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
838 os.remove( outFileName )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
839 lFiles = glob.glob( "*bp_*nb" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
840 lFiles.sort( key=lambda s:int(s.split("bp_")[0]) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
841 for fileName in lFiles:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
842 cmd = "cat %s >> %s" % ( fileName, outFileName )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
843 returnValue = os.system( cmd )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
844 if returnValue != 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
845 print "ERROR while concatenating '%s' with '%s'" % ( fileName, outFileName )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
846 sys.exit(1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
847 os.remove( fileName )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
848
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
849 return 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
850
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
851
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
852 ## Sort sequences by header
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
853 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
854 # @param inFileName string name of the input fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
855 # @param outFileName string name of the output fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
856 # @param verbose integer verbosity level
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
857 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
858 @staticmethod
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
859 def sortSequencesByHeader(inFileName, outFileName = "", verbose = 0):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
860 if outFileName == "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
861 outFileName = "%s_sortByHeaders.fa" % os.path.splitext(inFileName)[0]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
862 iBioseqDB = BioseqDB(inFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
863 f = open(outFileName, "w")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
864 lHeaders = sorted(iBioseqDB.getHeaderList())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
865 for header in lHeaders:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
866 iBioseq = iBioseqDB.fetch(header)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
867 iBioseq.write(f)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
868 f.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
869
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
870
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
871 ## Return a dictionary which keys are the headers and values the length of the sequences
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
872 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
873 # @param inFile string name of the input fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
874 # @param verbose integer verbosity level
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
875 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
876 @staticmethod
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
877 def getLengthPerHeader( inFile, verbose=0 ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
878 dHeader2Length = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
879
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
880 inFileHandler = open( inFile, "r" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
881 currentSeqHeader = ""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
882 currentSeqLength = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
883 line = inFileHandler.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
884 while line:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
885 if line[0] == ">":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
886 if currentSeqHeader != "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
887 dHeader2Length[ currentSeqHeader ] = currentSeqLength
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
888 currentSeqLength = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
889 currentSeqHeader = line[1:-1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
890 if verbose > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
891 print "current header: %s" % ( currentSeqHeader )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
892 sys.stdout.flush()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
893 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
894 currentSeqLength += len( line.replace("\n","") )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
895 line = inFileHandler.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
896 dHeader2Length[ currentSeqHeader ] = currentSeqLength
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
897 inFileHandler.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
898
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
899 return dHeader2Length
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
900
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
901
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
902 ## Convert headers from a fasta file having chunk coordinates
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
903 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
904 # @param inFile string name of the input fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
905 # @param mapFile string name of the map file with the coordinates of the chunks on the chromosomes
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
906 # @param outFile string name of the output file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
907 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
908 @staticmethod
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
909 def convertFastaHeadersFromChkToChr(inFile, mapFile, outFile):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
910 inFileHandler = open(inFile, "r")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
911 outFileHandler = open(outFile, "w")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
912 dChunk2Map = MapUtils.getDictPerNameFromMapFile(mapFile)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
913 iConvCoord = ConvCoord()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
914 line = inFileHandler.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
915 while line:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
916 if line[0] == ">":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
917 if "{Fragment}" in line:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
918 chkName = line.split(" ")[1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
919 chrName = dChunk2Map[chkName].seqname
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
920 lCoordPairs = line.split(" ")[3].split(",")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
921 lRangesOnChk = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
922 for i in lCoordPairs:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
923 iRange = Range(chkName, int(i.split("..")[0]), int(i.split("..")[1]))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
924 lRangesOnChk.append(iRange)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
925 lRangesOnChr = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
926 for iRange in lRangesOnChk:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
927 lRangesOnChr.append(iConvCoord.getRangeOnChromosome(iRange, dChunk2Map))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
928 newHeader = line[1:-1].split(" ")[0]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
929 newHeader += " %s" % chrName
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
930 newHeader += " {Fragment}"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
931 newHeader += " %i..%i" % (lRangesOnChr[0].start, lRangesOnChr[0].end)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
932 for iRange in lRangesOnChr[1:]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
933 newHeader += ",%i..%i" % (iRange.start, iRange.end)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
934 outFileHandler.write(">%s\n" % newHeader)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
935 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
936 chkName = line.split("_")[1].split(" ")[0]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
937 chrName = dChunk2Map[chkName].seqname
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
938 coords = line[line.find("[")+1 : line.find("]")]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
939 start = int(coords.split(",")[0])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
940 end = int(coords.split(",")[1])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
941 iRangeOnChk = Range(chkName, start, end)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
942 iRangeOnChr = iConvCoord.getRangeOnChromosome(iRangeOnChk, dChunk2Map)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
943 newHeader = line[1:-1].split("_")[0]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
944 newHeader += " %s" % chrName
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
945 newHeader += " %s" % line[line.find("(") : line.find(")")+1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
946 newHeader += " %i..%i" % (iRangeOnChr.getStart(), iRangeOnChr.getEnd())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
947 outFileHandler.write(">%s\n" % newHeader)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
948 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
949 outFileHandler.write(line)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
950 line = inFileHandler.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
951 inFileHandler.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
952 outFileHandler.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
953
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
954
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
955 ## Convert a fasta file to a length file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
956 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
957 # @param inFile string name of the input fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
958 # @param outFile string name of the output file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
959 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
960 @staticmethod
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
961 def convertFastaToLength(inFile, outFile = ""):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
962 if outFile == "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
963 outFile = "%s.length" % inFile
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
964
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
965 if inFile != "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
966 with open(inFile, "r") as inFH:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
967 with open(outFile, "w") as outFH:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
968 bioseq = Bioseq()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
969 bioseq.read(inFH)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
970 while bioseq.sequence != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
971 seqLen = bioseq.getLength()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
972 outFH.write("%s\t%d\n" % (bioseq.header.split()[0], seqLen))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
973 bioseq.read(inFH)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
974
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
975
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
976 ## Convert a fasta file to a seq file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
977 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
978 # @param inFile string name of the input fasta file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
979 # @param outFile string name of the output file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
980 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
981 @staticmethod
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
982 def convertFastaToSeq(inFile, outFile = ""):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
983 if outFile == "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
984 outFile = "%s.seq" % inFile
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
985
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
986 if inFile != "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
987 with open(inFile, "r") as inFH:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
988 with open(outFile, "w") as outFH:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
989 bioseq = Bioseq()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
990 bioseq.read(inFH)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
991 while bioseq.sequence != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
992 seqLen = bioseq.getLength()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
993 outFH.write("%s\t%s\t%s\t%d\n" % (bioseq.header.split()[0], \
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
994 bioseq.sequence, bioseq.header, seqLen))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
995 bioseq.read(inFH)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
996
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
997
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
998 ## Splice an input fasta file using coordinates in a Map file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
999 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1000 # @note the coordinates should be merged beforehand!
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1001 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1002 @staticmethod
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1003 def spliceFromCoords( genomeFile, coordFile, obsFile ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1004 genomeFileHandler = open( genomeFile, "r" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1005 obsFileHandler = open( obsFile, "w" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1006 dChr2Maps = MapUtils.getDictPerSeqNameFromMapFile( coordFile )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1007
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1008 bs = Bioseq()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1009 bs.read( genomeFileHandler )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1010 while bs.sequence:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1011 if dChr2Maps.has_key( bs.header ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1012 lCoords = MapUtils.getMapListSortedByIncreasingMinThenMax( dChr2Maps[ bs.header ] )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1013 splicedSeq = ""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1014 currentSite = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1015 for iMap in lCoords:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1016 minSplice = iMap.getMin() - 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1017 if minSplice > currentSite:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1018 splicedSeq += bs.sequence[ currentSite : minSplice ]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1019 currentSite = iMap.getMax()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1020 splicedSeq += bs.sequence[ currentSite : ]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1021 bs.sequence = splicedSeq
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1022 bs.write( obsFileHandler )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1023 bs.read( genomeFileHandler )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1024
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1025 genomeFileHandler.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1026 obsFileHandler.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1027
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1028
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1029 ## Shuffle input sequences (single file or files in a directory)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1030 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1031 @staticmethod
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1032 def dbShuffle( inData, outData, verbose=0 ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1033 if CheckerUtils.isExecutableInUserPath("esl-shuffle"):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1034 prg = "esl-shuffle"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1035 else : prg = "shuffle"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1036 genericCmd = prg + " -d INPUT > OUTPUT"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1037 if os.path.isfile( inData ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1038 if verbose > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1039 print "shuffle input file '%s'" % inData
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1040 cmd = genericCmd.replace("INPUT",inData).replace("OUTPUT",outData)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1041 print cmd
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1042 returnStatus = os.system( cmd )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1043 if returnStatus != 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1044 sys.stderr.write( "ERROR: 'shuffle' returned '%i'\n" % returnStatus )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1045 sys.exit(1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1046
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1047 elif os.path.isdir( inData ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1048 if verbose > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1049 print "shuffle files in input directory '%s'" % inData
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1050 if os.path.exists( outData ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1051 shutil.rmtree( outData )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1052 os.mkdir( outData )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1053 lInputFiles = glob.glob( "%s/*.fa" %( inData ) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1054 nbFastaFiles = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1055 for inputFile in lInputFiles:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1056 nbFastaFiles += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1057 if verbose > 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1058 print "%3i / %3i" % ( nbFastaFiles, len(lInputFiles) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1059 fastaBaseName = os.path.basename( inputFile )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1060 prefix, extension = os.path.splitext( fastaBaseName )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1061 cmd = genericCmd.replace("INPUT",inputFile).replace("OUTPUT","%s/%s_shuffle.fa"%(outData,prefix))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1062 returnStatus = os.system( cmd )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1063 if returnStatus != 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1064 sys.stderr.write( "ERROR: 'shuffle' returned '%i'\n" % returnStatus )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1065 sys.exit(1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1066
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1067
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1068 ## Convert a cluster file (one line = one cluster = one headers list) into a fasta file with cluster info in headers
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1069 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1070 # @param inClusterFileName string input cluster file name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1071 # @param inFastaFileName string input fasta file name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1072 # @param outFileName string output file name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1073 # @param verbosity integer verbosity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1074 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1075 @staticmethod
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1076 def convertClusterFileToFastaFile(inClusterFileName, inFastaFileName, outFileName, clusteringTool = "", verbosity = 0):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1077 dHeader2ClusterClusterMember, clusterIdForSingletonCluster = FastaUtils._createHeader2ClusterMemberDict(inClusterFileName, verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1078 iFastaParser = FastaParser(inFastaFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1079 with open(outFileName, "w") as f:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1080 for iSequence in iFastaParser.getIterator():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1081
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1082 header = iSequence.getName()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1083 if dHeader2ClusterClusterMember.get(header):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1084 cluster = dHeader2ClusterClusterMember[header][0]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1085 member = dHeader2ClusterClusterMember[header][1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1086 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1087 clusterIdForSingletonCluster += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1088 cluster = clusterIdForSingletonCluster
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1089 member = 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1090
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1091 newHeader = "%sCluster%sMb%s_%s" % (clusteringTool, cluster, member, header)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1092 iSequence.setName(newHeader)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1093 f.write(iSequence.printFasta())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1094
18
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1095 @staticmethod
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1096 def _createHeader2ClusterMemberDict(inClusterFileName, verbosity = 0):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1097 dHeader2ClusterClusterMember = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1098 clusterId = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1099 with open(inClusterFileName) as f:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1100 line = f.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1101 while line:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1102 lineWithoutLastChar = line.rstrip()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1103 lHeaders = lineWithoutLastChar.split("\t")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1104 clusterId += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1105 if verbosity > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1106 print "%i sequences in cluster %i" % (len(lHeaders), clusterId)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1107 memberId = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1108 for header in lHeaders:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1109 memberId += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1110 dHeader2ClusterClusterMember[header] = (clusterId, memberId)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1111 line = f.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1112 if verbosity > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1113 print "%i clusters" % clusterId
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1114 return dHeader2ClusterClusterMember, clusterId
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1115
18
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1116 @staticmethod
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1117 def convertClusteredFastaFileToMapFile(fastaFileNameFromClustering, outMapFileName = ""):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1118 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1119 Write a map file from fasta output of clustering tool.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1120 Warning: only works if input fasta headers are formated like LTRharvest fasta output.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1121 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1122 if not outMapFileName:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1123 outMapFileName = "%s.map" % (os.path.splitext(fastaFileNameFromClustering)[0])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1124
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1125 fileDb = open(fastaFileNameFromClustering , "r")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1126 fileMap = open(outMapFileName, "w")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1127 seq = Bioseq()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1128 numseq = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1129 while 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1130 seq.read(fileDb)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1131 if seq.sequence == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1132 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1133 numseq = numseq + 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1134 ID = seq.header.split(' ')[0].split('_')[0]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1135 chunk = seq.header.split(' ')[0].split('_')[1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1136 start = seq.header.split(' ')[-1].split(',')[0][1:]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1137 end = seq.header.split(' ')[-1].split(',')[1][:-1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1138 line = '%s\t%s\t%s\t%s' % (ID, chunk, start, end)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1139 fileMap.write(line + "\n")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1140
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1141 fileDb.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1142 fileMap.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1143 print "saved in %s" % outMapFileName
18
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1144
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1145 @staticmethod
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1146 def writeNstreches(fastaFileName, nbN = 2, outFileName = "", outFormat = "map"):
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1147 outFormat = outFormat.lower()
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1148 if outFormat in ["gff", "gff3"]:
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1149 outFormat = "gff3"
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1150 else:
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1151 outFormat = "map"
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1152
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1153 lTNstretches = []
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1154 if nbN != 0:
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1155 iBSDB = BioseqDB(fastaFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1156 for iBS in iBSDB.db:
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1157 nbNFound = 0
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1158 start = 1
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1159 pos = 1
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1160 lastPos = 0
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1161
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1162 while pos <= iBS.getLength():
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1163 if nbNFound == 0:
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1164 start = pos
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1165
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1166 while pos <= iBS.getLength() and iBS.getNtFromPosition(pos).lower() in ['n', 'x']:
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1167 nbNFound += 1
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1168 pos += 1
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1169 lastPos = pos
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1170
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1171 if pos - lastPos >= nbN:
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1172 if nbNFound >= nbN:
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1173 lTNstretches.append((iBS.getHeader(), start, lastPos - 1))
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1174 nbNFound = 0
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1175 pos += 1
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1176
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1177 if nbNFound >= nbN:
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1178 lTNstretches.append((iBS.getHeader(), start, lastPos - 1))
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1179
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1180 lTNstretches.sort(key = itemgetter(0, 1, 2))
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1181
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1182 if outFileName == "":
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1183 outFileName = "%s_Nstretches.%s" % (os.path.splitext(os.path.split(fastaFileName)[1])[0], outFormat)
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1184
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1185 with open(outFileName, "w") as fH:
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1186 if outFormat == "gff3":
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1187 fH.write("##gff-version 3\n")
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1188 for item in lTNstretches:
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1189 seq = item[0]
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1190 start = item[1]
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1191 end = item[2]
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1192 if outFormat == "gff3":
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1193 fH.write("%s\tFastaUtils\tN_stretch\t%s\t%s\t.\t.\t.\tName=N_stretch_%s-%s\n" % (seq, start, end, start, end))
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1194 else:
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1195 fH.write("N_stretch\t%s\t%s\t%s\n" % (seq, start, end))
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1196
94ab73e8a190 Uploaded
m-zytnicki
parents: 6
diff changeset
1197