diff uniqprimer-0.5.0/primertools/fastaparser.py @ 3:3249d78ecfc2 draft

Uploaded
author dereeper
date Mon, 03 Jan 2022 09:56:55 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/uniqprimer-0.5.0/primertools/fastaparser.py	Mon Jan 03 09:56:55 2022 +0000
@@ -0,0 +1,63 @@
+'''
+Created on Jan 1, 2011
+
+@author: John L. Herndon
+@contact: herndon@cs.colostate.edu
+@organization: Colorado State University
+@group: Computer Science Department, Asa Ben-Hur's laboratory 
+'''
+
+
+import utils
+import primersequence
+
+from Bio import SeqIO
+from Bio import Seq
+from Bio import Alphabet
+
+def parseFastaFileAsPrimerSequence( fileName ):
+    
+    utils.logMessage("fastaparser::parseFastaFileAsPrimerSequence( )", "parsing fasta file {0}".format( fileName ) )
+    returnValue = { }
+    
+    sequences = SeqIO.parse( open( fileName ), "fasta" )
+    
+    for sequence in sequences:
+        seqdata = primersequence.PrimerSequence( sequence.id, len( sequence ), sequence.seq )
+        returnValue[ sequence.id ] = seqdata
+    
+    utils.logMessage("fastaparser::parseFastaFileAsPrimerSequence( )", "read {0} sequences".format( len( returnValue.keys( ) ) ) )
+    
+    return returnValue
+    
+def parseFastaFile( fileName ):
+    '''
+    parse a fasta file and return a list of Bio.Seq
+    '''
+    utils.logMessage("fastaparser::parseFastaFile( )", "parsing fasta file {0}".format( fileName ) )
+    
+    sequences =  SeqIO.parse( open( fileName ), "fasta" )
+    
+    return sequences
+
+def writeFastaFile( sequences, fileName ):
+    '''
+    write a set of sequences to a fasta file.
+    returns the name of the new file
+    ''' 
+    
+    primerSequenceIdent = "primer_sequences"
+    utils.logMessage( "PrimerManager::writeFastaFile( )", "Writing {0} sequences to fasta file".format( len( sequences ) ) )
+    seqRecords = [ ]
+    i = 0
+    for sequence in sequences:
+        seqStr = str( reduce( lambda x, y: str( x )+str( y ), sequence) )
+        seqRecord = SeqIO.SeqRecord( Seq.Seq( seqStr, Alphabet.IUPAC.extended_dna ),  id="seq_{0}".format( i ) )
+        seqRecords.append( seqRecord )
+        i += 1
+
+    SeqIO.write( seqRecords, open( fileName, "w" ), "fasta" )
+        
+    utils.logMessage( "PrimerManager::writeFastaFile( )", "writing fasta file complete" )    
+    return fileName
+        
\ No newline at end of file