<commit_before>#!/usr/bin/env perl

use Getopt::Std;
use warnings;
use strict;

our ($opt_F, $opt_R, $opt_s, $opt_r, $opt_n, $opt_N, $opt_H, $opt_q, $opt_p, $opt_b, $opt_f, $opt_c, $opt_u, $opt_D, $opt_Q, $opt_P, $opt_M, $opt_o, $opt_V, $opt_C, $opt_G, $opt_A);
getopts('suHabR:F:f:n:r:p:q:c:D:P:Q:M:o:V:C:G:A:') || USAGE();

my %AA_code = (
    "ALA" => "A",  "ILE" => "I",  "LEU" => "L",  "VAL" => "V",
    "PHE" => "F",  "TRP" => "W",  "TYR" => "Y",  "ASN" => "N",
    "CYS" => "C",  "GLN" => "Q",  "MET" => "M",  "SER" => "S",
    "THR" => "T",  "ASP" => "D",  "GLU" => "E",  "ARG" => "R",
    "HIS" => "H",  "LYS" => "K",  "GLY" => "G",  "PRO" => "P", "TER" => "*"
);

USAGE() if ( $opt_H );
my $FRACTION = $opt_r ? $opt_r : 0.4;
my $MAXRATIO = $opt_R ? $opt_R : 1.0;
my $CNT = $opt_n ? $opt_n : 10;
my $AVEFREQ = $opt_F ? $opt_F : 0.15;
my $MINPMEAN = $opt_p ? $opt_p : 5;
my $MINQMEAN = $opt_q ? $opt_q : 25;
my $FILPMEAN = $opt_P ? $opt_P : 0; # will be filtered on the first place
my $FILQMEAN = $opt_Q ? $opt_Q : 0; # will be filtered on the first place
my $FILDEPTH = $opt_D ? $opt_D : 0; # will be filtered on the first place
my $MINFREQ = $opt_f ? $opt_f : 0.02;
my $MINMQ = $opt_M ? $opt_M : 10;
my $MINVD = $opt_V ? $opt_V : 2; # minimum variant depth
my $MAF = $opt_G ? $opt_G : 0.0025;
my $SN = $opt_o ? $opt_o : 1.5;
my @controls = $opt_c ? split(/:/, $opt_c) : ();
my %controls = map { ($_, 1); } @controls;
my %MultiMaf;
$opt_A = defined($opt_A) ? $opt_A : "/ngs/reference_data/genomes/Hsapiens/hg19/variation/dbSNP_multi_mafs_latest.txt";
setupMultiMaf($opt_A) if ( -e $opt_A );


# SBF: Strand Bias Fisher Exact test
my @columns = qw(CDS AA CNT END DP AF BIAS PMEAN PSTD QUAL QSTD SBF CAF VD CLNSIG ODDRATIO HIAF MQ SN ADJAF NM SHIFT3 MSI dbSNPBuildID);
my @ampcols = ();
my @paircols = ();
my @appcols = $opt_C ? split(/:/, $opt_C) : ();
push(@columns, @appcols) if ($opt_C);
my @data;
my %sample;
my %var;
my %CONTROL;
while( <> ) {
    next if ( /^#/ );
    chomp;
    my @a = split(/\t/);
    next unless( $a[6] eq "PASS" );
    $a[7] .= ";";
    my %d;
    while( $a[7] =~ /([^=;]+)=([^=]+?);/g ) {
	$d{ $1 } = $2;
    }
    @ampcols = qw(GDAMP TLAMP NCAMP AMPFLAG) if ( $d{GDAMP} && $d{TLAMP} );
    my @formats = split(/:/, $a[8]);
    my @fdata = split(/:/, $a[9]);
    my @mfdata = $a[10] ? split(/:/, $a[10]) : ();
    for(my $i = 0; $i < @formats; $i++) {
	$d{ $formats[$i] } = $fdata[$i];
	$d{ "M_$formats[$i]" } = $mfdata[$i] if ( $a[10] );
    }

    # Adapt for MuTect or FreeBayes
    unless( $d{ PMEAN } ) {  # Meaning not VarDict
	if ( $d{ AD } ) { # in case it's not defined in freebayes
	    my @ads = split(/,/, $d{ AD });
	    my $ads_sum = 0;
	    $ads_sum += $_ foreach( @ads );
	    $d{ AF } = sprintf("%.3f", $ads[1]/$ads_sum);
	    $d{ VD } = $ads[1];<commit_msg>Checking for div by zero at ads_sum (adapt for the gatk-haplotype caller)<commit_after>#!/usr/bin/env perl

use Getopt::Std;
use warnings;
use strict;

our ($opt_F, $opt_R, $opt_s, $opt_r, $opt_n, $opt_N, $opt_H, $opt_q, $opt_p, $opt_b, $opt_f, $opt_c, $opt_u, $opt_D, $opt_Q, $opt_P, $opt_M, $opt_o, $opt_V, $opt_C, $opt_G, $opt_A);
getopts('suHabR:F:f:n:r:p:q:c:D:P:Q:M:o:V:C:G:A:') || USAGE();

my %AA_code = (
    "ALA" => "A",  "ILE" => "I",  "LEU" => "L",  "VAL" => "V",
    "PHE" => "F",  "TRP" => "W",  "TYR" => "Y",  "ASN" => "N",
    "CYS" => "C",  "GLN" => "Q",  "MET" => "M",  "SER" => "S",
    "THR" => "T",  "ASP" => "D",  "GLU" => "E",  "ARG" => "R",
    "HIS" => "H",  "LYS" => "K",  "GLY" => "G",  "PRO" => "P", "TER" => "*"
);

USAGE() if ( $opt_H );
my $FRACTION = $opt_r ? $opt_r : 0.4;
my $MAXRATIO = $opt_R ? $opt_R : 1.0;
my $CNT = $opt_n ? $opt_n : 10;
my $AVEFREQ = $opt_F ? $opt_F : 0.15;
my $MINPMEAN = $opt_p ? $opt_p : 5;
my $MINQMEAN = $opt_q ? $opt_q : 25;
my $FILPMEAN = $opt_P ? $opt_P : 0; # will be filtered on the first place
my $FILQMEAN = $opt_Q ? $opt_Q : 0; # will be filtered on the first place
my $FILDEPTH = $opt_D ? $opt_D : 0; # will be filtered on the first place
my $MINFREQ = $opt_f ? $opt_f : 0.02;
my $MINMQ = $opt_M ? $opt_M : 10;
my $MINVD = $opt_V ? $opt_V : 2; # minimum variant depth
my $MAF = $opt_G ? $opt_G : 0.0025;
my $SN = $opt_o ? $opt_o : 1.5;
my @controls = $opt_c ? split(/:/, $opt_c) : ();
my %controls = map { ($_, 1); } @controls;
my %MultiMaf;
$opt_A = defined($opt_A) ? $opt_A : "/ngs/reference_data/genomes/Hsapiens/hg19/variation/dbSNP_multi_mafs_latest.txt";
setupMultiMaf($opt_A) if ( -e $opt_A );


# SBF: Strand Bias Fisher Exact test
my @columns = qw(CDS AA CNT END DP AF BIAS PMEAN PSTD QUAL QSTD SBF CAF VD CLNSIG ODDRATIO HIAF MQ SN ADJAF NM SHIFT3 MSI dbSNPBuildID);
my @ampcols = ();
my @paircols = ();
my @appcols = $opt_C ? split(/:/, $opt_C) : ();
push(@columns, @appcols) if ($opt_C);
my @data;
my %sample;
my %var;
my %CONTROL;
while( <> ) {
    next if ( /^#/ );
    chomp;
    my @a = split(/\t/);
    next unless( $a[6] eq "PASS" );
    $a[7] .= ";";
    my %d;
    while( $a[7] =~ /([^=;]+)=([^=]+?);/g ) {
	$d{ $1 } = $2;
    }
    @ampcols = qw(GDAMP TLAMP NCAMP AMPFLAG) if ( $d{GDAMP} && $d{TLAMP} );
    my @formats = split(/:/, $a[8]);
    my @fdata = split(/:/, $a[9]);
    my @mfdata = $a[10] ? split(/:/, $a[10]) : ();
    for(my $i = 0; $i < @formats; $i++) {
	$d{ $formats[$i] } = $fdata[$i];
	$d{ "M_$formats[$i]" } = $mfdata[$i] if ( $a[10] );
    }

    # Adapt for MuTect or FreeBayes
    unless( $d{ PMEAN } ) {  # Meaning not VarDict
	if ( $d{ AD } ) { # in case it's not defined in freebayes
	    my @ads = split(/,/, $d{ AD });
	    my $ads_sum = 0;
	    $ads_sum += $_ foreach( @ads );
	    if ($ads_sum > 0) {
		    $d{ AF } = sprintf("%.3f", $ads[1]/$ads_sum);
		} else {
		    $d{ AF } = '0';
		}
	    $d{ VD } = $ads[1];
{'fraction_non_alphanumeric': 0.2467374282234209, 'fraction_numerical': 0.01774839046459022, 'mean_word_length': 3.162201303403331, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2696065', 'n_tokens_mistral': 2910, 'n_tokens_neox': 2771, 'n_words': 699}