#!/usr/bin/env perl srand; my $reiterate = 100000; my ($zerocount, $correctcount, $wrongcount); # Load in the list of snps and hla alleles from a file my (%snphla, %guesslist, @guessfromhere, %count, %snphapcount, %guesscount, $snp, $hla, @hlas); while (<>){ last if ($_ eq "\n"); ($snp, $hla) = split; chomp ($snp); chomp ($hla); # print $snp, " ", $hla, "\n"; $snphla{$snp}->{$hla} += 1 ; push (@guessfromhere, $snp); my $current = $guesslist{$snp}; $guesslist{$snp} = "$current $hla"; $countEntries ++; $snphapcount{$snp} += 1; } print $countEntries . "\n"; for ($i = 1; $i <= $reiterate ; $i++){ # Make the guess and return the array of values associated with it my $random = int(rand(scalar(@guessfromhere))); my $guess = $guessfromhere[$random]; my @parts = split(" ", $guesslist{$guess}); my $number = int(rand(scalar(@parts))); my $answer = @parts[$number]; # print "guess " . $guess . " answer " . $answer . "\n"; # Count the number of times a guess is guessed $guesscount{$guess} += 1; # Drop it out! Remove the guess from the count in the snphla hash $snphla{$guess}->{$answer} -= 1 ; # Move only the guess' counts into a sorted array my @counts = values %{$snphla{$guess}}; my @sorted_counts = sort { $b <=> $a } @counts; my $highest_count = shift @sorted_counts; if ($highest_count != 0){ my @matching_hla_types = (); foreach my $hla_type (keys %{$snphla{$guess}}) { if ($snphla{$guess}->{$hla_type} == $highest_count) { push(@matching_hla_types, $hla_type); } } my $random_index = int(rand(scalar(@matching_hla_types))); # print "guess " . $guess . " count " . $highest_count . " hla " . $matching_hla_types[$random_index] . "\n"; if ($answer eq $matching_hla_types[$random_index]){ $correctcount++; # print "$correctcount yeah!\n"; } else { $wrongcount++; # print "nope!\n"; } } else { $zerocount++; # print "zerocount!\n"; } # Add it back for the next round! $snphla{$guess}->{$answer} += 1 ; } print "The number of wrong guesses was: " . $wrongcount . "\n"; print "The number of correct guesses was: " . $correctcount . "\n"; print "The percentage right was: " . $correctcount / ($reiterate - $zerocount) . "\n"; print "The number of singletons was: ". $zerocount . "\n"; foreach $key (sort keys %guesscount){ $guessvalue = ($guesscount{$key})/$reiterate; $hapvalue = ($snphapcount{$key})/$countEntries; print "Haplotype " . $key . " guessed " . $guessvalue . " present " . $hapvalue . "\n"; } exit;