annotate uniqprimer-0.5.0/primertools/excludefilemanager.py @ 6:8218425fda1d draft default tip

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