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

Uploaded
author dereeper
date Mon, 03 Jan 2022 09:56:55 +0000
parents
children
comparison
equal deleted inserted replaced
2:7e0438dad4e9 3:3249d78ecfc2
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 crossValidatePrimers2( self, primers, includeFile, j ):
78 includeSequences = fastaparser.parseFastaFile( includeFile )
79 #write a primer search input file with using the primers argument
80 primerInputFileName = utils.getTemporaryDirectory( ) + "/tmpinputprimers2.ps" + str(j)
81 primerOutputFileName = utils.getTemporaryDirectory( ) + "/tmpoutputprimers2.ps" + str(j)
82 primersearchutils.writePrimerSearchInputFile( primers, primerInputFileName )
83
84 utils.logMessage( "PrimerManager::crossValidatePrimers", "finding primers that are in the supplied include file" )
85 #run primer search to identify the primers
86 self.primersearch.execute( [ includeFile, primerInputFileName, primerOutputFileName, "0" ] )
87
88 #read the found primers from the file
89 commonPrimers = primersearchutils.parsePrimerSearchFile( primerOutputFileName )
90
91 #compose a list of primers that are not found in the exclude file...
92 returnValue = [ ]
93
94 for primer in primers:
95 if primer.id in commonPrimers:
96 returnValue.append( primer )
97
98 utils.logMessage( "PrimerManager::crossValidatePrimers", "{0} unique primers identified out of {1}".format( len( returnValue ), len( primers ) ) )
99
100 if len( returnValue ) == 0:
101 raise utils.NoPrimersExistException( )
102
103 return returnValue
104
105
106 def crossValidatePrimers( self, primers, excludeFile ):
107
108 excludeSequences = fastaparser.parseFastaFile( excludeFile )
109
110 #write a primer search input file with using the primers argument
111 primerInputFileName = utils.getTemporaryDirectory( ) + "/tmpinputprimers.ps"
112 primerOutputFileName = utils.getTemporaryDirectory( ) + "/tmpoutputprimers.ps"
113 primersearchutils.writePrimerSearchInputFile( primers, primerInputFileName )
114
115 utils.logMessage( "PrimerManager::crossValidatePrimers", "finding primers that are not in the supplied exclude file" )
116 #run primer search to identify the primers
117 self.primersearch.execute( [ excludeFile, primerInputFileName, primerOutputFileName, "10" ] )
118
119 #read the found primers from the file
120 commonPrimers = primersearchutils.parsePrimerSearchFile( primerOutputFileName )
121
122 #compose a list of primers that are not found in the exclude file...
123 returnValue = [ ]
124
125 for primer in primers:
126 if primer.id not in commonPrimers:
127 returnValue.append( primer )
128
129 utils.logMessage( "PrimerManager::crossValidatePrimers", "{0} unique primers identified out of {1}".format( len( returnValue ), len( primers ) ) )
130
131 if len( returnValue ) == 0:
132 raise utils.NoPrimersExistException( )
133
134 return returnValue
135
136
137