pyjanitor-devs/pyjanitor

[ENH] to_fasta as part of janitor.biology

Open

#646 opened on Mar 12, 2020

View on GitHub
 (1 comment) (0 reactions) (0 assignees)Python (164 forks)batch import
available for hackingenhancementgood first issue

Repository metrics

Stars
 (1,217 stars)
PR merge metrics
 (Avg merge 2d 12h) (4 merged PRs in 30d)

Description

janitor.biology could do with a to_fasta function, I think. The intent here would be to conveniently export a dataframe of sequences as a FASTA file, using one column as the fasta header.

strawman implementation below:

import pandas_flavor as pf
from Bio.SeqRecord import SeqRecord
from Bio.Seq import Seq
from Bio import SeqIO

@pf.register_dataframe_method
def to_fasta(df, identifier_column_name, sequence_column_name, filename):
    seq_records = []
    for r, d in df.iterrows():
        seq = Seq(d[sequence_column_name])
        seq_record = SeqRecord(seq, id=d[identifier_column_name], description="", name="")
        seq_records.append(seq_record)
    SeqIO.write(seq_records, filename, format="fasta")

Contributor guide