Monday, May 5, 2014

How to find your RNA-Seq data is unstranded or stranded

If you cannot figure out the strandness of your RNA-Seq data by BLAT or tags of SAM file -- there are couple of very nice documents on the web --  here is an alternative with IGV and I built a script based on that idea.

With the IGV

1. Generate a BAM file (by mapping to genome with your preferred aligner)
2. Generate an index by SAMTools.
3. Import the BAM file in the IGV.
5. Right click -> Color alignemnt by -> first-in-pair strand

If your data is stranded, you can see some dominant color in the area.


Stranded Unstranded


With a script

Based on this, I built the following script for detecting strandness. 
It will check reads on the GAPDH or first 3 IDs of the GTF file and report the ratios of the major strand. The strandness will be determined if more than 90% of majority reads share the same strand. 

Save it as 'strandness.sh' and add executable permission. ('chmod a+x strandness.sh')
It will check if it doesn't have a index file or it mismatched organism. 
You need to install samtools first to use this script.

Usage) 

 a) Checking human (hg19)

 strandness.sh test.bam 

 or 

 strandness.sh test.bam human 

 b) Checking mouse (mm9)

 strandness.sh test.bam mouse 

 c) Checking with GTF file 

 strandness.sh test.bam test.gtf 


Output) 

 Checking standness script v0.01
 Input file: unstrand.bam
 GTF file  : ../goat_genes.gtf

 85.57% of reads (4409/5152) are pos-strand in NC_005044.2
 48.94% of reads (557854/1139785) are neg-strand in NC_022293.1
 45.06% of reads (846306/1877961) are neg-strand in NC_022294.1
 The data looks like un-stranded.

Script)

 echo -e "Checking standness script v0.01\n" 

 #Default test region is Human GAPDH  REGION="chr12:6643571-6647541"
 if [ -z "$1" ]
 then
    echo -e "\nNo alingment file selected.\n"
 elif [ -a "$1" ]
 then
   echo Input file: $1
   if [ -a $1.bai ]
   then
     if [ -z "$2" ]
     then 
       echo -e "Organism : Human (hg19)"
     else
       if [ $2 == "mouse" ]
       then
         echo -e "Organism : Mouse (mm9)"
         # This is Mouse GAPDH         
         REGION="chr6:125111870-125115791" 
       elif [ $2 == "human" ] 
       then 
         echo -e "Organism : Human (hg19)" 
       else 
         echo -e "GTF file : $2" 
         if [ -a $2 ] 
         then
           REGION="`cut -f1 $2 | uniq | head -n 3`"
         else
           echo -e "\nCouldn\'t find the GTF file.\n" 
           exit
         fi
       fi
     fi
     STRANDED="YES"
     echo
     for R in `echo $REGION`
     do
       SUB_TOTAL=`samtools view -f 0x40 $1 $R | wc -l`
       if [ $SUB_TOTAL == "0" ]
       then
         echo -e "\nYour BAM file and the oranism are not matched.\n"
         exit
       fi
       SUB_NEG=`samtools view -f 0x40 $1 $R | grep XS:A:- | wc -l`
       SUB_POS=`samtools view -f 0x40 $1 $R | grep XS:A:+ | wc -l`
       SUB_DOM=$SUB_NEG 
       MAJOR=neg-strand
       if [ $SUB_POS -gt $SUB_DOM ]
       then
         SUB_DOM=$SUB_POS
         MAJOR=pos-strand
       fi
       RATIO=`echo "scale = 2; $SUB_DOM * 100 / $SUB_TOTAL"| bc`
       echo $RATIO% of reads \($SUB_DOM\/$SUB_TOTAL\) are $MAJOR in $R
       if [ "$(echo $RATIO '<' 90.00 | bc -l)" -eq 1 ]
       then
         STRANDED="NO"
       fi
     done
     if [ $STRANDED == "YES" ]
     then
       echo -e "\nThe data may be stranded."
     else
       echo -e "\nThe data looks like un-stranded."
     fi
   else
     echo -e "\nNo index file found, quitting...\n"
   fi
 fi

No comments:

Post a Comment