Permalink
Browse files

Merge pull request #18 from andrewjpage/dont_add_common_name_by_default

dont add common name by default
  • Loading branch information...
andrewjpage committed May 10, 2012
2 parents 49345f7 + fbb618f commit 5935b4e765f004452f740572422592a9b2077ad8
Showing with 23 additions and 4 deletions.
  1. +5 −1 modules/UpdatePipeline/UpdateAllMetaData.pm
  2. +12 −1 modules/UpdatePipeline/VRTrack/Sample.pm
  3. +6 −2 t/VRTrack/Sample.t
@@ -104,7 +104,11 @@ sub _update_lane
my $vstudy = UpdatePipeline::VRTrack::Study->new(accession => $file_metadata->study_accession_number, _vr_project => $vproject)->vr_study();
$vproject->update;
}
- my $vr_sample = UpdatePipeline::VRTrack::Sample->new(name => $file_metadata->sample_name, external_id => $file_metadata->sample_ssid, common_name => $file_metadata->sample_common_name, accession => $file_metadata->sample_accession_number, _vrtrack => $self->_vrtrack,_vr_project => $vproject)->vr_sample();
+ my $vr_sample = UpdatePipeline::VRTrack::Sample->new(
+ common_name_required => $self->common_name_required,
+ name => $file_metadata->sample_name,
+ external_id => $file_metadata->sample_ssid,
+ common_name => $file_metadata->sample_common_name, accession => $file_metadata->sample_accession_number, _vrtrack => $self->_vrtrack,_vr_project => $vproject)->vr_sample();
my $vr_library = UpdatePipeline::VRTrack::Library->new(
name => $file_metadata->library_name,
external_id => $file_metadata->library_ssid,
@@ -29,6 +29,7 @@ has '_vr_project' => ( is => 'ro', required => 1 );
has 'accession' => ( is => 'ro', isa => 'Maybe[Str]' );
has 'external_id' => ( is => 'ro', isa => 'Maybe[Int]' );
+has 'common_name_required' => ( is => 'rw', default => 1, isa => 'Bool');
# external variable
has 'vr_sample' => ( is => 'ro', lazy_build => 1 );
@@ -69,7 +70,17 @@ sub _build_vr_sample
sub _build__vr_species
{
my ($self) = @_;
- my $vr_species = VRTrack::Species->new_by_name( $self->_vrtrack, $self->common_name) || VRTrack::Species->create( $self->_vrtrack, $self->common_name);
+ my $vr_species = VRTrack::Species->new_by_name( $self->_vrtrack, $self->common_name);
+
+ if((not defined($vr_species) )&& $self->common_name_required ==0)
+ {
+ $vr_species = VRTrack::Species->create( $self->_vrtrack, $self->common_name);
+ }
+ elsif((not defined($vr_species) )&& $self->common_name_required ==1)
+ {
+ UpdatePipeline::Exceptions::UnknownCommonName->throw( error => $self->common_name );
+ }
+
return $vr_species;
}
View
@@ -41,10 +41,14 @@ is_deeply $vr_sample2->individual->species, $vr_sample->individual->species, 're
# a species should be added if it doesnt previously exist
-ok my $sample3 = UpdatePipeline::VRTrack::Sample->new(name => 'Another name',common_name => 'UnseenCommonName',_vrtrack => $vrtrack,_vr_project => $vproject), 'initialise a sample';
+ok my $sample3 = UpdatePipeline::VRTrack::Sample->new(common_name_required => 0, name => 'Another name',common_name => 'UnseenCommonName',_vrtrack => $vrtrack,_vr_project => $vproject), 'initialise a sample';
ok my $vr_sample3 = $sample3->vr_sample(), 'find a vr sample';
is $vr_sample3->individual->species->name, "UnseenCommonName", 'get the species back';
+
+# a species should throw an error if a common name is required and its not alrady in the database
+throws_ok( sub{UpdatePipeline::VRTrack::Sample->new(common_name_required => 1, name => 'Another name',common_name => 'YetAnotherUnseenCommonName',_vrtrack => $vrtrack,_vr_project => $vproject)->vr_sample()}, qr/YetAnotherUnseenCommonName/, 'common name required but doesnt exist in database already');
+
# individual previously exists
my $preexisting_individual = VRTrack::Individual->create($vrtrack, 'SampleNameThatAlreadyExists');
ok my $sample4 = UpdatePipeline::VRTrack::Sample->new(name => 'SampleNameThatAlreadyExists',common_name => 'SomeBacteria',_vrtrack => $vrtrack,_vr_project => $vproject), 'initialise a sample';
@@ -66,7 +70,7 @@ sub delete_test_data
$vrtrack->{_dbh}->do('delete from project where name="My project"');
$vrtrack->{_dbh}->do('delete from sample where name in ("SampleNameThatAlreadyExists","My name","Another name" )');
$vrtrack->{_dbh}->do('delete from individual where name in ("SampleNameThatAlreadyExists","My name","Another name" )');
- $vrtrack->{_dbh}->do('delete from species where name in ("SomeBacteria","UnseenCommonName")');
+ $vrtrack->{_dbh}->do('delete from species where name in ("YetAnotherUnseenCommonName","SomeBacteria","UnseenCommonName")');
$vrtrack->{_dbh}->do('delete from population where name="Population"');
}

0 comments on commit 5935b4e

Please sign in to comment.