简体   繁体   English

使用成对的输入文件和多个输出文件编写bash或python for循环

[英]Writing a bash or python for loop with paired input files and multiple output files

I'm working on a very common set of commands used to analyze RNA-seq data. 我正在研究一组非常常见的用于分析RNA序列数据的命令。 However, since this question is not specific to bioinformatics, I've chosen to post here instead of BioStars, etc. 但是,由于该问题并非特定于生物信息学,因此我选择在此处发布文章,而不是BioStars等。

Specifically, I am trimming Illumina Truseq adapters from paired end sequencing data. 具体来说,我正在从配对末端测序数据中修剪Illumina Truseq适配器。 To do so, I use Trimmomatic 0.36. 为此,我使用Trimmomatic 0.36。

I have two input files: 我有两个输入文件:

S6D10MajUnt1-1217_S12_R1_001.fastq.gz
S6D10MajUnt1-1217_S12_R2_001.fastq.gz

And the command generates five output files: 该命令生成五个输出文件:

S6D10MajUnt1-1217_S12_R1_001.paired.fq.gz
S6D10MajUnt1-1217_S12_R1_001.unpaired.fq.gz
S6D10MajUnt1-1217_S12_R2_001.paired.fq.gz
S6D10MajUnt1-1217_S12_R2_001.unpaired.fq.gz
S6D10MajUnt1-1217_S12.trimlog

I'm trying to write a python or bash script to recursively loop over all the contents of a folder and perform the trim command with appropriate files and outputs. 我正在尝试编写python或bash脚本以递归地循环访问文件夹的所有内容,并使用适当的文件和输出执行trim命令。

#!/bin/bash
for DR in *.fastq.gz
do
FL1=$(ls ~/home/path/to/files/${DR}*_R1_*.fastq.gz)
FL2=$(ls ~/home/path/to/files/${DR}*_R2_*.fastq.gz)
java -jar ~/data2/RJG_Seq/apps/Trimmomatic-0.36/trimmomatic-0.36.jar PE -threads 12 -phred33 -trimlog ~/data2/RJG_Seq/trimming/sample_folder/$FL1.trimlog ~/data2/RJG_Seq/demultiplexing/sample_folder/$FL1 ~/data2/RJG_Seq/demultiplexing/sample_folder/$FL2 ~/data2/RJG_Seq/trimming/sample_folder/$FL1.pair.fq.gz ~/data2/RJG_Seq/trimming/sample_folder/$FL1.unpair.fq.gz ~/data2/RJG_Seq/trimming/sample_folder/$FL2.pair.fq.gz ~/data2/RJG_Seq/trimming/sample_folder/$FL2.unpair.fq.gz ILLUMINACLIP:/media/RJG_Seq/apps/Trimmomatic-0.36/TruSeq3-PE.fa:2:30:10 LEADING:5 TRAILING:5 SLIDINGWINDOW:4:15 MINLEN:28
done

I believe there's something wrong with the way I am assigning and invoking FL1 and FL2, and ultimately I'm looking for help creating an excecutable command trim-my-reads.py or trim-my-reads.sh that could be modified to accept any arbitrarily named input R1.fastq.gz and R2.fastq.gz files. 我认为我分配和调用FL1和FL2的方式出了问题,最终我正在寻求帮助,以创建可修改为接受的可执行命令trim-my-reads.py或trim-my-reads.sh任何任意命名的输入R1.fastq.gz和R2.fastq.gz文件。

You can write a simple python script to loop over all the files in a folder. 您可以编写一个简单的python脚本来遍历文件夹中的所有文件。

Note : I have assumed that the output files will be generated in a folder named "example" 注意:我假设输出文件将在名为“ example”的文件夹中生成

import glob
for file in glob.glob("*.fastq.gz"):
    #here you'll unzip the file to a folder assuming "example"
    for files in glob.glob("/example/*"):
        #here you can parse all the files inside the output folder

Each pair of samples has a matching string ( SN =sample N) A solution to this question in bash could be: 每对样本都有一个匹配的字符串( SN =样本N)。bash中此问题的解决方案可能是:

#!/bin/bash
#apply loop function to samples 1-12
for SAMPLE in {1..12} 
do
#set input file 1 to "FL1", input file 2 to "FL2"
FL1=$(ls ~path/to/input/files/_S${SAMPLE}_*_R1_*.gz)
FL2=$(ls ~path/to/input/files/_S${SAMPLE}_*_R2_*.gz)

#invoke java ,send FL1 and FL2 to appropriate output folders
java -jar ~/path/to/trimming/apps/Trimmomatic-0.36/trimmomatic-0.36.jar PE
-threads 12 -phred33 -trimlog ~/path/to/output/folders/${FL1}.trimlog
~/path/to/input/file1/${FL1} ~/path/to/input/file2/${FL2} 
~/path/to/paired/output/folder/${FL1}.pair.fq.gz ~/path/to/unpaired/output/folder/${FL1}.unpair.fq.gz 
~/path/to/paired/output/folder/${FL2}.pair.fq.gz ~/path/to/unpaired/output/folder/${FL2}.unpair.fq.gz 
ILLUMINACLIP:/path/to/trimming/apps/Trimmomatic-0.36/TruSeq3-PE.fa:2:30:10 LEADING:5 TRAILING:5 SLIDINGWINDOW:4:15 MINLEN:28

#add verbose option to track progress
echo "Sample ${SAMPLE} done"
done

This is an inelegant solution, because it requires the format I'm using. 这是一个不太好的解决方案,因为它需要我使用的格式。 A better method would be to grep each filename and assign them to FL1, FL2 accordingly, because this would generalize the method. 更好的方法是grep每个文件名,并相应地将它们分配给FL1,FL2,因为这将使方法通用化。 Still, this is what worked for me, and I can easily control which samples are subjected to the for loop, as long as I always have the _S * _ format in the filename strings. 尽管如此,这对我还是有用的,只要我在文件名字符串中始终使用_S * _格式,就可以轻松控制哪些样本要进行for循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM