我有一個包含多個序列的 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
這不起作用,請有人指導我。
提前致謝
問候
uj5u.com熱心網友回復:
你好,不是開發者,所以不要無禮。
希望對你有幫助:
我創建了一個檔案 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://www.uj5u.com/caozuo/462382.html
