<commit_before><commit_msg>for filtering megahit contigs on length and/or coverage<commit_after>use strict;
use Getopt::Long; 
use JSON;
use Pod::Usage;

my $help = 0;
my ($in, $out, $coverage, $length);
my $PRINT = 0;




GetOptions(
	'h'	=> \$help,
        'i=s'   => \$in,
        'o=s'   => \$out,
	'help'	=> \$help,
	'input=s'  => \$in,
	'output=s' => \$out,
	'c=f'    => \$coverage,
  	'coverage=f' => \$coverage,
	'l=i'  => \$length,
	'length=i' => \$length,

) or pod2usage(0);


pod2usage(-exitstatus => 0,
          -output => \*STDOUT,
          -verbose => 2,
          -noperldoc => 1,
         ) if $help;


# do a little validation on the parameters


my ($ih, $oh);

if ($in) {
    open $ih, "<", $in or die "Cannot open input file $in: $!";
}
elsif (! (-t STDIN))  {
    $ih = \*STDIN;
}
if ($out) {
    open $oh, ">", $out or die "Cannot open output file $out: $!";
}
else {
    $oh = \*STDOUT;
}


# main logic



while(<$ih>){
  my $cov = 0;
  my $id;
  my $len = 0;

  if (/>/) {
    # look at the id
    $id = $1 if />(\S+)/;
    $id = $1 if />(\S+)_length/;
    die "could not parse id" unless $id;
    # look at coverage
    $cov = $1 if /multi=([\d\.]+)/;
    $cov = $1 if /multi_([\d\.]+)/;
    die "could not parse coverage" unless $cov;

    # look at length
    $len = $1 if /len=(\d+)/;
    $len = $1 if /length_(\d+)/;
    die "could not parse length" unless $len;

    if ($cov >= $coverage && $len >= $length) {
      $PRINT = 1;
    }
    else {
      $PRINT = 0;
    }
  }

  if ($PRINT == 1) {
    print STDERR "problem" unless $_;
    print $oh $_;
    print STDERR "$id\t$len\t$cov\n" if />/;
  }
}



 

=pod

=head1	NAME

filter_megahit_contigs.pl

=head1	SYNOPSIS

filter_megahit_contigs.pl <options>

=head1	DESCRIPTION

The filter_megahit_contigs.pl command ...

=head1	OPTIONS

=over

=item	-h, --help

Basic usage documentation

=item   -i, --input

The input file, default is STDIN

=item   -o, --output

The output file, default is STDOUT

=back

=head1	AUTHORS

<NAME>

=cut

1;

{'fraction_non_alphanumeric': 0.19536260483473114, 'fraction_numerical': 0.0123334977799704, 'mean_word_length': 2.3137254901960786, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 27, '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': '4918104', 'n_tokens_mistral': 890, 'n_tokens_neox': 837, 'n_words': 239}