find function with samtools - convert format to all subfolders
2
0
Entering edit mode
2.6 years ago
Korsocius ▴ 260

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 • 682 views
ADD COMMENT
4
Entering edit mode
2.6 years ago

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)
ADD COMMENT
1
Entering edit mode
2.6 years ago

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
ADD COMMENT

Login before adding your answer.

Traffic: 1304 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6