Skip to content

Commit

Permalink
Merge pull request #1498 from tripal/tv4g0-issue1485-port-pull-1244
Browse files Browse the repository at this point in the history
Tripal 4 chado_unabbreviate_infraspecific_rank (port tripal 3 pull 1244) ported and tests now work with "correct" schema 馃榿
  • Loading branch information
dsenalik committed May 11, 2023
2 parents 3ff13d4 + a3d1aec commit a2f1172
Show file tree
Hide file tree
Showing 2 changed files with 281 additions and 12 deletions.
100 changes: 88 additions & 12 deletions tripal_chado/src/api/tripal_chado.organism.api.inc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
* - Any keys supported by chado_generate_var(). See that function
* definition for additional details.
*
* @param string $schema_name
* The name of the schema to pull the variable from.
*
* NOTE: the $identifier parameter can really be any array similar to $values
* passed into chado_select_record(). It should fully specify the organism
* record to be returned.
Expand All @@ -44,7 +47,7 @@
*
* @ingroup tripal_organism_api
*/
function chado_get_organism($identifiers, $options = []) {
function chado_get_organism($identifiers, $options = [], $schema_name = 'chado') {

// Set Defaults.
if (!isset($options['include_fk'])) {
Expand Down Expand Up @@ -81,11 +84,24 @@ function chado_get_organism($identifiers, $options = []) {
if (isset($identifiers['property'])) {
$property = $identifiers['property'];
unset($identifiers['property']);
$organism = chado_get_record_with_property(
['table' => 'organism', 'base_records' => $identifiers],
['type_name' => $property],
$options
// @to-do chado_get_record_with_property() does not exist in Tripal 4
tripal_report_error(
'tripal_organism_api',
TRIPAL_ERROR,
"chado_get_organism: chado_get_record_with_property() is not yet implemented in Tripal 4",
// You did not pass in anything to identify the organism you want. The identifier
// is expected to be an array with the key matching a column name in the organism table
// (ie: organism_id or name). You passed in %identifier.",
[
// '%identifier' => print_r($identifiers, TRUE),
]
);
// $organism = chado_get_record_with_property(
// ['table' => 'organism', 'base_records' => $identifiers],
// ['type_name' => $property],
// $options,
// $schema_name
// );
}

// Else we have a simple case and we can just use chado_generate_var to get
Expand All @@ -96,7 +112,8 @@ function chado_get_organism($identifiers, $options = []) {
$organism = chado_generate_var(
'organism',
$identifiers,
$options
$options,
$schema_name
);
}

Expand Down Expand Up @@ -139,14 +156,29 @@ function chado_get_organism($identifiers, $options = []) {
* @param $organism
* An organism object.
*
* @param string $schema_name
* The name of the schema to pull the variable from.
*
* @return
* The full scientific name of the organism.
*
* @ingroup tripal_organism_api
*/
function chado_get_organism_scientific_name($organism) {
$name = $organism->genus . ' ' . $organism->species;
function chado_get_organism_scientific_name($organism, $schema_name = 'chado') {
// Validation
if (!is_object($organism)) {
tripal_report_error(
'tripal_organism_api',
TRIPAL_ERROR,
"chado_get_organism_scientific_name: passed organism parameter is not an object.
You passed in %identifier.",
[
'%identifier' => print_r($organism, TRUE),
]
);
}

$name = $organism->genus . ' ' . $organism->species;
$rank = '';
// For organism objects created using chado_generate_var.
if (is_object($organism->type_id)) {
Expand All @@ -155,7 +187,7 @@ function chado_get_organism_scientific_name($organism) {
}
}
else {
$rank_term = chado_get_cvterm(['cvterm_id' => $organism->type_id]);
$rank_term = chado_get_cvterm(['cvterm_id' => $organism->type_id], [], $schema_name);
if ($rank_term) {
$rank = $rank_term->name;
}
Expand All @@ -182,13 +214,16 @@ function chado_get_organism_scientific_name($organism) {
* @param $show_common_name
* When true, include the organism common name, if present, in parentheses.
*
* @param string $schema_name
* The name of the schema to pull the variable from.
*
* @return
* An array of organisms where each value is the organism
* scientific name and the keys are organism_id's.
*
* @ingroup tripal_organism_api
*/
function chado_get_organism_select_options($published_only = FALSE, $show_common_name = FALSE) {
function chado_get_organism_select_options($published_only = FALSE, $show_common_name = FALSE, $schema_name = 'chado') {
$org_list = [];

if ($published_only) {
Expand All @@ -215,7 +250,7 @@ function chado_get_organism_select_options($published_only = FALSE, $show_common
// Include abbreviated infraspecific nomenclature in name when present,
// e.g. subspecies becomes subsp.
if ($org->infraspecific_type or $org->infraspecific_name) {
$org_list[$org->organism_id] = chado_get_organism_scientific_name($org);
$org_list[$org->organism_id] = chado_get_organism_scientific_name($org, $schema_name);
}
// Append common name when requested and when present.
if ($show_common_name and $org->common_name) {
Expand All @@ -234,7 +269,7 @@ function chado_get_organism_select_options($published_only = FALSE, $show_common
*
* @return
* If the type parameter is 'url' (the default) then the fully qualified
* url to the image is returend. If no image is present then NULL is returned.
* url to the image is returned. If no image is present then NULL is returned.
*
* @ingroup tripal_organism_api
*/
Expand Down Expand Up @@ -358,6 +393,9 @@ function chado_abbreviate_infraspecific_rank($rank) {
case 'subvariety':
$abb = 'subvar.';
break;
case 'cultivar':
$abb = 'cv.';
break;
case 'forma':
$abb = 'f.';
break;
Expand All @@ -369,3 +407,41 @@ function chado_abbreviate_infraspecific_rank($rank) {
}
return $abb;
}

/**
* A handy function to expand the infraspecific rank from an abbreviation.
*
* @param $rank
* The rank below species or its abbreviation.
* A period at the end of the abbreviation is optional.
*
* @return
* The proper unabbreviated form for the rank.
*
* @ingroup tripal_organism_api
*/
function chado_unabbreviate_infraspecific_rank($rank) {
if (preg_match('/^subsp\.?$/', $rank)) {
$rank = 'subspecies';
}
elseif (preg_match('/^ssp\.?$/', $rank)) {
$rank = 'subspecies';
}
elseif (preg_match('/^var\.?$/', $rank)) {
$rank = 'varietas';
}
elseif (preg_match('/^subvar\.?$/', $rank)) {
$rank = 'subvarietas';
}
elseif (preg_match('/^cv\.?$/', $rank)) {
$rank = 'cultivar';
}
elseif (preg_match('/^f\.?$/', $rank)) {
$rank = 'forma';
}
elseif (preg_match('/^subf\.?$/', $rank)) {
$rank = 'subforma';
}
// if none of the above matched, rank is returned unchanged
return $rank;
}
193 changes: 193 additions & 0 deletions tripal_chado/tests/src/Functional/api/ChadoOrganismAPITest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
<?php

namespace Drupal\Tests\tripal_chado;

use Drupal\Core\Url;
use Drupal\Tests\tripal_chado\Functional\ChadoTestBrowserBase;
use Drupal\Core\Database\Database;
use Drupal\tripal_chado\api\ChadoSchema;

/**
* Testing the tripal_chado/api/tripal_chado.organism.api.inc functions.
*
* @group Tripal
* @group Tripal Chado
* @group Tripal API
*/
class ChadoOrganismAPITest extends ChadoTestBrowserBase {

protected $defaultTheme = 'stable';

/**
* Modules to enable.
* @var array
*/
protected static $modules = ['tripal', 'tripal_chado'];

/**
* Schema to do testing out of.
* @var string
*/
protected $schemaName;

/**
* Tests the following organism API functions:
* chado_get_organism()
* chado_get_organism_scientific_name()
* chado_get_organism_select_options()
* chado_abbreviate_infraspecific_rank()
* chado_unabbreviate_infraspecific_rank()
* @to-do: The following API functions do not have tests yet:
* chado_get_organism_image_url($organism)
* chado_autocomplete_organism($text)
*
* @group tripal-chado
* @group chado-organism
*/
public function test_chado_organism_api_functions() {

putenv('TRIPAL_SUPPRESS_ERRORS=TRUE');

// Create a Test Chado schema which contains the expected cvterms added during the prepare step.
$connection = $this->createTestSchema(ChadoTestBrowserBase::PREPARE_TEST_CHADO);
$this->schemaName = $connection->getSchemaName();

// Lookup cvterm_id for 'subspecies'
$cvterm = chado_get_cvterm(['name' => 'subspecies'], [], $this->schemaName);
$this->assertNotNull($cvterm, 'Unable to retrieve cvterm for "subspecies" using chado_get_cvterm()');
$subspecies_id = $cvterm->cvterm_id;

// Create two test organisms of the same genus and species
$species = 'bogusii' . uniqid();
$organism_ids = [];
$org = [
'genus' => 'Tripalus',
'species' => $species,
'type_id' => $subspecies_id,
'infraspecific_name' => 'sativus',
'common_name' => 'False Tripal',
'abbreviation' => 'T. ' . $species . ' subsp. sativus',
];
$dbq = chado_insert_record('organism', $org, [], $this->schemaName);
$this->assertNotNull($dbq, 'Unable to insert test organism 1.');
$organism_ids[0] = $dbq['organism_id'];

$org['infraspecific_name'] = 'selvaticus';
$org['abbreviation'] = 'T. ' . $species . ' subsp. selvaticus';
$dbq = chado_insert_record('organism', $org, [], $this->schemaName);
$this->assertNotNull($dbq, 'Unable to insert test organism 2.');
$organism_ids[1] = $dbq['organism_id'];

// Test invalid $identifiers ($identifiers must be an array) = Should fail, and in fact causes an exception
$identifiers = 1; // not an array
try {
$org = chado_get_organism($identifiers, []);
}
catch (\Exception $e) {
$org = NULL;
}
$this->assertNull($org, 'Did not flag invalid $identifiers passed to chado_get_organism() (not an array)');

// Test using an empty array for $identifiers = Should fail
$identifiers = [];
$org = chado_get_organism($identifiers, [], $this->schemaName);
$this->assertNull($org, 'Did not flag invalid $identifiers passed to chado_get_organism() (empty array)');

// Test retrieving organism that does not exist = Should fail
$identifiers = ['genus' => 'Wrong', 'species' => 'incorrect'];
$org = chado_get_organism($identifiers, [], $this->schemaName);
$this->assertNull($org, 'Returned an organism from invalid $identifiers passed to chado_get_organism()');

// Get organism from organism_id = Should succeed
$identifiers = ['organism_id' => $organism_ids[0]];
$org = chado_get_organism($identifiers, [], $this->schemaName);
$this->assertIsObject($org, 'Did not return the organism with organism_id='
. $organism_ids[0] . ' using chado_get_organism()');

// Test ambiguous $identifiers = Should fail
$identifiers = ['genus' => 'Tripalus', 'species' => $species]; // subspecies not specified, thus ambiguous
$org = chado_get_organism($identifiers, [], $this->schemaName);
$this->assertNull($org, 'Returned an organism from ambiguous $identifiers passed to chado_get_organism()');

// Test unambiguous $identifiers = Should succeed
$identifiers = ['genus' => 'Tripalus', 'species' => $species, 'type_id' => $subspecies_id, 'infraspecific_name' => 'selvaticus'];
$org = chado_get_organism($identifiers, [], $this->schemaName);
$this->assertIsObject($org, 'Did not return an organism from unambiguous $identifiers passed to chado_get_organism()');

// Test getting scientific name = Should succeed
$name = chado_get_organism_scientific_name($org, $this->schemaName);
$expect = 'Tripalus ' . $species . ' subsp. selvaticus';
$this->assertEquals($name, $expect, 'Did not return the expected scientific name ' . $expect
. ' instead returned ' . $name . ' using chado_get_organism_scientific_name()');

// Test organism select options with default parameters, and test that an array is returned = should succeed
$select_options = chado_get_organism_select_options(FALSE, FALSE, $this->schemaName);
$this->assertIsArray($select_options, 'Did not return an array from chado_get_organism_select_options()');

// Test that the array contains at least the two test organisms = should succeed
$count = count($select_options);
$this->assertGreaterThanOrEqual(2, $count, 'Did not return at least two organisms from chado_get_organism_select_options()');

// Test that both of the test organisms are in the returned array = should succeed
$expect = 'Tripalus ' . $species . ' subsp. sativus';
$this->assertArrayHasKey($organism_ids[0], $select_options, 'Returned array does not contain the expected organism id '
. $organism_ids[0] . ' using chado_get_organism_select_options()');
$this->assertEquals($expect, $select_options[$organism_ids[0]], 'Array element 0 does not contain the expected organism text '
. $expect . ' using chado_get_organism_select_options()');
$expect = 'Tripalus ' . $species . ' subsp. selvaticus';
$this->assertArrayHasKey($organism_ids[1], $select_options, 'Returned array does not contain the expected organism id '
. $organism_ids[1] . ' using chado_get_organism_select_options()');
$this->assertEquals($expect, $select_options[$organism_ids[1]], 'Array element 1 does not contain the expected organism text '
. $expect . ' using chado_get_organism_select_options()');

// Data to test abbreviation of infraspecific rank
$expected_abbreviated = [
'no_rank' => '',
'subspecies' => 'subsp.',
'varietas' => 'var.',
'subvarietas' => 'subvar.',
'cultivar' => 'cv.',
'forma' => 'f.',
'subforma' => 'subf.',
'anything_else' => 'anything_else',
];

// Data to test unabbreviation of infraspecific rank
$expected_unabbreviated = [
'' => '',
'subsp' => 'subspecies',
'subsp.' => 'subspecies',
'ssp' => 'subspecies',
'ssp.' => 'subspecies',
'var' => 'varietas',
'var.' => 'varietas',
'subvar' => 'subvarietas',
'subvar.' => 'subvarietas',
'cv' => 'cultivar',
'cv.' => 'cultivar',
'f' => 'forma',
'f.' => 'forma',
'subf' => 'subforma',
'subf.' => 'subforma',
'anything_else' => 'anything_else',
'anything_else.' => 'anything_else.',
];

// Test abbreviation of infraspecific rank
foreach ($expected_abbreviated as $full => $abbreviation) {
$result = chado_abbreviate_infraspecific_rank($full);
$this->assertEqual($result, $abbreviation, 'Did not properly abbreviate ' . $full
. ' returned ' . $result . ' using chado_abbreviate_infraspecific_rank()');
}

// Test unabbreviation of infraspecific rank
foreach ($expected_unabbreviated as $abbreviation => $full) {
$result = chado_unabbreviate_infraspecific_rank($full);
$this->assertEqual($result, $full, 'Did not properly unabbreviate ' . $abbreviation
. ' returned ' . $result . ' using chado_unabbreviate_infraspecific_rank()');
}

putenv('TRIPAL_SUPPRESS_ERRORS=FALSE');
}

}

0 comments on commit a2f1172

Please sign in to comment.