使用grep和单词计数添加标头



大家好,我有很多(200个左右)的文件

>Sample_NameA
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT
>Sample_NameB
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT

我需要在包含样本数量的每个文件,字母数量(a,c,g,t)和第一。

因此,例如,第一个文件应该看起来像这样:

2 441 1
>Sample_NameA
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT
>Sample_NameB
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT

我正在考虑使用第一个grep">"来计算样本的数量,因为每个示例名称都先于此,并使用wc来计算字符的数量直到另一个>,但我无法管理。片刻之后,我将在到目前为止的脚本上载我的脚本,但是如果有人解决了它,我会非常感谢它。

感谢ID Advance。

这个尴尬可以在一个命令中完成所有这些操作:

awk '{if (d) d=d RS $0; else d=$0}
      />/{s++;next}
      s==1 && /[ACGT]/{gsub(/[^ACGT]+/, ""); n+=length($0)}
      END{print s, n, "1" RS d}' file
2 441 1
>Sample_NameA
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT
>Sample_NameB
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT
ACGTGTCTAGTATGCATGCATAGCTACGACTACGACTAGCATCAGCATCGACTAGCATCGACT
#!/bin/bash
DATA="$1" # for readability; give /path/to/data/file as first argument to the script
SAMPLES=$(grep -c '>' "$DATA")
CHARS=$(grep -v '>' "$DATA" | wc -c)
echo "$SAMPLES $CHARS 1"
cat "$DATA"

无法想到只用一个GREP来完成此操作的方法,但肯定可以使用一些管道可行。

,例如

> grep -E "^>" file 
2
> grep -E -v "^>" file | grep -o "[ACGT]" | wc -l
756

,并完全归功于@anubhava,这是一种无法将整个文件保留在内存中的方法:

ed file <<END
1i
$(awk '/^>/ {s++; next} s==1 {l+=length()} END {print s,l,1}' file)
.
w
q
END

相关内容

  • 没有找到相关文章

最新更新