School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,145 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,735 people online right now. Registration is fast and FREE... Join Now!




String comparisons

 

String comparisons, Need some help with this code.

dstorey

29 Sep, 2009 - 01:28 PM
Post #1

New D.I.C Head
*

Joined: 14 Sep, 2009
Posts: 7

Ok so the idea here is that i'm searching a string ( DNA ) for restriction cuts. ( I'm actually searching two strings and then comparing them and putting them in three classes : both sequences lack the search sequence , one sequence has a cut site , both sequences have a cut site.

For some reason the string isn't matching when i know that a cut site exists any suggestions?
BTW the array resequence is an array of the sequences I'm looking for.
CODE

@A = ();
@B = ();
foreach $reseq (@reseq){
    if ($seq1 =~ /$reseq/)
        {push (@A,1);}
    else    
        {push (@A,0);}
}
foreach $reseq (@reseq){
    if ($seq2 =~ /$reseq/)
        {push (@B,1);}
    else
        {push (@B,0);}
}

## Truncates the array with an x for later use ##
push (@A,"x");
push (@B,"x");

## compare the two arrays ##
$n=0;
@C = ();

## checks both arrays with a conditional statement 0=cuts in neither sequence , 1=polymorphic cuts (+/-), 2=cuts both seqences ##
while ($A[$n] ne "x"){
    if ($A[$n]+$B[$n] == 0){
    @C[$n] = @rename[$n]." cuts in neither sequence.\n";}
    elsif ($A[$n]+$B[$n] == 1){
    @C[$n] = @rename[$n]." polymorphic cutter.\n";}
    elsif ($A[$n]+$B[$n] == 2){
    @C[$n] = @rename[$n]." cuts both sequences.\n";}  
    $n=$n+1;}


User is offlineProfile CardPM
+Quote Post


dsherohman

RE: String Comparisons

30 Sep, 2009 - 05:44 AM
Post #2

D.I.C Head
**

Joined: 29 Mar, 2009
Posts: 204



Thanked: 36 times
My Contributions
QUOTE(dstorey @ 29 Sep, 2009 - 09:28 PM) *

Ok so the idea here is that i'm searching a string ( DNA ) for restriction cuts. ( I'm actually searching two strings and then comparing them and putting them in three classes : both sequences lack the search sequence , one sequence has a cut site , both sequences have a cut site.

For some reason the string isn't matching when i know that a cut site exists any suggestions?


Can you reduce the problem to a minimal program which both runs and demonstrates the problem you're having? Doing so, along with telling us what output you want to result, will make it much easier for us to identify the cause of the problem. While I could easily turn the fragment of code you posted into a complete runnable program, I would still only be guessing at what input it should be given or what output it should produce, so I'd be likely to end up "fixing" something that wasn't broken in the first place.

I can offer some stylistic revisions from what you've provided, though. biggrin.gif My assumptions here are that:
  • You have two input strings, $seq1 and $seq2.
  • You have a list of target sequences, @reseq.
  • Each entry in @reseq has a corresponding entry in @rename.
  • The desired output is a list of the entries in @rename along with an indication for each of whether the corresponding @reseq entry appears in neither, one, or both of $seq1 and $seq2.

Given that those assumptions are correct, the following code should do what you want:
CODE

my @rename_tmp = @rename;
my %name_map;
my %cuts;
for my $reseq (@reseq) {
  $name_map{$reseq} = shift @rename_tmp;
  # The following line is solely to protect against
  # duplicate sequences in @reseq.  If all @reseq
  # entries will always be unique, you don't need it.
  next if exists $cuts{$reseq};

  $cuts{$reseq}++ if $seq1 =~ /$reseq/;
  $cuts{$reseq}++ if $seq2 =~ /$reseq/;
}

for my $reseq (@reseq) {
  print $name_map{$reseq};

  if (!$cuts{$reseq}) {
    print " cuts in neither sequence.\n";
  } elsif ($cuts{$reseq} == 1) {
    print " polymorphic cutter.\n";
  } else {
    print " cuts both sequences.\n";
  }
}


Also note that I only used separate processing and output loops because that was what you'd done in your original code. If you don't need to do anything else between those stages, this can be reduced to a single loop:
CODE

my @rename_tmp = @rename;
for my $reseq (@reseq) {
  my $cuts;
  $cuts++ if $seq1 =~ /$reseq/;
  $cuts++ if $seq2 =~ /$reseq/;

  print shift @rename_tmp;

  if (!$cuts) {
    print " cuts in neither sequence.\n";
  } elsif ($cuts == 1) {
    print " polymorphic cutter.\n";
  } else {
    print " cuts both sequences.\n";
  }
}

User is offlineProfile CardPM
+Quote Post

dstorey

RE: String Comparisons

1 Oct, 2009 - 06:39 AM
Post #3

New D.I.C Head
*

Joined: 14 Sep, 2009
Posts: 7

Wow, thanks - very helpful and your assessment of my inputs and what I'm attempting were spot on. I'll definitely sit down with that tonight and chew on it. One questions the "%" symbol I've only seen used as a mod operation, what does it mean in the context you placed it? ( I poked around on the interwebs but couldn't seem to find anything explaining that context)
User is offlineProfile CardPM
+Quote Post

dsherohman

RE: String Comparisons

2 Oct, 2009 - 06:02 AM
Post #4

D.I.C Head
**

Joined: 29 Mar, 2009
Posts: 204



Thanked: 36 times
My Contributions
QUOTE(dstorey @ 1 Oct, 2009 - 02:39 PM) *

Wow, thanks - very helpful and your assessment of my inputs and what I'm attempting were spot on. I'll definitely sit down with that tonight and chew on it. One questions the "%" symbol I've only seen used as a mod operation, what does it mean in the context you placed it? ( I poked around on the interwebs but couldn't seem to find anything explaining that context)


Perl uses the % sigil as a prefix on variable names to indicate that the variable is a hash, just like the $ sigil indicates a scalar and @ indicates an array. http://www.tizag.com/perlT/perlhashes.php looks like a decent introduction to using hashes in Perl, although I haven't examined it too closely. (If you're familiar with "associative arrays" from other languages, Perl hashes are roughly the same thing, although people who know more Lisp than I do have said there are technically some differences.)

If you want some help understanding any of the details of how my version of your code works, feel free to ask and I'd be happy to explain them.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 03:48PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month