我已经尝试了几周不让这个程序运行。我是编程的新手,它肯定是一个挑战。我认为我的问题出现在我的if语句中。我可以让它将名称附加到新文件,但它只是将整个序列附加到文件而不是计算它。我正在使用包含多个序列的fasta文件,名称以'>'开头以及它下面一行的序列。这是我的代码。请帮助,并提前非常感谢!!
#! /bin/bash
#exit program with error if user does not specify input on command line
if [ $# != 1 ]; then
echo "Please specify fasta input on command line and rerun"
exit
else echo "Beginning count"
fi
#collect input from user each time they run the program
input=`cat $1`
#seperate the sequence from the sequence name
name=`grep '>' $1`
sequence=`grep -v '>' $1`
#if name, if sequence
IFS=$'\n'
set -f
for i in $(cat "$1");
do
if [ $i=">" ]; then
echo "$i" >> GCcontent.txt
else
#count number of occurence of motif ATGC in fasta sequence
countG=`echo $i | grep -o "G" | wc -l`
countC=`echo $i | grep -o "C" | wc -l`
total=`echo $i | wc -m`
count=`echo "scale=2" ; ($countG+$countC) | bc`
#calculate percent over total divided by 3bp
percent=`echo "scale=2 ; ($count/$total*100)" | bc`
#print output name and percent to file
echo "$percent" >> GCcontent.txt
fi
done
echo "Exiting"
exit
编辑: 输入文件:(>周围没有引号,没有它我就不会把它放在那里) '>' GI | 226451773 | GB | FJ846591.1 CATTATAGACTGCGTGGTCCGTATTCCCAAGGAGCAGGGAGTTCTGTCCTTCTGGCGCGGTAACCTGGCCAATGTCATCAGATACTTCCCCACCCAGGCTCTTAACTTCGCCTTCAAAGATAAATACAAGCAGATCTTCCTAGGTGGTGTGGACAAGAGGACCCAGTTTTGGCGCTACTTTGCAGGGAATCTGGCATCAGGTGGTGCCGCAGGGGCCACATCCCTGTGTTTTGTGTACCCTCTTGATTTTGCCCGTACCCGTCTAGCAGCTGATGTGGGTAAAGCTGGAGCTGAAAGGGAATTCCGAGGCCTCGGTGACTGCCTGGTTAAGATCTACAAATCTGATGGGATTAAGGGCCTGTACCAAGGCTTTAACGTGTCTGTGCAGGGTATTATCATCTACCGAGCCGCCTACTTCGGTATCTATGACACTGCAAAGGGTAAGTTTGCTGTGGGCTTTAAAGTTGTGTTCTTAGGAGACAATTTAAAAGAGCGTTGTACCAACCTAACATTCCAAGAGCTAGAGAGTTTTTTTAATTGCTGAAGGAAGCCAAGATCATCCAGTGCGACCCTCATGCACAGATGACATGTTTAGGGGATGTGGGGAAAGGAAGTCAGTAAAACTCTACTTTTTGGTAAAAGCATCTCTTTCCTATTCCCAGGAATGCTTCCGGATCCCAAAAACACTCACATCGTCATCAGCTGGATGATCGCACAGACTGTCACTGCTGTTGCTGGGTTGACTTCCTATCCATTTGA (但文件中这些的倍数)
我希望输出文件具有: '>' GI | 226451773 | GB | FJ846591.1 '百分比会在这里'
答案 0 :(得分:1)
awk
救援!
在这里使用您的输入文件(行)是一个概念证明
$ awk '{s=length($2);
g=gsub("G","",$2); c=gsub("C","",$2); t=gsub("T","",$2); a=gsub("A","",$2);
total=a+c+g+t;
print a,c,g,t,total,100*(c+g)/total,total==s}' file
185 175 190 209 759 48.0896 1
最后一个值1
检查各个匹配项的总匹配数。
您希望将逻辑应用于以>
符号
$ awk '/^>/{...; print $1, 100*(c+g)/total}' inputfile > outputfile
复制上面的相关部分以填补空白。
如果您的行没有任何匹配,则总计将为零,除法将失败。你可以添加一个警卫。
完整的脚本可以像这样
$ awk '/^>/{g=gsub("G","",$2); c=gsub("C","",$2); total=length($2);
print $1, (total?100*(c+g)/total:"no match found")}' inputfile > outputfile
答案 1 :(得分:0)
好的,这是我写的: #! /斌/庆典
#exit program with error if user does not specify input on command line
if [ $# != 1 ]; then
echo "Please specify fasta input on command line and rerun"
exit
else echo "Begining count"
fi
#collect input from user each time they run the program
input=`cat $1`
awk '/>/{...; print $1, 100*(c+g)/total}' $input > GCcontent.txt
awk '{s=length($2);
g=gsub("G","",$2); c=gsub("C","",$2); t=gsub("T","",$2); a=gsub("A","",$2);
total=a+c+g+t;
print a,c,g,t,total,100*(c+g)/total,total}' "$1" >> GCcontent.txt
这就是我的回忆:
开始计数
awk:/> / {...;打印$ 1,100 *(c + g)/总计}
awk:^语法错误
awk:cmd。 line:3:(FILENAME = sample.fa FNR = 1)致命:尝试除零