#! /usr/bin/perl 
# delete the INDELs that convered by another one.

# author: X.Gan

use strict;
use Getopt::Std;
use List::Util qw[min max];

my %opts =( S=>undef, t=>10);
getopts('St:', \%opts);
my $isSam =$opts{S} ;
my $thrsh= $opts{t};

die "Usage: get_rnasta.pl bamfile > out.file\n" if (@ARGV <1); 

my $bamfile =shift;
my $oldchid;
my $seqstr;

my $k;
if ( $isSam) {
	open(BAM_F, $bamfile) or die "Can't find the file";
} else {
	my $cmd="samtools view $bamfile | ";
	open(BAM_F, $cmd) or die "Can't find the file";
}

my @sticky;
my %ct;
while ( <BAM_F> ) 
{
    my $secline = $_;
    my ($contig_id2, $oritNum, $chid2, $startI2, $cig2, $mystrq2);	
    my $rub;
    my $cig;
    ($contig_id2, $oritNum, $chid2, $startI2, $rub, $cig2,$rub, $rub, $rub, $mystrq2) = split(/\t/, $secline);
    $ct{$chid2}++;
}


my @allkeys=keys %ct;
my @gg =reverse sort {  $ct{$a}  <=>  $ct{$b}   } @allkeys;
foreach (@gg) {
    my $key=$_;
    print $key, "\t",  $ct{$key}, "\n";
}



