分割一个fasta文件并在第一行的基础上重命名 [英] split a fasta file and rename on the basis of first line

查看:369
本文介绍了分割一个fasta文件并在第一行的基础上重命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个巨大的文件,其中包含以下内容:

I have a huge file with following content:

文件名:input.txt

filename: input.txt

>chr1
jdlfnhl
dh,ndh
dnh.

dhjl

>chr2
dhfl
dhl
dh;l

>chr3

shgl
sgl

>chr2_random
dgld

我需要以以下方式拆分此文件,以使我得到四个单独的文件,如下所示:

I need to split this file in such a way that I get four separate file as below:

文件1:chr1.fa

file 1: chr1.fa

>chr1
jdlfnhl
dh,ndh
dnh.

dhjl

文件2:chr2.fa

file 2: chr2.fa

>chr2
dhfl
dhl
dh;l

文件3:chr3.fa

file 3: chr3.fa

>chr3

shgl
sgl

文件4:chr2_random.fa

file 4: chr2_random.fa

>chr2_random
dgld

我在Linux中尝试了csplit,但无法在>"之后立即用文本重命名.

I tried csplit in linux, but could not rename them by the text immediately after ">".

csplit -z input.txt '/>/' '{*}'

推荐答案

由于您表示自己在Linux机器上,因此"awk"似乎是完成此任务的正确工具.

Since you indicate you're on a Linux box 'awk' seems to be the right tool for the job.

用法:
./foo.awk your_input_file

foo.awk:

#!/usr/bin/awk -f

/^>chr/ {
    OUT=substr($0,2) ".fa"
}

OUT {
    print >OUT
}

您也可以在一行中做到这一点:

You can do that also in one line:

awk '/^>chr/ {OUT=substr($0,2) ".fa"}; OUT {print >OUT}' your_input

这篇关于分割一个fasta文件并在第一行的基础上重命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆