2

I am using Perl for a script that takes in input as two short strings of DNA. As an output, I concatenate the two strings strings then print the second string lined up over its copy at the end of the concatenated string. For example: if input string are AAAA and TTTTT then print:

AAAAATTTTT
     TTTTT

I know there are other ways to do this but I am curious to know why my use of tr/// isn't working.

The code for the program is:

use strict;
use warnings;
print "enter a DNA sequence \n";
$DNA1=<>; #<> shorthand for STDIN
$DNA1=~ s/\r?\n?$//;
print $DNA1 "\n\n";
print "enter second DNA sequence \n";
$DNA2=<>;
$DNA2=~ s/\r?\n?$//;
print $DNA2 "\n\n";
$DNA= join("",($DNA1,$DNA2));
print "Both DNA sequences are \"$DNA\" \n\n";
$DNA3=$DNA1;
$DNA3=~ tr/ATCGatcg//;
print $DNA3 "\n\n";
$DNA4= join("",($DNA3,$DNA2));
print $DNA4 "\n\n";
exit;
brian d foy
  • 129,424
  • 31
  • 207
  • 592
shubster
  • 825
  • 4
  • 13
  • 39
  • Is this just poorly formatted or is Perl's syntax format actually look like this? – Bobby Cannon Apr 28 '09 at 15:25
  • isn't "$DNA1=~ s/\r?\n?$//;" the same as "chomp $DNA1;"? – Nathan Fellman Apr 28 '09 at 15:28
  • yes "$DNA1=~ s/\r?\n?$//;" is the same as "chomp $DNA. but perl 4 and lower versions dont support chomp so its an alternative. and my machine has perl 4 installed. – shubster Apr 28 '09 at 15:31
  • @Bobby - beginners' Perl syntax looks like this ;-) – Alnitak Apr 28 '09 at 15:36
  • 3
    @Alnitak & @Bobby - and, assuming perl 4 on the target machine, the code can't look much better (those stricts and warnings are not going to do much either). Aside: it's truly scary to find a machine running perl 4. Perl 5 was released fifteen years ago! – Nic Gibson Apr 28 '09 at 16:00
  • "use warning"? That's not right. Wasn't there a warn... Oh. – AmbroseChapel Apr 29 '09 at 01:01
  • 1
    $DNA1=~ s/\r?\n?$//; is only the same thing as chomp if the input record separator is \r\n, which it might not be. – brian d foy Apr 29 '09 at 15:02

4 Answers4

1

You need to put a space in the second half of the tr command.

Alternatively, it seems that what you're trying to do is create a variable containing as many spaces as there were characters in the first string:

my $spaces = ' ' x length($DNA1);
Alnitak
  • 334,560
  • 70
  • 407
  • 495
1

Your tr changes any of ACTGatcg and removes them. I think you want

$DNA3 =~ tr/atcgATCG/ /;
Paul Tomblin
  • 179,021
  • 58
  • 319
  • 408
1

It might just be a simple syntax error. Try:

$DNA3 =~ tr/ATCGatcg/ /;

where the second slash separates your two translation entities, and you have a space character between the second and third slashes.

Good luck!

Edit: my mistake - misunderstood what you wanted to do. Answer adjusted accordingly :)

Mike
  • 4,542
  • 1
  • 26
  • 29
0

Is this the program that you want?

#!perl

my $s1 = 'AAAAAAAAA';
my $s2 = 'TCGAGCTA';

print 
    $s1, $s2, "\n", 
    ' ' x length( $s1 ), $s2, "\n";
brian d foy
  • 129,424
  • 31
  • 207
  • 592