This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How Can I Locate The Single Letter Change Between Two Strings?

The following is a program to find out where changes are present in mutiple sequences, but this tells only the locations in number. I need to find out the particular change, for example:

source string: ATGCTTG string 1: ACGCGTG

The following program gives the output as "2,5", but I need the output as [T/C],[T/G], i.e. T has been replaced by C in the second position and also T has been replaced by G in the fifth position.

use strict;
use warnings;

print "Enter the source string";
my $str_source=<>;
print "Enter the 1st strng:";
my $str1=<>;
print "Enter the 2nd string:";
my $str2=<>;
print "Enter the 3rd string";
my $str3=<>;
print "Enyer the 4th string";
my $str4=<>;

sub diff_index
{
    my($a,$b)=@_;
    my $cmp=$a^$b;
    my @cmp;
    while($cmp=~/[^\0]/g)
    {
        push @cmp, pos($cmp)-1;
    }
    return @cmp;
}

for my $str ( $str_source, $str1, $str2, $str3,$str4 )
{
    print "$str";
    my @ret = diff_index $str_source, $str;
    if( @ret )
    {
        print"@ret" ;
    }
    else
    {
        print "match";
    }
}
perl

why don't you just align it, instead of developing a home-made solution?

1 answer

Brent Pedersen's nwalign module will allow you to align sequences, then all you need is to iterate in parallel over the results to generate the the output you seek:

import nwalign
from itertools import count

seq1, seq2 = nwalign.global_align("ATGCTTG", "ACGCGTG")

for i, a, b in zip(count(1), seq1, seq2):        
    if a != b:
        print "%s -> %s/%s" % (i, a, b)

will produce the output:

2 -> T/C
5 -> T/G

Log in to answer this question.