This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How can I only took the mapped ones from STAR ?

Hello,

I wanted to obtain only the "mapped" reads as an output of the STAR. I forgot to delete "--outSAMunmapped Within " and all of my mapped output files also contain all unmapped reads,too. Data size is huge and re-mapping properly wiil take so much time...

How can I fix the BAM files which were supposed to be mapped reads but also include unmapped reads in it ?

#!/bin/bash
mkdir /mnt/data/Toxo_scan/GBR_Male/ToxoMap
while read -r line
do
mkdir ToxoMap/$line
echo $line" -> Running STAR - Toxo now"
STAR  --runThreadN 12 --alignIntronMax 1 --outSAMunmapped Within --outSAMtype BAM SortedByCoordinate --genomeDir "/mnt/data/Toxo_scan/toxo_genome" --readFilesIn "/mnt/data/Toxo_scan/GBR_Male/sickle/"$line"_1_clean.fastq" "/mnt/data/Toxo_scan/GBR_Male/sickle/"$line"_2_clean.fastq" --outFileNamePrefix "/mnt/data/Toxo_scan/GBR_Male/ToxoMap/"$line"_" --outReadsUnmapped Fastx
done
rna-seq star genome samunmapped

Is the presence of unmapped reads causing issues? If you were going to do counts etc those reads will be ignored.

They were causing the issues because, I was supposed to process only the mapped reads in the workflow. For my case, presence of the unmapped reads in the mapped reads is like having false positive results into the true positive results...

Out of curiosity what was the downstream workflow component that was causing a problem? Most programs should understand reads that are unmapped easily.

The issue was not related with downstream workflow components, it was about my supervisor's satisfaction :) When I came with millions of undesired "unmapped" reads as a result of "mapped" reads, I was in the position of not doing the task well. That's all :)

it was about my supervisor's satisfaction :)

Aha. No logical solution would suffice in that case.

2 answers

To get your mapped reads:

samtools view -b -F 4 alignment.bam > mapped.bam

To get your unmapped reads:

samtools view -b -f 4 alignment.bam > unmapped.bam

The -f filters based on bitwise samflags. Flag 4 (or 0x0004) is for unmapped reads. -F does the inverse.

Thanks a lot friend, it worked.

Go ahead and accept the answer (green checkmark) to provide closure to the thread. You can accept more than one answer.

This is what you're looking for:

samtools view -f 0x2 foo.bam > foo.filtered.bam

It will keep only the proper pairs in your alignment (if paired end). Proper pairs: correct orientation and within insert size.

Log in to answer this question.