----- Original Message -----
From: "Kathryn Bushley" <[EMAIL PROTECTED]>
Newsgroups: perl.beginners
To: <beginners@perl.org>
Sent: Tuesday, October 10, 2006 3:17 PM
Subject: saving surrounding text in substitution
Hi,
I've written the following script to substitute names
for codes into a phylogenetic tree (newick format-file
attached) using a global hash with codes as key and
names as values...it seems to be working up to the
second to last line where I make the substitution...I
am able to make the substitution but cannot seem to
save the surrounding text...I've tried $1 and $2 but
in this case no substitution at all happens...any
suggestions or is there an easier to do this?
#!/usr/bin/perl -w
use warnings;
use Bio::Seq;
use Bio::SeqIO;
#initialize id and fasta files
my $codefile = $ARGV[0];
my $treefile = $ARGV[1];
#open alignment list and create a global hash of tree
codes with species name as
#open gi id file and assign file handle
open(ID,$codefile)|| die "can't open id file: $!\n";
#initialize hash
my(@ID);
my %id_global;
my $id;
while (<ID>){
#split on spaces
my @ID = split(/\s+/);
#print "ID->".$ID[0]."<-\n"."Code->".$ID[1]."<-\n";
#get rid of carriage returns
chomp;
#define id as the code number
my $id = $ID[1];
#print "ID->".$id."<-\n";
#create a global hash with code number as key and
species name as value
$id_global{$id}= $ID[0];
#print $id."VALUE->".$id_global{$id}."<-\n";
}
#test that keys loaded to global hash with correct
value
#foreach my $key (keys %id_global){
#print
"KEY->".$key."<-->VALUE->".$id_global{$key}."<-\n";
#}
#open treefile and while through it, substituting
species name (value) when key(code) found in file.
open (TREE,$treefile)|| die "can't open tree file:
$\n";
#my @TREE;
my $line;
while (<TREE>){
foreach my $code (keys %id_global){
#print
"CODE->".$code."<-VALUE->".$id_global{$code}."\n";
#initialize line as $_
$line = $_;
#if ($line =~ m/(.*).$code.(.*)/) {print "MATCH"."
".$code."VALUE->".$id_global{$code}."<-\n";}
#else {print "NO MATCH"." ".$code."<-\n";}
$line =~
s/(.*).$code.(.*)/$1.$id_global{$code}.$2/;
Hello Kathryn
As Tom noted in his reply, its probably not necessary to capture $1 and $2.
Just s/$code/$id_global{$code}/ should do it.
I am assuming that the periods you had in there were for concatenation.
If so, they are not necessary. (On the other hand, if you wanted the periods
then you would need to backslash them, s/\.$code\./.$id_global{$code}./ ).
(I've re-writteen your entire code below.You how much simpler it could be.)
print $line;
}
}
kathryn
#!/usr/bin/perl
use strict;
use warnings;
use Bio::Seq;
use Bio::SeqIO;
#initialize id and fasta files
my $codefile = $ARGV[0];
my $treefile = $ARGV[1];
#open alignment list and create a global hash of tree codes with species
name as
#open gi id file and assign file handle
open(ID,$codefile)|| die "can't open id file: $!\n";
my %id_global;
while (<ID>){
my ($id, $code, undef) = split ' ', $_, 3;
$id_global{$code} = $id;
}
#open treefile and while through it, substituting species name (value) when
key(code) found in file.
open (TREE,$treefile)|| die "can't open tree file: $\n";
my $line;
while (<TREE>){
s/$code/$id_global{$code}/ && print;
}
Chris
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>