justaguy
justaguy

Reputation: 3022

bash to create process log for each file

I am using bash to run several processes one after another. I would like to create a log of each process, a log that the process started and completed, but the below only creates and empty log. Thank you :).

So when the first process (# create BAM Index) is run a log is created with the time it was started and completed.

There could be several files for each process but only 1 log is needed with each of the files in it.

When the second process (# strip off @PG tags) is run a log is created with the time it was started and completed. Thank you :).

bash

# create BAM Index
logfile=/home/cmccabe/Desktop/NGS/API/2-12-2015/log.log
for f in /home/cmccabe/Desktop/NGS/API/2-12-2015/*.bam ; do
 bname=`basename $f`
 pref=${bname%%.bam}
 samtools index $f
done > "$logfile"

# strip off @PG tags
logfile=/home/cmccabe/Desktop/NGS/API/2-12-2015/log.log
for f in /home/cmccabe/Desktop/NGS/API/2-12-2015/*.bam ; do
 bname=`basename $f`
 pref=${bname%%.bam}
 samtools view -H $f | sed '/^@PG/d' | samtools reheader - $f > /home/cmccabe/Desktop/NGS/API/2-12-2015/${pref}_newheader.bam
done > "$logfile"

Upvotes: 1

Views: 74

Answers (1)

jkdba
jkdba

Reputation: 2509

As Mentioned in my comment:

I do not recommend that you define your output log file in the loop that you will be using using to generate said output.

Second to output data from the loop I would pipe the data at the end of the loop here: done > "$logfile"

I ran your script like below however, it does not actually output anything.

logfile=log.log

# create BAM Index
for f in *.bam ; do
 bname=`basename $f`
 pref=${bname%%.bam}
 samtools index $f
done > "$logfile"


# strip off @PG tags
for f in *.bam ; do
 bname=`basename $f`
 pref=${bname%%.bam}
 samtools view -H $f | sed '/^@PG/d' | samtools reheader - $f > ${pref}_newheader.bam
done > "$logfile"

If you want to achieve this add a line that will echo the starting and ending of the process like below. Please also notice that on the second loop that the output is appended to the log via syntax >> otherwise you will overwrite the log with >

logfile=log.log

# create BAM Index
for f in *.bam ; do
    echo "Start Index creation: $(date) - File: $f"
    bname=$(basename $f)
    pref=${bname%%.bam}
    samtools index $f
    echo "End Index  creation: $(date) - File: $f"
done > "$logfile"


# strip off @PG tags
for f in *.bam ; do
    echo "Start @PG tag strip creation: $(date) - File: $f"
    bname=$(basename $f)
    pref=${bname%%.bam}
    samtools view -H $f | sed '/^@PG/d' | samtools reheader - $f > ${pref}_newheader.bam
    echo "End @PG tag strip creation: $(date) - File: $f"
done >> "$logfile"

Log file look likes:

Start Index creation: Sat Feb 20 09:58:46 EST 2016 - File: wgEncodeUwRepliSeqBjG1bAlnRep1.bam
End Index  creation: Sat Feb 20 09:58:47 EST 2016 - File: wgEncodeUwRepliSeqBjG1bAlnRep1.bam
Start @PG tag strip creation: Sat Feb 20 09:58:47 EST 2016 - File: wgEncodeUwRepliSeqBjG1bAlnRep1.bam
End @PG tag strip creation: Sat Feb 20 09:58:47 EST 2016 - File: wgEncodeUwRepliSeqBjG1bAlnRep1.bam

Upvotes: 3

Related Questions