How Can I Locate The Single Letter Change Between Two Strings?
1
1
Entering edit mode
13.3 years ago
Payal ▴ 10

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 • 2.1k views
ADD COMMENT
0
Entering edit mode

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

ADD REPLY
6
Entering edit mode
13.3 years ago

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
ADD COMMENT

Login before adding your answer.

Traffic: 1313 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6