0

Relative unix newbie. I have a number of directories (Sample*/), within which I want to merge all raw.sort.bam files using samtools. I have working code to do this within each directory, but I want to deal with all directories at once by running code from the parent directory. My problem is that I’m forced to call up samtools with a full path, and I’m having trouble figuring out how this path will work within a unix loop.

First, here’s my working code for merging and converting from within each directory:

/home/user/pathtosamtools/sam merge -o all.sort.bam *raw.sort.bam

Now, my NON-working code attempting to do this for all directories when run from the parent directory:

for f in `ls Sample*/`; do /home/user/pathtosamtools/sam merge -o $f all.sort.bam Sample*/*raw.sort.bam; done

Errors:

[bam_merge_core_ext] fail to open file all.sort.bam
[bam_header_read] bgzf_check_EOF: Invalid argument
[bam_header_read] invalid BAM binary header (this is not a BAM file).
Segmentation fault

Thanks in advance.

1 Answers1

0

Don't parse ls

for dir in Sample*/; do 
    /home/user/pathtosamtools/sam merge -o "$dir/all.sort.bam" "$dir"/*raw.sort.bam
done

You provide the glob pattern in the for loop.

For safety, always quote your variables.

Community
  • 1
  • 1
glenn jackman
  • 238,783
  • 38
  • 220
  • 352