GenDBWiki/DeveloperDocumentation/GenDBDemoScript: Difference between revisions

From BRF-Software
Jump to navigation Jump to search
No edit summary
 
m (2 revisions)
 
(One intermediate revision by one other user not shown)
(No difference)

Latest revision as of 07:16, 26 October 2011

A simple GenDB Demo Script

This simple demo script listed below fetches all contigs from a GenDB project database and prints out their names.



#!/usr/bin/env perl

# simple GenDB demo script that reads all contigs and writes their names

use strict;
use Carp;
use Getopt::Std;
use Term::ReadKey;
use IO::Handle;
use GPMS::Application_Frame::GENDB;

#
#  this is necessary if the script is started via rsh(1)
#  otherwise you won't see any output until the first <RETURN>
#
STDOUT->autoflush(1);

sub usage {
    print "gendb_demo - get all contig sequences and write their names\n";
    print "usage: gendb_demo -p <project>\n\n";
}

# global variables
our($opt_p);

getopts('p:');

# start sanity checks
if (!$opt_p) {
    usage;
    print "ERROR: Can't initialize GenDB: No project name given!\n";
    exit 1;
};

# get the login name of the current user
my $user = defined( $ENV{'LOGNAME'} ) ? $ENV{'LOGNAME'} : (getpwuid( $> ))[0];

print "Enter your database password: ";
ReadMode('noecho');
my $password = ReadLine(0);
chomp $password;
print "\n";
ReadMode('normal');

# try to initialize GenDB project
# initialize an Application_Frame for the current project
my $gendbAppFrame = GPMS::Application_Frame::GENDB->new($user, $password);

# check if the initialization succeeded
die "Unable to initialize ApplicationFrame for GenDB project!" unless (ref $gendbAppFrame);

# try to initialize a project for the given name
$gendbAppFrame->project($opt_p);

# check a basic privilege   
exit unless $gendbAppFrame->right("basic_access");

# get a global O2DBI-2 master object
my $master = $gendbAppFrame->application_master();

# fetchall contigs and print their names
print "Contigs in GenDB project $opt_p:\n\n";
my $contigs = $master->Region->Source->Contig->fetchall();
foreach my $contig (@$contigs) {
    print $contig->name."\n";
}

print "\nDone.\n\n";