#!perl use strict; use 5.010; my $STARFile; # File handle to reference file my @Stars; $Stars[0] = "CYP2C19_10"; # Mock array of values to cross reference $Stars[1] = "CYP2C19_12"; # if(@Stars==0){return;} # If no Star Alleles were specified then no need to do this so return to the main body if(! open $STARFile,"<","test.csv"){die "Reference file could not be found or could not be opened.";} # Open the Star reference file to prepare to convert information and store the file handle to $STARFile. print "Converting specified Star Designations to SNPs..."; # The conversion table is opened so convert the Star name to rs numbers and then store the rs numbers to the @SNPs array and the corresponding Star name to the @Stars array at the same index. my @SNPs; my @StarsCon; my $RefIndex; # Holds the line in the reference table file my $StarIndex; # Holds the index of the @Stars Array that is being checked my $tmpSNPIndex; # Holds the index in the @SNPs array that we are comparing my $tmpStar; # Holds the Star Allele name my $tmpRS; # Holds the SNP's rs number my @tmpConv; # Holds the split Star and rs numbers say "\n--------- Converting Star Allele references to rs numbers ---------"; while (<$STARFile>){ # Input a line from the database and as long as we haven't reached the end of the file chomp; # Remove the trailing newline say "Current input line is @_"; @tmpConv = split ",",$_; # Split the CSV line from the reference table such that @tmpConv[0] = Star name and @tmpConv[1] = rs number $tmpStar = $tmpConv [1]; $tmpRS = $tmpConv[0]; say "Index 0 is $tmpConv[0]"; # Displays correctly say "Index 0 is $tmpConv[0] is stored as $tmpRS"; # Displays correctly say "Index 1 is $tmpConv[1]"; # Displays correctly say "Index 1 is $tmpConv[1] is stored as $tmpStar"; # Displays INcorrectly for($StarIndex=0;$StarIndex<@Stars;$StarIndex++){ say "Comparing $tmpStar and $Stars[$StarIndex]"; if($tmpStar eq $Stars[$StarIndex]){ # If the current line of the database file contains the Star Allele rs number then $tmpSNPIndex = @SNPs; # Get the number of entries in the @SNPs array. say "1. $tmpRS was converted from $tmpStar"; say "2. $tmpStar was converted to $tmpRS"; say "3. $tmpStar was converted to $tmpRS"; say "4. $tmpRS was converted from $tmpStar"; push @StarsCon, $tmpStar; # Add the Star allele name to the @StarsCon array push @SNPs, $tmpRS; # Add the new rs number to the @SNPs array if(@Stars>0){ # If we have more than one SNP then splice @Stars,$StarIndex,1; # and @Stars array }else{ # Otherwise Pop off the last one pop @Stars; # } last; # Exit the for loop } } if(! @Stars>0){last;} # If that was the last entry then stop searching } say "-------------- Done converting Star Allele references -------------"; if(@Stars>0){ # If any SNPs have not been found then say "\n"."Conversions not completed: @Stars."; # Inform the user which ones were not found }else{ # Otherwise say "\n"."All conversions successful."; # Inform the user that all were found } close $STARFile; # Close the reference file print "Done!\n";