open (FILE1, "Mock3_SNP.txt"); #### while () { chomp; my $input_orig = $_; #### while (defined (my $input_line = <$input>)) { chomp $input_line; #### my @line = split /\s+/, $_; #### my @line = split ' ', $input_line; #### my $chr = "chr$line[1]"; #### $input{$name}[0] = $name; $input{$name}[1] = $chr; $input{$name}[2] = $pos; #### $input{$name} = [ $name, $chr, $pos ]; #### push @{$input[$chr]} => [ $name, $pos ]; #### push @{$input[$chr]} => { name => $name, pos => $pos }; #### open (OUT, "> test.txt"); ### Change if file name changes #### ### At the top: my $output_file = 'test.txt'; ### Change if the file name changes ### the open is then: open my $out, '>', $output_file or die "Can't open $output_file: $!"; #### foreach my $name(keys %input) { if ($input{$name}[1] eq $chr) { if ($input{$name}[2] > $pos-1 && $input{$name}[2] < $pos+36) { #### $chr =~ s/^chr//; # remove chr if it's there ### or, if chr must be there: # substr $chr, 0, 3, ''; # removes first three characters if (defined $input[$chr]) { for my $i (@{$input[$chr]}) { if ($pos <= $i->{pos} && $i->{pos} <= $pos+35) { #### if ($input{$name}[2] == $pos) { my $fSNP = substr($seq, 0, 1); print OUT "$fSNP\n"; } if ($input{$name}[2] == $pos+35) { my $fSNP = substr($seq, 35,1); print OUT "$fSNP\n"; } if ($input{$name}[2] != $pos+35 && $input{$name}[2] != $pos) { my $SNP = substr($seq, 0, $input{$name}[2]-($pos-1)); my $fSNP = substr($SNP, $input{$name}[2]-$pos); print OUT "$fSNP\n"; } #### my $fSNP = substr($seq, $i->{pos} - $pos, 1); print $out "$fSNP\n";