【问题标题】:How to make the bash script work with one command after another?如何使 bash 脚本与一个又一个命令一起工作?
【发布时间】:2018-04-03 11:49:53
【问题描述】:

我有一个如下所示的 bash 脚本。首先,它将 sorted.bam 文件作为输入并使用“stringtie”工具将每个样本 gtf 作为输出。然后每个样本 gtf 的路径将被输入到 mergelist.txt 中。然后对它们使用“stringtie merge”得到“stringtie_merged.gtf”。

我总共有 40 个 sorted.bam 文件。

for sample in /path/*.sorted.bam
do
dir="/pathto/hisat2_output"
dir2="/pathto/folder"
base=`basename $sample '.sorted.bam'`
"stringtie -p 8 -G gencode.v27.primary_assembly.annotation_nochr.gtf -o ${dir2}/stringtie_output/${base}/${base}_GRCh38.gtf -l ${dir2}/stringtie_output/${base}/${base} ${dir}/${base}.sorted.bam; ls ${dir2}/stringtie_output/*/*_GRCh38.gtf > mergelist.txt; stringtie --merge -p 8 -G gencode.v27.primary_assembly.annotation_nochr.gtf -o ${dir2}/stringtie_output/stringtie_merged.gtf mergelist.txt"
done

我用; 分隔命令在对所有 sorted.bam 文件运行脚本并完成作业后,我看到 mergelist.txt 仅包含 33 个示例 gtf 的路径。这意味着合并 list.txt 中缺少其他 7 个示例 gtfs 的路径。

;分隔命令是正确的还是有其他方法?

脚本应该首先使用一个命令,输出的路径需要在文本文件中给出,然后使用另一个命令。

【问题讨论】:

  • 有什么问题?这会失败吗?
  • 是的。我在合并列表中仅看到 33 个样本的路径。txt 没有找到其他 7 个样本的路径。此外,我看到在前 4 个样本上完成 stringtie 后,我看到了 mergelist.txt 和 stringtiemerged.gtf 文件。我需要的是它应该首先在所有样本上应用 stringtie。完成后,它应该将所有输出的路径放入文本文件中,然后对其应用 stringtie 合并。
  • 不要引用整个stringtie

标签: linux bash shell bioinformatics


【解决方案1】:

您没有用分号分隔命令;您调用了一个嵌入了分号的命令。考虑一下简单的脚本:

"ls; pwd"

此脚本调用ls,后跟pwd。相反,shell 将搜索 PATH 以查找名为 ls; pwd 的文件(即名称中带有分号和空格的文件),可能找不到并返回错误消息。您需要删除双引号。

【讨论】:

    【解决方案2】:

    多行有什么问题,因为你已经有不止一行了:

    dir="/pathto/hisat2_output"
    dir2="/pathto/folder"
    for sample in /path/*.sorted.bam ;do
      base=$(basename ${sample} '.sorted.bam')
      stringtie -p 8 -G gencode.v27.primary_assembly.annotation_nochr.gtf -o ${dir2}/stringtie_output/${base}/${base}_GRCh38.gtf -l ${dir2}/stringtie_output/${base}/${base} ${dir}/${base}.sorted.bam
      ls ${dir2}/stringtie_output/*/*_GRCh38.gtf > mergelist.txt 
      stringtie --merge -p 8 -G gencode.v27.primary_assembly.annotation_nochr.gtf -o ${dir2}/stringtie_output/stringtie_merged.gtf mergelist.txt
    done
    

    无论如何,我看不出在循环中包含第二个 stringtie 命令的意义,它应该在之后正常工作。

    如果 stringtie 能够处理 STDIN,您可能会在没有 mergelist.txt 的情况下使用:

    stringtie --merge -p 8 -G gencode.v27.primary_assembly.annotation_nochr.gtf -o ${dir2}/stringtie_output/stringtie_merged.gtf <<< $(echo ${dir2}/stringtie_output/*/*_GRCh38.gtf)
    

    【讨论】:

      【解决方案3】:

      你应该双引号你的变量并使用$( command )而不是反引号

      base=$( basename $sample '.sorted.bam' ) : 文件名中有空格??

      喜欢:

      base=$( basename "$sample.sorted.bam" ) # with or without space
      

      如果你有空格,你必须双引号:

      stringtie -p 8 \
          -G gencode.v27.primary_assembly.annotation_nochr.gtf \
          -o "$dir2/stringtie_output/$base/$base_GRCh38.gtf" \
          -l "$dir2/stringtie_output/$base/$base" \
          "$dir/$base.sorted.bam"
      ls "$dir2"/stringtie_output/*/*_GRCh38.gtf > mergelist.txt
      ...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-05-28
        • 1970-01-01
        • 2020-02-05
        • 1970-01-01
        • 1970-01-01
        • 2015-04-10
        • 1970-01-01
        相关资源
        最近更新 更多