[英]bash loop file in one folder against all files in another folder
I am trying to do a ( bedtools ) intersect.我正在尝试做一个( bedtools )相交。 Anyone see what is wrong with my bash code here?
有人在这里看到我的 bash 代码有什么问题吗?
#!/bin/bash
dir=$(pwd)
query=$dir/query_beds
pgs=$dir/pgs_beds
for file in $query/*; do
bedtools intersect -wa -wb -a $file -b $pgs/* -c -C -sorted -filenames
done > ${file%.*}-results.txt
"query" directory contains many files which need to each be iteratively queried against many files in "pgs" directory using the package bedtools and command intersect, which allows an asterisk for file "-b" to cycle multiple files. “query”目录包含许多文件,每个文件都需要使用 package bedtools 和命令 intersect 对“pgs”目录中的许多文件进行迭代查询,这允许文件“-b”的星号循环多个文件。
Per usage:每次使用:
One or more BAM/BED/GFF/VCF file(s) “B”. Use “stdin” if passing B with a UNIX pipe.
NEW!!!: -b may be followed with multiple databases and/or wildcard (*) character(s).
I believe the problem is with $pgs/*, looping one file against multiple files in another directory and I may need to restructure my loop to change directory or something.我认为问题出在 $pgs/*,将一个文件循环到另一个目录中的多个文件,我可能需要重组我的循环以更改目录或其他内容。
UPDATE:更新:
I have updated the script to work and was able to get everything working with:我已经更新了脚本来工作并且能够让所有的东西都工作:
#!/bin/bash -x
dir=$(pwd)
query=$dir/query_beds
pgs=$dir/pgs_beds
for f in $(ls $query/*.bed); do
for g in $(ls $pgs/*); do
bedtools intersect -wa -wb -a $f -b $g* -C -filenames
done > $(basename $f .ext).txt
done
In your code the output of the whole loop is redirected to one file.在您的代码中,整个循环的 output 被重定向到一个文件。 Maybe you want something like below (redirect the output of each bedtool to a separate file).
也许你想要像下面这样的东西(将每个 bedtool 的 output 重定向到一个单独的文件)。
Also note that if you use ${file%.*}
the outputs will be created in the $query
directory so executing the script twice might result in unexpected results.另请注意,如果您使用
${file%.*}
,输出将创建在$query
目录中,因此执行脚本两次可能会导致意外结果。 I would recommend to use $(basename $file.ext).txt
(where.ext is the extension of the files in $query
. The basename command keeps only the name of the file (so without the directory) and also removes the.ext if present.我建议使用
$(basename $file.ext).txt
(where.ext 是$query
中文件的扩展名。basename 命令只保留文件的名称(因此没有目录)并且还删除了。分机如果存在。
#!/bin/bash
dir=$(pwd)
query=$dir/query_beds
pgs=$dir/pgs_beds
for file in $query/*; do
bedtools intersect -wa -wb -a $file -b $pgs/* -c -C -sorted -filenames > ${file%.*}-results.txt
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.