This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Unmapped reads to fastq (hisat2 / samtools / bedtools) not matching pair-end reads

I want to obtain a fastq file containing reads (RNASeq) that failed to map to a reference genome.

hisat2 -p 3 -x index -1 TD1.R1.fastq.gz -2 TD1.R2.fastq.gz -S out.sam --un-conc out.un.sam --summary-file out.info

I obtained that way 4 files (.sam + .info + out.un.1.sam + out.un.2.sam). Those ".un" files seems like the paired end reads that failed to map. The problem is that the bam2fastq scripts out there want only one bam and create two fastq files.

$ bedtools bamtofastq -i aln.qsort.bam \
                      -fq aln.end1.fq \
                      -fq2 aln.end2.fq

What can I use to take both .un files and create the two fastq files that I need? thanks!

Update: At the time I do the following

#align against first genome
hisat2 -p 3 -x index/svevo -1 reads/TD1.R1.fastq.gz -2 reads/TD1.R2.fastq.gz -S data/landic_svevo_1.sam --un-conc data/landic_svevo_1.unmapped.sam --summary-file data/landic_svevo_1.sam.info
# unmapped files by hisat2 are broken, error when converting to bam

#get unmapped reads with samtools
samtools view -h -f 4 data/landic_svevo_1.sam > data/landic_svevo_1.un.sam

#convert to bam
samtools view -b -S data/landic_svevo_1.un.sam > data/landic_svevo_1.un.bam

#get fastq from unmapped bam
samtools fastq -1 data/landic_svevo_1.un.1.fastq -2 data/landic_svevo_1.un.2.fastq data/landic_svevo_1.un.bam

#align unmapped against second genome
hisat2 -p 3 -x index/zavitan -1 data/landic_svevo_1.un.1.fastq -2 data/landic_svevo_1.un.2.fastq -S data/landic_un_zavitan_1.sam --un data/landic_un_zavitan_1.un.sam --summary-file data/landic_un_zavitan_1.sam.info

But get the next error with hisat2 at the end:

Error, fewer reads in file specified with -1 than in file specified
with -2 terminate called after throwing an instance of 'int' Aborted
(core dumped)
transcriptomics unmapped

You could use samtools fastq (or reformat.sh from BBMap suite) to convert the two .un files to fastq and then use repair.sh from BBMap suite to sync them up, so you can separate any singleton reads and properly pair the rest.

Edit: samtools fastq needs BAM files. I have a complete answer using reformat.sh below.

Any ideas why the unmapped file gives this error?

samtools view -S -b out.un.1.sam
[W::sam_read1] Parse error at line 2
[main_samview] truncated file.

SAM format file is already text. You don't need to use samtools view. Use less/cat/more if you want to look inside. You should not need to convert it to BAM to use samtools fastq, if that is what you intended to do.

seems like samtools fastq only wants bam files, I've updated the question

You are right. Use reformat.sh from BBMap suite in that case.

reformat.sh in=your.R1.sam out=R1.fq.gz
reformat.sh in=your.R2.sam out=R2.fq.gz
repair.sh in1=R1.fq.gz in2=R2.fq.gz out1=proper.R1.fq.gz out2=proper.R2.fq.gz outs=singletons.fq.gz
Error, fewer reads in file specified with -1 than in file specified
with -2 terminate called after throwing an instance of 'int' Aborted
(core dumped)

That is because your read 1/read 2 files are no longer in sync.

Use repair.sh from BBMap suite to fix that issue.

repair.sh in1=landic_svevo_1.un.1.fastq in2=landic_svevo_1.un.2.fastq out1=proper.R1.fq.gz out2=proper.R2.fq.gz outs=singletons.fq.gz

seems like the output of hisat2 --un-conc is broken.

 samtools view -b -S data/landic_svevo_1.un.1.sam > data/landic_svevo_1.un.1.bam
[W::sam_read1] Parse error at line 2
[main_samview] truncated file.

0 answers

No answers yet.

Log in to answer this question.