Posts

Showing posts with the label Biopython

Condense fasta header

''' Biopython hack to condense fasta header. When there is a lengthy header in fasta file like the following: >geneid1213 len = 234 covStat = val otherparam = sval, Shorten it to make it >geneid1213. ''' from Bio import SeqIO new_header = [] with open ( "test.fasta" , "rU" ) as infile: for record in SeqIO . parse(infile, "fasta" ): record . description = record . name record . id = record . name new_header . append(record) SeqIO . write(new_header, "short_header.fasta" , "fasta" ) print ( "program complete" )

Calculate Cys-Richness for a protein

''' Code description: Calculate Cys-richness of a protein with criteria set as: >=4 'C's over the length of protein AND >=5% total cysteine content Function: Take input sequence => Count number of 'C's & length => Calculate percentage Output True or False if criteria is met ''' from Bio import SeqIO def Cys_rich (record_seq): C_count = record_seq . count( 'C' ) seq_len = len (record_seq) Cys_perc = float (C_count) / float (seq_len) * 100 if C_count >= 4.0 and Cys_perc >= 5.0 : return 'Cys-rich' else : return 'No' CysRichSeq = [] for record in SeqIO . parse( 'filename.fasta' , 'fasta' ): if Cys_rich(record . seq) == 'Cys-rich' : CysRichSeq . append(record) SeqIO . write(CysRichSeq, 'Cys-rich_sequences.fasta' , 'fasta' ) print 'Cys-rich sequences written to file..'