Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

tr function with a variable, bioinformatics

Status
Not open for further replies.

torstens

Technical User
Oct 9, 2006
26
0
0
US
I am trying to count the occurences of a given base in a sequence. The problem I'm running into is with this code:

$count = ($Sequence =~ tr/$base//);

where the $base variable is the given base to count. The code works perfectly if I manually replace $base with a given base, but otherwise does not. I really appreciate any help with this, thanks in advance
 
I think more code is needed to see what is going on.

If you are just trying to see how many times something occurs how come you aren't using grep? Or are you really needing to do a replacement?
 
I believe I'm right in saying that the tr function does not allow the use of variables in it. Thus,
Code:
tr/ABC/abc/
is ok, but
Code:
tr/$variable/abc/
is not allowed.
 
tonykent is correct.

Quoted from
Because the transliteration table is built at compile time, neither the SEARCHLIST nor the REPLACEMENTLIST are subjected to double quote interpolation. That means that if you want to use variables, you must use an eval():

eval "tr/$oldlist/$newlist/";
die $@ if $@;

eval "tr/$oldlist/$newlist/, 1" or die $@;
 
Does s/// have that same issue? I have only ever used s/// and never tr///.
 
travs69 said:
Does s/// have that same issue? I have only ever used s/// and never tr///.

No, regular expressions (s///) do interpolate.

This is something specific to tr, which I also have little familiarity with, but am aware of this one issue.

- M
 
I should clarify. regular expressions do interpolate unless you specifically make them not by using the single quote delimiter. s'''

- M
 
alternatve suggestion:

Code:
my $string = 'AATTGGCCTGACTGACTGTGTCAAA';
$n++ while $string =~ m/A/g;
print $n;

not sure it it's anymore or less efficient than:

Code:
my $string = 'AATTGGCCTGACTGACTGTGTCAAA';
$n = $string =~ s/A/A/g;
print $n;

or using t/// as an expression to eval.





------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
What you could maybe also do, though it isn't necessarily very elegant, is strip out all that is not the base your interested in, and see what's left.

So
Code:
my $string = 'AATTGGCCTGACTGACTGTGTCAAA';
$string =~ s/[^A]//g;
print "I saw " . length($string) . " A's\n";

I mean if you have to do this for ten thousand different bases, it would be somewhat tiresome, maybe. But there are only 4 of them, right..?!?
 
Expanding on Kevin's solution to the proposed problem, find all the base counts at the same time:

Code:
my $string = 'AATTGGCCTGACTGACTGTGTCAAA';

my %basecounts = ();
$basecounts{$1}++ while $string =~ m/(\w)/g;

print join ', ', map {"$_ => $basecounts{$_}"} sort keys %basecounts;

# Outputs: A => 7, C => 5, G => 6, T => 7
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top