我有一个包含几个序列的fasta文件,但是所有序列的第一行以相同的字符串(ABI)开头,我想更改它,并将其替换为存储在不同文本文件中的物种的名称。
我的fasta文件看起来
>ABI
AGCTAGTCCCGGGTTTATCGGCTATAC
>ABI
ACCCCTTGACTGACATGGTACGATGAC
>ABI
ATTTCGACTGGTGTCGATAGGCAGCAT
>ABI
ACGTGGCTGACATGTATGTAGCGATGA
spp的列表如下所示:
Alsophila cuspidata
Bunchosia argentea
Miconia cf.gracilis
Meliosma frondosa
我如何能够改变我的序列的ABI头,并用我的物种的名称来替换它们,使用这个精确的顺序。
所需产出:
>Alsophila cuspidata
AGCTAGTCCCGGGTTTATCGGCTATAC
>Bunchosia argentea
ACCCCTTGACTGACATGGTACGATGAC
>Miconia cf.gracilis
ATTTCGACTGGTGTCGATAGGCAGCAT
>Meliosma frondosa
ACGTGGCTGACATGTATGTAGCGATGA
我用的是:
awk '
FNR==NR{
a[$1]=$2
next
}
($2 in a) && /^>/{
print ">"a[$2]
next
}
1
' spp_list.txt FS="[> ]" all_spp.fasta
这不管用,能有人指点我吗?
发布于 2022-04-22 22:24:34
你好,不是开发人员,所以别无礼。
希望这能帮到你:
我创建了一个包含以下内容的文件fasta.txt:
>ABI
AGCTAGTCCCGGGTTTATCGGCTATAC
>ABI
ACCCCTTGACTGACATGGTACGATGAC
>ABI
ATTTCGACTGGTGTCGATAGGCAGCAT
>ABI
ACGTGGCTGACATGTATGTAGCGATGA
我还创建了一个文件spplist.txt,其中包含:
Alsophila cuspidata
Bunchosia argentea
Miconia cf.gracilis
Meliosma frondosa
然后我创建了一个名为fasta.py的python脚本,如下所示:
#!/bin/python3
#import re library: https://docs.python.org/3/library/re.html
#import sys library: https://docs.python.org/3/library/sys.html
import re,sys
#saving the reference of the standard output into "original_stdout"
original_stdout = sys.stdout
with open("spplist.txt", "r") as spplist:
x = spplist.readlines()
with open("fasta.txt", "r") as fasta:
output_file = open("output.txt", "w")
#redirecting standard output to output_file
sys.stdout = output_file
for line in fasta:
if re.match(r">ABI", line):
print(x[0].rstrip())
del x[0]
else:
print(line.rstrip())
#restoring the native standard output
sys.stdout = original_stdout
#Notify the user at the end of the work
print("job done")
(如果希望脚本按原样工作,这三个文件必须位于同一个目录中)
这是我的玩具树:
❯ tree
.
├── fasta.py
├── fasta.txt
└── spplist.txt
要执行脚本,请在目录中打开一个shell,cd并键入:
❯ python3 fasta.py
job done
您将在目录中看到一个名为output.txt的新文件:
❯ tree
.
├── fasta.py
├── fasta.txt
├── output.txt
└── spplist.txt
以下是它的内容:
Alsophila cuspidata
AGCTAGTCCCGGGTTTATCGGCTATAC
Bunchosia argentea
ACCCCTTGACTGACATGGTACGATGAC
Miconia cf.gracilis
ATTTCGACTGGTGTCGATAGGCAGCAT
Meliosma frondosa
ACGTGGCTGACATGTATGTAGCGATGA
希望这能帮到你。猜一下。
https://stackoverflow.com/questions/71977001
复制相似问题