Re: saving surrounding text in substitution

2006-10-11 Thread Dr.Ruud
Kathryn Bushley schreef:

> #!/usr/bin/perl -w
> use warnings;

Make that:

#!/usr/bin/perl
use warnings ;
use strict ;


See `perldoc perllexwarn`.

-- 
Affijn, Ruud

"Gewoon is een tijger."

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: saving surrounding text in substitution

2006-10-10 Thread Chris Charley


- Original Message - 
From: ""John W. Krahn"" <[EMAIL PROTECTED]>

Newsgroups: perl.beginners
To: "Perl Beginners" 
Sent: Tuesday, October 10, 2006 4:43 PM
Subject: Re: saving surrounding text in substitution



Chris Charley wrote:


(I've re-writteen your entire code below.You how much simpler it could 
be.)


#!/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 (){
 my ($id, $code, undef) = split ' ', $_, 3;


If you want simpler then:

 my ($id, $code) = split;


Yes indeed. In addition, I mucked up the last while loop.
Left my thinking cap in the garage :-(

while (){
   foreach my $code (keys %id_global){
 s/$code/$id_global{$code}/;
  }
  print;
}





 $id_global{$code} = $id;
}



:-)

John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.   -- Larry Wall 




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




Re: saving surrounding text in substitution

2006-10-10 Thread John W. Krahn
Chris Charley wrote:
> 
> (I've re-writteen your entire code below.You how much simpler it could be.)
> 
> #!/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 (){
>  my ($id, $code, undef) = split ' ', $_, 3;

If you want simpler then:

  my ($id, $code) = split;


>  $id_global{$code} = $id;
> }


:-)

John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.   -- Larry Wall

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: saving surrounding text in substitution

2006-10-10 Thread Chris Charley


- Original Message - 
From: "Kathryn Bushley" <[EMAIL PROTECTED]>

Newsgroups: perl.beginners
To: 
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 (){
 #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 (){
  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 (){
 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 (){
  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>




saving surrounding text in substitution

2006-10-10 Thread Kathryn Bushley
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 (){
  #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 (){
   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]
 




Re: saving surrounding text in substitution

2006-10-10 Thread Tom Phoenix

On 10/10/06, Kathryn Bushley <[EMAIL PROTECTED]> wrote:


s/(.*).$code.(.*)/$1.$id_global{$code}.$2/;


I haven't looked at all of your program in detail, so there may be
other fixes needed; but this substitution jumped out at me. It doesn't
look as if you really need $1 and $2; and it looks as if you mean to
have literal dots in the string. Do you perhaps want something like
this?

   s/\.$code\./.$id_global{$code}./g;

Hope this helps!

--Tom Phoenix
Stonehenge Perl Training

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




saving surrounding text in substitution

2006-10-10 Thread Kathryn Bushley
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 (){
  #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 (){
   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]