Ir para conteúdo
Fórum Script Brasil
  • 0

retornando um hash de uma subrotina


Rondon

Pergunta

tenho o seguinte script e gostaria de imprimir/usar os dados do hash retornado pela subrotina blast_m8. Mas não tem dado certo. como que faço?

obrigado

Rondon

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

my $est = $ARGV[0];
my $CDS = $ARGV[1]

my %m8_est_VS_CDS = blast_m8($est, $CDS);
print %m8_est_VS_CDS;

exit;

##############################################
sub get_file_data {
  use strict;
  use warnings;

  my $filename = $_[0];

  print ("$filename\n");
  my @filedata = ();
    open(GET_FILE_DATA, $filename) ||  die ($!);

  @filedata = <GET_FILE_DATA>;
  close GET_FILE_DATA;
  
  return @filedata;
}

##############################################
sub m8_into_hash {
use warnings;
use strict;

my $file = $_[0];
my %hash;
my @m8 = get_file_data($file);

foreach my $line (@m8){
  chomp $line;
  my @aux = split (/\t/, $line);
  my $query_id = $aux[0];
  my $subject_id = $aux[1];
  my $identity = $aux[2];
  my $ali_length = $aux[3];
  my $mismatches = $aux[4];
  my $gaps = $aux[5]; 
  my $q_start = $aux[6];
  my $q_end = $aux[7];
  my $s_start = $aux[8];
  my $s_end = $aux[9];
  my $evalue = $aux[10];
  my $score = $aux[11];
  $hash{$query_id}{subject_id} = $subject_id;
  $hash{$query_id}{identity} = $identity;
  $hash{$query_id}{ali_length} = $ali_length;
  $hash{$query_id}{mismatches} = $mismatches;
  $hash{$query_id}{gaps} = $gaps;
  $hash{$query_id}{q_start} = $q_start;
  $hash{$query_id}{q_end} = $q_end;
  $hash{$query_id}{s_start} = $s_start;
  $hash{$query_id}{s_end} = $s_end;
  $hash{$query_id}{evalue} = $evalue;
  $hash{$query_id}{score} = $score;
}
  return %hash;
}

##############################################
sub blast_m8 {
#perform blast (first "query" than "DB")
use strict;
    my ($est, $CDS) = @_;
    my $outfile = "megablast_results";
    my $blast_commands = "megablast -i $est -d $CDS -m8 > $outfile";
    my $blast_output = system($blast_commands);
    
    my %m8 = m8_into_hash ($outfile);
    
    return %m8;
}

Editado por kuroi
Adicionar tag CODE
Link para o comentário
Compartilhar em outros sites

1 resposta a esta questão

Posts Recomendados

  • 0

Caro Rondon,

Para imprimir o conteudo do hash você pode usar o modulo Data::Dumper;

ex:

use Data::Dumper;

print Dumper %m8_est_VS_CDS;

para usar os dados do hash vai depender de como você quer usar, mas ...

supondo que temos o %hash = ( teste => "123", teste2 => "456", teste3 => "678" );

para usar o valor da key "teste";

print $hash{teste} . "\n";

espero que tenha ajudado.

tenho o seguinte script e gostaria de imprimir/usar os dados do hash retornado pela subrotina blast_m8. Mas não tem dado certo. como que faço?

obrigado

Rondon

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

my $est = $ARGV[0];
my $CDS = $ARGV[1]

my %m8_est_VS_CDS = blast_m8($est, $CDS);
print %m8_est_VS_CDS;

exit;

##############################################
sub get_file_data {
  use strict;
  use warnings;

  my $filename = $_[0];

  print ("$filename\n");
  my @filedata = ();
    open(GET_FILE_DATA, $filename) ||  die ($!);

  @filedata = <GET_FILE_DATA>;
  close GET_FILE_DATA;
  
  return @filedata;
}

##############################################
sub m8_into_hash {
use warnings;
use strict;

my $file = $_[0];
my %hash;
my @m8 = get_file_data($file);

foreach my $line (@m8){
  chomp $line;
  my @aux = split (/\t/, $line);
  my $query_id = $aux[0];
  my $subject_id = $aux[1];
  my $identity = $aux[2];
  my $ali_length = $aux[3];
  my $mismatches = $aux[4];
  my $gaps = $aux[5]; 
  my $q_start = $aux[6];
  my $q_end = $aux[7];
  my $s_start = $aux[8];
  my $s_end = $aux[9];
  my $evalue = $aux[10];
  my $score = $aux[11];
  $hash{$query_id}{subject_id} = $subject_id;
  $hash{$query_id}{identity} = $identity;
  $hash{$query_id}{ali_length} = $ali_length;
  $hash{$query_id}{mismatches} = $mismatches;
  $hash{$query_id}{gaps} = $gaps;
  $hash{$query_id}{q_start} = $q_start;
  $hash{$query_id}{q_end} = $q_end;
  $hash{$query_id}{s_start} = $s_start;
  $hash{$query_id}{s_end} = $s_end;
  $hash{$query_id}{evalue} = $evalue;
  $hash{$query_id}{score} = $score;
}
  return %hash;
}

##############################################
sub blast_m8 {
#perform blast (first "query" than "DB")
use strict;
    my ($est, $CDS) = @_;
    my $outfile = "megablast_results";
    my $blast_commands = "megablast -i $est -d $CDS -m8 > $outfile";
    my $blast_output = system($blast_commands);
    
    my %m8 = m8_into_hash ($outfile);
    
    return %m8;
}

Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,1k
    • Posts
      651,8k
×
×
  • Criar Novo...