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