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
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. 