CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
sub and hashes

 



lostagain
New User

Nov 21, 2012, 10:32 AM

Post #1 of 9 (1407 views)
sub and hashes Can't Post

I cant figure out how to return hashes created with this sub, anyone can help me

sub Lfasta {
my $input=$_[0];
chomp $input;
my $output=$_[1]; #
chomp $output; #

open (INPUT, "<$input")|| die "Nao foi possivel abrir arquivo de leitura";
open (OUTPUT, ">$output")|| die "Nao foi possivel abrir saida";

my %seq=();
my %qual=();
my $a=0;
my $b=0;

while(<INPUT>){
if ($_ =~ m/^(\>.+)/){
$seq{$a}=$1;$a++;
} elsif ($_ =~ m/([ATCG]+)/){
$qual{$b}=$1;$b++;
} else {
print"Dado nao direcionado";
}
}


return OUTPUT (\%qual, \%seq);


FishMonger
Veteran / Moderator

Nov 21, 2012, 11:05 AM

Post #2 of 9 (1404 views)
Re: [lostagain] sub and hashes [In reply to] Can't Post

Change:

Code
return OUTPUT (\%qual, \%seq);


To:

Code
return (\%qual, \%seq);



lostagain
New User

Nov 21, 2012, 11:48 AM

Post #3 of 9 (1400 views)
Re: [FishMonger] sub and hashes [In reply to] Can't Post

With this OUTPUT i was trying to check my hashes .. The real problem is that I need to use this hashes in another sub, this one to check for restrictions sites ( sequences like AATT inside the hashes.


(This post was edited by lostagain on Nov 21, 2012, 11:51 AM)


FishMonger
Veteran / Moderator

Nov 21, 2012, 12:25 PM

Post #4 of 9 (1396 views)
Re: [lostagain] sub and hashes [In reply to] Can't Post


Quote
With this OUTPUT i was trying to check my hashes

Wrong approach. Use the Data::Dumper module. http://search.cpan.org/~smueller/Data-Dumper-2.136/Dumper.pm


Quote
The real problem is that I need to use this hashes in another sub, this one to check for restrictions sites

I'm not sure I understand your problem. Based on your Lfasta() sub, it appears that you know how to pass args to a sub and the return statement is used to pass back value(s) to the caller.

Please explain in clear terms where you need assistance.


lostagain
New User

Nov 21, 2012, 2:45 PM

Post #5 of 9 (1393 views)
Re: [FishMonger] sub and hashes [In reply to] Can't Post

Ok, let me try to explain :P
I always have a huge problem with hashes !
Once it's more difficult to recall values in hashes, the procedures (that I try to make) to see if what I wrote is working doesn't work well......... and I get lost where its the real problem.

What I'm trying to do is a little script with 3 subs: The first one read a file and split names and qualities in two hashes.
The second one have find in the quality hash the pattern that I ask for. I couldn't understand what do I have to do to get the hashes from my first sub to use in the second one (I thought the problem was with return function, because i coudnt see anywhere the hash with this function).

do i have to transform this hashes in arrays ? Does it send the first hash to the first array, the second to the second, etc or do I have to specify some how?
my (@seq, @qual)= Lfasta ($arquivo);

If i can call sub2 with arrays, do i have to divide than into hashes again inside sub? Isn't there a direct way to use the hashes from one sub to another ?



Chris Charley
User

Nov 21, 2012, 4:01 PM

Post #6 of 9 (1389 views)
Re: [lostagain] sub and hashes [In reply to] Can't Post

Here is a small example of fasta parsing, but I should state that I'm by no means an expert or even that knowledgable using the Bio::Seq module and the Bio::SeqIO module.


Code
  #!/usr/bin/perl   
use strict;
use warnings;
use Bio::SeqIO;

my $in = Bio::SeqIO->new( -file => "input1.txt" ,
-format => 'fasta');

while ( my $seq = $in->next_seq() ) {
my $fasta = $seq->seq;
while ($fasta =~ /AATT/ig) {
print pos($fasta), ' ', $seq->id, "\n";
}
}

__END__
*** contents of input1.txt
>chr1
AACCCCCCCCTCCCCCCGCTTCTGGCCACAGCACTTAAACACATCTCTGC
CAAACCCCAAAAACAAAGAACCCTAACACCAGCCTAACCAGATTTCAAAT
TTTATCTTTAGGCGGTATGCACTTTTAACAAAAAANNNNNNNNNNNNNNN
NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
GCCCATCCTACCCAGCACACACACACCGCTGCTAACCCCATACCCCGAAC
CAACCAAACCCCAAAGACACCCCCCACAGTTTATGTAGCTTACCTCNNNN
>chrM
GATCACAGGTCTATCACCCTATTAACCACTCACGGGAGCTCTCCATGCAT
TTGGTATTTTCGTCTGGGGGGTGTGCACGCGATAGCATTGCGAGACGCTG
GAGCCGGAGCACCCTATGTCGCAGTATCTGTCTTTGATTCCTGCCTCATT
CTATTATTTATCGCACCTACGTTCAATATTACAGGCGAACATACCTACTA
AAGTGTGTTAATTAATTAATGCTTGTAGGACATAATAATAACAATTGAAT
GTCTGCACAGCCGCTTTCCACACAGACATCATAACAAAANAATTTCCACC
>GJKKTUG01DYDGC
GGGTATTCCTTCTCCACCTTGCAGCTAACATCAGTGTTTCGTCTACTCAAGCACGCCAAC
ACGCCCTAGAGCGCCCTGTCCAGGGGATGGCAACCAACTCTGACCCTGCAAGTGCAGCAG
ACATGAGGAATACAAACTACAATCTTTTACTTGATGATGCAATGCCGGACAAACTCTAGA
>F0Z7V0F01EDB3V
AAGGCGAGNGGTATCACGCAGTAAGTTACGGTTTTCGGGTAACGCGTCNGNGGNACTAAC
CCACGGNGGGTAACCCGTCNCTACCGGTATAGGACTAAGGTTACCGGAACGTCGTGGGGT
ACCCCCCGGACGGGGACCGTCCCCTCATANAGTCAACNGTNTGAGATGGACTAACTCAAA
CCTAGTTTCAAGTACTATTTAACTTACTTACGTTACCCGTAATTTCGGCGTTTAGAGGCG


Output from the sample 'input1.txt' fasta file.


Code
  C:\Old_Data\perlp>perl t33.pl   
101 chr1
213 chrM
217 chrM
246 chrM
294 chrM
224 F0Z7V0F01EDB3V


Most likely, someone else has already solved your problem. There is a How To, especially the How To for Beginners and for SeqIO. There is also
bedtools
and other software developed to solve problems in Genomics.


(This post was edited by Chris Charley on Nov 21, 2012, 5:28 PM)


lostagain
New User

Nov 21, 2012, 6:37 PM

Post #7 of 9 (1375 views)
Re: [lostagain] sub and hashes [In reply to] Can't Post

 
I was trying to do an exercise and I needed in that specific way , using hashes made in one sub to find in another one the restriction site. But thanks anyway!!!


(This post was edited by lostagain on Nov 21, 2012, 6:46 PM)


Laurent_R
Veteran / Moderator

Nov 21, 2012, 11:27 PM

Post #8 of 9 (1369 views)
Re: [lostagain] sub and hashes [In reply to] Can't Post

Is you problem passing around hashes from a sub to another one?

Use references to hashes. If you don't know what this means, just ask how to do it.


FishMonger
Veteran / Moderator

Nov 22, 2012, 7:03 AM

Post #9 of 9 (1316 views)
Re: [lostagain] sub and hashes [In reply to] Can't Post


Code
# execute first sub which takes in 1 arg and returns 2 hash references. 
my ($hash_ref1, $hash_ref2) = first($arquivo);

# pass those hash refs to the second sub, which returns adjusted versions
($hash_ref1, $hash_ref2) = second($hash_ref1, $hash_ref2);

# pass those adjusted versions to the 3rd sub
third($hash_ref1, $hash_ref2);

#######################################

sub first {
....
....
return \%qual, \%seq;
}

sub second {
my ($first_hash_ref, $second_hash_ref) = @_;
# process/adjust the hashes as needed
....
....
# return the adjusted hashes
return $first_hash_ref, $second_hash_ref;
}

sub third {
my ($first_hash_ref, $second_hash_ref) = @_;
# process the hashes as needed
....
....
}


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives