我有多个 .fasta 文件(名为 barcode*_consensus.fasta),如下所示:
>|>consensus_cl_id_1018_total_supporting_reads_12 LN:i:1369 RC:i:12 XC:f:1.000000
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
|>consensus_cl_id_107_total_supporting_reads_6 LN:i:1440 RC:i:6 XC:f:1.000000
GACTTCAGCCCAGTCATTAGTCCTACCATGGACCCCCATATTACTAGAGGAGCTTCCGATATTACTAACTCCCATGCCGTGACGGGCG
|>consensus_cl_id_116_total_supporting_reads_5 LN:i:1314 RC:i:558 XC:f:1.000000
AGAACGAACGCTGGCGGCAGGCCTAACACATGCAAGTCGAGCGCTACCTTCGGGGGAGCGGCGGACGGGTTAGTAACGCGTGGGAATAT
我想每两行重复 n 次,如“总支持读取次数”后所指定。例如,我想将前两行重复 12 次,将后两行重复 6 次,等等。
使用 awk,我确实设法选择了以“>”开头的每一行以及下一行:
awk '/>/{nr[NR]; nr[NR+1]} NR in nr' barcode01_consensus.fasta
但我不知道如何使用变量来打印这个 n 次。
任何帮助深表感谢。
更新: 所以我希望最终的文件看起来像这样:
|>共识_cl_id_1018_total_supporting_reads_12 LN:i:1369 RC:i:12 XC:f:1.000000 TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTT
|>共识_cl_id_1018_total_supporting_reads_12 LN:i:1369 RC:i:12 XC:f:1.000000 TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTT
|>共识_cl_id_1018_total_supporting_reads_12 LN:i:1369 RC:i:12 XC:f:1.000000 TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTT
....x 12次....
答案1
我会使用空格或下划线作为字段分隔符。然后,计数是第 8 个字段:
awk -F'[ _]' '
$1 ~ /[>|]+consensus$/ {n = $8; print; next}
{while (--n >= 0) print}
' file
输出
>|>consensus_cl_id_1018_total_supporting_reads_12 LN:i:1369 RC:i:12 XC:f:1.000000
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
|>consensus_cl_id_107_total_supporting_reads_6 LN:i:1440 RC:i:6 XC:f:1.000000
GACTTCAGCCCAGTCATTAGTCCTACCATGGACCCCCATATTACTAGAGGAGCTTCCGATATTACTAACTCCCATGCCGTGACGGGCG
GACTTCAGCCCAGTCATTAGTCCTACCATGGACCCCCATATTACTAGAGGAGCTTCCGATATTACTAACTCCCATGCCGTGACGGGCG
GACTTCAGCCCAGTCATTAGTCCTACCATGGACCCCCATATTACTAGAGGAGCTTCCGATATTACTAACTCCCATGCCGTGACGGGCG
GACTTCAGCCCAGTCATTAGTCCTACCATGGACCCCCATATTACTAGAGGAGCTTCCGATATTACTAACTCCCATGCCGTGACGGGCG
GACTTCAGCCCAGTCATTAGTCCTACCATGGACCCCCATATTACTAGAGGAGCTTCCGATATTACTAACTCCCATGCCGTGACGGGCG
GACTTCAGCCCAGTCATTAGTCCTACCATGGACCCCCATATTACTAGAGGAGCTTCCGATATTACTAACTCCCATGCCGTGACGGGCG
|>consensus_cl_id_116_total_supporting_reads_5 LN:i:1314 RC:i:558 XC:f:1.000000
AGAACGAACGCTGGCGGCAGGCCTAACACATGCAAGTCGAGCGCTACCTTCGGGGGAGCGGCGGACGGGTTAGTAACGCGTGGGAATAT
AGAACGAACGCTGGCGGCAGGCCTAACACATGCAAGTCGAGCGCTACCTTCGGGGGAGCGGCGGACGGGTTAGTAACGCGTGGGAATAT
AGAACGAACGCTGGCGGCAGGCCTAACACATGCAAGTCGAGCGCTACCTTCGGGGGAGCGGCGGACGGGTTAGTAACGCGTGGGAATAT
AGAACGAACGCTGGCGGCAGGCCTAACACATGCAAGTCGAGCGCTACCTTCGGGGGAGCGGCGGACGGGTTAGTAACGCGTGGGAATAT
AGAACGAACGCTGGCGGCAGGCCTAACACATGCAAGTCGAGCGCTACCTTCGGGGGAGCGGCGGACGGGTTAGTAACGCGTGGGAATAT
打印每对线n时代只需要小小的改变:
awk -F'[ _]' '
$1 ~ /[>|]+consensus$/ {firstline = $0; n = $8; next}
{while (--n >= 0) print firstline ORS $0}
' file
输出
>|>consensus_cl_id_1018_total_supporting_reads_12 LN:i:1369 RC:i:12 XC:f:1.000000
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
>|>consensus_cl_id_1018_total_supporting_reads_12 LN:i:1369 RC:i:12 XC:f:1.000000
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
>|>consensus_cl_id_1018_total_supporting_reads_12 LN:i:1369 RC:i:12 XC:f:1.000000
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
>|>consensus_cl_id_1018_total_supporting_reads_12 LN:i:1369 RC:i:12 XC:f:1.000000
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
>|>consensus_cl_id_1018_total_supporting_reads_12 LN:i:1369 RC:i:12 XC:f:1.000000
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
>|>consensus_cl_id_1018_total_supporting_reads_12 LN:i:1369 RC:i:12 XC:f:1.000000
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
>|>consensus_cl_id_1018_total_supporting_reads_12 LN:i:1369 RC:i:12 XC:f:1.000000
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
>|>consensus_cl_id_1018_total_supporting_reads_12 LN:i:1369 RC:i:12 XC:f:1.000000
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
>|>consensus_cl_id_1018_total_supporting_reads_12 LN:i:1369 RC:i:12 XC:f:1.000000
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
>|>consensus_cl_id_1018_total_supporting_reads_12 LN:i:1369 RC:i:12 XC:f:1.000000
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
>|>consensus_cl_id_1018_total_supporting_reads_12 LN:i:1369 RC:i:12 XC:f:1.000000
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
>|>consensus_cl_id_1018_total_supporting_reads_12 LN:i:1369 RC:i:12 XC:f:1.000000
TCATTAACCACAAAGTGGTGAGCGTTCTCCCGAAGGTTAAACTACCCACTTCTTTTGCAGCCAACTCCCATGGTGTGACGGG
|>consensus_cl_id_107_total_supporting_reads_6 LN:i:1440 RC:i:6 XC:f:1.000000
GACTTCAGCCCAGTCATTAGTCCTACCATGGACCCCCATATTACTAGAGGAGCTTCCGATATTACTAACTCCCATGCCGTGACGGGCG
|>consensus_cl_id_107_total_supporting_reads_6 LN:i:1440 RC:i:6 XC:f:1.000000
GACTTCAGCCCAGTCATTAGTCCTACCATGGACCCCCATATTACTAGAGGAGCTTCCGATATTACTAACTCCCATGCCGTGACGGGCG
|>consensus_cl_id_107_total_supporting_reads_6 LN:i:1440 RC:i:6 XC:f:1.000000
GACTTCAGCCCAGTCATTAGTCCTACCATGGACCCCCATATTACTAGAGGAGCTTCCGATATTACTAACTCCCATGCCGTGACGGGCG
|>consensus_cl_id_107_total_supporting_reads_6 LN:i:1440 RC:i:6 XC:f:1.000000
GACTTCAGCCCAGTCATTAGTCCTACCATGGACCCCCATATTACTAGAGGAGCTTCCGATATTACTAACTCCCATGCCGTGACGGGCG
|>consensus_cl_id_107_total_supporting_reads_6 LN:i:1440 RC:i:6 XC:f:1.000000
GACTTCAGCCCAGTCATTAGTCCTACCATGGACCCCCATATTACTAGAGGAGCTTCCGATATTACTAACTCCCATGCCGTGACGGGCG
|>consensus_cl_id_107_total_supporting_reads_6 LN:i:1440 RC:i:6 XC:f:1.000000
GACTTCAGCCCAGTCATTAGTCCTACCATGGACCCCCATATTACTAGAGGAGCTTCCGATATTACTAACTCCCATGCCGTGACGGGCG
|>consensus_cl_id_116_total_supporting_reads_5 LN:i:1314 RC:i:558 XC:f:1.000000
AGAACGAACGCTGGCGGCAGGCCTAACACATGCAAGTCGAGCGCTACCTTCGGGGGAGCGGCGGACGGGTTAGTAACGCGTGGGAATAT
|>consensus_cl_id_116_total_supporting_reads_5 LN:i:1314 RC:i:558 XC:f:1.000000
AGAACGAACGCTGGCGGCAGGCCTAACACATGCAAGTCGAGCGCTACCTTCGGGGGAGCGGCGGACGGGTTAGTAACGCGTGGGAATAT
|>consensus_cl_id_116_total_supporting_reads_5 LN:i:1314 RC:i:558 XC:f:1.000000
AGAACGAACGCTGGCGGCAGGCCTAACACATGCAAGTCGAGCGCTACCTTCGGGGGAGCGGCGGACGGGTTAGTAACGCGTGGGAATAT
|>consensus_cl_id_116_total_supporting_reads_5 LN:i:1314 RC:i:558 XC:f:1.000000
AGAACGAACGCTGGCGGCAGGCCTAACACATGCAAGTCGAGCGCTACCTTCGGGGGAGCGGCGGACGGGTTAGTAACGCGTGGGAATAT
|>consensus_cl_id_116_total_supporting_reads_5 LN:i:1314 RC:i:558 XC:f:1.000000
AGAACGAACGCTGGCGGCAGGCCTAACACATGCAAGTCGAGCGCTACCTTCGGGGGAGCGGCGGACGGGTTAGTAACGCGTGGGAATAT
答案2
我怀疑有一个合适的生物信息学工具包可以更稳健地完成你想要的工作,但是使用 GNU awk 你可以匹配并捕获重复的数字,例如
gawk '
/^>?[|]>/ {
if((getline seq) > 0) $0 = $0 ORS seq
}
match($0,/total_supporting_reads_([0-9]+)/,a) {
while(a[1]--) print
}
' file.fasta
您可以使用非 GNU awk 执行相同操作,只不过您需要使用RSTART
和RMATCH
值将匹配的数字部分提取为子字符串,而不是将其捕获到数组中。