This is a test version of Biostars. For the public version, visit https://www.biostars.org.
find function with samtools - convert format to all subfolders

I would like to find all .bam files in subfolders and exec it with samtools. Concretely conversion of bam to cram to same folder and after conversion remove all bam files.

I tried:

find . -maxdepth 3 -name *bam -exec samtools view -Ch -T Path/to/reference/ -@ 5 -o {.}.cram {} \;

But it is creating only one file into folder, where are the script running, not in subfolders. And how to combine with removing files? Thank you

cram bam samtools

2 answers

I don't believe {.} is supported in find -exec, you may be thinking of the GNU parallel where it removes the extension.

If you want to do this purely in find -exec.

find . -maxdepth 3 -name *bam -exec sh -c 'samtools view -Ch -T Path/to/reference/ -@ 5 -o ${0%.bam}.cram $0' {} \;

Or if you wanted to use parallel.

parallel samtools view -Ch -T Path/to/reference/ -@ 5 -o {.}.cram {} ::: $(find . -maxdepth 3 -name *bam)

use fd. Available in brew and ubuntu repos.

$ tree test1

test1
├── file.bam
└── test2
    └── test3
        └── file2.bam

2 directories, 2 files

$ fd -e bam --min-depth 1 -d 4

test1/file.bam
test1/test2/test3/file2.bam

$ fd -e bam --min-depth 1 -d 4 --exec cp {} {.}.cram

$ tree test1

test1
├── file.bam
├── file.cram
└── test2
    └── test3
        ├── file2.bam
        └── file2.cram

2 directories, 4 files

Log in to answer this question.