0

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

0

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I tried the upper suggestion and it is working!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.