1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458 |
- <?php
- class OBOImporter extends TripalImporter {
-
-
-
-
-
-
-
-
- public static $name = 'OBO Vocabulary Loader';
-
- public static $machine_name = 'chado_obo_loader';
-
- public static $description = 'Import vocabularies and terms in OBO format.';
-
- public static $file_types = array('obo');
-
- public static $upload_description = 'Please provide the details for importing a new OBO file. The file must have a .obo extension.';
-
- public static $upload_title = 'New OBO File';
-
- public static $use_analysis = FALSE;
-
- public static $require_analysis = TRUE;
-
- public static $button_text = 'Import OBO File';
-
- public static $methods = array(
-
- 'file_upload' => FALSE,
-
- 'file_local' => FALSE,
-
- 'file_remote' => FALSE,
- );
-
- public static $menu_path = 'admin/tripal/loaders/chado_vocabs/obo_loader';
- public static $file_required = FALSE;
-
- private $obo_namespaces = array();
-
- private $all_cvs = [];
-
- private $all_dbs = [];
-
- private $syn_types = [
- 'exact' => NULL,
- 'broad' => NULL,
- 'narrow' => NULL,
- 'related' => NULL,
- ];
-
- private $termStanzaCache = [
- 'ids' => [],
- 'count' => [
- 'Typedef' => 0,
- 'Term' => 0,
- 'Instance' => 0,
- ],
- 'types' => [
- 'Typedef' => [],
- 'Term' => [],
- 'Instance' => [],
- ],
- ];
-
- private $cache_type = 'memory';
-
- private $default_namespace = '';
-
- private $default_db = '';
-
- private $used_terms = [];
-
- private $baseIRIs = [];
-
- private $ebi_warned = FALSE;
-
- private $is_subset = FALSE;
-
- private $term_names = [];
-
- public function form($form, &$form_state) {
-
- $sql = "SELECT * FROM {tripal_cv_obo} ORDER BY name";
- $results = db_query($sql);
- $obos = array();
- $obos[] = 'Select a Vocabulary';
- foreach ($results as $obo) {
- $obos[$obo->obo_id] = $obo->name;
- }
- $obo_id = '';
- if (array_key_exists('values', $form_state)) {
- $obo_id = array_key_exists('obo_id', $form_state['values']) ? $form_state['values']['obo_id'] : '';
- }
- $form['instructions']['info'] = array(
- '#type' => 'item',
- '#markup' => t('This page allows you to load vocabularies and ontologies
- that are in OBO format. Once loaded, the terms from these
- vocabularies can be used to create content.
- You may use the form below to either reload a vocabulary that is already
- loaded (as when new updates to that vocabulary are available) or load a new
- vocabulary.'),
- );
- $form['obo_existing'] = array(
- '#type' => 'fieldset',
- '#title' => t('Use a Saved Ontology OBO Reference'),
- '#prefix' => '<span id="obo-existing-fieldset">',
- '#suffix' => '</span>'
- );
- $form['obo_existing']['existing_instructions']= array(
- '#type' => 'item',
- '#markup' => t('The vocabularies listed in the select box below have bene pre-populated
- upon installation of Tripal or have been previously loaded. Select one to edit
- its settings or submit for loading. You may reload any vocabulary that has
- already been loaded to retrieve any new updates.'),
- );
- $form['obo_existing']['obo_id'] = array(
- '#title' => t('Ontology OBO File Reference'),
- '#type' => 'select',
- '#options' => $obos,
- '#ajax' => array(
- 'callback' => 'tripal_cv_obo_form_ajax_callback',
- 'wrapper' => 'obo-existing-fieldset',
- ),
- '#description' => t('Select a vocabulary to import.')
- );
-
-
- if ($obo_id) {
- $uobo_name = '';
- $uobo_url = '';
- $uobo_file = '';
- $vocab = db_select('tripal_cv_obo', 't')
- ->fields('t', array('name', 'path'))
- ->condition('obo_id', $obo_id)
- ->execute()
- ->fetchObject();
- $uobo_name = $vocab->name;
- if (preg_match('/^http/', $vocab->path)) {
- $uobo_url = $vocab->path;
- }
- else {
- $uobo_file = trim($vocab->path);
- $matches = array();
- if (preg_match('/\{(.*?)\}/', $uobo_file, $matches)) {
- $modpath = drupal_get_path('module', $matches[1]);
- $uobo_file = preg_replace('/\{.*?\}/', $modpath, $uobo_file);
- }
- }
-
-
- unset($form_state['input']['uobo_name']);
- unset($form_state['input']['uobo_url']);
- unset($form_state['input']['uobo_file']);
- $form['obo_existing']['uobo_name']= array(
- '#type' => 'textfield',
- '#title' => t('Vocabulary Name'),
- '#description' => t('Please provide a name for this vocabulary. After upload, this name will appear in the drop down
- list above for use again later.'),
- '#default_value' => $uobo_name,
- );
- $form['obo_existing']['uobo_url']= array(
- '#type' => 'textfield',
- '#title' => t('Remote URL'),
- '#description' => t('Please enter a URL for the online OBO file. The file will be downloaded and parsed.
- (e.g. http://www.obofoundry.org/ro/ro.obo)'),
- '#default_value' => $uobo_url,
- );
- $form['obo_existing']['uobo_file']= array(
- '#type' => 'textfield',
- '#title' => t('Local File'),
- '#description' => t('Please enter the file system path for an OBO
- definition file. If entering a path relative to
- the Drupal installation you may use a relative path that excludes the
- Drupal installation directory (e.g. sites/default/files/xyz.obo). Note
- that Drupal relative paths have no preceeding slash.
- Otherwise, please provide the full path on the filesystem. The path
- must be accessible to the web server on which this Drupal instance is running.'),
- '#default_value' => $uobo_file,
- );
- $form['obo_existing']['update_obo_details'] = array(
- '#type' => 'submit',
- '#value' => 'Update Ontology Details',
- '#name' => 'update_obo_details'
- );
- }
- $form['obo_new'] = array(
- '#type' => 'fieldset',
- '#title' => t('Add a New Ontology OBO Reference'),
- '#collapsible' => TRUE,
- '#collapsed' => TRUE,
- );
- $form['obo_new']['path_instructions']= array(
- '#value' => t('Provide the name and path for the OBO file. If the vocabulary OBO file
- is stored local to the server provide a file name. If the vocabulry is stored remotely,
- provide a URL. Only provide a URL or a local file, not both.'),
- );
- $form['obo_new']['obo_name']= array(
- '#type' => 'textfield',
- '#title' => t('New Vocabulary Name'),
- '#description' => t('Please provide a name for this vocabulary. After upload, this name will appear in the drop down
- list above for use again later. Additionally, if a default namespace is not provided in the OBO
- header this name will be used as the default_namespace.'),
- );
- $form['obo_new']['obo_url']= array(
- '#type' => 'textfield',
- '#title' => t('Remote URL'),
- '#description' => t('Please enter a URL for the online OBO file. The file will be downloaded and parsed.
- (e.g. http://www.obofoundry.org/ro/ro.obo)'),
- );
- $form['obo_new']['obo_file']= array(
- '#type' => 'textfield',
- '#title' => t('Local File'),
- '#description' => t('Please enter the file system path for an OBO
- definition file. If entering a path relative to
- the Drupal installation you may use a relative path that excludes the
- Drupal installation directory (e.g. sites/default/files/xyz.obo). Note
- that Drupal relative paths have no preceeding slash.
- Otherwise, please provide the full path on the filesystem. The path
- must be accessible to the web server on which this Drupal instance is running.'),
- );
- return $form;
- }
-
- public function formSubmit($form, &$form_state) {
- $obo_id = $form_state['values']['obo_id'];
- $obo_name = trim($form_state['values']['obo_name']);
- $obo_url = trim($form_state['values']['obo_url']);
- $obo_file = trim($form_state['values']['obo_file']);
- $uobo_name = array_key_exists('uobo_name', $form_state['values']) ? trim($form_state['values']['uobo_name']) : '';
- $uobo_url = array_key_exists('uobo_url', $form_state['values']) ? trim($form_state['values']['uobo_url']) : '';
- $uobo_file = array_key_exists('uobo_file', $form_state['values']) ? trim($form_state['values']['uobo_file']) : '';
-
- if ($form_state['clicked_button']['#name'] == 'update_obo_details') {
- $form_state['rebuild'] = TRUE;
- $success = db_update('tripal_cv_obo')
- ->fields(array(
- 'name' => $uobo_name,
- 'path' => $uobo_url ? $uobo_url : $uobo_file,
- ))
- ->condition('obo_id', $obo_id)
- ->execute();
- if ($success) {
- drupal_set_message(t("The vocabulary !vocab has been updated.", array('!vocab' => $uobo_name)));
- }
- else {
- drupal_set_message(t("The vocabulary !vocab could not be updated.", array('!vocab' => $uobo_name)), 'error');
- }
- }
- elseif (!empty($obo_name)) {
- $obo_id = db_insert('tripal_cv_obo')
- ->fields(array(
- 'name' => $obo_name,
- 'path' => $obo_url ? $obo_url : $obo_file,
- ))
- ->execute();
-
- $form_state['values']['obo_id'] = $obo_id;
- if ($obo_id) {
- drupal_set_message(t("The vocabulary !vocab has been added.", array('!vocab' => $obo_name)));
- }
- else {
- $form_state['rebuild'] = TRUE;
- drupal_set_message(t("The vocabulary !vocab could not be added.", array('!vocab' => $obo_name)), 'error');
- }
- }
- }
-
- public function formValidate($form, &$form_state) {
- $obo_id = $form_state['values']['obo_id'];
- $obo_name = trim($form_state['values']['obo_name']);
- $obo_url = trim($form_state['values']['obo_url']);
- $obo_file = trim($form_state['values']['obo_file']);
- $uobo_name = array_key_exists('uobo_name', $form_state['values']) ? trim($form_state['values']['uobo_name']) : '';
- $uobo_url = array_key_exists('uobo_url', $form_state['values']) ? trim($form_state['values']['uobo_url']) : '';
- $uobo_file = array_key_exists('uobo_file', $form_state['values']) ? trim($form_state['values']['uobo_file']) : '';
-
- if ($form_state['clicked_button']['#name'] == 'update_obo_details' or
- $form_state['clicked_button']['#name'] == 'update_load_obo') {
-
- $vocab = db_select('tripal_cv_obo', 't')
- ->fields('t', array('obo_id', 'name', 'path'))
- ->condition('name', $uobo_name)
- ->execute()
- ->fetchObject();
- if ($vocab and $vocab->obo_id != $obo_id) {
- form_set_error('uobo_name', 'The vocabulary name must be different from existing vocabularies');
- }
-
- $dfile = $_SERVER['DOCUMENT_ROOT'] . base_path() . $uobo_file;
- if (!file_exists($dfile)) {
- if (!file_exists($uobo_file)) {
- form_set_error('uobo_file', t('The specified path, !path, does not exist or cannot be read.'), ['!path' => $dfile]);
- }
- }
- if (!$uobo_url and !$uobo_file) {
- form_set_error('uobo_url', 'Please provide a URL or a path for the vocabulary.');
- }
- if ($uobo_url and $uobo_file) {
- form_set_error('uobo_url', 'Please provide only a URL or a path for the vocabulary, but not both.');
- }
- }
- if ($form_state['clicked_button']['#name'] == 'add_new_obo') {
-
- $vocab = db_select('tripal_cv_obo', 't')
- ->fields('t', array('obo_id', 'name', 'path'))
- ->condition('name', $obo_name)
- ->execute()
- ->fetchObject();
- if ($vocab) {
- form_set_error('obo_name', 'The vocabulary name must be different from existing vocabularies');
- }
-
- $dfile = $_SERVER['DOCUMENT_ROOT'] . base_path() . $obo_file;
- if (!file_exists($dfile)) {
- if (!file_exists($obo_file)) {
- form_set_error('obo_file', t('The specified path, !path, does not exist or cannot be read.'), ['!path' => $dfile]);
- }
- }
- if (!$obo_url and !$obo_file) {
- form_set_error('obo_url', 'Please provide a URL or a path for the vocabulary.');
- }
- if ($obo_url and $obo_file) {
- form_set_error('obo_url', 'Please provide only a URL or a path for the vocabulary, but not both.');
- }
- }
- }
-
- public function run() {
- $arguments = $this->arguments['run_args'];
- $obo_id = $arguments['obo_id'];
-
- $obo = db_select('tripal_cv_obo', 'tco')
- ->fields('tco')
- ->condition('obo_id', $obo_id)
- ->execute()
- ->fetchObject();
- if (!$obo) {
- throw new Exception("Invalid OBO ID provided: '$obo_id'.");
- }
-
- $sql = "SELECT * FROM {cv} CV";
- $cvs = chado_query($sql);
- while ($cv = $cvs->fetchObject()) {
- $this->all_cvs[$cv->name] = $cv;
- }
-
- $sql = "SELECT * FROM {db} DB";
- $dbs = chado_query($sql);
- while ($db = $dbs->fetchObject()) {
- $this->all_dbs[$db->name] = $db;
- }
-
- $term = chado_get_cvterm(['id' => 'NCIT:C25693']);
- $this->used_terms['NCIT:C25693'] = $term->cvterm_id;
-
- $term = chado_get_cvterm(['id' => 'rdfs:comment']);
- $this->used_terms['rdfs:comment'] = $term->cvterm_id;
-
- $syn_cv = new ChadoRecord('cv');
- $syn_cv->setValues(['name' => 'synonym_type']);
- $syn_cv->save();
- $this->all_cvs['synonym_type'] = (object) $syn_cv->getValues();
-
- $syn_db = new ChadoRecord('db');
- $syn_db->setValues(['name' => 'synonym_type']);
- $syn_db->save();
- $this->all_dbs['synonym_type'] = (object) $syn_db->getValues();
-
- foreach (array_keys($this->syn_types) as $syn_type) {
- $syn_dbxref = new ChadoRecord('dbxref');
- $syn_dbxref->setValues([
- 'accession' => $syn_type,
- 'db_id' => $syn_db->getID(),
- ]);
- $syn_dbxref->save();
- $syn_term = new ChadoRecord('cvterm');
- $syn_term->setValues([
- 'name' => $syn_type,
- 'cv_id' => $syn_cv->getID(),
- ]);
- if (!$syn_term->find()) {
- $syn_term->setValues([
- 'name' => $syn_type,
- 'definition' => '',
- 'is_obsolete' => 0,
- 'cv_id' => $syn_cv->getID(),
- 'is_relationshiptype' => 0,
- 'dbxref_id' => $syn_dbxref->getID(),
- ]);
- $syn_term->insert();
- }
- $this->syn_types[$syn_type] = (object) $syn_term->getValues();
- }
-
- $this->loadOBO_v1_2_id($obo);
- }
-
- public function postRun() {
-
-
- cache_clear_all('tripal_chado:term:*', 'cache', TRUE);
-
- $this->logMessage("Updating the cv_root_mview materialized view...");
- $mview_id = tripal_get_mview_id('cv_root_mview');
- tripal_populate_mview($mview_id);
- $this->logMessage("Updating the db2cv_mview materialized view...");
- $mview_id = tripal_get_mview_id('db2cv_mview');
- tripal_populate_mview($mview_id);
-
- $this->logMessage("Updating cvtermpath table. This may take a while...");
- foreach ($this->obo_namespaces as $namespace => $cv_id) {
- $this->logMessage("- Loading paths for vocabulary: @vocab", array('@vocab' => $namespace));
- chado_update_cvtermpath($cv_id, $this->job);
- }
- }
-
- private function loadOBO_v1_2_id($obo) {
-
- if (preg_match("/\{(.*?)\}/", $obo->path, $matches)) {
- $module = $matches[1];
- $path = drupal_realpath(drupal_get_path('module', $module));
- $obo->path = preg_replace("/\{.*?\}/", $path, $obo->path);
- }
-
- if (preg_match("/^https:\/\//", $obo->path) or
- preg_match("/^http:\/\//", $obo->path) or
- preg_match("/^ftp:\/\//", $obo->path)) {
- $this->loadOBO_v1_2_url($obo->name, $obo->path, 0);
- }
-
- else {
-
- $dfile = $_SERVER['DOCUMENT_ROOT'] . base_path() . $obo->path;
- if (file_exists($dfile)) {
- $this->loadOBO_v1_2_file($obo->name, $dfile, 0);
- }
-
-
- else {
- if (file_exists($obo->path)) {
- $this->loadOBO_v1_2_file($obo->name, $obo->path, 0);
- }
- else {
- print "ERROR: could not find OBO file: '$obo->path'\n";
- }
- }
- }
- }
-
- private function loadOBO_v1_2_file($obo_name, $file, $is_new = TRUE) {
- if ($is_new) {
- tripal_insert_obo($obo_name, $file);
- }
- $success = $this->loadOBO_v1_2($file, $obo_name);
- }
-
- private function loadOBO_v1_2_url($obo_name, $url, $is_new = TRUE) {
-
- $temp = tempnam(sys_get_temp_dir(), 'obo_');
- print "Downloading URL $url, saving to $temp\n";
- $url_fh = fopen($url, "r");
- $obo_fh = fopen($temp, "w");
- if (!$url_fh) {
- throw new Exception("Unable to download the remote OBO file at $url. Could a firewall be blocking outgoing connections? " .
- " if you are unable to download the file you may manually downlod the OBO file and use the web interface to " .
- " specify the location of the file on your server.");
- }
- while (!feof($url_fh)) {
- fwrite($obo_fh, fread($url_fh, 255), 255);
- }
- fclose($url_fh);
- fclose($obo_fh);
- if ($is_new) {
- tripal_insert_obo($obo_name, $url);
- }
-
- $this->loadOBO_v1_2($temp, $obo_name);
-
- unlink($temp);
- }
-
- private function loadOBO_v1_2($file, $obo_name) {
- $header = array();
- $ret = array();
-
- $this->clearTermStanzaCache();
-
- $this->logMessage("Step 1: Preloading File $file...");
- $this->parse($file, $header);
-
- $this->logMessage("Step 2: Examining relationships...");
- $this->cacheRelationships();
-
- $this->logMessage("Step 3: Loading type defs...");
- $this->processTypeDefs();
-
- $this->logMessage("Step 4: Loading terms...");
- $this->processTerms();
-
- $this->logMessage("Step 5: Cleanup...");
- $this->clearTermStanzaCache();
- }
-
- private function processTypeDefs() {
- $typedefs = $this->getCachedTermStanzas('Typedef');
- $count = $this->getCacheSize('Typedef');
- $this->setTotalItems($count);
- $this->setItemsHandled(0);
- $this->setInterval(5);
- $i = 1;
- foreach ($typedefs as $t) {
-
-
- if ($this->cache_type == 'table') {
- $stanza = unserialize(base64_decode($t->stanza));
- }
- else {
- $stanza = $this->termStanzaCache['ids'][$t];
- }
- $this->setItemsHandled($i++);
- $this->processTerm($stanza, TRUE);
- }
- $this->setItemsHandled($i);
- return 1;
- }
-
- private function processTerms() {
- $i = 0;
- $external = FALSE;
- $terms = $this->getCachedTermStanzas('Term');
- $count = $this->getCacheSize('Term');
- $this->setTotalItems($count);
- $this->setItemsHandled(0);
- $this->setInterval(1);
-
- foreach ($terms as $t) {
-
-
- if ($this->cache_type == 'table') {
- $term = unserialize(base64_decode($t->stanza));
- }
- else {
- $term = $this->termStanzaCache['ids'][$t];
- }
- $this->setItemsHandled($i);
-
- $this->processTerm($term, FALSE);
- $i++;
- }
- $this->setItemsHandled($i);
- return 1;
- }
-
- private function setDefaults($header) {
- $short_name = '';
- $namespace = '';
-
-
- if (array_key_exists('ontology', $header)) {
- $short_name = strtoupper($header['ontology'][0]);
- }
- if (array_key_exists('default-namespace', $header)) {
- $namespace = $header['default-namespace'][0];
- }
-
-
-
- $matches = [];
- if (preg_match('/^(.+?)\/.*/', $short_name, $matches)) {
- $short_name = $matches[1];
- $this->is_subset = TRUE;
- }
-
-
- if (!$namespace and $short_name) {
- $namespace = $this->findEBIOntologyNamespace($short_name);
- }
-
-
- if ($namespace and !$short_name) {
-
-
- $sql = "SELECT dbname FROM {db2cv_mview} WHERE cvname = :cvname";
- $short_name = chado_query($sql, [':cvname' => $namespace])->fetchField();
- if (!$short_name and array_key_exists('namespace-id-rule', $header)) {
- $matches = [];
- if (preg_match('/^.*\s(.+?):.+$/', $header['namespace-id-rule'][0],$matches)){
- $short_name = $matches[1];
- }
- }
-
- if (!$short_name) {
-
- }
- }
-
- if (!$namespace and !$short_name) {
- throw new ErrorException('Cannot determine the namespace or ontology prefix from this OBO file. It is missing both the "default-namespace" and "ontology" headers.');
- }
-
- $this->default_namespace = $namespace;
- $this->default_db = $short_name;
- $this->addDB($this->default_db);
- $cv = $this->addCV($this->default_namespace);
- $this->obo_namespaces[$namespace] = $cv->cv_id;
- }
-
- private function findEBIOntologyNamespace($ontology) {
-
- try {
- $results = $this->oboEbiLookup($ontology, 'ontology');
- if (array_key_exists('default-namespace', $results['config']['annotations'])) {
- $namespace = $results['config']['annotations']['default-namespace'];
- if (is_array($namespace)) {
- $namespace = $namespace[0];
- }
- }
- elseif (array_key_exists('namespace', $results['config'])) {
- $namespace = $results['config']['namespace'];
- }
-
-
- else {
- $namespace = $this->default_db;
- }
- return $namespace;
- }
- catch (Exception $e) {
- watchdog_exception('Cannot find the namespace for this ontology.', $e);
- throw $e;
- }
- }
-
- private function findEBIOntologyPrefix($namespace) {
-
-
- $options = array();
- $page = 1;
- $size = 25;
- $full_url = 'https://www.ebi.ac.uk/ols/api/ontologies?page=' . $page. '&size=' . $size;
- while ($response = drupal_http_request($full_url, $options)) {
- $response = drupal_json_decode($response->data);
- foreach ($response['_embedded']['ontologies'] as $ontology) {
- $namespace = $ontology['config']['namespace'];
- }
- $page++;
- $full_url = 'https://www.ebi.ac.uk/ols/api/ontologies?page=' . $page. '&size=' . $size;
- }
- }
-
- private function findEBITerm($id) {
-
-
- if ($this->ebi_warned == FALSE) {
- $this->logMessage(
- "A term that belongs to another ontology is used within this " .
- "vocabulary. Therefore a lookup will be performed with the EBI Ontology " .
- "Lookup Service to retrieve the information for this term. " .
- "Please note, that vocabularies with many non-local terms " .
- "require remote lookups and these lookups can dramatically " .
- "decrease loading time. " ,
- ['!vocab' => $this->default_namespace], TRIPAL_WARNING);
- $this->ebi_warned = TRUE;
-
-
-
- $this->setInterval(1);
- }
- $this->logMessage("Performing EBI OLS Lookup for: !id", ['!id' => $id]);
-
- $pair = explode(":", $id, 2);
- $short_name = $pair[0];
- $accession = $pair[1];
-
- $base_iri = '';
- $ontologyID = '';
- if (array_key_exists($short_name, $this->baseIRIs)) {
- list($ontologyID, $base_iri) = $this->baseIRIs[$short_name];
- }
- else {
- $full_url = 'http://www.ebi.ac.uk/ols/api/ontologies/' . $short_name;
- $response = drupal_http_request($full_url, []);
- if (!$response) {
- throw new Exception(t('Did not get a response from EBI OLS trying to lookup ontology: !ontology',
- ['!ontology' => $short_name]));
- }
- $ontology_results = drupal_json_decode($response->data);
- if ($ontology_results['error']) {
- throw new Exception(t('Cannot find the ontology via an EBI OLS lookup: !short_name. ' .
- 'EBI Reported: !message. ' .
- 'Consider finding the OBO file for this ontology and manually loading it first.',
- ['!message' => $ontology_results['message'], '!short_name' => $short_name]));
- }
- $base_iri = $ontology_results['config']['baseUris'][0];
- $ontologyID = $ontology_results['ontologyId'];
- $this->baseIRIs[$short_name] = [$ontologyID, $base_iri];
- }
-
- $iri = urlencode(urlencode($base_iri . $accession));
- $full_url = 'http://www.ebi.ac.uk/ols/api/ontologies/' . $ontologyID . '/terms/' . $iri;
- $response = drupal_http_request($full_url, []);
- if(!$response){
- throw new Exception(t('Did not get a response from EBI OLS trying to lookup term: !id',
- ['!id' => $id]));
- }
- $results = drupal_json_decode($response->data);
-
- if ($results['error']) {
- throw new Exception(t('Cannot find the term via an EBI OLS lookup: !term. ' .
- 'EBI Reported: !message.' .
- 'Consider finding the OBO file for this ontology and manually loading it first.',
- ['!message' => $results['message'], '!term' => $id]));
- }
-
- if ($results['is_defining_ontology'] != 1) {
- }
-
-
- $this->logMessage("Found !term in EBI OLS.", ['!term' => $id]);
- $stanza = [];
- $stanza['id'][0] = $id;
- $stanza['name'][0] = $results['label'];
- $stanza['def'][0] = $results['def'];
- $stanza['namespace'][0] = $results['ontology_name'];
- $stanza['is_obsolete'][0] = $results['is_obsolete'] ? 'true' : '';
- $stanza['is_relationshiptype'][0] = '';
- $stanza['db_name'][0] = $short_name;
- $stanza['comment'][0] = 'Term obtained using the EBI Ontology Lookup Service.';
- if (array_key_exists('in_subset', $results)) {
- if (is_array($results['in_subset'])) {
- $stanza['subset'] = $results['in_subset'];
- }
- elseif($results['in_subset']) {
- $stanza['subset'][0] = $results['in_subset'];
- }
- }
-
- if (array_key_exists('term_replaced_by', $results) and isset($results['term_replaced_by'])) {
- $replaced_by = $results['term_replaced_by'];
- $replaced_by = preg_replace('/_/', ':', $replaced_by);
- $this->logMessage("The term, !term, is replaced by, !replaced",
- ['!term' => $id, '!replaced' => $replaced_by]);
-
-
- if (array_key_exists($replaced_by, $this->termStanzaCache['ids'])) {
- $this->logMessage("Found term, !replaced in the term cache.",
- ['!term' => $id, '!replaced' => $replaced_by]);
- return $this->termStanzaCache['ids'][$id];
- }
-
- $rpair = explode(":", $replaced_by, 2);
- $found = $this->lookupTerm($rpair[0], $rpair[1]);
- if ($found) {
- $this->logMessage("Found term, !replaced in the local data store.",
- ['!term' => $id, '!replaced' => $replaced_by]);
- return $found;
- }
-
- $stanza = $this->findEBITerm($replaced_by);
- }
- return $stanza;
- }
-
- private function saveTerm($stanza, $is_relationship = FALSE) {
-
- $id = $stanza['id'][0];
-
- if (array_key_exists($id, $this->used_terms)) {
- return $this->used_terms[$id];
- }
-
- $id = $stanza['id'][0];
- $name = $stanza['name'][0];
- $cvname = $stanza['namespace'][0];
- $dbname = $stanza['db_name'][0];
- $namespace = $stanza['namespace'][0];
-
-
-
- $accession = '';
- $matches = [];
- if (preg_match('/^(.+?):(.*)$/', $id, $matches)) {
- $accession = $matches[2];
- }
- else {
- $accession = $id;
- }
-
- $definition = '';
- if (array_key_exists('def', $stanza)) {
- $definition = preg_replace('/^\"(.*)\"/', '\1', $stanza['def'][0]);
- }
-
- $is_obsolete = 0;
- if (array_key_exists('is_obsolete', $stanza)) {
- $is_obsolete = $stanza['is_obsolete'][0] == 'true' ? 1 : 0;
- }
-
- $is_relationshiptype = 0;
- if (array_key_exists('is_relationshiptype', $stanza)) {
- $is_relationshiptype = $stanza['is_relationshiptype'][0] == 'true' ? 1 : 0;
- }
-
- $is_borrowed = $this->isTermBorrowed($stanza);
-
- $cvterm = NULL;
-
- $cv = $this->all_cvs[$cvname];
- $db = $this->all_dbs[$dbname];
-
- $do_cvterm_insert = TRUE;
-
-
-
- $dbxref = new ChadoRecord('dbxref');
- $dbxref->setValues([
- 'db_id' => $db->db_id,
- 'accession' => $accession
- ]);
- if ($dbxref->find()) {
-
-
-
- $dbx_cvterm = new ChadoRecord('cvterm');
- $dbx_cvterm->setValues(['dbxref_id' => $dbxref->getID()]);
- if ($dbx_cvterm->find()) {
- $do_cvterm_insert = FALSE;
- $cvterm = $dbx_cvterm;
-
-
- if (!$is_borrowed) {
-
-
- $this->fixTermMismatch($stanza, $dbxref, $cv, $name);
-
- $cvterm->setValue('name', $name);
- $cvterm->setValue('definition', $definition);
- $cvterm->setValue('is_obsolete', $is_obsolete);
- $cvterm->setValue('is_relationshiptype', $is_relationshiptype);
- try {
- $cvterm->update();
- }
- catch (Exception $e) {
- $this->logMessage('Could not update the term, "!term", with name, "!name" for vocabulary, "!vocab". ERROR: !error.',
- ['!term' => $id,
- '!name' => $name,
- '!vocab' => $cv->name,
- '!error' => $e->getMessage(),
- ],
- TRIPAL_ERROR);
- throw $e;
- }
- }
- }
- }
-
- else {
- $dbxref->insert();
- }
-
- if ($do_cvterm_insert) {
-
-
- $cvterm = new ChadoRecord('cvterm');
- $cvterm->setValue('cv_id', $cv->cv_id);
- $cvterm->setValue('name', $name);
- if ($cvterm->find()) {
- $fixed = $this->fixTermMismatch($stanza, $dbxref, $cv, $name);
- }
-
- $cvterm->setValues([
- 'cv_id' => $cv->cv_id,
- 'name' => $name,
- 'definition' => $definition,
- 'dbxref_id' => $dbxref->getID(),
- 'is_relationshiptype' => $is_relationshiptype,
- 'is_obsolete' => $is_obsolete,
- 'dbxref_id' => $dbxref->getValue('dbxref_id'),
- ]);
-
-
- try {
- $cvterm->insert();
- }
- catch (Exception $e) {
- $this->logMessage('Could not insert the term, "!term", with name, "!name" for vocabulary, "!vocab". ERROR: !error.',
- ['!term' => $id,
- '!name' => $name,
- '!vocab' => $cv->name,
- '!error' => $e->getMessage(),
- ],
- TRIPAL_ERROR);
- throw $e;
- }
- }
-
- $cvterm_id = $cvterm->getID();
- $this->used_terms[$id] = $cvterm_id;
-
- return $cvterm_id;
- }
-
- public function fixTermMismatch($stanza, $dbxref, $cv, $name) {
- $id = $stanza['id'][0];
- $name = $stanza['name'][0];
-
- $sql = "
- SELECT cvterm_id
- FROM {cvterm}
- WHERE name = :name and cv_id = :cv_id and dbxref_id != :dbxref_id
- ";
- $args = [
- ':name' => $name,
- ':cv_id' => $cv->cv_id,
- ':dbxref_id' => $dbxref->getID(),
- ];
- $results = chado_query($sql, $args);
- while ($conflict_id = $results->fetchField()) {
- $check_cvterm = new ChadoRecord('cvterm', $conflict_id);
-
-
- if ($dbxref->getID() == $check_cvterm->getValue('dbxref_id')) {
- return FALSE;
- }
-
-
- $check_dbxref = new ChadoRecord('dbxref', $check_cvterm->getValue('dbxref_id'));
- $check_db = new ChadoRecord('db', $check_dbxref->getValue('db_id'));
- $check_accession = $check_db->getValue('name') . ':' . $check_dbxref->getValue('accession');
-
-
-
-
-
-
-
-
-
- $check_stanza = $this->getCachedTermStanza($check_accession);
- if (!$check_stanza) {
- $new_name = $check_cvterm->getValue('name') . ' (' . $check_accession . ')';
- $check_cvterm->setValue('name', $new_name);
- $check_cvterm->setValue('is_obsolete', '1');
- $check_cvterm->update();
- return TRUE;
- }
-
-
-
- else if (array_key_exists('is_obsolete', $check_stanza) and ($check_stanza['is_obsolete'][0] == 'true') and (!array_key_exists('is_obsolete', $stanza) or ($stanza['is_obsolete'][0] != 'true'))) {
- $new_name = $check_cvterm->getValue('name') . ' (obsolete)';
- $check_cvterm->setValue('name', $new_name);
- $check_cvterm->update();
- return TRUE;
- }
-
-
-
-
-
-
-
- else {
- $new_name = $check_cvterm->getValue('name') . ' (' . $check_accession . ')';
- $check_cvterm->setValue('name', $new_name);
- $check_cvterm->update();
- return TRUE;
- }
- }
-
- return FALSE;
- }
-
- private function processTerm($stanza, $is_relationship = 0) {
-
-
-
-
-
-
- $cvterm_id = $this->saveTerm($stanza, FALSE);
- $id = $stanza['id'][0];
-
-
- if ($this->isTermBorrowed($stanza)) {
- return;
- }
-
-
-
- $sql = "
- DELETE FROM {cvterm_relationship}
- WHERE subject_id = :cvterm_id
- ";
- chado_query($sql, array(':cvterm_id' => $cvterm_id));
-
-
- if (in_array('is_obsolete', $stanza) and $stanza['is_obsolete'] == 'true') {
- $sql = "
- DELETE FROM {cvterm_relationship}
- WHERE object_id = :cvterm_id
- ";
- chado_query($sql, array(':cvterm_id' => $cvterm_id));
- }
- $sql = "
- DELETE FROM {cvtermprop}
- WHERE cvterm_id = :cvterm_id
- ";
- chado_query($sql, array(':cvterm_id' => $cvterm_id));
- $sql = "
- DELETE FROM {cvterm_dbxref}
- WHERE cvterm_id = :cvterm_id
- ";
- chado_query($sql, array(':cvterm_id' => $cvterm_id));
- $sql = "
- DELETE FROM {cvtermsynonym} CVTSYN
- WHERE cvterm_id = :cvterm_id
- ";
- chado_query($sql, array(':cvterm_id' => $cvterm_id));
-
-
-
- if (!$cvterm_id) {
- throw new Exception(t('Missing cvterm after saving term: !term',
- ['!term' => print_r($stanza, TRUE)]));
- }
-
-
-
- if (array_key_exists('alt_id', $stanza)) {
- foreach ($stanza['alt_id'] as $alt_id) {
- $this->addAltID($id, $cvterm_id, $alt_id);
- }
- }
-
-
-
- if (array_key_exists('synonym', $stanza)) {
- foreach ($stanza['synonym'] as $synonym) {
- $this->addSynonym($id, $cvterm_id, $synonym);
- }
- }
-
-
-
- if (array_key_exists('exact_synonym', $stanza)) {
- foreach ($stanza['exact_synonym'] as $synonym) {
- $fixed = preg_replace('/^\s*(\".+?\")(.*?)$/', '$1 EXACT $2', $synonym);
- $this->addSynonym($id, $cvterm_id, $fixed);
- }
- }
-
-
-
- if (array_key_exists('narrow_synonym', $stanza)) {
- foreach ($stanza['narrow_synonym'] as $synonym) {
- $fixed = preg_replace('/^\s*(\".+?\")(.*?)$/', '$1 NARROW $2', $synonym);
- $this->addSynonym($id, $cvterm_id, $fixed);
- }
- }
-
-
-
- if (array_key_exists('broad_synonym', $stanza)) {
- foreach ($stanza['broad_synonym'] as $synonym) {
- $fixed = preg_replace('/^\s*(\".+?\")(.*?)$/', '$1 BROAD $2', $synonym);
- $this->addSynonym($id, $cvterm_id, $fixed);
- }
- }
-
-
-
- if (array_key_exists('comment', $stanza)) {
- $comments = $stanza['comment'];
- foreach ($comments as $rank => $comment) {
- $this->addComment($id, $cvterm_id, $comment, $rank);
- }
- }
-
-
-
- if (array_key_exists('xref', $stanza)) {
- foreach ($stanza['xref'] as $xref) {
- $this->addXref($id, $cvterm_id, $xref);
- }
- }
-
-
-
- if (array_key_exists('xref_analog', $stanza)) {
- foreach ($stanza['xref_analog'] as $xref) {
- $this->addXref($id, $cvterm_id, $xref);
- }
- }
-
-
-
- if (array_key_exists('xref_unk', $stanza)) {
- foreach ($stanza['xref_unk'] as $xref) {
- $this->addXref($id, $cvterm_id, $xref);
- }
- }
-
-
-
- if (array_key_exists('subset', $stanza)) {
- foreach ($stanza['subset'] as $subset) {
- $this->addSubset($id, $cvterm_id, $subset);
- }
- }
-
-
-
- if (array_key_exists('is_a', $stanza)) {
- foreach ($stanza['is_a'] as $is_a) {
- $this->addRelationship($id, $cvterm_id, 'is_a', $is_a);
- }
- }
-
-
-
- if (array_key_exists('relationship', $stanza)) {
- foreach ($stanza['relationship'] as $value) {
- $rel = preg_replace('/^(.+?)\s.+?$/', '\1', $value);
- $object = preg_replace('/^.+?\s(.+?)$/', '\1', $value);
- $this->addRelationship($id, $cvterm_id, $rel, $object);
- }
- }
-
- }
-
- private function addRelationship($id, $cvterm_id, $rel_id, $obj_id) {
-
-
-
- $rel_stanza = $this->getCachedTermStanza($rel_id);
- if (!$rel_stanza) {
- throw new Exception(t('Cannot add relationship: "!source !rel !object". ' .
- 'The term, !rel, is not in the term cache.',
- ['!source' => $id, '!rel' => $rel_id, '!name' => $obj_id]));
- }
- $rel_cvterm_id = $this->saveTerm($rel_stanza, TRUE);
-
- $obj_stanza = $this->getCachedTermStanza($obj_id);
- if (!$obj_stanza) {
- throw new Exception(t('Cannot add relationship: "!source !rel !object". ' .
- 'The term, !object, is not in the term cache.',
- ['!source' => $id, '!rel' => $rel_id, '!object' => $obj_id]));
- }
- $obj_cvterm_id = $this->saveTerm($obj_stanza);
-
- $cvterm_relationship = new ChadoRecord('cvterm_relationship');
- $cvterm_relationship->setValues([
- 'type_id' => $rel_cvterm_id,
- 'subject_id' => $cvterm_id,
- 'object_id' => $obj_cvterm_id
- ]);
-
-
- try {
- $cvterm_relationship->insert();
- }
- catch (Exception $e) {
- throw new Exception(t('Cannot add relationship: "!source !rel !object". ' .
- 'ERROR: !error.',
- ['!source' => $id, '!rel' => $rel_id, '!object' => $obj_id, '!error' => $e->getMessage()]));
- }
- }
-
- private function getCachedTermStanza($id) {
- if ($this->cache_type == 'table') {
- $values = array('id' => $id);
- $result = chado_select_record('tripal_obo_temp', array('stanza'), $values);
- if (count($result) == 0) {
- return FALSE;
- }
- return unserialize(base64_decode($result['stanza']));
- }
- if (array_key_exists($id, $this->termStanzaCache['ids'])) {
- return $this->termStanzaCache['ids'][$id];
- }
- else {
- return FALSE;
- }
- }
-
- private function lookupTerm($short_name, $accession) {
-
- if (!array_key_exists($short_name, $this->all_dbs)) {
- return NULL;
- }
- $db = $this->all_dbs[$short_name];
-
- $dbxref = new ChadoRecord('dbxref');
- $dbxref->setValues([
- 'db_id' => $db->db_id,
- 'accession' => $accession,
- ]);
- if (!$dbxref->find()) {
- return NULL;
- }
-
- $cvterm = new ChadoRecord('cvterm');
- $cvterm->setValues(['dbxref_id' => $dbxref->getID()]);
- if (!$cvterm->find()) {
- return NULL;
- }
-
- $cv = new ChadoRecord('cv');
- $cv->setValues(['cv_id' => $cvterm->getValue('cv_id')]);
- $cv->find();
-
- $stanza = [];
- $stanza['id'][0] = $short_name . ':' . $accession;
- $stanza['name'][0] = $cvterm->getValue('name');
- $stanza['def'][0] = $cvterm->getValue('definition');
- $stanza['namespace'][0] = $cv->getValue('name');
- $stanza['is_obsolete'][0] = $cvterm->getValue('is_obsolete') == 1 ? 'true' : '';
- $stanza['is_relationshiptype'][0] = '';
- $stanza['db_name'][0] = $db->name;
- $stanza['cv_name'][0] = $cv->getValue('name');
- return $stanza;
- }
-
- private function cacheTermStanza($stanza, $type) {
-
- if (!$this->default_namespace) {
- throw new Exception('Cannot cache terms without a default CV.' . print_r($stanza, TRUE));
- }
- if (!$this->default_db) {
- throw new Exception('Cannot cache terms without a default DB.' . print_r($stanza, TRUE));
- }
- $id = $stanza['id'][0];
-
- if ($this->getCachedTermStanza($id)) {
- return;
- }
-
-
-
-
- $matches = [];
- if (preg_match('/^(.+):(.+)$/', $id, $matches)) {
- $short_name = $matches[1];
- $accession = $matches[2];
-
- if ($short_name != $this->default_db) {
-
-
- $found = $this->lookupTerm($short_name, $accession);
- if ($found) {
- $stanza = $found;
- }
-
- else {
- $stanza = $this->findEBITerm($id);
-
- $this->addDB($stanza['db_name'][0]);
- $this->addCV($stanza['namespace'][0]);
- }
- }
-
- else {
- if (!array_key_exists('namespace', $stanza)) {
- $stanza['namespace'][0] = $this->default_namespace;
- }
- $stanza['db_name'][0] = $short_name;
- }
-
-
- $this->addDB($short_name);
- }
-
- else {
- if (!array_key_exists('namespace', $stanza)) {
- $stanza['namespace'][0] = $this->default_namespace;
- }
- $stanza['db_name'][0] = $this->default_db;
- }
- $stanza['is_relationshiptype'][0] = '';
- if ($type == 'Typedef') {
- $stanza['is_relationshiptype'][0] = 'true';
- }
-
-
- if (array_key_exists('is_a', $stanza)) {
- foreach ($stanza['is_a'] as $index => $is_a) {
- $stanza['is_a'][$index] = trim(preg_replace('/\{.+?\}/', '', $is_a));
- }
- }
- if (array_key_exists('relationship', $stanza)) {
- foreach ($stanza['relationship'] as $index => $relationship) {
- $stanza['relationship'][$index] = trim(preg_replace('/\{.+?\}/', '', $relationship));
- }
- }
-
-
- if (array_key_exists('synonym', $stanza)) {
- foreach ($stanza['synonym'] as $index => $synonym) {
- if (preg_match('/\"(.*?)\".*(EXACT|NARROW|BROAD|RELATED)/', $synonym, $matches)) {
- $stanza['synonym'][$index] = '"' . $matches[1] . '" ' . $matches[2];
- }
- }
- }
-
-
-
-
-
- foreach ($stanza as $key => $values) {
- $stanza[$key] = array_unique($values);
- }
-
-
- if ($this->cache_type == 'table') {
-
- $values = [
- 'id' => $id,
- 'stanza' => base64_encode(serialize($stanza)),
- 'type' => $type,
- ];
- $success = chado_insert_record('tripal_obo_temp', $values);
- if (!$success) {
- throw new Exception("Cannot insert stanza into temporary table.");
- }
- return;
- }
-
- $this->termStanzaCache['ids'][$id] = $stanza;
- $this->termStanzaCache['count'][$type]++;
- $this->termStanzaCache['types'][$type][] = $id;
-
- $name = $stanza['name'][0];
- $this->term_names[$name] = 1;
- }
-
- private function getCacheSize($type) {
- if ($this->cache_type == 'table') {
- $sql = "
- SELECT count(*) as num_terms
- FROM {tripal_obo_temp}
- WHERE type = :type
- ";
- $result = chado_query($sql, [':type' => $type])->fetchObject();
- return $result->num_terms;
- }
- return $this->termStanzaCache['count'][$type];
- }
-
- private function getCachedTermStanzas($type) {
- if ($this->cache_type == 'table') {
- $sql = "SELECT id FROM {tripal_obo_temp} WHERE type = 'Typedef' ";
- $typedefs = chado_query($sql);
- return $typedefs;
- }
- return $this->termStanzaCache['types'][$type];
- }
-
- private function clearTermStanzaCache() {
- if ($this->cache_type == 'table') {
- $sql = "DELETE FROM {tripal_obo_temp}";
- chado_query($sql);
- return;
- }
- $this->termStanzaCache = [
- 'ids' => [],
- 'count' => [
- 'Typedef' => 0,
- 'Term' => 0,
- 'Instance' => 0,
- ],
- 'types' => [
- 'Typedef' => [],
- 'Term' => [],
- 'Instance' => [],
- ],
- ];
- }
-
- private function addSynonym($id, $cvterm_id, $synonym) {
- $def = $synonym;
- $syn_type = '';
-
- $matches = [];
- if (preg_match('/\"(.*?)\".*(EXACT|NARROW|BROAD|RELATED)/', $synonym, $matches)) {
- $def = $matches[1];
- $syn_type = strtolower($matches[2]);
- }
-
- if (!$syn_type) {
- $syn_type = 'exact';
- }
- $syn_type_term = $this->syn_types[$syn_type];
- if (!$syn_type_term) {
- throw new Exception(t('Cannot find synonym type: !type', ['!type' => $syn_type]));
- }
-
-
- if (strlen($def) > 255) {
- $def = substr($def, 0, 252) . "...";
- }
-
- $cvtermsynonym = new ChadoRecord('cvtermsynonym');
- $cvtermsynonym->setValues([
- 'cvterm_id' => $cvterm_id,
- 'synonym' => $def,
- ]);
-
-
- try {
-
-
-
-
-
-
-
-
- if (!$cvtermsynonym->find()) {
- $cvtermsynonym->setValue('type_id', $syn_type_term->cvterm_id);
- $cvtermsynonym->insert();
- }
- }
- catch (Exception $e) {
- throw new Exception(t('Cannot add synonym, "!synonym" to term: !id. ' .
- 'ERROR: !error.',
- ['!synonym' => $def, '!id' => $id, '!error' => $e->getMessage()]));
- }
- }
-
- private function parse($obo_file, &$header) {
-
- $in_header = TRUE;
-
- $stanza = array();
-
- $db_short_name = '';
- $line_num = 0;
- $num_read = 0;
-
- $type = '';
- $filesize = filesize($obo_file);
- $this->setTotalItems($filesize);
- $this->setItemsHandled(0);
- $this->setInterval(5);
-
- $fh = fopen($obo_file, 'r');
- while ($line = fgets($fh)) {
- $line_num++;
- $size = drupal_strlen($line);
- $num_read += $size;
- $line = trim($line);
- $this->setItemsHandled($num_read);
-
- $line = rtrim($line);
-
- $line = preg_replace('/[^(\x20-\x7F)]*/', '', $line);
-
- if (strcmp($line, '') == 0) {
- continue;
- }
-
- $line = preg_replace('/^(.*?)\!.*$/', '\1', $line);
-
- if (preg_match('/^\s*\[/', $line)) {
-
- if ($in_header == TRUE) {
- $this->setDefaults($header);
- $in_header = FALSE;
- }
-
- if (sizeof($stanza) > 0) {
-
- if (array_key_exists('namespace', $stanza)) {
- $namespace = $stanza['namespace'][0];
- $cv = $this->all_cvs[$namespace];
- $this->obo_namespaces[$namespace] = $cv->cv_id;
- }
-
-
-
- if (array_key_exists($stanza['name'][0], $this->term_names)) {
- $new_name = $stanza['name'][0] . '(' . $stanza['id'][0] .')';
- $stanza['name'][0] = $stanza['name'][0];
- }
- $this->cacheTermStanza($stanza, $type);
- }
-
- $type = preg_replace('/^\s*\[\s*(.+?)\s*\]\s*$/', '\1', $line);
-
- $stanza = array();
- continue;
- }
-
- preg_replace("/\\:/", "|-|-|", $line);
- $pair = explode(":", $line, 2);
- $tag = $pair[0];
- $value = ltrim(rtrim($pair[1]));
-
- $matches = array();
- if ($tag == 'id' and preg_match('/^(.+?):.*$/', $value, $matches)) {
- $db_short_name = $matches[1];
- }
- $tag = preg_replace("/\|-\|-\|/", "\:", $tag);
- $value = preg_replace("/\|-\|-\|/", "\:", $value);
- if ($in_header) {
- if (!array_key_exists($tag, $header)) {
- $header[$tag] = array();
- }
- $header[$tag][] = $value;
- }
- else {
- if (!array_key_exists($tag, $stanza)) {
- $stanza[$tag] = array();
- }
- $stanza[$tag][] = $value;
- }
- }
-
- if (sizeof($stanza) > 0) {
-
- if (array_key_exists('namespace', $stanza)) {
- $namespace = $stanza['namespace'][0];
- $cv = $this->all_cvs[$namespace];
- $this->obo_namespaces[$namespace] = $cv->cv_id;
- }
- $this->cacheTermStanza($stanza, $type);
- $this->setItemsHandled($num_read);
- }
-
- $message = t('Found the following namespaces: !namespaces.',
- ['!namespaces' => implode(', ', array_keys($this->obo_namespaces))]);
- foreach ($this->obo_namespaces as $namespace => $cv_id) {
- $this->addCV($namespace);
- }
- $this->logMessage($message);
- }
-
- private function cacheRelationships() {
-
-
-
- $terms = $this->getCachedTermStanzas('Term');
- $count = $this->getCacheSize('Term');
- $this->setTotalItems($count);
- $this->setItemsHandled(0);
- $this->setInterval(25);
-
- $i = 1;
- foreach ($terms as $t) {
-
-
- if ($this->cache_type == 'table') {
- $stanza = unserialize(base64_decode($t->stanza));
- }
- else {
- $stanza = $this->termStanzaCache['ids'][$t];
- }
-
- if (array_key_exists('is_a', $stanza)) {
- foreach ($stanza['is_a'] as $object_term) {
- $rstanza = [];
- $rstanza['id'][] = $object_term;
- $this->cacheTermStanza($rstanza, 'Term');
- }
- }
-
- if (array_key_exists('relationship', $stanza)) {
- foreach ($stanza['relationship'] as $value) {
-
- $rel_term = preg_replace('/^(.+?)\s.+?$/', '\1', $value);
- $object_term = preg_replace('/^.+?\s(.+?)$/', '\1', $value);
- $rstanza = [];
- $rstanza['id'][] = $rel_term;
- $this->cacheTermStanza($rstanza, 'Typedef');
- $rstanza = [];
- $rstanza['id'][] = $object_term;
- $this->cacheTermStanza($rstanza, 'Term');
- }
- }
- }
- $this->setItemsHandled($i++);
-
-
-
- if (!$this->getCachedTermStanza('is_a')) {
- $stanza = [];
- $stanza['id'][0] = 'is_a';
- $stanza['name'][0] = 'is_a';
- $stanza['namespace'][0] = $this->default_namespace;
- $stanza['db_name'][0] = $this->default_db;
- $this->cacheTermStanza($stanza, 'Typedef');
- }
- }
-
- private function addSubset($id, $cvterm_id, $subset) {
- $cvtermprop = new ChadoRecord('cvtermprop');
- $cvtermprop->setValues([
- 'cvterm_id' => $cvterm_id,
- 'type_id' => $this->used_terms['NCIT:C25693'],
- 'value' => $subset
- ]);
-
-
- try {
- $cvtermprop->insert();
- }
- catch (Exception $e) {
- throw new Exception(t('Cannot add subset, "!subset" to term: !id. ' .
- 'ERROR: !error.',
- ['!subset' => $subset, '!id' => $id, '!error' => $e->getMessage()]));
- }
- }
-
- private function addDB($dbname) {
-
- $db = NULL;
- if (array_key_exists($dbname, $this->all_dbs)) {
- $db = $this->all_dbs[$dbname];
- }
- else {
-
- $db = new ChadoRecord('db');
- $db->setValues(['name' => $dbname]);
- $db->insert();
- $db = (object) $db->getValues();
- $this->all_dbs[$dbname] = $db;
- }
- return $db;
- }
-
- private function addCV($cvname) {
-
-
-
-
- $cv = NULL;
- if (array_key_exists($cvname, $this->all_cvs)) {
- $cv = $this->all_cvs[$cvname];
- }
- else {
-
- $cv = new ChadoRecord('cv');
- $cv->setValues(['name' => $cvname]);
- $cv->insert();
- $cv = (object) $cv->getValues();
- $this->all_cvs[$cvname] = $cv;
- $this->obo_namespaces[$cvname] = $cv->cv_id;
- }
- return $cv;
- }
-
- private function isTermBorrowed($stanza) {
- $namespace = $stanza['namespace'][0];
- if (array_key_exists($namespace, $this->obo_namespaces)) {
- return FALSE;
- }
- return TRUE;
- }
-
- private function addAltID($id, $cvterm_id, $alt_id) {
- $dbname = '';
- $accession = '';
- $matches = [];
- if (preg_match('/^(.+?):(.*)$/', $alt_id, $matches)) {
- $dbname = $matches[1];
- $accession = $matches[2];
- }
- if (!$accession) {
- throw new Exception("Cannot add an Alt ID without an accession: '$alt_id'");
- }
-
- $db = $this->addDB($dbname);
- $db_id = $db->db_id;
-
- $dbxref = new ChadoRecord('dbxref');
- $dbxref->setValues([
- 'db_id' => $db_id,
- 'accession' => $accession
- ]);
- if (!$dbxref->find()) {
- $dbxref->insert();
- }
-
- $cvterm_dbxref = new ChadoRecord('cvterm_dbxref');
- $cvterm_dbxref->setValues([
- 'cvterm_id' => $cvterm_id,
- 'dbxref_id' => $dbxref->getID(),
- ]);
- if (!$cvterm_dbxref->find()) {
- $cvterm_dbxref->insert();
- }
- }
-
- private function addXref($id, $cvterm_id, $xref) {
- $dbname = preg_replace('/^(.+?):.*$/', '$1', $xref);
- $accession = preg_replace('/^.+?:\s*(.*?)(\{.+$|\[.+$|\s.+$|\".+$|$)/', '$1', $xref);
- $description = preg_replace('/^.+?\"(.+?)\".*?$/', '$1', $xref);
- $dbxrefs = preg_replace('/^.+?\[(.+?)\].*?$/', '$1', $xref);
- if (!$accession) {
- throw new Exception("Cannot add an xref without an accession: '$xref'");
- }
-
- if (strcmp($dbname, 'http') == 0) {
- return;
- }
-
- $db = $this->addDB($dbname);
- $db_id = $db->db_id;
-
- $dbxref = new ChadoRecord('dbxref');
- $dbxref->setValues([
- 'db_id' => $db_id,
- 'accession' => $accession
- ]);
- if (!$dbxref->find()) {
- $dbxref->insert();
- }
-
- $cvterm_dbxref = new ChadoRecord('cvterm_dbxref');
- $cvterm_dbxref->setValues([
- 'cvterm_id' => $cvterm_id,
- 'dbxref_id' => $dbxref->getID(),
- ]);
- if (!$cvterm_dbxref->find()) {
- $cvterm_dbxref->insert();
- }
- }
-
- private function addComment($id, $cvterm_id, $comment, $rank) {
-
- $comment_type_id = $this->used_terms['rdfs:comment'];
-
- $cvtermprop = new ChadoRecord('cvtermprop');
- $cvtermprop->setValues([
- 'cvterm_id' => $cvterm_id,
- 'type_id' => $comment_type_id,
- 'value' => $comment,
- 'rank' => $rank,
- ]);
-
-
- try {
- $cvtermprop->insert();
- }
- catch (Exception $e) {
- throw new Exception(t('Cannot add comment, "!comment" to term: !id. ' .
- 'ERROR: !error.',
- ['!comment' => $comment, '!id' => $id, '!error' => $e->getMessage()]));
- }
- }
-
- private function oboEbiLookup($accession, $type_of_search) {
-
- $parts = explode(':', $accession);
- $ontology = strtolower($parts[0]);
- $ontology = preg_replace('/\s+/', '', $ontology);
- if ($type_of_search == 'ontology') {
- $options = array();
- $full_url = 'http://www.ebi.ac.uk/ols/api/ontologies/' . $ontology;
- $response = drupal_http_request($full_url, $options);
- if(!empty($response)){
- $response = drupal_json_decode($response->data);
- }
- }
- elseif ($type_of_search == 'term') {
-
- $iri = urlencode(urlencode("http://purl.obolibrary.org/obo/" . str_replace(':' , '_', $accession)));
- $options = array();
- $full_url = 'http://www.ebi.ac.uk/ols/api/ontologies/' . $ontology . '/' . 'terms/' . $iri;
- $response = drupal_http_request($full_url, $options);
- if(!empty($response)){
- $response = drupal_json_decode($response->data);
- }
- }
- elseif($type_of_search == 'query') {
- $options = array();
- $full_url = 'http://www.ebi.ac.uk/ols/api/search?q=' . $accession . '&queryFields=obo_id&local=true';
- $response = drupal_http_request($full_url, $options);
- if(!empty($response)){
- $response = drupal_json_decode($response->data);
- }
- }
- elseif($type_of_search == 'query-non-local') {
- $options = array();
- $full_url = 'http://www.ebi.ac.uk/ols/api/search?q=' . $accession . '&queryFields=obo_id';
- $response = drupal_http_request($full_url, $options);
- if(!empty($response)){
- $response = drupal_json_decode($response->data);
- }
- }
- return $response;
- }
- }
- function tripal_cv_obo_form_ajax_callback($form, $form_state) {
- return $form['obo_existing'];
- }
|