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/; 
   print $line;
   } 
}




kathryn

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to