Flag to human.pl

From BITS wiki
Jump to: navigation, search

This script converts FLAG values in the sam file to human readable output

#!/usr/bin/perl
# author:joachim.jacob@gmail.com
# http://www.bits.vib.be
 
use strict;
use warnings;
 
my $flag = 117;
 
my %flag_set=( 
		'1'  , 'Read has mate reads',
		'2'  , 'All mate reads mapped',
		'4'  , 'Read is unmapped',
		'8'   , 'Next mate read unmapped',
		'16'  , 'Read on reverse strand',
		'32'  , 'Next mate read on rev str',
		'64'   , 'First of all mate reads',
		'128'  , 'Last of all mate reads',
		'256'  , 'Secondary alignment',
		'512'  , 'Read fails quality checks',
		'1024' , 'Read is PCR/opt duplicate',
);
 
 
my %flag_unset=( 
		'1'  , 'Read has NO mate reads',
		'2'  , 'Not all mate reads mapped',
		'4'  , 'Read is mapped',
		'8'   , 'Next mate read is mapped',
		'16'  , 'Read on forward strand',
		'32'  , 'Next mate read on forw str',
		'64'   , 'Not first of all mate reads',
		'128'  , 'Not last of all mate reads',
		'256'  , 'Primary alignment',
		'512'  , 'Read passes quality checks',
		'1024' , 'Read is no duplicate',
);
 
##foreach my $keys (keys %flag_translation){
#	print $keys,"\t", $flag_translation{$keys},"\n";
#}
 
 
my @values;
push @values,1;
 
for (my $i=2; $i<513; $i*=2) {
	push @values,$i;
}
 
foreach (@values){
	print $_,"\t";
	if($flag & $_){
		print "1: ",$flag_set{$_},"\n";
	} else {
		print "0: ",$flag_unset{$_},"\n";
	}
}