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

use 5.014;
use strict;
use warnings;
use File::Basename;
use autodie qw(open);
use Getopt::Long;

my $fasta;
my $fastq;
my $outfile;
my $help;

GetOptions(
	   'fa|fasta=s'   => \$fasta,
	   'fq|fastq=s'   => \$fastq,
	   'o|outfile=s'  => \$outfile,
	   'h|help'       => \$help,
	   );

usage() and exit(0) if $help;

if (!$fasta || !$fastq || !$outfile) {
    say "\nERROR: No input was given.";
    usage();
    exit(1);
}

unless (-e $fasta && -e $fastq) {
    say "\nERROR: One or more input files not found.";
    usage();
    exit(1);
}

my $fqct = 0;
my ($fa_idx, $fact) = make_fasta_index($fasta);
my $fq = get_fh($fastq);
open my $out, '>', $outfile;

my ($name, $comm, $seq, $qual);
my @aux = undef;
while (($name, $comm, $seq, $qual) = readfq(\*$fq, \@aux)) {
    if (exists $fa_idx->{$name}) {
	$fqct++;
	my $seq_match;
	#($seq_match = $seq) =~ /($fa_idx->{$name})/;
	($seq_match = $fa_idx->{$name}) =~ /($seq)/;
	my $seqlen = length($seq_match);
	my $qual_region = substr $qual, 0, $seqlen;
	say $out join "\n", "@".$name, $seq_match, q{+}, $qual_region;
    }
}
close $fq;
close $out;

say STDERR "$$fact sequences from $fasta were indexed.";
say STDERR "$fqct sequences were matched in $fastq and written to $fastq.";

#
# methods
#
sub make_fasta_index {
    my ($fasta) = @_;

    my $fact = 0;
    my $fa = get_fh($fasta);
    my %index;
    my ($name, $comm, $seq, $qual);
    my @aux = undef;<commit_msg>bug fix to get the correct qual string<commit_after>#!/usr/bin/env perl

use 5.014;
use strict;
use warnings;
use File::Basename;
use autodie qw(open);
use Getopt::Long;

my $fasta;
my $fastq;
my $outfile;
my $help;

GetOptions(
	   'fa|fasta=s'   => \$fasta,
	   'fq|fastq=s'   => \$fastq,
	   'o|outfile=s'  => \$outfile,
	   'h|help'       => \$help,
	   );

usage() and exit(0) if $help;

if (!$fasta || !$fastq || !$outfile) {
    say "\nERROR: No input was given.";
    usage();
    exit(1);
}

unless (-e $fasta && -e $fastq) {
    say "\nERROR: One or more input files not found.";
    usage();
    exit(1);
}

my $fqct = 0;
my ($fa_idx, $fact) = make_fasta_index($fasta);
my $fq = get_fh($fastq);
open my $out, '>', $outfile;

my ($name, $comm, $seq, $qual);
my @aux = undef;
while (($name, $comm, $seq, $qual) = readfq(\*$fq, \@aux)) {
    if (exists $fa_idx->{$name}) {
	$fqct++;
	my $seq_match;
	if ($seq =~ /($fa_idx->{$name})/) {
	    my ($seq_match, $start, $end) = ($1, $-[0], $+[0]);
	    my $qual_region = substr $qual, $start, length($seq_match);
	    say $out join "\n", "@".$name, $seq_match, q{+}, $qual_region;
	}
    }
}
close $fq;
close $out;

say STDERR "$$fact sequences from $fasta were indexed.";
say STDERR "$fqct sequences were matched in $fastq and written to $fastq.";

#
# methods
#
sub make_fasta_index {
    my ($fasta) = @_;

    my $fact = 0;
    my $fa = get_fh($fasta);
    my %index;
    my ($name, $comm, $seq, $qual);
    my @aux = undef;
{'fraction_non_alphanumeric': 0.21712538226299694, 'fraction_numerical': 0.007475365273530411, 'mean_word_length': 3.2666666666666666, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 18, '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': '3089018', 'n_tokens_mistral': 1292, 'n_tokens_neox': 1246, 'n_words': 343}