kwiesel
kwiesel

Reputation: 3

How can I generate multiple output files in for loop

I am new to bash so this is a very basic question: I am trying to use the below written $for loop to perform a series of commands for several files in a directory, which should end in a new output (f:r.bw) for every single file.
Basically, I have files like chr1.gz, chr2.gz and so on that should end up as chr1.bw, chr2.bw ... The way it is now, it seems to constantly overwrite the same output file and I cannot figure out what the correct syntax is.

$ for file in *.gz
do
zcat < $file | grep -v ^track > f:r.temp
wigToBigWig -clip -fixedSummaries -keepAllChromosomes f:r.temp hg19.chrom.sizes f:r.bw
rm f:r.temp
done

Thanks for help

Upvotes: 0

Views: 1081

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295815

Instead of using a fixed filename f:r.temp, base your destination name on $file:

for file in *.gz; do
  zcat <"$file" | grep -v '^track' >"${file%.gz}.temp"
  wigToBigWig -clip -fixedSummaries -keepAllChromosomes \
    "${file%.gz}.temp" hg19.chrom.sizes "${file%.gz}.bw"
  rm -f "${file%.gz}.temp"
done

${file%.gz} is a parameter expansion operation, which trims .gz off the end of the name; ${file%.gz}.bw, thus, trims the .gz and adds a .bw.


Even better, if wigToBigWig doesn't need a real (seekable) input file, you can give it a pipeline into the zcat | grep process directly and not need any temporary file:

for file in *.gz; do
  wigToBigWig -clip -fixedSummaries -keepAllChromosomes \
    <(zcat <"$file" | grep -v '^track') \
    hg19.chrom.sizes \
    "${file%.gz}.bw"
done

Upvotes: 2

Related Questions