如何在bash脚本中使用for循环为目录中的文件赋予不同的名称?

huangapple go评论90阅读模式
英文:

How can I give different names to files in a directory with a for loop in a bash script?

问题

我希望获得17个不同的配对末端fastq文件(总共34个),所以我想制作一个bash脚本,可以一次性运行我的代码处理目录中的所有fastq文件。如何在每次脚本运行时更改输入和输出文件的名称,以便在移动到file_002时,所有名称都以file_002开头,而不是file_001,依此类推。此外,在合并R1和R2读取时,如何确保只合并相应的文件并使用循环?例如,只合并file_001_R1与file_001_R2,file_002_R1与file_002_R2,file_003_R1与file_003_R2,依此类推。

  1. for file in directory_name
  2. do
  3. pear -f file_001_R1.fastq.gz -r file_001_R2.fastq.gz -o file_001.fastq
  4. cutadapt -g TGATAACAATTGGAGCAGCCTC...GGATCGACCAAGAACCAGCA -o file_001_barcode.fastq file_001.fastq
  5. cutadapt -g GTGTACAAATAATTGTCAAC...CTGTCTCTTATACACATCTC -o file_001_UMI.fastq file_001.fastq
  6. seqkit concat file_001_barcode.fastq file_001_UMI.fastq > file_001_concatenation.fastq
  7. seqkit rmdup -s file_001_concatenation.fastq -o file_001_unique_pairs.fastq
  8. seqkit subseq -r file_001_unique_pairs.fastq > file_001_unique_barcodes.fasta
  9. bowtie -q --suppress 1,2,4,6,7,8 -x ref_index file_001_unique_barcodes.fasta > file_001_barcodes_allignment.bowtie
  10. sort file_001_barcodes_allignment.bowtie | uniq -c > file_001_barcode_counts.txt
  11. awk 'BEGIN{print "Barcode,TF_variant,Code"}{print $3","$2","$1}' file_001_barcode_counts.txt > file_001_barcode_counts.csv
  12. done
英文:

I'm expecting to get 17 different paired-end fastq files (34 in total), so I want to make a bash script to just run my code through all the fastq files in a directory at once. How can I change the name of the input and output files each time the script runs through each file? So when it moves to the file_002, all names have file_002 at the beginning instead of file_001, and so on. And also, when merging the R1 and R2 reads how can I make that it only merges the correspondant files with a loop? for examples merging only file_001_R1 with file_001_R2, file_002_R1 with file_002_R2, file_003_R1 with file_003_R2, and so on.

  1. for file in directory_name
  2. do
  3. pear -f file_001_R1.fastq.gz -r file_001_R2.fastq.gz -o file_001.fastq
  4. cutadapt -g TGATAACAATTGGAGCAGCCTC...GGATCGACCAAGAACCAGCA -o file_001_barcode.fastq file_001.fastq
  5. cutadapt -g GTGTACAAATAATTGTCAAC...CTGTCTCTTATACACATCTC -o file_001_UMI.fastq file_001.fastq
  6. seqkit concat file_001_barcode.fastq file_001_UMI.fastq > file_001_concatenation.fastq
  7. seqkit rmdup -s file_001_concatenation.fastq -o file_001_unique_pairs.fastq
  8. seqkit subseq -r file_001_unique_pairs.fastq > file_001_unique_barcodes.fasta
  9. bowtie -q --suppress 1,2,4,6,7,8 -x ref_index file_001_unique_barcodes.fasta > file_001_barcodes_allignment.bowtie
  10. sort file_001_barcodes_allignment.bowtie | uniq -c > file_001_barcode_counts.txt
  11. awk 'BEGIN{print "Barcode,TF_variant,Code"}{print $3","$2","$1}' file_001_barcode_counts.txt > file_001_barcode_counts.csv
  12. done

答案1

得分: 0

你可以使用bash 参数扩展 来捕获文件名中的 "file_001" 部分,例如。

  1. cd directory_name
  2. for file in ./*_R1.fastq.gz
  3. do
  4. pear -f "$file" -r "${file%_*}_R2.fastq.gz" -o "${file%_*}.fastq"
  5. cutadapt -g TGATAACAATTGGAGCAGCCTC...GGATCGACCAAGAACCAGCA -o "${file%_*}_barcode.fastq" "${file%_*}.fastq"
  6. cutadapt -g GTGTACAAATAATTGTCAAC...CTGTCTCTTATACACATCTC -o "${file%_*}_UMI.fastq" "${file%_*}.fastq"
  7. seqkit concat "${file%_*}_barcode.fastq" "${file%_*}_UMI.fastq" > "${file%_*}_concatenation.fastq"
  8. seqkit rmdup -s "${file%_*}_concatenation.fastq" -o "${file%_*}_unique_pairs.fastq"
  9. seqkit subseq -r "${file%_*}_unique_pairs.fastq" > "${file%_*}_unique_barcodes.fasta"
  10. bowtie -q --suppress 1,2,4,6,7,8 -x ref_index "${file%_*}_unique_barcodes.fasta" > "${file%_*}_barcodes_allignment.bowtie"
  11. sort "${file%_*}_barcodes_allignment.bowtie" | uniq -c > "${file%_*}_barcode_counts.txt"
  12. awk 'BEGIN{print "Barcode,TF_variant,Code"} {print $3 "," $2 "," $1}' "${file%_*}_barcode_counts.txt" > "${file%_*}_barcode_counts.csv"
  13. done

不确定你的流程是否适当/最佳,你可能想向 https://bioinformatics.stackexchange.com 的专家寻求建议。

英文:

You can use bash parameter expansion to capture the "file_001" part of the filename, e.g.

  1. cd directory_name
  2. for file in ./*_R1.fastq.gz
  3. do
  4. pear -f "$file" -r "${file%_*}_R2.fastq.gz" -o "${file%_*}.fastq"
  5. cutadapt -g TGATAACAATTGGAGCAGCCTC...GGATCGACCAAGAACCAGCA -o "${file%_*}_barcode.fastq" "${file%_*}.fastq"
  6. cutadapt -g GTGTACAAATAATTGTCAAC...CTGTCTCTTATACACATCTC -o "${file%_*}_UMI.fastq" "${file%_*}.fastq"
  7. seqkit concat "${file%_*}_barcode.fastq" "${file%_*}_UMI.fastq" > "${file%_*}_concatenation.fastq"
  8. seqkit rmdup -s "${file%_*}_concatenation.fastq" -o "${file%_*}_unique_pairs.fastq"
  9. seqkit subseq -r "${file%_*}_unique_pairs.fastq" > "${file%_*}_unique_barcodes.fasta"
  10. bowtie -q --suppress 1,2,4,6,7,8 -x ref_index "${file%_*}_unique_barcodes.fasta" > "${file%_*}_barcodes_allignment.bowtie"
  11. sort "${file%_*}_barcodes_allignment.bowtie" | uniq -c > "${file%_*}_barcode_counts.txt"
  12. awk 'BEGIN{print "Barcode,TF_variant,Code"} {print $3 "," $2 "," $1}' "${file%_*}_barcode_counts.txt" > "${file%_*}_barcode_counts.csv"
  13. done

Not sure whether your pipeline is appropriate/optimal though; you might want to ask for advice from the experts over at https://bioinformatics.stackexchange.com

答案2

得分: 0

我不确定我完全理解你的问题,但你可以使用一个bash脚本来做类似这样的事情:循环遍历文件并提取没有扩展名的文件名,然后合并相应的R1和R2文件。

  1. # 设置存放文件的目录
  2. directory="/path/to/directory"
  3. # 遍历目录中的文件
  4. for file in "$directory"/*_R1.fastq; do
  5. # 提取没有扩展名和后缀的文件名
  6. filename=$(basename "$file" | sed 's/_R1.fastq//')
  7. # 设置R1和R2文件名
  8. r1_file="${filename}_R1.fastq"
  9. r2_file="${filename}_R2.fastq"
  10. # 设置输出文件名
  11. output_file="${filename}_merged.fastq"
  12. # 使用相应的R1和R2文件执行合并操作
  13. # 用你想运行的代码替换这一行
  14. done
英文:

I am not sure entirely if I understand your question but you can use a bash script to do something like this: loop through files and extract the file names without the extensions and merge only the corresponding R1 R2 files.

  1. # Set the directory where your files are located
  2. directory="/path/to/directory"
  3. # Loop through the files in the directory
  4. for file in "$directory"/*_R1.fastq; do
  5. # Extract the file name without the extension and suffix
  6. filename=$(basename "$file" | sed 's/_R1.fastq//')
  7. # Set the R1 and R2 file names
  8. r1_file="${filename}_R1.fastq"
  9. r2_file="${filename}_R2.fastq"
  10. # Set the output file name
  11. output_file="${filename}_merged.fastq"
  12. # Perform the merge operation using the corresponding R1 and R2 files
  13. # Replace this line with whatever code you want to run
  14. done

huangapple
  • 本文由 发表于 2023年6月29日 10:43:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577770.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定