3
|
1 '''
|
|
2 Created on Jan 16, 2011
|
|
3
|
|
4 @author: jlh
|
|
5 '''
|
|
6 import utils
|
|
7 import fastaparser
|
|
8 from Bio import SeqIO
|
|
9 import os
|
|
10
|
|
11 class ExcludeFileManager( object ):
|
|
12 '''
|
|
13 A class to manage fasta files to be excluded
|
|
14 '''
|
|
15
|
|
16 def __init__( self ):
|
|
17 """
|
|
18 Initialize the ExcludeFileManager
|
|
19 """
|
|
20 self.excludeFiles = [ ]
|
|
21 self.outputFile = ""
|
|
22
|
|
23 def getOutputFileName( self ):
|
|
24 """
|
|
25 get the name of the file combined-sequence fasta file
|
|
26 """
|
|
27 return self.outputFile
|
|
28
|
|
29 def addExcludeFile( self, excludeFile ):
|
|
30 """
|
|
31 add a file to be managed by the ExcludeFileManager
|
|
32 """
|
|
33
|
|
34 if os.path.exists( excludeFile ) == False:
|
|
35 utils.logMessage( "ExcludeFileManager::addExcludeFile( )", "exclude file not found: {0}".format( excludeFile ) )
|
|
36 raise utils.NoFileFoundException( excludeFile )
|
|
37
|
|
38 utils.logMessage( "ExcludeFileManager::addExcludeFile( )", "adding exclude file {0}".format( excludeFile ) )
|
|
39 self.excludeFiles.append( excludeFile )
|
|
40
|
|
41 def buildOutputFileName( self ):
|
|
42 """
|
|
43 build a unique file name to store the combined output sequences to
|
|
44 """
|
|
45 self.outputFile = utils.getTemporaryDirectory( ) + "/combined_exlude.ffn"
|
|
46 utils.logMessage( "ExcludeFileManager::buildOutputFileName( )", " exclude file: {0}".format( self.outputFile ) )
|
|
47
|
|
48 def exportSequences( self ):
|
|
49 """
|
|
50 combine all exclude files into a single exclude file
|
|
51 """
|
|
52
|
|
53 utils.logMessage( "ExcludeFileManager::exportSequences( )", "parsing exclude sequences")
|
|
54
|
|
55 #read all exclude file sequences into memory
|
|
56 sequences = [ ]
|
|
57 for excludeFile in self.excludeFiles:
|
|
58 sequences.extend( fastaparser.parseFastaFile( excludeFile ) )
|
|
59
|
|
60 utils.logMessage( "ExcludeFileManager::exportSequences( )", "finished parsing, writing to a common file" )
|
|
61
|
|
62 self.buildOutputFileName( )
|
|
63 #combine the sequences and write them to a file
|
|
64
|
|
65 SeqIO.write( sequences, open( self.outputFile, "w" ), "fasta" )
|
|
66
|
|
67 utils.logMessage( "ExcludeFileManager::exportSequences( )", "All sequences exported" )
|
|
68
|
|
69
|
|
70
|
|
71
|
|
72
|
|
73
|
|
74
|
|
75 |