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

View of /FortyEightMeta/TaxStr.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (download) (as text) (annotate)
Fri Mar 28 20:20:09 2008 UTC (12 years, 3 months ago) by olson
Branch: MAIN
Module for dealing with tokenized taxonomy elements.

package 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..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 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];
	$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 $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