1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
#!/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);
}
|