annotate uniqprimer-0.5.0/build/lib/primertools/primermanager.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 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 crossValidatePrimers( self, primers, excludeFile ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
78
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
79 excludeSequences = fastaparser.parseFastaFile( excludeFile )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
80
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
81 #write a primer search input file with using the primers argument
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
82 primerInputFileName = utils.getTemporaryDirectory( ) + "/tmpinputprimers.ps"
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
83 primerOutputFileName = utils.getTemporaryDirectory( ) + "/tmpoutputprimers.ps"
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
84 primersearchutils.writePrimerSearchInputFile( primers, primerInputFileName )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
85
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
86 utils.logMessage( "PrimerManager::crossValidatePrimers", "finding primers that are not in the supplied exclude file" )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
87 #run primer search to identify the primers
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
88 self.primersearch.execute( [ excludeFile, primerInputFileName, primerOutputFileName, "0" ] )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
89
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
90 #read the found primers from the file
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
91 commonPrimers = primersearchutils.parsePrimerSearchFile( primerOutputFileName )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
92
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
93 #compose a list of primers that are not found in the exclude file...
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
94 returnValue = [ ]
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
95
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
96 for primer in primers:
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
97 if primer.id not in commonPrimers:
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
98 returnValue.append( primer )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
99
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
100 utils.logMessage( "PrimerManager::crossValidatePrimers", "{0} unique primers identified out of {1}".format( len( returnValue ), len( primers ) ) )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
101
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
102 if len( returnValue ) == 0:
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
103 raise utils.NoPrimersExistException( )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
104
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
105 return returnValue
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
106
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
107
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
108