根据.csv重命名fasta-header的部分

问题描述 投票:0回答:4

我想用一个包含.tsv部分的列表来更改我的fasta标题的部分内容。

我不是一名生物信息学家,只是一名具有bash和python初学技能的微生物学家。谢谢你的帮助。

例:

标题:

Prevalence_Sequence_ID:1 | ARO:3003072 | RES:mphL |蛋白质同源模型

关口

ARO:3003072 mphL mphL是染色体编码的大环内酯磷酸转移酶,其灭活14-和15-元大环内酯类如红霉素,克拉霉素,阿奇霉素。

新标题

Prevalence_Sequence_ID:1 | mphL mphL是染色体编码的大环内酯磷酸转移酶,可使14-和15-元大环内酯类如红霉素,克拉霉素,阿奇霉素失活。| RES:mphL | Protein Homolog Model

可能是.tsv中没有给出fasta标题中的ARO然后忽略它。

禁食的例子

>Prevalence_Sequence_ID:1|ARO:3003072|RES:mphL|Protein Homolog Model
MTTLKVKQLANKKGLNILEDS
>gb|ARO:3004145|RES:AxyZ|Achromobacter_insuavis_AXX-A_
MARKTKEESQRTRDRILDAAEHVFLSKG
>Prevalence_Sequence_ID:31298|ARO:3000777|RES:adeF|Protein Homolog Model
MDFSRFFIDRPIFAAVLSILIFI

示例.tsv

ARO:3003072 mphL    mphL is a chromosomally-encoded macrolide phosphotransferases that inactivate 14- and 15-membered macrolides such as erythromycin, clarithromycin, azithromycin.
ARO:3004145 AxyZ    AxyZ is a transcriptional regulator of the AxyXY-OprZ efflux pump system.
ARO:3000777 adeF    AdeF is the membrane fusion protein of the multidrug efflux complex AdeFGH.
python-3.x bash bioinformatics fasta
4个回答
0
投票

如果序列不需要排序,我们可以使用第二个字段对fasta进行排序,将.tsv中的第一个空格替换为|作为分隔符,并通过第一个字段对其进行排序,然后使用正确的输出格式进行连接:

cat <<EOF >fasta
>Prevalence_Sequence_ID:1|ARO:3003072|RES:mphL|Protein Homolog Model
MTTLKVKQLANKKGLNILEDS
>gb|ARO:3004145|RES:AxyZ|Achromobacter_insuavis_AXX-A_
MARKTKEESQRTRDRILDAAEHVFLSKG
>Prevalence_Sequence_ID:31298|ARO:3000777|RES:adeF|Protein Homolog Model
MDFSRFFIDRPIFAAVLSILIFI
EOF
cat <<EOF >tsv
ARO:3003072 mphL    mphL is a chromosomally-encoded macrolide phosphotransferases that inactivate 14- and 15-membered macrolides such as erythromycin, clarithromycin, azithromycin.
ARO:3004145 AxyZ    AxyZ is a transcriptional regulator of the AxyXY-OprZ efflux pump system.
ARO:3000777 adeF    AdeF is the membrane fusion protein of the multidrug efflux complex AdeFGH.
EOF

join -t'|' -12 -21 -o1.1,2.2,1.3 <(
    <fasta sort -t'|' -k2) <(
    <tsv sed 's/ /|/' | sort -t'|' -k1)

如果您需要根据fasta对输出进行排序,我们可以使用nl -w1对行进行编号,然后连接,然后使用数字对输出进行排序并删除数字:

join -t'|' -12 -21 -o1.1,2.2,1.3 <(
    <fasta nl -w1 | sort -t'|' -k2) <(
    <tsv sed 's/ /|/' | sort -t'|' -k1) |
sort -t $'\t' -n -k2 | cut -f2-

0
投票

如果您使用,则可以执行以下步骤:

  1. 读取完整的tsv文件并将所有值存储到由第一列索引的数组中。
  2. 解析fasta文件: 如果你遇到标题(以>开头)那么 从标题中提取密钥(|之后的第一个字符串) 用数组的内容替换密钥 打印当前行

这些步骤也可以在python中完成,但您可以使用以下行在awk中轻松完成:

awk '# Read the TSV file only
     (NR==FNR) { key=$1; sub(key"[[:space:]]*",""); a[key]=$0; next }
     # From here process the fasta file
     # - if header, perform action:
     /^>/ { match($0,"[|][^|]+"); key=substr($0,RSTART+1,RLENGTH-1);sub(key,a[key]) }
     # print the current line
     1' index.tsv file.fasta > file.new.fasta

0
投票
import pandas as pd
from Bio import SeqIO

tsvdata = pd.read_csv('example.tsv', sep='/t', header=None, names=['aro','_', 'description'])

for record in SeqIO.parse("example.fasta", "fasta"): 
    fasta_record = str(record).split('|')
    key = fasta_record[1]
    fasta_record[1]=tsvdata[tsvdata['aro']==key]['description'].values[0] 
    print('|'.join(fasta_record))

0
投票

我将你的示例Fasta和TSV数据保存到example.fastaexample.tsv中。以下是输入文件内容 -

$ cat example.fasta
>Prevalence_Sequence_ID:1|ARO:3003072|RES:mphL|Protein Homolog Model
MTTLKVKQLANKKGLNILEDS
>gb|ARO:3004145|RES:AxyZ|Achromobacter_insuavis_AXX-A_
MARKTKEESQRTRDRILDAAEHVFLSKG
>Prevalence_Sequence_ID:31298|ARO:3000777|RES:adeF|Protein Homolog Model
MDFSRFFIDRPIFAAVLSILIFI
$ cat example.tsv
ARO:3003072 mphL    mphL is a chromosomally-encoded macrolide phosphotransferases that inactivate 14- and 15-membered macrolides such as erythromycin, clarithromycin, azithromycin.
ARO:3004145 AxyZ    AxyZ is a transcriptional regulator of the AxyXY-OprZ efflux pump system.
ARO:3000777 adeF    AdeF is the membrane fusion protein of the multidrug efflux complex AdeFGH.
# import biopython, bioython needs to be installed in your environment/machine
from Bio.SeqIO.FastaIO import SimpleFastaParser as sfp

# read in the tsv data into a dict
with open("example.tsv") as tsvdata:
    tsv_data = {line.strip().split("\t")[0]: " ".join(line.strip().split("\t")[1:])
                for line in tsvdata}

# read input fasta file contents and write to a separate file in real time
with open("example_out.fasta", "w") as outfasta:
    with open("example.fasta") as infasta:
        for header, seq in sfp(infasta):
            aro = header.strip().split("|")[1]  # get ARO for header
            header = header.replace(aro, tsv_data.get(aro, aro))  # lookup ARO in dict and replace if found, otherwise ignore it
            outfasta.write(">{0}\n{1}\n".format(header, seq))

这是输出文件的内容 -

$ cat example_out.fasta
>Prevalence_Sequence_ID:1|mphL mphL is a chromosomally-encoded macrolide phosphotransferases that inactivate 14- and 15-membered macrolides such as erythromycin, clarithromycin, azithromycin.|RES:mphL|Protein Homolog Model
MTTLKVKQLANKKGLNILEDS
>gb|AxyZ AxyZ is a transcriptional regulator of the AxyXY-OprZ efflux pump system.|RES:AxyZ|Achromobacter_insuavis_AXX-A_
MARKTKEESQRTRDRILDAAEHVFLSKG
>Prevalence_Sequence_ID:31298|adeF AdeF is the membrane fusion protein of the multidrug efflux complex AdeFGH.|RES:adeF|Protein Homolog Model
MDFSRFFIDRPIFAAVLSILIFI
© www.soinside.com 2019 - 2024. All rights reserved.