Skip to content

Commit

Permalink
Merge pull request #61 from UofS-Pulse-Binfo/calls-samples
Browse files Browse the repository at this point in the history
Creates unit test for samples call
  • Loading branch information
laceysanderson authored Jun 27, 2024
2 parents be9c993 + 5b64525 commit 6e36918
Show file tree
Hide file tree
Showing 6 changed files with 286 additions and 113 deletions.
141 changes: 102 additions & 39 deletions calls/v1/samples/TripalWebServiceBrapiV1Samples.inc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class TripalWebServiceBrapiV1Samples extends TripalWebServiceDatabaseCall {
// Example: page => int, searchResultDbId => hash-code.
public $call_parameter = [
// Key : Expected value for this key.
'sampleDbId' => 'text', // The internal DB id for a sample.
'observationUnitDbId' => 'text', // The internal DB id for an observation unit where a sample was taken from.
'plateDbId' => 'text', // The internal DB id for a plate of samples
'germplasmDbId' => 'int', // Filter by location.
'page' => 'int', // Which result page is requested.
'pageSize' => 'int', // The size of the pages to be returned.
Expand Down Expand Up @@ -78,23 +81,27 @@ class TripalWebServiceBrapiV1Samples extends TripalWebServiceDatabaseCall {
$filter = [];
$args = [];

// Terms used in stockprop.
// Terms used to query:
$arr_terms = [];
$req_terms = [
'source_institute',
'comment',
'stock_property' => ['comment'],
'CO_324' => ['Plant Number', 'Plot Number'],
'NCIT' => ['Plate Device', 'Tissue', 'Index Value', 'Computer Time Stamp', 'Collected By']
];

$cv_name = 'stock_property';
$cv_id = chado_get_cv(array('name' => $cv_name))
->cv_id;
// Fetch cvterm id of each term and make it available in the query using
// [cv][cvterm name] combination.
foreach($req_terms as $cv => $terms) {
foreach($terms as $term) {
$cv_id = chado_get_cv(array('name' => $cv))
->cv_id;

foreach($req_terms as $term) {
$cvterm_id = chado_get_cvterm(['name' => $term, 'cv_id' => $cv_id])
->cvterm_id;
$cvterm_id = chado_get_cvterm(['name' => $term, 'cv_id' => $cv_id])
->cvterm_id;

if ($cvterm_id) {
$arr_terms[ $term ] = $cvterm_id;
if ($cvterm_id) {
$arr_terms[ $cv ][ $term ] = $cvterm_id;
}
}
}

Expand All @@ -107,11 +114,36 @@ class TripalWebServiceBrapiV1Samples extends TripalWebServiceDatabaseCall {
// page.
$offset = $call_asset['parameter']['page'] * $page_size;


// sampleDbId.
if (isset($call_asset['parameter']['sampleDbId'])
&& !empty($call_asset['parameter']['sampleDbId'])) {

$filter[] = 'germplasm.uniquename = :sampleDbId';
$args[':sampleDbId'] = $call_asset['parameter']['sampleDbId'];
}

// observationUnitDbId.
if (isset($call_asset['parameter']['observationUnitDbId'])
&& !empty($call_asset['parameter']['observationUnitDbId'])) {

$filter[] = 'phenotypes.unit_id::varchar = :observationUnitDbId';
$args[':observationUnitDbId'] = $call_asset['parameter']['observationUnitDbId'];
}

// plateDbId.
if (isset($call_asset['parameter']['plateDbId'])
&& !empty($call_asset['parameter']['plateDbId'])) {

$filter[] = 'plt.value = :plateDbId';
$args[':plateDbId'] = $call_asset['parameter']['plateDbId'];
}

// germplasmDbId
if (isset($call_asset['parameter']['germplasmDbId'])
&& !empty($call_asset['parameter']['germplasmDbId'])) {

$filter[] = 'sample.stock_id = :germplasmDbId';
$filter[] = 'germplasm.stock_id = :germplasmDbId';
$args[':germplasmDbId'] = $call_asset['parameter']['germplasmDbId'];
}

Expand All @@ -121,47 +153,78 @@ class TripalWebServiceBrapiV1Samples extends TripalWebServiceDatabaseCall {
$where = ' WHERE ' . implode(' AND ', $filter);
}


// Build query.
// Phenotypes specific fields may retrun null
// when not applicable (samples not of type DNA).
// In this case, n/a instead of null is set as the value.
$sql = "
SELECT
sample.stock_id AS germplasmdbid,
notes.value AS notes,
CONCAT(cv.name, ':', accession.accession) AS observationunitdbid,
'n/a' AS plantdbid,
'n/a' AS platedbid,
'n/a' AS plateindex,
'n/a' AS plotdbid,
sample.uniquename AS sampledbid,
'n/a' AS sampletimestamp,
germplasm.stock_id AS germplasmdbid,
nts.value AS notes,
COALESCE(phenotypes.unit_id::varchar, 'n/a') AS observationunitdbid,
COALESCE(phenotypes.plantdbid::varchar, 'n/a') AS plantdbid,
plt.value AS platedbid,
ind.value AS plateindex,
COALESCE(phenotypes.plotdbid::varchar, 'n/a') AS plotdbid,
germplasm.uniquename AS sampledbid,
COALESCE(phenotypes.sampletimestamp, 'n/a') AS sampletimestamp,
type.name AS sampletype,
'n/a' AS studydbid,
takenby.value AS takenby,
'n/a' AS tissuetype,
COUNT(sample.stock_id) OVER() AS full_count
FROM {%s} AS sample
LEFT JOIN {cvterm} AS type ON sample.type_id = type.cvterm_id
LEFT JOIN {stockprop} AS takenby ON sample.stock_id = takenby.stock_id AND takenby.type_id = %d
LEFT JOIN {stockprop} AS notes ON sample.stock_id = notes.stock_id AND notes.type_id = %d
RIGHT JOIN {phenotype} AS pheno ON sample.stock_id = pheno.stock_id
LEFT JOIN {cvterm} AS unit ON pheno.unit_id = unit.cvterm_id
LEFT JOIN {cv} AS cv ON unit.cv_id = cv.cv_id
LEFT JOIN {dbxref} AS accession ON unit.dbxref_id = accession.dbxref_id
COALESCE(phenotypes.studydbid::varchar, 'n/a') AS studydbid,
COALESCE(phenotypes.takenby, 'n/a') AS takenby,
tis.value AS tissuetype,
COUNT(germplasm.stock_id) OVER() AS full_count
FROM {%s} AS germplasm
LEFT JOIN {cvterm} AS type ON germplasm.type_id = type.cvterm_id
LEFT JOIN {stockprop} AS nts ON germplasm.stock_id = nts.stock_id AND nts.type_id = %d
LEFT JOIN {stockprop} AS plt ON germplasm.stock_id = plt.stock_id AND plt.type_id = %d
LEFT JOIN {stockprop} AS ind ON germplasm.stock_id = ind.stock_id AND ind.type_id = %d
LEFT JOIN {stockprop} AS tis ON germplasm.stock_id = tis.stock_id AND tis.type_id = %d
LEFT JOIN (
SELECT
pheno.stock_id,
pheno.unit_id AS unit_id,
plant.value AS plantdbid,
plot.value AS plotdbid,
time.value AS sampletimestamp,
take.value AS takenby,
study.project_id AS studydbid
FROM
{phenotype} AS pheno
LEFT JOIN {phenotypeprop} AS plant ON pheno.phenotype_id = plant.phenotype_id AND plant.type_id = %d
LEFT JOIN {phenotypeprop} AS plot ON pheno.phenotype_id = plot.phenotype_id AND plot.type_id = %d
LEFT JOIN {phenotypeprop} AS time ON pheno.phenotype_id = time.phenotype_id AND time.type_id = %d
LEFT JOIN {phenotypeprop} AS take ON pheno.phenotype_id = take.phenotype_id AND take.type_id = %d
LEFT JOIN {project} AS study ON pheno.project_id = study.project_id
WHERE
pheno.stock_id IN (
SELECT stock_id
FROM {stock} AS s INNER JOIN {cvterm} AS c ON s.type_id = c.cvterm_id
WHERE c.name = 'DNA'
)
) AS phenotypes ON phenotypes.stock_id = germplasm.stock_id
%s
ORDER BY sample.name ASC
GROUP BY
germplasm.stock_id, type.name, nts.value, plt.value, ind.value, tis.value, phenotypes.unit_id,
germplasm.uniquename, phenotypes.plantdbid, phenotypes.plotdbid, phenotypes.studydbid,
phenotypes.sampletimestamp, phenotypes.takenby
ORDER BY type.name ASC, germplasm.name ASC
LIMIT %d OFFSET %d
";

$query = sprintf($sql, $this->class_name::$chado_table,
$arr_terms['source_institute'], $arr_terms['comment'],
$where, $page_size, $offset);
$query = sprintf($sql, $this->class_name::$chado_table, $arr_terms['stock_property']['comment'],
$arr_terms['NCIT']['Plate Device'], $arr_terms['NCIT']['Index Value'],
$arr_terms['NCIT']['Tissue'], $arr_terms['CO_324']['Plant Number'],
$arr_terms['CO_324']['Plot Number'], $arr_terms['NCIT']['Computer Time Stamp'],
$arr_terms['NCIT']['Collected By'], $where, $page_size, $offset);

$result = chado_query($query, $args);

$samples = [];

// Additional steps to apply to response field.
if ($result->rowCount() > 0) {
foreach($result as $sample) {
$sample->plateindex = (int) $sample->plateindex;

array_push($samples, $sample);
}
}
Expand Down
105 changes: 105 additions & 0 deletions tests/calls-v.13/samplesCallTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
namespace Tests\calls;

use StatonLab\TripalTestSuite\TripalTestCase;
use Tests\GenericHttpTestCase;

/**
* Samples Call (Version 1.3)
* https://brapi.docs.apiary.io/#reference/samples/samples/get-samples
*/
class samplesCallTest extends GenericHttpTestCase {

/**
* The short name of the call for Assestion messages.
*/
public $callname = 'samples';

/**
* The URL for the call for requests.
* For example: web-services/brapi/v1/people
*/
public $url = 'web-services/brapi/v1/samples';

/**
* The structure of the data result.
* Each entry should be key => valuetype | array.
* Valid valuetypes are: string, integer, object, array.
*/
public $data_structure = [
'germplasmDbId' => 'string',
'notes' => 'string',
'observationUnitDbId' => 'string',
'plantDbId' => 'string',
'plateDbId' => 'string',
'plateIndex' => 'integer',
'plotDbId' => 'string',
'sampleDbId' => 'string',
'sampleTimestamp'=> 'string',
'sampleType' => 'string',
'studyDbId' => 'string',
'takenBy' => 'string',
'tissueType'=> 'string'

];

/**
* CORE TEST.
*/
public function testCall() {

//---------------------------------
// 1. NO PARAMETERS.
$response = NULL;
$this->assertWithNoParameters($response);
$numOfResults = sizeof($response->result->data);

//---------------------------------
// 2. WITH PARAMETERS: pageSize
$response = NULL;
$this->assertPageSize($response, $numOfResults);
$page1_results = $response->result->data;

//---------------------------------
// 3. WITH PARAMETERS: page, pageSize
$response = NULL;
$this->assertPaging($response, $numOfResults, $page1_results);

//---------------------------------
// 4. WITH PARAMETERS: sampleDbId
$response = NULL;
$this->assertWithParameter($response, [
'name' => 'sampleDbId', // Parameter name.
'key' => 'sampleDbId', // The response key the parameter name will match.
'value' => '', // Pick one.
], TRUE);

//---------------------------------
// 5. WITH PARAMETERS: observationUnitDbId
$unit = $this->translateValue('cvterm', 'cvterm_id', ['name' => 'Unit C']);
$response = NULL;
$this->assertWithParameter($response, [
'name' => 'observationUnitDbId', // Parameter name.
'key' => 'observationUnitDbId', // The response key the parameter name will match.
'value' => $unit, // Parameter value.
]);

//---------------------------------
// 6. WITH PARAMETERS: plateDbId
$response = NULL;
$this->assertWithParameter($response, [
'name' => 'plateDbId', // Parameter name.
'key' => 'plateDbId', // The response key the parameter name will match.
'value' => 'PL1', // Parameter value.
]);

//---------------------------------
// 7. WITH PARAMETERS: germplasmDbId
$response = NULL;
$this->assertWithParameter($response, [
'name' => 'germplasmDbId', // Parameter name.
'key' => 'germplasmDbId', // The response key the parameter name will match.
'value' => '', // Pick one.
], TRUE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ uniquename38_2019-03-26T17:11:26-06:00_38, "Trait PQR", 6, "Method 2", "Project
uniquename39_2019-03-26T17:11:26-06:00_39, "Trait PQR", 6, "Method 2", "Project E", "emma", "Unit C", Adaptation, cvalue
uniquename40_2019-03-26T17:11:26-06:00_40, "Trait STU", 7, "Method 1", "Project F", "ava", "Unit A", Adaptation, cvalue
uniquename41_2019-03-26T17:11:26-06:00_41, "Trait STU", 8, "Method 1", "Project F", "mia", "Unit A", Adaptation, cvalue
uniquename42_2019-03-26T17:11:26-06:00_42, "Trait STU", 6, "Method 1", "Project F", "aria", "Unit A", Adaptation, cvalue
uniquename42_2019-03-26T17:11:26-06:00_42, "Trait STU", 6, "Method 1", "Project F", "nala", "Unit A", Adaptation, cvalue
uniquename43_2019-03-26T17:11:26-06:00_43, "Trait VWX", 9, "Method 3", "Project G", "mila", "Unit B", Adaptation, cvalue
uniquename44_2019-03-26T17:11:26-06:00_44, "Trait VWX", 8, "Method 3", "Project G", "mila", "Unit B", Adaptation, cvalue
uniquename45_2019-03-26T17:11:26-06:00_45, "Trait VWX", 6, "Method 3", "Project G", "mila", "Unit B", Adaptation, cvalue
Expand Down
30 changes: 15 additions & 15 deletions tripal_ws_brapi_testdata/includes/raw-data/stock/stock-csv.csv
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name, uniquename, description, translate_term:type_id, translate_value:organism_id
lola, "Uniquename lola", "Aliquam nec sem eu nulla maximus efficitur", accession, "Cultivated Tripal"
stella, "Uniquename stella", "Lorem ipsum dolor sit amet, consectetur adipiscing elit", accession, "Cultivated Tripal"
lola, "Uniquename lola", "Aliquam nec sem eu nulla maximus efficitur", DNA, "Cultivated Tripal"
stella, "Uniquename stella", "Lorem ipsum dolor sit amet, consectetur adipiscing elit", DNA, "Cultivated Tripal"
luna, "Uniquename luna", "Mauris cursus, libero eget volutpat sagittis", accession, "Cultivated Tripal"
nala, "Uniquename nala", "Dui diam semper lacus, et hendrerit eros neque eget eros", accession, "Cultivated Tripal"
izzy, "Uniquename izzy", "Fusce cursus nisl ac iaculis tristique. Suspendisse commodo auctor egestas", accession, "Cultivated Tripal"
izzy, "Uniquename izzy", "Fusce cursus nisl ac iaculis tristique. Suspendisse commodo auctor egestas", DNA, "Cultivated Tripal"
layla, "Uniquename layla", "Etiam condimentum mollis erat semper placerat", accession, "Cultivated Tripal"
lulu, "Uniquename lulu", "Sed erat mattis, ac tristique tortor malesuada", accession, "Cultivated Tripal"
lulu, "Uniquename lulu", "Sed erat mattis, ac tristique tortor malesuada", DNA, "Cultivated Tripal"
ellie, "Uniquename ellie", "Aenean elementum blandit magna, at euismod augue aliquet quis", accession, "Cultivated Tripal"
piper, "Uniquename piper", "Cras efficitur venenatis purus maximus tristique", accession, "Cultivated Tripal"
olivia,"Uniquename olivia", "Suspendisse scelerisque dui arcu, at ullamcorper leo ullamcorper vel", accession, "Cultivated Tripal"
Expand All @@ -15,10 +15,10 @@ owen, "Uniquename owen", "Aliquam nec sem eu nulla maximus efficitur", accession
ray, "Uniquename ray", "Integer accumsan ex libero, non placerat purus dapibus eget", accession, "Cultivated Tripal"
ralph, "Uniquename ralph", "Ut eu dui maximus, consequat mauris at, vehicula metus", accession, "Wild Tripal"
rex, "Uniquename rex", "Maecenas fermentum justo scelerisque posuere gravida", accession, "Wild Tripal"
scout, "Uniquename scout", "Ut eu dui maximus, consequat mauris at, vehicula metus", accession, "Wild Tripal"
scout, "Uniquename scout", "Ut eu dui maximus, consequat mauris at, vehicula metus", DNA, "Wild Tripal"
tesla, "Uniquename tesla", "Orci varius natoque penatibus et magnis dis parturient", "generated germplasm", "Wild Tripal"
wyatt, "Uniquename wyatt", "Proin eu laoreet est. Integer eget tincidunt ipsum. Vestibulum lacinia commodo lectus", "generated germplasm", "Wild Tripal"
ace, "Uniquename ace", "Nulla pharetra purus tortor, sed laoreet turpis lobortis non", "generated germplasm", "Wild Tripal"
ace, "Uniquename ace", "Nulla pharetra purus tortor, sed laoreet turpis lobortis non", DNA, "Wild Tripal"
bailey,"Uniquename bailey", "Ut eu dui maximus, consequat mauris at, vehicula metus", "generated germplasm", "Wild Tripal"
bennet, "Uniquename bennet", "Morbi dignissim leo lacinia interdum tempus", "generated germplasm", "Wild Tripal"
chase, "Uniquename chase", "Aultricies justo, nec tempor dui. Nulla pharetra purus tortor", "generated germplasm", "Wild Tripal"
Expand All @@ -27,17 +27,17 @@ emmet, "Uniquename emmet", "Aenean elementum blandit magna, at euismod augue ali
felix, "Uniquename felix", "Cras efficitur venenatis purus maximus tristique", "generated germplasm", "Wild Tripal"
forrest, "Uniquename forrest", "Aliquam erat volutpat. Fusce mollis, sapien vel scelerisque", "generated germplasm", "Wild Tripal"
hudson, "Uniquename hudson", "Donec a sollicitudin sapien. In pulvinar semper augue", cultivar, "Wild Tripal"
jack, "Uniquename jack", "Duis dictum suscipit nibh, sodales posuere", cultivar, "Tripal WS"
emma, "Uniquename emma", "Maecenas fermentum justo scelerisque posuere gravida", cultivar, "Tripal WS"
aria, "Uniquename aria", "Nam lectus tortor, tempus eget tempus vel, ornare a libero", cultivar, "Tripal WS"
ava, "Uniquename ava", "Orci varius natoque penatibus et magnis dis parturient monte", cultivar, "Tripal WS"
mia, "Uniquename mia", "Fermentum iaculis, elit mauris ultricies lacus, vel eleifend.", cultivar, "Tripal WS"
mila, "Uniquename mila", "Maecenas fermentum justo scelerisque posuere gravida", cultivar, "Tripal WS"
jack, "Uniquename jack", "Duis dictum suscipit nibh, sodales posuere", DNA, "Tripal WS"
emma, "Uniquename emma", "Maecenas fermentum justo scelerisque posuere gravida", DNA, "Tripal WS"
aria, "Uniquename aria", "Nam lectus tortor, tempus eget tempus vel, ornare a libero", DNA, "Tripal WS"
ava, "Uniquename ava", "Orci varius natoque penatibus et magnis dis parturient monte", DNA, "Tripal WS"
mia, "Uniquename mia", "Fermentum iaculis, elit mauris ultricies lacus, vel eleifend.", DNA, "Tripal WS"
mila, "Uniquename mila", "Maecenas fermentum justo scelerisque posuere gravida", DNA, "Tripal WS"
chloe, "Uniquename chloe", "Quis bibendum diam varius et. Etiam condimentum mollis erat semper placerat", cultivar, "Tripal WS"
zoe, "Uniquename zoe", "Aenean eleifend tortor quis arcu auctor, at placerat nunc suscipit", cultivar, "Tripal WS"
zoe, "Uniquename zoe", "Aenean eleifend tortor quis arcu auctor, at placerat nunc suscipit", DNA, "Tripal WS"
abigail, "Uniquename abigail", "Nulla tincidunt eleifend ex, et egestas nunc malesuada sed", cultivar, "Tripal WS"
ariana, "Uniquename ariana", "Nam scelerisque vel arcu in vulputate. Orci varius natoque", cultivar, "Cultivated BrAPI"
elena, "Uniquename elena", "In pulvinar semper augue, fringilla fringilla massa imperdiet at", cultivar, "Cultivated BrAPI"
ariana, "Uniquename ariana", "Nam scelerisque vel arcu in vulputate. Orci varius natoque", DNA, "Cultivated BrAPI"
elena, "Uniquename elena", "In pulvinar semper augue, fringilla fringilla massa imperdiet at", DNA, "Cultivated BrAPI"
eva, "Uniquename eva", "Ut eu dui maximus, consequat mauris at, vehicula metus", cultivar, "Cultivated BrAPI"
sarah, "Uniquename sarah", "Curabitur pellentesque volutpat lorem eget aliquet. Sed consectetur", cultivar, "Cultivated BrAPI"
naomi, "Uniquename naomi", "Asapien pretium tempus. Maecenas fermentum justo scelerisque", "414 inbred line", "Cultivated BrAPI"
Expand Down
Loading

0 comments on commit 6e36918

Please sign in to comment.