G
Messenger
Summary
G::Messenger.pm G-language GAE Messaging API
Package variables
Privates (from "my" definitions)
%data;
$term;
$gimv = 'display'
$progress;
$console;
Included modules
Carp
SubOpt
autouse(1) ' Cwd ' => qw ( getcwd )
autouse(2) ' Data::Dumper ' => qw ( Dumper )
autouse(3) ' LWP::Simple ' => qw ( get )
base ' Exporter '
Synopsis
use G::Messenger;
&msg_send("this is an output"); #prints message to STDOUT
&msg_error("this is an error"); #prints message to STDERR
&msg_gimv("graph.png"); #shows the image via gimv (G-language IMage Viewer)
Description
Methods used in Odyssey:
msg_send()
Prints out a regular output message. The output is STDOUT in CUI mode,
and text output in Gtk+, wxWindows, and INSPIRE modes.
Synonym of "print".
msg_error()
Prints out an error or system message. The output is STDERR in CUI mode,
and system console in Gtk+, wxWindows, and INSPIRE modes.
Synonym of "print STDOUT".
msg_gimv()
Shows the image via gimv (G-language IMage Viewer). The gimv program will
startup in Gtk+ and wxWindows mode, and only the file path is passed on
in INSPIRE mode.
Synonym of "system('gimv image.png')".
Methods used in Engines:
msg_percent()
Sets the progress bar to the given percentage.
Methods used in User Interfaces (INSPIRE):
msg_interface()
Sets the interface that the system currently uses. 'GUI' for Gtk+ mode,
'Wx' for wxWindows mode, 'Inspire' for INSPIRE mode, and none or 'STDOUT'
for CUI mode. All Messenger output bases the interpretation of the current
user interface on the value set with this function.
msg_system_console()
In Gtk+ and wxWindows mode, system console widget instance should be supplied.
In INSPIRE mode, the reference to a subroutine that interpret the job of system
console should be supplied. Messages sent with msg_error() are passed to the
given reference.
msg_term_console()
In Gtk+ and wxWindows mode, term console widget instance should be supplied.
In INSPIRE mode, the reference to a subroutine that interpret the job of term
console should be supplied. Messages sent with msg_send() are passed to the
given reference.
msg_progress()
In Gtk+ and wxWindows mode, progress bar widget instance should be supplied.
In INSPIRE mode, the reference to a subroutine that interpret the job of progress
bar may be supplied. Messages sent with msg_percent() are passed to the
given reference.
msg_set_gimv()
In INSPIRE mode, the reference to a subroutine that interpret the job of gimv
should be supplied. Messages sent with msg_gimv() are passed to the given
reference.
msg_ask_interface()
Returns the current mode.
Methods
msg_ask_interface | No description | Code |
msg_datafile | No description | Code |
msg_error | No description | Code |
msg_gimv | No description | Code |
msg_interface | No description | Code |
msg_percent | No description | Code |
msg_progress | No description | Code |
msg_send | No description | Code |
msg_set_gimv | No description | Code |
msg_system_console | No description | Code |
msg_term_console | No description | Code |
p | No description | Code |
puts | No description | Code |
readFile | Description | Code |
say | No description | Code |
writeFile | Description | Code |
Methods description
Name: readFile - reads in text from a file
Description:
This method simply reads all contents of a file and returns
the contents as a scalar or an array of lines depending on the
context. Specify second argument as 1 to chomp each lines.
The file can be local, remote (starting with http:// or ftp://), or gzipped.
Usage:
#in array context
@text = readFile($filename);
#in scalar context
$text = readFile($filename);
#with URL
@text = readFile('http://togows.dbcls.jp/entry/pubmed/12538262')
#with FastA/FastQ files
%sequences = readFile($fasta.fasta);
# key corresponds to the header line, value to the sequence.
# quality information for FastQ file is thrown away.
Options:
Specify second argument as 1 to chomp each lines.
-format 'plain' to read as plain text, 'url' to read as url,
'command' to run a set of UNIX commands and get the output,
'fasta' for FASTA file, 'fastq' for FASTQ file.
'kegg' for parsing KEGG flatfiles, and 'swiss' for
parsing UniProt flatfiles, returning a data structure
object representing the file content.
Usually the format is automatically interpreted, but
this option can be specified to force interpretation.
-gzip 0 for uncompressed files, 1 for gzipped files (default:0)
automatically turned on when filename ends with ".gz"
-output 'json' to output in JSON format
Author:
Kazuharu Arakawa (gaou@sfc.keio.ac.jp)
History:
20110428-01 added support for UNIX commands
20100122-01 added support for gzipped files
20091202-01 added FastA/FastQ support
20090520-01 added URL support
20090313-01 initial posting |
Name: writeFile - writes a given scalar to a file
Description:
This method simply writes a given scalar value (text) to a file.
Filename should be given as the second argument, and if ommited,
a file named 'out.txt' is created.
Usage:
$filename = writeFile($text, $filename);
Options:
none
Author:
Kazuharu Arakawa (gaou@sfc.keio.ac.jp)
History:
20090313-01 initial posting |
Methods code
msg_ask_interface | description | prev | next | Top |
sub msg_ask_interface
{ return $data{"interface"}; } |
sub msg_datafile
{ my ($content, $filename) = @_;
if(substr($filename, 0, 1) eq '/' || substr($filename, 0, 1) eq '.'){
writeFile($content, $filename);
}else{
mkdir ("data", 0777);
writeFile($content, 'data/' . $filename);
} } |
sub msg_error
{ my @msg = @_;
if ($data{"interface"} eq 'GUI'){
$data{"system"} .= join('', @msg);
$console->insert('','','',join('', @msg));
}elsif ($data{"interface"} eq 'Wx'){
my $tmp = join('', @msg);
$data{"system"} .= $tmp;
$console->WriteText(scalar($tmp));
}elsif ($data{'interface'} eq 'Inspire'){
my $tmp = join('', @msg);
$data{"system"} .= $tmp;
&$console($tmp);
}elsif ($data{'interface'} eq 'NULL'){
}else{
print STDERR @msg;
} } |
sub msg_gimv
{ my $file = shift;
my $time = 'gimv' . time();
if ($data{"interface"} eq 'Inspire'){
&$gimv($file);
}else{
if('MSWin32' eq $^O){
require Win32::InternetExplorer::Window;
require Image::Size;
my $dir = getcwd() unless (substr($file, 0, 1) eq '/');
my $filename = $dir . '/' . $file;
if ($filename =~ /[htm|html]$/){
$data{$time} = Win32::InternetExplorer::Window->new();
}else{
my ($x, $y) = Image::Size::imgsize($filename);
$data{$time} = Win32::InternetExplorer::Window->new(
height=>$y+60,
width=>$x+50
);
}
$data{$time}->display('\G-language\title.jpg');
$data{$time}->display_wait($filename);
}else{
$gimv = 'firefox' if ($file =~ /html$/ && $gimv eq 'gimv');
$gimv = 'firefox' if ($file =~ /svg$/ && $gimv eq 'gimv');
$file = 'file://' . getcwd() . '/' . $file if ($gimv eq 'mozilla');
system("$gimv $file &");
}
} } |
sub msg_interface
{ $data{"interface"} = shift; } |
sub msg_percent
{ my $percent = shift;
if ($data{"interface"} eq 'GUI'){
$progress->update($percent);
}elsif ($data{"interface"} eq 'Wx'){
$progress->SetValue($percent);
$progress->Refresh();
} } |
sub msg_progress
{ $progress = shift; } |
sub msg_send
{ my @msg = @_;
if ($data{"interface"} eq 'GUI'){
$data{"term"} .= join('',@msg);
$term->insert('','','',join('',@msg));
}elsif ($data{"interface"} eq 'Wx'){
my $tmp = join('', @msg);
$data{"term"} .= $tmp;
$term->WriteText(scalar($tmp));
}elsif ($data{'interface'} eq 'Inspire'){
my $tmp = join('', @msg);
$data{"term"} .= $tmp;
&$term($tmp);
}elsif ($data{'interface'} eq 'NULL'){
}else{
print @msg;
} } |
sub msg_set_gimv
{ $gimv = shift; } |
sub msg_system_console
{ $console = shift; } |
sub msg_term_console
{ $term = shift; } |
sub p
{ print Dumper(@_), "\n"; } |
sub puts
{ print @_, "\n"; } |
sub readFile
{ opt_default('format'=>'', 'gzip'=>0);
my @argv = opt_get(@_);
my $file = shift @argv;
my $chomp = shift @argv || 0;
my $format = lc(opt_val('format'));
my $gzip = lc(opt_val('gzip'));
my $output = opt_val('output');
if($format eq "kegg"){
my ($kegg, $key, $entry);
open(FILE, $file) || croak($!);
while(<FILE>){
chomp;
if(/^(\S+?)\s+(.*)/){
my $two = $2;
if ($1 eq 'ENTRY'){
($entry, undef) = split(/\s+/, $two);
}else{
$key = $1;
$kegg->{$entry}->{$key} = $two;
}
}elsif(/^\s+(.*)/){
$kegg->{$entry}->{$key} .= ' ' . $1;
}
}
close(FILE);
return $kegg;
}elsif($format =~ /swiss/){
my $sprot = {};
my $key = '';
open(FILE, $file) || croak($!);
while(<FILE>){
chomp;
if (/^ID\s+(.+?)\s+/){
$key = $1;
$sprot->{$key}->{LOCUS}->{id} = $key;
}elsif(/^\/\//){
$key = '';
}elsif(/^(..)\s+(.*)/){
$sprot->{$key}->{$1} .= length($sprot->{$key}->{$1}) ? ' ' . $2 : $2;
}
}
close(FILE);
return $sprot;
}
unless($format){
if ($file =~ /\:\/\// && $file !~ / /){
$format = 'url';
}elsif (lc($file) =~ /\.(fasta|fast|seq|fst|fa|fsa|nt|aa|ffn|fna)/i){
$format = 'fasta';
}elsif (lc($file) =~ /\.(fastq|fq)/i){
$format = 'fastq';
}else{
unless(-e $file){
$format = 'command';
}
}
}
my @result;
if($format eq 'url'){
foreach my $line (split(/\n/, get($file))){
$line .= "\n" unless($chomp);
push(@result, $line);
}
}elsif($format eq 'fasta' || $format eq 'fastq'){
my %hash;
my $gb = new G::IO($file, 'no msg', 'no cache', $format);
do{
$hash{$gb->{LOCUS}->{id}} = $gb->{SEQ};
}while($gb->next_locus('no msg'));
msg_error('Loaded ' . scalar(keys(%hash)) . ' ' . uc($format) . ' sequences.');
return %hash;
}elsif($format eq 'command'){
foreach my $line (`$file`){
chomp($line) if ($chomp);
push(@result, $line);
}
}else{
my $fh;
if($file =~ /\.gz$/ || $gzip){
$fh = new IO::Zlib;
$fh->open($file, 'rb') or croak($!);
}else{
$fh = new IO::File;
open($fh, $file) or croak($!);
}
open(FILE, $file) || croak($!);
while(<$fh>){
chomp if($chomp);
push(@result, $_);
}
close($fh);
}
if(wantarray()){
return @result;
}else{
return join('', @result);
} } |
sub say
{ print join(',', @_), "\n"; } |
sub writeFile
{ my $data = shift;
my $file = shift || "out.txt";
open(OUT, '>' . $file) || croak($!);
print OUT $data;
close(OUT);
return $file; } |
General documentation
One of the key features of the G-language Genome Analysis Environment is the
ease of use and development of different user interfaces. With very little
modification, a script intended for commandline Perl use can be transformed
into a cross-platform GUI application using wxWindows, or a web application
using INSPIRE. The Messaging API G::Messenger.pm is what makes this possible.
All output in the software system is passed to the interface provided by this
API, and Messenger interprets the current user interface to give correct form
of output. It is essential to create Odyssey Subroutines using this API, and
to create new user interfaces.
perl(1).