<commit_before>#!/usr/bin/perl -w

# Author: lh3

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

my $version = '0.3.1';
&usage if (@ARGV < 1);

my $command = shift(@ARGV);
my %func = (showALEN=>\&showALEN, pileup2fq=>\&pileup2fq, varFilter=>\&varFilter);

die("Unknown command \"$command\".\n") if (!defined($func{$command}));
&{$func{$command}};
exit(0);

#
# showALEN
#

sub showALEN {
  die(qq/Usage: samtools.pl showALEN <in.sam>\n/) if (@ARGV == 0 && -t STDIN);
  while (<>) {
	my @t = split;
	my $l = 0;
	$_ = $t[5];
	s/(\d+)[SMI]/$l+=$1/eg;
	print join("\t", @t[0..5]), "\t$l\t", join("\t", @t[6..$#t]), "\n";
  }
}

#
# varFilter
#

sub varFilter {
  my %opts = (d=>3, D=>100, l=>30, Q=>25, G=>25, s=>100, w=>10, W=>10, N=>2, p=>undef);
  getopts('pd:D:l:Q:w:W:N:G:', \%opts);
  die(qq/
Usage:   samtools.pl varFilter [options] <in.cns-pileup>

Options: -Q INT    minimum RMS mapping quality [$opts{Q}]
         -d INT    minimum read depth [$opts{d}]
         -D INT    maximum read depth [$opts{D}]

         -G INT    min indel score for nearby SNP filtering [$opts{G}]
         -w INT    SNP within INT bp around a gap to be filtered [$opts{w}]

         -W INT    window size for filtering dense SNPs [$opts{W}]
         -N INT    max number of SNPs in a window [$opts{W}]

         -l INT    window size for filtering adjacent gaps [$opts{l}]

         -p        print filtered variants
/) if (@ARGV == 0 && -t STDIN);

  # calculate the window size
  my ($ol, $ow, $oW) = ($opts{l}, $opts{w}, $opts{W});
  my $max_dist = $ol > $ow? $ol : $ow;
  $max_dist = $oW if ($max_dist < $oW);
  # the core loop
  my @staging; # (indel_filtering_score, flt_tag)
  while (<>) {
	my @t = split;
	next if ($t[2] eq $t[3] || $t[3] eq '*/*'); # skip non-var sites
	# clear the out-of-range elements
	while (@staging) {
	  if ($staging[0][2] ne $t[0] || $staging[0][3] + $max_dist < $t[1]) {
		varFilter_aux(shift @staging, $opts{p});
	  } else {
		last;
	  }
	}
	my ($flt, $score) = (0, -1);
	# first a simple filter
	if ($t[6] < $opts{Q}) {
	  $flt = 1;
	} elsif ($t[7] < $opts{d}) {
	  $flt = 2;
	} elsif ($t[7] > $opts{D}) {
	  $flt = 3;
	}<commit_msg>a little bit code cleanup<commit_after>#!/usr/bin/perl -w

# Author: lh3

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

my $version = '0.3.1';
&usage if (@ARGV < 1);

my $command = shift(@ARGV);
my %func = (showALEN=>\&showALEN, pileup2fq=>\&pileup2fq, varFilter=>\&varFilter);

die("Unknown command \"$command\".\n") if (!defined($func{$command}));
&{$func{$command}};
exit(0);

#
# showALEN
#

sub showALEN {
  die(qq/Usage: samtools.pl showALEN <in.sam>\n/) if (@ARGV == 0 && -t STDIN);
  while (<>) {
	my @t = split;
	my $l = 0;
	$_ = $t[5];
	s/(\d+)[SMI]/$l+=$1/eg;
	print join("\t", @t[0..5]), "\t$l\t", join("\t", @t[6..$#t]), "\n";
  }
}

#
# varFilter
#

sub varFilter {
  my %opts = (d=>3, D=>100, l=>30, Q=>25, G=>25, s=>100, w=>10, W=>10, N=>2, p=>undef);
  getopts('pd:D:l:Q:w:W:N:G:', \%opts);
  die(qq/
Usage:   samtools.pl varFilter [options] <in.cns-pileup>

Options: -Q INT    minimum RMS mapping quality [$opts{Q}]
         -d INT    minimum read depth [$opts{d}]
         -D INT    maximum read depth [$opts{D}]

         -G INT    min indel score for nearby SNP filtering [$opts{G}]
         -w INT    SNP within INT bp around a gap to be filtered [$opts{w}]

         -W INT    window size for filtering dense SNPs [$opts{W}]
         -N INT    max number of SNPs in a window [$opts{W}]

         -l INT    window size for filtering adjacent gaps [$opts{l}]

         -p        print filtered variants
/) if (@ARGV == 0 && -t STDIN);

  # calculate the window size
  my ($ol, $ow, $oW) = ($opts{l}, $opts{w}, $opts{W});
  my $max_dist = $ol > $ow? $ol : $ow;
  $max_dist = $oW if ($max_dist < $oW);
  # the core loop
  my @staging; # (indel_filtering_score, flt_tag)
  while (<>) {
	my @t = split;
	next if ($t[2] eq $t[3] || $t[3] eq '*/*'); # skip non-var sites
	# clear the out-of-range elements
	while (@staging) {
	  last if ($staging[0][2] eq $t[0] && $staging[0][3] + $max_dist >= $t[1]);
	  varFilter_aux(shift(@staging), $opts{p}); # calling a function is a bit slower, not much
	}
	my ($flt, $score) = (0, -1);
	# first a simple filter
	if ($t[6] < $opts{Q}) {
	  $flt = 1;
	} elsif ($t[7] < $opts{d}) {
	  $flt = 2;
	} elsif ($t[7] > $opts{D}) {
	  $flt = 3;
	}
{'fraction_non_alphanumeric': 0.22081510476629057, 'fraction_numerical': 0.023486069537186276, 'mean_word_length': 2.941923774954628, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 42, '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': '1151798', 'n_tokens_mistral': 1923, 'n_tokens_neox': 1818, 'n_words': 507}