Question: Picard tools samtofastq for a folder
0
gravatar for Cindy
2.0 years ago by
Cindy20
Cindy20 wrote:

I want to use SamToFastq in Picard tools to convert 100 bam files to fastq (pair-end). I wrote a loop, but it does not work. Anyone can help me to look at the code. I have no idea what's going on. Appreciate ahead.

#!/bin/bash
bamfolder="/path/to/folder/*"
for b in $bamfolder
do   
    echo $b   
    java -jar picard.jar SamToFastq \
    I=$b \
    FASTQ= /path/to/folder/*_R1.fastq \
    SECOND_END_FASTQ= /path/to/folder/*_R2.fastq
done
sequencing • 1.4k views
ADD COMMENTlink modified 2.0 years ago • written 2.0 years ago by Cindy20

Are you getting anything back from echo $b. There can be no spaces between FASTQ= and SECOND_END_FASTQ= and the paths.

ADD REPLYlink modified 2.0 years ago • written 2.0 years ago by genomax67k

When I run echo $b done, It listed all bam files in the folder.

ADD REPLYlink written 2.0 years ago by Cindy20

This is academic since @Pierre's solution works but the reason yours is not working is because your are passing $b as the entire file name (with .bam extension intact). So instead if you try the following and see if that works.

for b in $bamfolder
do
    echo $b
    java -jar picard.jar SamToFastq \
    I=$b \
    FASTQ=/path/to/folder/${b%.*}_R1.fastq \
    SECOND_END_FASTQ=/path/to/folder/${b%.*}_R2.fastq
done
ADD REPLYlink modified 2.0 years ago • written 2.0 years ago by genomax67k

I have tried. ERROE: Neither file nor parent directory exist.

I think for the "FASTQ=" and "SECOND_END_FASTQ=" I can not use /path/to/folder/${b%.*}, because it shows output=/path/to/folder/path/to/folder/. If only use FASTQ=${b%.bam}_R1.fq.gz, it works.

ADD REPLYlink written 2.0 years ago by Cindy20

As is customary with these /path/to/folder/ is a placeholder that needs to be replaced with a real path available on your computer. It is there to show that you can use/write data from/to any other location.

ADD REPLYlink modified 2.0 years ago • written 2.0 years ago by genomax67k

Does not work? How it doesn't work? What is the error message?

Anyway, I think there should not be spaces after FASTQ= and SECOND_END_FASTQ=.

ADD REPLYlink written 2.0 years ago by h.mon25k

erroe msg is INPUT is required. It's kind wired. I think the I=$b is right....

ADD REPLYlink written 2.0 years ago by Cindy20
3
gravatar for Pierre Lindenbaum
2.0 years ago by
France/Nantes/Institut du Thorax - INSERM UMR1087
Pierre Lindenbaum120k wrote:
find dir/ -name "*.bam" | while read F ; do  java -jar picard.jar SamToFastq I=$F FASTQ=${F%.bam}_R1.fq.gz SECOND_END_FASTQ=${F%.bam}_R2.fq.gz ; done
ADD COMMENTlink written 2.0 years ago by Pierre Lindenbaum120k

Thanks. It works!!! Can I generate a folder call fastq under the "/path/to/folder/" to output all fastq using the same input filename?

ADD REPLYlink written 2.0 years ago by Cindy20

it it works, please, check the green mark on the left to close the question.

Use 'mkdir -p ' to create a directory

ADD REPLYlink written 2.0 years ago by Pierre Lindenbaum120k

I appreciate your help!

ADD REPLYlink written 2.0 years ago by Cindy20
1
gravatar for badribio
2.0 years ago by
badribio240
badribio240 wrote:
#!/bin/bash
bamfolder="/path/to/folder/*"
for b in $bamfolder
do   
    echo ${b}
    java -jar picard.jar SamToFastq \
    I=$b \
    FASTQ=/path/to/folder/{b}_R1.fastq \
    SECOND_END_FASTQ=/path/to/folder/{b}_R2.fastq
done

space edited per @genomax2

ADD COMMENTlink modified 2.0 years ago • written 2.0 years ago by badribio240

I have tried, same error: ERROR: Option 'INPUT' is required. I don't know why it's always request INPUT

ADD REPLYlink written 2.0 years ago by Cindy20
0
gravatar for Cindy
2.0 years ago by
Cindy20
Cindy20 wrote:

That's my final code and it works. First, use SamToFastq to generate *_R1_fq.gz and *_R2_fq.gz, then use mkdir and mv to move all fastq files to a new folder.

find /path/to/folder/ -name "*.bam" | while read F ; 
do   
    java -jar picard.jar SamToFastq \
         INPUT=$F \
         FASTQ=${F%.*}_R1.fq.gz \
         SECOND_END_FASTQ=${F%.*}_R2.fq.gz 
done

bamfolder="/path/to/folder/*.fq.gz"
for b in $bamfolder
do
fastq="/path/to/folder/fastq/"
mkdir -p $fastq
mv $b $fastq/
done
ADD COMMENTlink modified 2.0 years ago by genomax67k • written 2.0 years ago by Cindy20
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1818 users visited in the last hour