A Simple Python Script to Convert FASTA file to CSV format
Posted onEdited on
I have searched online for converting FASTA file into CSV format for sequence visualization like the output from GPCRdb.org, but I didn’t find what I want. So I wrote this simple and dirty script to convert FASTA to CSV.
# Read in FASTA file = open(input, 'r') lines_i = file.readlines() seq = ''
for l in lines_i: if l[0] == '>': 'Fasta head line' seq_id = l.strip() else: 'Sequence line' seq += l.strip()
file.close()
print('The Input file is: %s' %input)
# Convert FASTA to CSV l = [] lines = [str(seq_id) + '\n'] for i, c inenumerate(seq): l.append(c) if i % 60 == 59: lines.append(','.join(l) + '\n') l = []
This script is unmature but good for use. It can convert a FASTA file containing one sequence into a CSV file, each residue are a single elemnt of CSV file. It is verty straightfoward so anyone can simply modify it for their own purpose.