#!/bin/env perl
use strict;
use warnings;

my @ACGT = (qw(A C G T));

my @quals;
open (QUAL, '<', $ARGV[0]) or die "Can not open $ARGV[0]\n";

open (SEQ, '<', $ARGV[1]) or die "Can not open $ARGV[1]\n";

while (my $line = <SEQ>) {
    my $seq   = <SEQ>;
    my $delim = <SEQ>;
    my $junk  = <SEQ>;

    $junk = <QUAL>;
    $junk = <QUAL>;
    $junk = <QUAL>;
    my $qual = <QUAL>;

    chomp $seq;
    chomp $qual;

    # Put in some SNPs into the reads themselves
    my $doit = rand(1);
    if ($doit > 0.95) {
        my @seq_parts = split //, $seq;

        my ($place, $how) = do_rand($#seq_parts);
        $seq_parts[$place] = $how;

        if ($doit > 0.98) {
            ($place, $how) = do_rand($#seq_parts);
            $seq_parts[$place] = $how;
        }

        if ($doit > 0.99) {
            ($place, $how) = do_rand($#seq_parts);
            $seq_parts[$place] = $how;
        }
        if ($doit > 0.998) {
            ($place, $how) = do_rand($#seq_parts);
            $seq_parts[$place] = $how;
        }

        $seq = join '', @seq_parts;
    }


    my $qlen = length($qual);
    my $slen = length($seq);
    my $len  = $qlen < $slen ? $qlen : $slen;
    
    my $seq2  = substr($seq,0,$len);
    my $qual2 = substr($qual,0,$len);

    print $line . $seq2 . "\n" . $delim . $qual2 . "\n";
}

sub do_rand {
    my $place = int(rand($_[0]));
    my $how   = $ACGT[int(rand(4))]; 
    return($place, $how);
}


#EOF
