对成对的文件执行命令,然后重命名输出



command.py,它将两个成对的文件CA01_S1_R1.fastqCA01_S1_R2.fastq合并在一起。然后,它将结果打印到新目录paired.out中,并将结果文件命名为paired.fastq。完整的命令将读取

command.py -f CA01_S1_R1.fastq -r CA01_S1_R2.fastq -o paired.out

但是,我希望在许多文件上执行此命令,然后将所有输出保存到同一目录中。此外,输出需要具有唯一的名称。因此,我也想发送文件2和3,同时有效地运行这些命令:

command.py -f CA02_S2_R1.fastq -r CA02_S2_R2.fastq -o paired.out

command.py -f CA03_S3_R1.fastq -r CA03_S3_R2.fastq -o paired.out

然而,即使我有代码在所有样本上循环此命令,该命令也会不断覆盖上一次配对的输出,因为所有输出都保存在文件夹paired.out中,文件名为paired.fastq。我是否可以编写一个简单的循环,通过命令发送每个文件对,然后输入文件夹并将文件输出从paired.fastq重命名为CA01_paired.fastq,然后对我的所有文件重复此操作?

我知道我可以通过以下命令发送多个文件:

for f in CA*_S*_R1.fastq; do
# Replace R1 with R2 in the filename and run the command on both files.
command.py -f "$f" -r "${f/R1/R2}" -o paired.ends
done; unset -v f

我想在这个循环中添加第二条指令,将cd放入这个文件夹,并重命名文件,每次递增1。我不知道如何设置增量变量。我想它会是这样的:

for f in CA*_S*_R1.fastq; do
# Replace R1 with R2 in the filename and run the command on both files.
command.py -f "$f" -r "${f/R1/R2}" -o paired.ends
#cd into the output folder
cd paired.ends
#create an environmental variable that keep tracks of which file number I am on
g=01
#rename the output file
mv fastqjoin.join.fastq CA$g_fastqjoin.join.fastq
#update the environmental variable that keeps track of which file number I am on
g= g + 1
#cd out of the folder where the outputs are being stored and back to the folder that contains all the files to be paired.
cd ..
done; unset -v f

假设文件通过blah_R1.fastqblah_R2.fastq配对:

for f in *_R1.fastq; do
    r=${f/_R1/_R2}
    command.py -f "$f" -r "$r" -o paired.out &&
        mv paired.out/paired.fastq paired.out/"${f%%_*}_paired.fastq"
done

相关内容

最新更新