<commit_before>#Package- merge VCF files and run pileup for SNP and exonerate for indels

package Sanger::CGP::Vaf::Data::ReadVcf;
##########LICENCE############################################################
# Copyright (c) 2016 Genome Research Ltd.
#
# Author: Cancer Genome Project <EMAIL>
#
# This file is part of cgpVAF.
#
# cgpVAF is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
##########LICENCE##############################################################

BEGIN {
  $SIG{__WARN__} = sub {warn $_[0] unless(($_[0] =~ m/^Odd number of elements in hash assignment/) || ($_[0] =~m/^Use of uninitialized value \$gtype/) || ($_[0] =~ m/^Use of uninitialized value \$buf/)|| ($_[0] =~ m/symlink exists/) || ($_[0] =~ m/gzip: stdout: Broken pipe/) )};

};

use strict;
use Vcf;
use Data::Dumper;
use English;
use FindBin qw($Bin);
use List::Util qw(first reduce max min);
use warnings FATAL => 'all';
use Capture::Tiny qw(:all);
use Const::Fast qw(const);
use Carp;
use Try::Tiny qw(try catch finally);
use File::Remove qw(remove);
use File::Path qw(remove_tree);

use Sanger::CGP::Vaf; # exports VERSION
use Bio::DB::HTS;
use Bio::DB::HTS::Constants;
use Sanger::CGP::Vaf::VafConstants;
use Sanger::CGP::Vaf::Process::Variant;

use Log::Log4perl;
my $log = Log::Log4perl->get_logger(__PACKAGE__);

use base qw(Sanger::CGP::Vaf::Data::AbstractVcf);

const my $SORT_N_BGZIP => q{(grep '^#' %s ; grep -v '^#' %s | sort -k1,1 -k2,2n -k4,4 -k5,5 )| bgzip -c >%s };
const my $TABIX_FILE => q{tabix -f -p vcf %s};
const my $VALIDATE_VCF => q{vcf-validator -u %s};

1;

sub _localInit {
    my $self=shift;
    $self->_isValid();
}

sub _isValid {
    my $self=shift;
    $log->logcroak("Input folder must be specified") unless(defined $self->{_d} && -e $self->{_d});
    $log->logcroak("variant type must be specified") unless(defined $self->{_a});
    $log->logcroak("Tumour sample name(s) must be specified") unless(defined $self->{_tn});
    $log->logcroak("Normal sample name must be specified") unless(defined $self->{_nn});
    $log->logcroak("vcf file extension with dot(.) must be specified") unless(defined $self->{_bo} || defined $self->{_e});
    $log->logcroak("Genome must be specified") unless(defined $self->{_g} && -e $self->{_g});
    return 1;
}


=head2 getChromosomes

get chromosome names and length from genome file, filtered by either user specification or
available data in VCF/BED inputs

=over 2
=back
=cut

sub getChromosomes {
    my($self,$chr_list)=@_;
    my %data_chr;
    if($chr_list && scalar @{$chr_list}) {
        %data_chr = map { $_ => 1 } @$chr_list;
    }
    else {
        $self->getVcfChromosomes(\%data_chrs);
        $self->getBedChromosomes(\%data_chrs);
    }

    my %filtered_chr;
    open my $fai_fh , '<', $self->{'_g'}.'.fai';
    while (<$fai_fh>) {
        next if ($_=~/^#/);
        my($chr,$len)=(split "\t", $_)[0,1];
        if(exists $data_chr{$chr}){
            %filtered_chr{$chr}=$len;
        }
    }
    return \%filtered_chr;
}

sub getVcfChromosomes {
    my ($self, $store) = @_;
    foreach my $sample (keys %{$self->{'vcf'}}) {
        if(-e $self->{'vcf'}{$sample}) {
            my $vcf = Vcf->new(file => $self->{'vcf'}{$sample});
            foreach my $chr($vcf->get_chromosomes()) {
                $store->{$chr} = 1;
            }
        }
    }
    return 1;
}

sub getBedChromosomes {
    my ($self, $store) = @_;<commit_msg>Fix variable name<commit_after>#Package- merge VCF files and run pileup for SNP and exonerate for indels

package Sanger::CGP::Vaf::Data::ReadVcf;
##########LICENCE############################################################
# Copyright (c) 2016 Genome Research Ltd.
#
# Author: Cancer Genome Project <EMAIL>
#
# This file is part of cgpVAF.
#
# cgpVAF is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
##########LICENCE##############################################################

BEGIN {
  $SIG{__WARN__} = sub {warn $_[0] unless(($_[0] =~ m/^Odd number of elements in hash assignment/) || ($_[0] =~m/^Use of uninitialized value \$gtype/) || ($_[0] =~ m/^Use of uninitialized value \$buf/)|| ($_[0] =~ m/symlink exists/) || ($_[0] =~ m/gzip: stdout: Broken pipe/) )};

};

use strict;
use Vcf;
use Data::Dumper;
use English;
use FindBin qw($Bin);
use List::Util qw(first reduce max min);
use warnings FATAL => 'all';
use Capture::Tiny qw(:all);
use Const::Fast qw(const);
use Carp;
use Try::Tiny qw(try catch finally);
use File::Remove qw(remove);
use File::Path qw(remove_tree);

use Sanger::CGP::Vaf; # exports VERSION
use Bio::DB::HTS;
use Bio::DB::HTS::Constants;
use Sanger::CGP::Vaf::VafConstants;
use Sanger::CGP::Vaf::Process::Variant;

use Log::Log4perl;
my $log = Log::Log4perl->get_logger(__PACKAGE__);

use base qw(Sanger::CGP::Vaf::Data::AbstractVcf);

const my $SORT_N_BGZIP => q{(grep '^#' %s ; grep -v '^#' %s | sort -k1,1 -k2,2n -k4,4 -k5,5 )| bgzip -c >%s };
const my $TABIX_FILE => q{tabix -f -p vcf %s};
const my $VALIDATE_VCF => q{vcf-validator -u %s};

1;

sub _localInit {
    my $self=shift;
    $self->_isValid();
}

sub _isValid {
    my $self=shift;
    $log->logcroak("Input folder must be specified") unless(defined $self->{_d} && -e $self->{_d});
    $log->logcroak("variant type must be specified") unless(defined $self->{_a});
    $log->logcroak("Tumour sample name(s) must be specified") unless(defined $self->{_tn});
    $log->logcroak("Normal sample name must be specified") unless(defined $self->{_nn});
    $log->logcroak("vcf file extension with dot(.) must be specified") unless(defined $self->{_bo} || defined $self->{_e});
    $log->logcroak("Genome must be specified") unless(defined $self->{_g} && -e $self->{_g});
    return 1;
}


=head2 getChromosomes

get chromosome names and length from genome file, filtered by either user specification or
available data in VCF/BED inputs

=over 2
=back
=cut

sub getChromosomes {
    my($self,$chr_list)=@_;
    my %data_chrs;
    if($chr_list && scalar @{$chr_list}) {
        %data_chrs = map { $_ => 1 } @$chr_list;
    }
    else {
        $self->getVcfChromosomes(\%data_chrs);
        $self->getBedChromosomes(\%data_chrs);
    }

    my %filtered_chr;
    open my $fai_fh , '<', $self->{'_g'}.'.fai';
    while (<$fai_fh>) {
        next if ($_=~/^#/);
        my($chr,$len)=(split "\t", $_)[0,1];
        if(exists $data_chrs{$chr}){
            %filtered_chr{$chr}=$len;
        }
    }
    return \%filtered_chr;
}

sub getVcfChromosomes {
    my ($self, $store) = @_;
    foreach my $sample (keys %{$self->{'vcf'}}) {
        if(-e $self->{'vcf'}{$sample}) {
            my $vcf = Vcf->new(file => $self->{'vcf'}{$sample});
            foreach my $chr($vcf->get_chromosomes()) {
                $store->{$chr} = 1;
            }
        }
    }
    return 1;
}

sub getBedChromosomes {
    my ($self, $store) = @_;
{'fraction_non_alphanumeric': 0.19707390271351757, 'fraction_numerical': 0.007502813555083156, 'mean_word_length': 3.832628398791541, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2543388', 'n_tokens_mistral': 2848, 'n_tokens_neox': 2755, 'n_words': 818}