[Bio] / FortyEightMeta / TaxStr.pm Repository:
ViewVC logotype

View of /FortyEightMeta/TaxStr.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (download) (as text) (annotate)
Fri May 30 23:23:51 2008 UTC (12 years ago) by olson
Branch: MAIN
CVS Tags: mgrast_dev_08112011, mgrast_dev_08022011, mgrast_dev_05262011, mgrast_dev_04082011, mgrast_version_3_2, mgrast_dev_12152011, mgrast_dev_06072011, mgrast_rel_2008_0806, mgrast_dev_10262011, mgrast_dev_02212011, mgrast_rel_2008_0923, mgrast_release_3_0, mgrast_dev_03252011, mgrast_rel_2008_0924, mgrast_rel_2008_1110_v2, mgrast_rel_2008_0625, mgrast_release_3_0_4, mgrast_release_3_0_2, mgrast_release_3_0_3, mgrast_release_3_0_1, mgrast_dev_03312011, mgrast_release_3_1_2, mgrast_release_3_1_1, mgrast_release_3_1_0, mgrast_dev_04132011, mgrast_dev_04012011, mgrast_rel_2008_0919, mgrast_rel_2008_1110, myrast_33, mgrast_rel_2008_0917, mgrast_dev_04052011, mgrast_dev_02222011, HEAD
Changes since 1.1: +12 -3 lines
Development checkin of new metagenomics RAST pipeline.

package FortyEightMeta::TaxStr;

#
# Simple module to wrap up the tax string manipulation routines.
#

use strict;
use base 'Class::Accessor';
use Math::BaseCalc;
use Data::Dumper;

__PACKAGE__->mk_accessors(qw(dbh taxstr_lookup taxstr_insert taxstr_update calc cache));

our $DIGITS;
BEGIN {
    $DIGITS = [map { chr($_) } 33..47, 48..57, 59..64, 65..90, 91, 93..96, 97..122, 123..126];
}

sub new
{
    my($class, $dbh) = @_;

    my $self= {
	dbh => $dbh,
	calc => new Math::BaseCalc(digits => $DIGITS),
	taxstr_lookup => $dbh->prepare(qq(SELECT dbkey FROM tax_item WHERE str = ?)),
	taxstr_insert => $dbh->prepare(qq(INSERT INTO tax_item (str) VALUES (?))),
	taxstr_update => $dbh->prepare(qq(UPDATE tax_item SET dbkey = ? WHERE id = ?)),
	cache => {},
    };
    
    return bless $self, $class;
}

sub DESTROY
{
    my($self) = @_;
    $self->taxstr_lookup()->finish();
    $self->taxstr_insert()->finish();
    $self->taxstr_update()->finish();
}

sub tax_name_to_id
{
    my($self, $str) = @_;

    $str = '' if !defined($str);
    $self->taxstr_lookup->execute($str);
    
    my $res = $self->taxstr_lookup->fetchrow_arrayref();
    if ($res)
    {
	my $k = $res->[0];
	$k =~ s/\s+$//;
	$self->cache->{$str} = ['F', $k];
#	print "F\t'$str'\t'$k'\n";
	return $k;
    }
    else
    {
	if ($self->cache->{$str})
	{
	    my $l = $self->cache->{$str};
	    print "XXXXX '$str': " . join("\t", map { "'$_'" } @$l), "\n";
	}
	$self->taxstr_insert->execute($str);
	
	#my $id = $self->dbh->{mysql_insertid};
	my $id = $self->dbh->last_insert_id(undef, undef, 'tax_item', undef);
	my $s = $self->calc->to_base($id);
	$self->taxstr_update->execute($s, $id);
#	print "N\t$str\t$id\t$s\n";
	$self->cache->{$str} = ['N', $s, $id];
		       
	return $s;
    }
}

1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3