#!/usr/bin/env perl
#
# Author: petr.danecek@sanger
#

use strict;
use warnings;
use Carp;

my $opts = parse_params();
sort_csq($opts);

exit;

#--------------------------------

sub error
{
    my (@msg) = @_;
    if ( scalar @msg ) { confess @msg; }
    print 
        "Usage: sort-csq [OPTIONS]\n",
        "Options:\n",
        "   -q, --query-output    Sort not a VCF but `bcftools query` output\n",
        "   -h, -?, --help        This help message.\n",
        "\n";
    exit -1;
}
sub parse_params
{
    my $opts = {};
    while (defined(my $arg=shift(@ARGV)))
    {
        if ( $arg eq '-q' || $arg eq '--query' ) { $$opts{query} = 1; next; }
        if ( $arg eq '-?' || $arg eq '-h' || $arg eq '--help' ) { error(); }
        error("Unknown parameter \"$arg\". Run -h for help.\n");
    }
    return $opts;
}

sub sort_csq
{
    my ($opts) = @_;
    while (my $line=<STDIN>)
    {
        if ( $line=~/^#/ ) { print $line; next; }
        chomp($line);
        if ( $$opts{query} )
        {
            $line = sort_comma_separated_fields($line);
        }
        else
        {
            $line = sort_csq_tag($line,'EXP');
            $line = sort_csq_tag($line,'BCSQ');
        }
        print $line."\n";
    }
}

sub sort_csq_tag
{
    my ($line,$tag) = @_;
    if ( !($line=~/$tag=([^;\t]+)/) ) { return $line; }
    my $beg = $`;
    my $end = $';
    my $hit = $1;
    my @vals = sort split(/,/,$hit);
    return $beg."$tag=".join(',',@vals).$end;
}

sub sort_comma_separated_fields
{
    my ($line) = @_;
    my @out = ();
    for my $col (split(/\s+/,$line))
    {
        my @tmp = sort split(/,/,$col);
        push @out,join(',',@tmp);
    }
    return join("\t",@out);
}


