annotate uniqprimer-0.5.0/primertools/primermanager.py @ 3:3249d78ecfc2 draft

Uploaded
author dereeper
date Mon, 03 Jan 2022 09:56:55 +0000
parents
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 1, 2011
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
3
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
4 @author: John L. Herndon
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
5 @contact: herndon@cs.colostate.edu
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
6 @organization: Colorado State University
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
7 @group: Computer Science Department, Asa Ben-Hur's laboratory
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
8 '''
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
9
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
10 import utils
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
11 import tempfile
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
12 import programs
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
13 import eprimerparser
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
14 import primersearchutils
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
15 import fastaparser
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
16 import exceptions
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
17
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
18
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
19
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
20
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
21 class PrimerManager( object ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
22 '''
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
23 A class used to find primers given a set of sequences.
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
24 '''
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
25
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
26 def __init__( self, eprimerOptions ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
27 self.eprimer = programs.Eprimer( eprimerOptions )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
28 self.primersearch = programs.PrimerSearch( )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
29
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
30
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
31 def findPrimers( self, sequences, outputFile, primerpairs = 20, returnPrimers = False ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
32 '''
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
33 A method to find a set of primers based on the given sequences
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
34 '''
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
35
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
36 utils.logMessage( "PrimerManager::findPrimer(s )", "writing sequences to a fasta file" )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
37
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
38 #eleminate all sequences that are lees than the desired amplification size...
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
39
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
40 if len( sequences ) == 4 :
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
41 print sequences
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
42 sequences = filter( lambda x: len( x ) >= 200, sequences )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
43
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
44 primerFastaFile = utils.getTemporaryDirectory( ) + "/sequenceForEprimer.fasta"
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
45 fastaparser.writeFastaFile( sequences, primerFastaFile )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
46
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
47 utils.logMessage( "PrimerManager::findPrimers( )", "executing eprimer3 program" )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
48 self.eprimer.execute( [ primerFastaFile, outputFile ] )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
49 utils.logMessage( "PrimerManager::findPrimer( )", "eprimer3 file {0} created. Parsing for primers.".format( outputFile ) )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
50
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
51 primers = eprimerparser.parsePrimerSequences( outputFile )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
52
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
53 utils.logMessage( "PrimerManager::findPrimers( )", "parsing for sequences complete" )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
54
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
55 if returnPrimers == True:
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
56 return primers
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
57
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
58
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
59 def getPrimers( self, sequences ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
60
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
61 utils.logMessage( "PrimerManager::getCommonPrimers", "finding primers that are common to all include files" )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
62
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
63 if len( sequences ) == 0:
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
64 raise utils.NoPrimersExistException( )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
65
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
66 referenceEPrimerFile = utils.getTemporaryDirectory( ) + "/referenceprimers.ep3"
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
67
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
68 #run eprimer to find primers in the reference file
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
69 primers = self.findPrimers( sequences, referenceEPrimerFile, 20, True )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
70
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
71
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
72 if len( primers ) == 0:
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
73 raise utils.NoPrimersExistException( )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
74
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
75 return primers
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
76
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
77 def crossValidatePrimers2( self, primers, includeFile, j ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
78 includeSequences = fastaparser.parseFastaFile( includeFile )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
79 #write a primer search input file with using the primers argument
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
80 primerInputFileName = utils.getTemporaryDirectory( ) + "/tmpinputprimers2.ps" + str(j)
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
81 primerOutputFileName = utils.getTemporaryDirectory( ) + "/tmpoutputprimers2.ps" + str(j)
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
82 primersearchutils.writePrimerSearchInputFile( primers, primerInputFileName )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
83
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
84 utils.logMessage( "PrimerManager::crossValidatePrimers", "finding primers that are in the supplied include file" )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
85 #run primer search to identify the primers
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
86 self.primersearch.execute( [ includeFile, primerInputFileName, primerOutputFileName, "0" ] )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
87
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
88 #read the found primers from the file
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
89 commonPrimers = primersearchutils.parsePrimerSearchFile( primerOutputFileName )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
90
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
91 #compose a list of primers that are not found in the exclude file...
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
92 returnValue = [ ]
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
93
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
94 for primer in primers:
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
95 if primer.id in commonPrimers:
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
96 returnValue.append( primer )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
97
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
98 utils.logMessage( "PrimerManager::crossValidatePrimers", "{0} unique primers identified out of {1}".format( len( returnValue ), len( primers ) ) )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
99
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
100 if len( returnValue ) == 0:
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
101 raise utils.NoPrimersExistException( )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
102
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
103 return returnValue
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
104
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
105
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
106 def crossValidatePrimers( self, primers, excludeFile ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
107
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
108 excludeSequences = fastaparser.parseFastaFile( excludeFile )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
109
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
110 #write a primer search input file with using the primers argument
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
111 primerInputFileName = utils.getTemporaryDirectory( ) + "/tmpinputprimers.ps"
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
112 primerOutputFileName = utils.getTemporaryDirectory( ) + "/tmpoutputprimers.ps"
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
113 primersearchutils.writePrimerSearchInputFile( primers, primerInputFileName )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
114
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
115 utils.logMessage( "PrimerManager::crossValidatePrimers", "finding primers that are not in the supplied exclude file" )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
116 #run primer search to identify the primers
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
117 self.primersearch.execute( [ excludeFile, primerInputFileName, primerOutputFileName, "10" ] )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
118
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
119 #read the found primers from the file
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
120 commonPrimers = primersearchutils.parsePrimerSearchFile( primerOutputFileName )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
121
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
122 #compose a list of primers that are not found in the exclude file...
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
123 returnValue = [ ]
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
124
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
125 for primer in primers:
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
126 if primer.id not in commonPrimers:
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
127 returnValue.append( primer )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
128
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
129 utils.logMessage( "PrimerManager::crossValidatePrimers", "{0} unique primers identified out of {1}".format( len( returnValue ), len( primers ) ) )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
130
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
131 if len( returnValue ) == 0:
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
132 raise utils.NoPrimersExistException( )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
133
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
134 return returnValue
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
135
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
136
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
137