view fasta_affixer.py @ 25:5dba804e6884 draft

planemo upload commit 20bdf879b52796d3fb251a20807191ff02084d3c-dirty
author petr-novak
date Wed, 02 Aug 2023 12:42:08 +0000
parents a4cd8608ef6b
children
line wrap: on
line source

#!/usr/bin/env python3
''' fasta affixer - adding prefixes and suffixes to fasta sequence names'''
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("-f", "--fasta", type=str, help="fasta file")
parser.add_argument("-o", "--output", type=str, help="output fasta file")
parser.add_argument(
    "-p", "--prefix",
    type=str, help="prefix to be added to names")
parser.add_argument(
    "-s", "--suffix",
    type=str, help="suffix to be added",
    default='')
parser.add_argument("-n",
                    "--nspace",
                    type=int,
                    help="number of spaces to ignore",
                    default='0')

args = parser.parse_args()

with open(args.fasta, "r") as f, open(args.output, "w") as out:
    for oneline in f:
        if oneline == "":
            continue
        if not oneline:
            break
        if oneline[0] == ">":
            header = " ".join(oneline.split()[:1 + args.nspace])
            header_out = header[0] + args.prefix + header[1:] + args.suffix + "\n"
            out.write(header_out)
        else:
            out.write(oneline)