Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 无法让我的程序运行-%GC计算器_Linux_Bash_Shell_Unix - Fatal编程技术网

Linux 无法让我的程序运行-%GC计算器

Linux 无法让我的程序运行-%GC计算器,linux,bash,shell,unix,Linux,Bash,Shell,Unix,我已经试了几个星期不让这个程序运行。我是编程新手,这绝对是一个挑战。我想我的问题在于我的if语句。我可以让它将名称附加到新文件中,但它只是将整个序列附加到文件中,而不是计算它。我正在使用一个fasta文件,该文件包含多个序列,名称以“>”开头,序列位于它下面的一行。这是我的密码。请帮忙,提前谢谢你 #! /bin/bash #exit program with error if user does not specify input on command line if [ $# != 1

我已经试了几个星期不让这个程序运行。我是编程新手,这绝对是一个挑战。我想我的问题在于我的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 catatagactgcgtggtccgtcatccagagagggttctctcgggtaacctggccaatgtccagctcatcatccacagccagtcttacttcgcctctcaagatccagatccagttcggtgtgtgtggagagaggatccagttcggttcggttcggttcgggtcgggtcgggtcgtcgtcgtcgttgtgtgtgtaccttcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgtcgt附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物附加物TCAGCTGGATCGCACAGACTGTCACTGCTGTGCTTGCTGGGTTGATTCTATCCATTTGA (但在文件中是这些的倍数)

我希望输出文件具有: “>”gi | 226451773 | gb | FJ846591.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

好的,下面是我写的: #! /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 "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://>{;打印1100美元*(c+g)/总计}

awk:^语法错误


awk:cmd。第3行:(FILENAME=sample.fa FNR=1)致命:尝试除以零

等号周围需要空格<代码>$i=“>”。我还建议您研究一下
awk
,在那里您将拥有更强大的编程语言表达能力。请发布一个示例输入文件,其中包含足够的详细信息和预期的输出。首先通过shellcheck.net运行您的代码,以确定更明显的问题。我已经尝试过了,但我不确定它是否有效。我无法插入输入文件使其运行insert
echo
在脚本运行时检查变量值的命令。例如
echo“countG=$countG”
。然后,当出现问题时,您将能够发现位置。我如何在新文件中的值之前添加名称而不使用循环?这是我对代码的主要问题。非常感谢你的帮助
awk
有一个隐式循环贯穿所有行,您不需要显式循环。你能试一下这个脚本吗?我正在写。我会随时告诉你最新情况。谢谢我在上面回答^^我为零添加了一个防护,但您没有完全复制模式。如果您的数据文件是一致的,我将首先检查模式,如果仍然失败,则添加防护。