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

use strict;
use warnings;
use Bio::Perl;
use Getopt::Long;

sub logmsg{print STDERR "@_\n";}
exit main();

sub main{
  my $settings={};
  GetOptions($settings,qw(ref|reference=s only-positional help)) or die $!;
  die usage() if($$settings{help} || !$$settings{ref});

  my %seq;
  my $in=Bio::SeqIO->new(-file=>$$settings{ref});
  while(my $seq=$in->next_seq){
    $seq{$seq->id}=$seq->seq;
  }
  
  while(<>){
    chomp;
    my @F=split /\t/;
    $F[1]||='.';

    if(/^#/){
      
    } else {
      my($contig,$pos)=findPosition($F[2],\%seq,$settings);
      if($contig && $pos){
        $F[0]=$contig;
        $F[1]=$pos;
      } elsif($$settings{'only-positional'}){
        next;
      }
    }

    print join("\t",@F)."\n";
  }
}

sub findPosition{
  my($kmerRegex,$seqHash,$settings)=@_;

  my $dotIndex=index($kmerRegex,'.');
  my @ID=keys(%$seqHash);

  for my $id(@ID){
    my $seq=$$seqHash{$id};
    if($seq=~/($kmerRegex)/i){
      my $pos=length($`)+$dotIndex;
      return ($id,$pos);
    }
  }

  logmsg "ERROR: I could not find kmer $kmerRegex in $$settings{ref}";
  return (undef,undef);
}

sub usage{
  "Usage: $0 -ref reference.fasta < file.vcf > fixed.vcf
  --only-positional  Removes any position whose kmer is not found
                     in the reference fasta
  "
}<commit_msg>revcom, more usage<commit_after>#!/usr/bin/env perl

use strict;
use warnings;
use Bio::Perl;
use Getopt::Long;

sub logmsg{print STDERR "@_\n";}
exit main();

sub main{
  my $settings={};
  GetOptions($settings,qw(ref|reference=s only-positional help)) or die $!;
  die usage() if($$settings{help} || !$$settings{ref});

  my %seq;
  my $in=Bio::SeqIO->new(-file=>$$settings{ref});
  while(my $seq=$in->next_seq){
    $seq{$seq->id}=$seq->seq;
  }
  
  while(<>){
    chomp;
    my @F=split /\t/;
    $F[1]||='.';

    if(/^#/){
      
    } else {
      my($contig,$pos)=findPosition($F[2],\%seq,$settings);
      if($contig && $pos){
        $F[0]=$contig;
        $F[1]=$pos;
      } elsif($$settings{'only-positional'}){
        next;
      }
    }

    print join("\t",@F)."\n";
  }
}

sub findPosition{
  my($kmerRegex,$seqHash,$settings)=@_;

  my $dotIndex=index($kmerRegex,'.');
  my $revcom=revcom($kmerRegex)->seq;
  my @ID=keys(%$seqHash);

  for my $id(@ID){
    my $seq=$$seqHash{$id};
    if($seq=~/($kmerRegex|$revcom)/i){
      my $pos=length($`)+$dotIndex;
      return ($id,$pos);
    }
  }

  logmsg "ERROR: I could not find kmer $kmerRegex in $$settings{ref}";
  return (undef,undef);
}

#sub revcom{
#  my($dna,$settings)=@_;
#  my $rev=reverse($dna);
#  $rev=~tr/ACGTacgt/TGCAtgca/;
#  return $rev
#}

sub usage{
  "Usage: $0 -ref reference.fasta < file.vcf > fixed.vcf
  --only-positional  Removes any position whose kmer is not found
                     in the reference fasta. However, this is 
                     just a sanity check. All kmers should be
                     found in the reference fasta.
  "
}
{'fraction_non_alphanumeric': 0.21019108280254778, 'fraction_numerical': 0.003352329869259135, 'mean_word_length': 2.9108781127129753, 'pattern_counts': {'":': 0, '<': 7, '<?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': '6666089', 'n_tokens_mistral': 1180, 'n_tokens_neox': 1153, 'n_words': 234}