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
|
|
11 import utils
|
|
12 import primersequence
|
|
13
|
|
14 from Bio import SeqIO
|
|
15 from Bio import Seq
|
|
16 from Bio import Alphabet
|
|
17
|
|
18 def parseFastaFileAsPrimerSequence( fileName ):
|
|
19
|
|
20 utils.logMessage("fastaparser::parseFastaFileAsPrimerSequence( )", "parsing fasta file {0}".format( fileName ) )
|
|
21 returnValue = { }
|
|
22
|
|
23 sequences = SeqIO.parse( open( fileName ), "fasta" )
|
|
24
|
|
25 for sequence in sequences:
|
|
26 seqdata = primersequence.PrimerSequence( sequence.id, len( sequence ), sequence.seq )
|
|
27 returnValue[ sequence.id ] = seqdata
|
|
28
|
|
29 utils.logMessage("fastaparser::parseFastaFileAsPrimerSequence( )", "read {0} sequences".format( len( returnValue.keys( ) ) ) )
|
|
30
|
|
31 return returnValue
|
|
32
|
|
33 def parseFastaFile( fileName ):
|
|
34 '''
|
|
35 parse a fasta file and return a list of Bio.Seq
|
|
36 '''
|
|
37 utils.logMessage("fastaparser::parseFastaFile( )", "parsing fasta file {0}".format( fileName ) )
|
|
38
|
|
39 sequences = SeqIO.parse( open( fileName ), "fasta" )
|
|
40
|
|
41 return sequences
|
|
42
|
|
43 def writeFastaFile( sequences, fileName ):
|
|
44 '''
|
|
45 write a set of sequences to a fasta file.
|
|
46 returns the name of the new file
|
|
47 '''
|
|
48
|
|
49 primerSequenceIdent = "primer_sequences"
|
|
50 utils.logMessage( "PrimerManager::writeFastaFile( )", "Writing {0} sequences to fasta file".format( len( sequences ) ) )
|
|
51 seqRecords = [ ]
|
|
52 i = 0
|
|
53 for sequence in sequences:
|
|
54 seqStr = str( reduce( lambda x, y: str( x )+str( y ), sequence) )
|
|
55 seqRecord = SeqIO.SeqRecord( Seq.Seq( seqStr, Alphabet.IUPAC.extended_dna ), id="seq_{0}".format( i ) )
|
|
56 seqRecords.append( seqRecord )
|
|
57 i += 1
|
|
58
|
|
59 SeqIO.write( seqRecords, open( fileName, "w" ), "fasta" )
|
|
60
|
|
61 utils.logMessage( "PrimerManager::writeFastaFile( )", "writing fasta file complete" )
|
|
62 return fileName
|
|
63 |