From 81d734cb09307b6a503bc61ef059bd966e1f7366 Mon Sep 17 00:00:00 2001 From: bradfordcondon Date: Wed, 27 Feb 2019 12:06:00 -0500 Subject: [PATCH] switch all select objects to chado select objects --- .../repositories/EUtilsAssemblyRepository.inc | 43 ++++++++----- .../EUtilsBioProjectRepository.inc | 28 +++++---- .../EUtilsBioSampleRepository.inc | 16 +++-- .../repositories/EUtilsPubmedRepository.inc | 11 +++- includes/repositories/EUtilsRepository.inc | 60 ++++++++++++------- tests/DataFactory.php | 9 +++ tests/EUtilsTest.php | 23 +++++++ 7 files changed, 130 insertions(+), 60 deletions(-) diff --git a/includes/repositories/EUtilsAssemblyRepository.inc b/includes/repositories/EUtilsAssemblyRepository.inc index 1c754a0..c56053c 100644 --- a/includes/repositories/EUtilsAssemblyRepository.inc +++ b/includes/repositories/EUtilsAssemblyRepository.inc @@ -202,20 +202,21 @@ class EUtilsAssemblyRepository extends EUtilsRepository { throw new Exception('The organism_analysis linker table doesnt exist. No way to link this organism.'); } - $result = db_select('chado.organism_analysis', 't') - ->fields('t', ['organism_analysis_id']) + $query = chado_db_select('organism_analysis', 't'); + $result = $query->fields('t', ['organism_analysis_id']) ->condition('t.organism_id', $organism->organism_id) ->condition('t.analysis_id', $this->base_record_id) ->execute() ->fetchField(); if (!$result) { - $result = db_insert('chado.organism_analysis') - ->fields([ - 'organism_id' => $organism->organism_id, - 'analysis_id' => $this->base_record_id, - ]) - ->execute(); + $values = [ + 'organism_id' => $organism->organism_id, + 'analysis_id' => $this->base_record_id, + ]; + + $result = chado_insert_record('organism_analysis', $values); + } if (!$result) { @@ -238,7 +239,7 @@ class EUtilsAssemblyRepository extends EUtilsRepository { $base = $this->base_fields; - $id = db_insert('chado.analysis')->fields([ + $values = [ 'name' => $base['name'], 'description' => $base['description'] ?? '', 'program' => $base['program'], @@ -248,14 +249,17 @@ class EUtilsAssemblyRepository extends EUtilsRepository { 'sourceversion' => $base['sourceversion'] ?? '', 'sourceuri' => $base['sourceuri'] ?? '', 'timeexecuted' => $base['timeexecuted'] ?? date_now(), - ])->execute(); + ]; + $record = chado_insert_record('analysis', $values); + + $id = $record['analysis_id']; if (!$id) { throw new Exception('Unable to create chado.analysis record'); } - $analysis = db_select('chado.analysis', 't') - ->fields('t') + $query = chado_db_select('analysis', 't'); + $analysis = $query->fields('t') ->condition('analysis_id', $id) ->execute() ->fetchObject(); @@ -278,8 +282,8 @@ class EUtilsAssemblyRepository extends EUtilsRepository { return static::$cache['analysis']; } - $exists = db_select('chado.analysis', 't') - ->fields('t') + $query = chado_db_select('analysis', 't'); + $exists = $query->fields('t') ->condition('name', $base['name']) ->condition('program', $base['program']) ->condition('programversion', $base['programversion']) @@ -298,11 +302,13 @@ class EUtilsAssemblyRepository extends EUtilsRepository { /** * Associates FTPs as properties. * - * @param $ftps + * @param array $ftps * Array of key value pars, where the key is the XML FTP type, the value is * the FTP address. + * + * @throws \Exception */ - public function addFTPLinks($ftps) { + public function addFTPLinks(array $ftps) { $cvterm_id = chado_get_cvterm(['id' => 'local:ncbi_FTP_links'])->cvterm_id; foreach ($ftps as $type => $ftp) { @@ -318,6 +324,11 @@ class EUtilsAssemblyRepository extends EUtilsRepository { * The type/category passed from the parser. * * @return bool + * Output of chado_insert_property. + * + * @see createProperty + * + * @throws \Exception */ private function setAnalysisType(string $type) { diff --git a/includes/repositories/EUtilsBioProjectRepository.inc b/includes/repositories/EUtilsBioProjectRepository.inc index c6ea5e3..a69ab4b 100644 --- a/includes/repositories/EUtilsBioProjectRepository.inc +++ b/includes/repositories/EUtilsBioProjectRepository.inc @@ -77,7 +77,6 @@ class EUtilsBioProjectRepository extends EUtilsRepository { $this->createPubs($pubs); } - return $project; } @@ -108,8 +107,8 @@ class EUtilsBioProjectRepository extends EUtilsRepository { throw new Exception('Unable to create chado.project record'); } - $project = db_select('chado.project', 't') - ->fields('t') + $query = chado_db_select('project', 't'); + $project = $query->fields('t') ->condition('project_id', $id) ->execute() ->fetchObject(); @@ -121,8 +120,10 @@ class EUtilsBioProjectRepository extends EUtilsRepository { * Get project from db or cache. * * @param string $name + * Chado project name field. * * @return null + * Returns a project object or NULL. */ public function getProject($name) { // If the project is available in our static cache, return it. @@ -131,8 +132,8 @@ class EUtilsBioProjectRepository extends EUtilsRepository { } // Find the project and add it to the cache. - $project = db_select('chado.project', 'p') - ->fields('p') + $query = chado_db_select('project', 't'); + $project = $query->fields('t') ->condition('name', $name) ->execute() ->fetchObject(); @@ -184,7 +185,8 @@ class EUtilsBioProjectRepository extends EUtilsRepository { foreach ($accessions as $db => $accession) { try { $data[] = $this->createAccession(['db' => $db, 'value' => $accession]); - } catch (Exception $exception) { + } + catch (Exception $exception) { // For the time being, ignore all exceptions. } } @@ -227,7 +229,7 @@ class EUtilsBioProjectRepository extends EUtilsRepository { public function linkBiomaterial($record) { $biomaterial_id = $record->biomaterial_id; - db_insert('chado.biomaterial_project')->fields([ + chado_insert_record('biomaterial_project', [ 'biomaterial_id' => $biomaterial_id, 'project_id' => $this->base_record_id, ]); @@ -238,13 +240,13 @@ class EUtilsBioProjectRepository extends EUtilsRepository { * Links an analysis to the base project record. * * @param $record - * Assembly record object. + * Assembly/analysis record object. */ private function linkAssembly($record) { $analysis_id = $record->analysis_id; - $exists = db_select('chado.project_analysis', 't') - ->fields('t') + $query = chado_db_select('project_analysis', 't'); + $exists = $query->fields('t') ->condition('project_id', $this->base_record_id) ->condition('analysis_id', $analysis_id) ->execute() @@ -278,8 +280,10 @@ class EUtilsBioProjectRepository extends EUtilsRepository { 'project_id' => $this->base_record_id, 'pub_id' => $pub->pub_id, ]; - $exists = db_select('chado.project_pub', 't') - ->fields('t') + + $query = chado_db_select('project_pub', 't'); + + $exists = $query->fields('t') ->condition('project_id', $this->base_record_id) ->condition('pub_id', $pub->pub_id) ->execute() diff --git a/includes/repositories/EUtilsBioSampleRepository.inc b/includes/repositories/EUtilsBioSampleRepository.inc index ea587f0..8d60001 100644 --- a/includes/repositories/EUtilsBioSampleRepository.inc +++ b/includes/repositories/EUtilsBioSampleRepository.inc @@ -134,7 +134,9 @@ class EUtilsBioSampleRepository extends EUtilsRepository { throw new Exception('Unable to create chado.biomaterial record'); } - $biosample = db_select('chado.biomaterial', 'B')->fields('B')->condition( + $query = chado_db_select('biomaterial', 'B'); + + $biosample = $query->fields('B')->condition( 'biomaterial_id', $id )->execute()->fetchObject(); @@ -145,8 +147,10 @@ class EUtilsBioSampleRepository extends EUtilsRepository { * Get biosample from db or cache. * * @param string $name + * Name for the name field of biomaterial table. * - * @return null + * @return mixed + * The Chado biosample record. */ public function getBioSample($name) { // If the biosample is available in our static cache, return it. @@ -155,8 +159,10 @@ class EUtilsBioSampleRepository extends EUtilsRepository { } // Find the biosample and add it to the cache. - $biosample = db_select('chado.biomaterial', 'b') - ->fields('b') + $query = chado_db_select('biomaterial', 'b'); + + // Name is in fact a unique key. + $biosample = $query->fields('b') ->condition('name', $name) ->execute() ->fetchObject(); @@ -164,8 +170,6 @@ class EUtilsBioSampleRepository extends EUtilsRepository { if ($biosample) { return static::$cache['biosamples'][$name] = $biosample; } - - return NULL; } /** diff --git a/includes/repositories/EUtilsPubmedRepository.inc b/includes/repositories/EUtilsPubmedRepository.inc index 7270e68..8f54517 100644 --- a/includes/repositories/EUtilsPubmedRepository.inc +++ b/includes/repositories/EUtilsPubmedRepository.inc @@ -17,6 +17,11 @@ class EUtilsPubmedRepository extends EUtilsRepository { 'description', ]; + /** + * Required base table attribute. + * + * @var string + */ protected $base_table = 'pub'; /** @@ -27,14 +32,14 @@ class EUtilsPubmedRepository extends EUtilsRepository { * * @return pub * A Chado publication record object. - **/ + */ public function create(array $data) { module_load_include('inc', 'tripal_chado', '/includes/loaders/tripal_chado.pub_importers'); tripal_pub_add_publications([$data], FALSE); $uname = $data['Citation']; - $pub = db_select('chado.pub', 'p')->fields('p')->condition('p.uniquename', $uname)->execute()->fetchObject(); - + $connection = chado_db_select('pub', 'p'); + $pub = $connection->fields('p')->condition('p.uniquename', $uname)->execute()->fetchObject(); return $pub; } diff --git a/includes/repositories/EUtilsRepository.inc b/includes/repositories/EUtilsRepository.inc index e5209fc..02413e7 100644 --- a/includes/repositories/EUtilsRepository.inc +++ b/includes/repositories/EUtilsRepository.inc @@ -110,11 +110,15 @@ abstract class EUtilsRepository { * Get accession by dbxref id. * * @param int $id + * The Chado dbxref id (dbxref.dbxref_id). * * @return mixed + * The Chado dbxref object. */ public function getAccessionByID($id) { - return db_select('chado.dbxref', 'd')->fields('d')->condition( + + $query = chado_db_select('dbxref', 'd'); + return $query->fields('d')->condition( 'dbxref_id', $id )->execute()->fetchObject(); } @@ -138,8 +142,9 @@ abstract class EUtilsRepository { return static::$cache['accessions'][$name]; } - $accession = db_select('chado.dbxref', 'd') - ->fields('d') + $query = chado_db_select('dbxref', 'd'); + + $accession = $query->fields('d') ->condition('accession', $name) ->condition('db_id', $db_id) ->execute() @@ -311,39 +316,44 @@ abstract class EUtilsRepository { /** * Get contact name. * - * @param static $contact_name + * @param string $contact_name * The contact name. * * @return mixed - * contact record + * contact record. * * @throws \Exception */ - public function createContact($contact_name) { + public function createContact(string $contact_name) { if (isset(static::$cache['contacts']) && static::$cache['contacts'][$contact_name]) { $contact = static::$cache['contacts'][$contact_name]; } else { - $contact = db_select('chado.contact', 'C')->fields('C')->condition( + $query = chado_db_select('contact', 'C'); + $contact = $query->fields('C')->condition( 'name', $contact_name )->execute()->fetchObject(); if (empty($contact)) { - $contact_id = db_insert('chado.contact')->fields( - [ - 'name' => $contact_name, - ] - )->execute(); + + $values = [ + 'name' => $contact_name, + 'description' => '', + ]; + $contact = chado_insert_record('contact', $values); + $contact_id = $contact['contact_id']; if (!$contact_id) { throw new Exception( 'Unable to create a contact for ' . $contact_name ); } - - $contact = db_select('chado.contact', 'C')->fields('C')->condition( + // Query again because we get an array instead of an object. + $query = chado_db_select('contact', 'C'); + $contact = $query->fields('C')->condition( 'contact_id', $contact_id )->execute()->fetchObject(); + } static::$cache['contacts'][$contact_name] = $contact; @@ -356,6 +366,7 @@ abstract class EUtilsRepository { * Set the Chado record id. * * @param int $id + * The Chado base record ID. * * @return $this */ @@ -437,7 +448,7 @@ abstract class EUtilsRepository { } /** - * Generate query to getch an organism. + * Generate query to fetch an organism. * * Query to check if an organism exists in the DB based on the NCBITaxon * accession. @@ -451,6 +462,7 @@ abstract class EUtilsRepository { private function organismQuery($accession) { $db = chado_get_db(['name' => 'NCBITaxon']); + // TODO: cant use chado_db_select here because of the joins. $query = db_select('chado.organism_dbxref', 'od'); $query->join('chado.organism', 'o', 'o.organism_id = od.organism_id'); $query->fields('o'); @@ -547,7 +559,7 @@ abstract class EUtilsRepository { * @param array $projects * Array of base chado record project objects. */ - public function linkProjects($projects) { + public function linkProjects(array $projects) { $base_record = $this->base_record_id; @@ -556,17 +568,19 @@ abstract class EUtilsRepository { $table = 'project_' . $base_table; foreach ($projects as $project) { - $exists = db_select('chado.' . $table, 'lt')->fields('lt')->condition( + $query = chado_db_select($table, 'lt'); + + $exists = $query->fields('lt')->condition( 'project_id', $project->project_id )->condition($base_table . '_id', $base_record)->execute()->fetchObject(); if (!$exists) { - db_insert('chado.' . $table)->fields( - [ - 'project_id' => $project->project_id, - $base_table . '_id' => $base_record, - ] - )->execute(); + $values = [ + 'project_id' => $project->project_id, + $base_table . '_id' => $base_record, + ]; + chado_insert_record($table, $values); + } } } diff --git a/tests/DataFactory.php b/tests/DataFactory.php index 026a7ca..9eee095 100644 --- a/tests/DataFactory.php +++ b/tests/DataFactory.php @@ -188,3 +188,12 @@ 'description' => $faker->text, ]; }); + +Factory::define('chado.pub', function (Faker\Generator $faker) { + return [ + + 'title' => $faker->word, + 'uniquename' => $faker->unique()->word, + 'type_id' => factory('chado.cvterm')->create()->cvterm_id + ]; +}); \ No newline at end of file diff --git a/tests/EUtilsTest.php b/tests/EUtilsTest.php index 6338e77..0bde561 100644 --- a/tests/EUtilsTest.php +++ b/tests/EUtilsTest.php @@ -49,4 +49,27 @@ public function accessionDataProvider() { ]; } +// +// /** +// * @group orange +// */ +// public function testChadoInsertRecordCanJoin(){ +// +// $pub = factory('chado.pub')->create(); +// $analysis = factory('chado.analysis')->create(); +// +// chado_insert_record('analysis_pub', ['pub_id' => $pub->pub_id, 'analysis_id' => $analysis->analysis_id]); +// $connection = chado_db_select('pub', 'p'); +// $connection->join('{analysis_pub}', 'ap', 'ap.pub_id = p.pub_id'); +// $connection->fields('p'); +// $connection->condition('ap.pub_id', $analysis->analysis); +// $result = $connection->execute()->fetchObject(); +// +// $this->assertNotFalse($result); +// +// var_dump($result); +// +// $this->assertEquals($result->pub_id, $pub->pub_id); +// +// } }