Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^3: Perl tk - How to integrate external scripts

by chessgui (Scribe)
on Jan 27, 2012 at 17:17 UTC ( #950418=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Perl tk - How to integrate external scripts
in thread Perl tk - How to integrate external scripts

Just leave it alone, run it separately as a system command and redirect its output to a file:

system('perl.exe yourstatisticalroutine.pl inputfile.name >outputfile. +name');
(Since this is a system command you have to find out the proper format of that on your system. This code is tested on Win32.)


Comment on Re^3: Perl tk - How to integrate external scripts
Download Code
Re^4: Perl tk - How to integrate external scripts
by Giorgio C (Novice) on Jan 30, 2012 at 09:19 UTC
    Thank for your answer but unfortunately it still doesn't work. The code with your suggestions is:
    # the firs button : $mw-> Button (-text =>'Open', -command =>\&open_file)->place(-x=>240, + -y=>35); #The seocnd button: $mw-> Button (-text =>'Get Statistics',-command =>\&get_statistics)->p +lace(-x=>320, -y=>35); # The sub open : sub open_file {my $open = $mw->getOpenFile( -filetypes => $types_OPEN, -defaultextension => '.sff'); $te->Load( "$open");} if (defined $open and -e $open){ $te->Load( "$open"); $current_file = $open; } # the sub get statistic: sub get_statistics {my $stat = Statistics::Descriptive::Full->new(); my (%distrib); my @bins = qw/18 19 20 21 22 23 24 25 26 27 28/; my $fastaFile = shift; unless (defined $fastaFile){ warn "No file name supplied.\n"; return; } open my $FASTA, '<', $fastaFile or warn "Couldn't open file: $!\n"; my $junkFirstOne = <FASTA>; while (<$FASTA>) {chomp; my ($def,@seqlines) = split /\n/, $_; my $seq = join '', @seqlines; $stat->add_data(length($seq));} %distrib = $stat->frequency_distribution(\@bins); print "Total reads:\t" . $stat->count() . "\n"; print "Total nt:\t" . $stat->sum() . "\n"; print "Mean length:\t" . $stat->mean() . "\n"; print "Median length:\t" . $stat->median() . "\n"; print "Mode length:\t" . $stat->mode() . "\n"; print "Max length:\t" . $stat->max() . "\n"; print "Min length:\t" . $stat->min() . "\n"; print "Length\t# Seqs\n"; foreach (sort {$a <=> $b} keys %distrib) { print "$_\t$distrib{$_}\n"; }}; # The scrolled window: $te = $mw->Scrolled( q/TextUndo/, -scrollbars => 'se', -background => +'white', -font => [-family =>'arial',-size => '12',-weight=> 'bold'], +-wrap => 'none',)->pack(-anchor =>'n',-side=>'right',-fill=>'none');
    It say me (after load the $open file in the scrolled window and push the get_statistic button) : "No file name supplied". Where i need to correct the script to make it works ? Thanks
      By leave it alone I mean that you leave the statistical routine unchanged (as it worked before) in a separate file called 'yourstatisticalroutine.pl' (or whatever name it has now). Execute this script as an external system process by the system command I mentioned then operate on the file to which its output was is redirected. The script you supplied does the opposite: the statistical routine is copy pasted and I see no 'system' command at all. Now your routine tries to get the file name from the argument list of the main program (which is probably empty). If you choose to do it this way the -command bind should look something like this:
      $mw-> Button (-text =>'Get Statistics', -command =>[ \&get_statistics, 'datafile.name' ] )->p +lace(-x=>320, -y=>35);
      In this way your get_statistics routine will be called with the argument 'datafile.name'. You should store your data to be analyzed in this file.
        Thanks but in this way i need to know before what is "datafile.name". I wanted instead that this would be the $open variable called with the get_open( that show all my directories). chosed a file with get open in my dir and on that working the get statistic command: get_open => $open => get_statistics on $open Is there a really way ?

      As has already been mentioned, your problem is that $open is scoped locally to the sub open_file. This doesn't work, because that way the variable is not accessible outside of the sub (when you click the Get Statistics button).

      Make it a global variable

      my $open; # variable with file scope # The sub open : sub open_file {$open = $mw->getOpenFile( # no my here! ...

      and then simply use the variable inside of get_statistics:

      sub get_statistics { ... my $fastaFile = $open; ...

      Alternatively, use the variable $current_file.  You do have assigned $open to it, but you are not using it anywhere...

      sub get_statistics { ... my $fastaFile = $current_file; ...
        Thank you very much for your answer, i've tried with your advices but it still doesn't work. Can you correct here the script to make it works ? It's clear i'm a newbie.
        my $open; $mw-> Button (-text =>'Open', -command =>\&open_file)->place(-x=>240, +-y=>35); $mw-> Button (-text =>'Get Statistics',-command =>[\&get_statistics, $ +open])->place(-x=>320, -y=>35); sub open_file {$open = $mw->getOpenFile( -filetypes => $types_OPEN, -defaultextension => '.sff'); $te->Load( "$open");} if (defined $open and -e $open){ $te->Load( "$open"); $fastaFile = $open; } sub get_statistics {my $stat = Statistics::Descriptive::Full->new(); my (%distrib); my @bins = qw/18 19 20 21 22 23 24 25 26 27 28/; my $fastaFile = $open; unless (defined $fastaFile){ warn "No file name supplied.\n"; return; } open my $FASTA, '<', $fastaFile or warn "Couldn't open file: $!\n"; my $junkFirstOne = <FASTA>; while (<$FASTA>) {chomp; my ($def,@seqlines) = split /\n/, $_; my $seq = join '', @seqlines; $stat->add_data(length($seq));} %distrib = $stat->frequency_distribution(\@bins); print "Total reads:\t" . $stat->count() . "\n"; print "Total nt:\t" . $stat->sum() . "\n"; print "Mean length:\t" . $stat->mean() . "\n"; print "Median length:\t" . $stat->median() . "\n"; print "Mode length:\t" . $stat->mode() . "\n"; print "Max length:\t" . $stat->max() . "\n"; print "Min length:\t" . $stat->min() . "\n"; print "Length\t# Seqs\n"; foreach (sort {$a <=> $b} keys %distrib) { print "$_\t$distrib{$_}\n"; }};

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://950418]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2014-07-30 00:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls