博主是一个刚刚接触生信的新手,正在学习Linux和Python,偶尔会在该博客上面发布自己练习编程写的脚本,用来记录自己的学习之路。
介绍
根据序列的ID号从FASTA文件中批量提取序列是在平时常常要做的工作,Linux当中grep和awk工具、Perl语言和Python语言,以及samtools等都可以实现,以下是博主用Python实现的从FASTA文件中批量提取序列的脚本。
说明
- 需要用到fasta文件和ID的list文件。
- 所要提取的序列的ID需要提前写进一个文件中,每行一个。
脚本如下
1. 采用click模块添加命令行参数。
# -*- coding: utf-8 -*-
"""
@author: gyw
@Date: Wed Feb 27 09:19:54 2019
@E-mail: willgyw@126.com
@Description: This program can extract sequences
from a fasta file, according to a
given id list.
"""
import click
@click.command()
@click.option('-f', '--fastafile', help='Input a fasta file', required=True)
@click.option('-i', '--idfile', help='Input an idlist', required=True)
@click.option('-o', '--outfile', help='Input the name of result file', default='result.fa')
def main(fastafile, idfile, outfile):
"""
Extract seqences from a fasta file
according to a id list.
"""
idfile = open(idfile, 'r')
resultfile = open(outfile, 'w')
for id in idfile:
qid = id.strip()
flag = 0
with open(fastafile,'r') as ffile:
for line in ffile:
line = line.strip()
if line.startswith('>'):
name = line.replace('>'