tripal_cv.obo_loader.inc 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  1. <?php
  2. /**
  3. * @file
  4. * Functions to aid in loading ontologies into the chado cv module
  5. */
  6. /**
  7. * @defgroup tripal_obo_loader Ontology Loader
  8. * @ingroup tripal_cv
  9. * @{
  10. * Functions to aid in loading ontologies into the chado cv module
  11. * @}
  12. */
  13. /**
  14. * Provides the form to load an already existing controlled
  15. * Vocabulary into chado
  16. *
  17. * @param $form
  18. * The form array
  19. * @param $form_state
  20. * The form state array
  21. *
  22. * @return
  23. * The form array with new additions
  24. *
  25. * @ingroup tripal_obo_loader
  26. */
  27. function tripal_cv_obo_form($form, &$form_state) {
  28. // get a list of db from chado for user to choose
  29. $sql = "SELECT * FROM {tripal_cv_obo} ORDER BY name";
  30. $results = db_query($sql);
  31. $obos = array();
  32. $obos[] = '';
  33. foreach ($results as $obo) {
  34. // $obos[$obo->obo_id] = "$obo->name | $obo->path";
  35. $obos[$obo->obo_id] = $obo->name;
  36. }
  37. $form['obo_existing'] = array(
  38. '#type' => 'fieldset',
  39. '#title' => t('Use a Saved Ontology OBO Reference')
  40. );
  41. $form['obo_new'] = array(
  42. '#type' => 'fieldset',
  43. '#title' => t('Use a New Ontology OBO Reference')
  44. );
  45. $form['obo_existing']['existing_instructions']= array(
  46. '#value' => t('The Ontology OBO files listed in the drop down below have been automatically added upon
  47. installation of the Tripal CV module or were added from a previous upload. Select
  48. an OBO, then click the submit button to load the vocabulary into the database. If the
  49. vocabularies already exist then the ontology will be updated.'),
  50. );
  51. $form['obo_existing']['obo_id'] = array(
  52. '#title' => t('Ontology OBO File Reference'),
  53. '#type' => 'select',
  54. '#options' => $obos,
  55. );
  56. $form['obo_new']['path_instructions']= array(
  57. '#value' => t('Provide the name and path for the OBO file. If the vocabulary OBO file
  58. is stored local to the server provide a file name. If the vocabulry is stored remotely,
  59. provide a URL. Only provide a URL or a local file, not both.'),
  60. );
  61. $form['obo_new']['obo_name']= array(
  62. '#type' => 'textfield',
  63. '#title' => t('New Vocabulary Name'),
  64. '#description' => t('Please provide a name for this vocabulary. After upload, this name will appear in the drop down
  65. list above for use again later.'),
  66. );
  67. $form['obo_new']['obo_url']= array(
  68. '#type' => 'textfield',
  69. '#title' => t('Remote URL'),
  70. '#description' => t('Please enter a URL for the online OBO file. The file will be downloaded and parsed.
  71. (e.g. http://www.obofoundry.org/ro/ro.obo'),
  72. );
  73. $form['obo_new']['obo_file']= array(
  74. '#type' => 'textfield',
  75. '#title' => t('Local File'),
  76. '#description' => t('Please enter the full system path for an OBO definition file, or a path within the Drupal
  77. installation (e.g. /sites/default/files/xyz.obo). The path must be accessible to the
  78. server on which this Drupal instance is running.'),
  79. );
  80. $form['submit'] = array(
  81. '#type' => 'submit',
  82. '#value' => t('Submit'),
  83. '#executes_submit_callback' => TRUE,
  84. );
  85. $form['#redirect'] = 'admin/tripal/tripal_cv/obo_loader';
  86. return $form;
  87. }
  88. /**
  89. * The submit function for the load ontology form. It registers a
  90. * tripal job to import the user specified ontology file
  91. *
  92. * @param $form
  93. * The form array
  94. * @param $form_state
  95. * The form state array
  96. *
  97. *
  98. * @ingroup tripal_obo_loader
  99. */
  100. function tripal_cv_obo_form_submit($form, &$form_state) {
  101. $obo_id = $form_state['values']['obo_id'];
  102. $obo_name = trim($form_state['values']['obo_name']);
  103. $obo_url = trim($form_state['values']['obo_url']);
  104. $obo_file = trim($form_state['values']['obo_file']);
  105. tripal_submit_obo_job(array(
  106. 'obo_id' => $obo_id,
  107. 'name' => $obo_name,
  108. 'url' => $obo_url,
  109. 'file' => $obo_file
  110. ));
  111. }
  112. /**
  113. * A wrapper function for importing the user specified OBO file into Chado by
  114. * specifying the obo_id of the OBO. It requires that the file be in OBO v1.2
  115. * compatible format. This function is typically executed via the Tripal jobs
  116. * management after a user submits a job via the Load Onotloies form.
  117. *
  118. * @param $obo_id
  119. * An obo_id from the tripal_cv_obo file that specifies which OBO file to import
  120. * @param $job_id
  121. * The job_id of the job from the Tripal jobs management system.
  122. *
  123. * @ingroup tripal_obo_loader
  124. */
  125. function tripal_cv_load_obo_v1_2_id($obo_id, $jobid = NULL) {
  126. // Get the OBO reference.
  127. $sql = "SELECT * FROM {tripal_cv_obo} WHERE obo_id = :obo_id";
  128. $obo = db_query($sql, array(':obo_id' => $obo_id))->fetchObject();
  129. // Convert the module name to the real path if present
  130. if (preg_match("/\{(.*?)\}/", $obo->path, $matches)) {
  131. $module = $matches[1];
  132. $path = drupal_realpath(drupal_get_path('module', $module));
  133. $obo->path = preg_replace("/\{.*?\}/", $path, $obo->path);
  134. }
  135. // if the reference is for a remote URL then run the URL processing function
  136. if (preg_match("/^http:\/\//", $obo->path) or preg_match("/^ftp:\/\//", $obo->path)) {
  137. tripal_cv_load_obo_v1_2_url($obo->name, $obo->path, $jobid, 0);
  138. }
  139. // if the reference is for a local file then run the file processing function
  140. else {
  141. // check to see if the file is located local to Drupal
  142. $dfile = $_SERVER['DOCUMENT_ROOT'] . base_path() . $obo->path;
  143. if (file_exists($dfile)) {
  144. tripal_cv_load_obo_v1_2_file($obo->name, $dfile , $jobid, 0);
  145. }
  146. // if not local to Drupal, the file must be someplace else, just use
  147. // the full path provided
  148. else {
  149. if (file_exists($obo->path)) {
  150. tripal_cv_load_obo_v1_2_file($obo->name, $obo->path, $jobid, 0);
  151. }
  152. else {
  153. print "ERROR: counld not find OBO file: '$obo->path'\n";
  154. }
  155. }
  156. }
  157. }
  158. /**
  159. * A wrapper function for importing the user specified OBO file into Chado by
  160. * specifying the filename and path of the OBO. It requires that the file be in OBO v1.2
  161. * compatible format. This function is typically executed via the Tripal jobs
  162. * management after a user submits a job via the Load Onotloies form.
  163. *
  164. * @param $obo_name
  165. * The name of the OBO (typially the ontology or controlled vocabulary name)
  166. * @param $file
  167. * The path on the file system where the ontology can be found
  168. * @param $job_id
  169. * The job_id of the job from the Tripal jobs management system.
  170. * @param $is_new
  171. * Set to TRUE if this is a new ontology that does not yet exist in the
  172. * tripal_cv_obo table. If TRUE the OBO will be added to the table.
  173. *
  174. * @ingroup tripal_obo_loader
  175. */
  176. function tripal_cv_load_obo_v1_2_file($obo_name, $file, $jobid = NULL, $is_new = TRUE) {
  177. $newcvs = array();
  178. if ($is_new) {
  179. tripal_insert_obo($obo_name, $file);
  180. }
  181. $success = tripal_cv_load_obo_v1_2($file, $jobid, $newcvs);
  182. if ($success) {
  183. // update the cvtermpath table
  184. tripal_cv_load_update_cvtermpath($newcvs, $jobid);
  185. print "\nDone\n";
  186. }
  187. }
  188. /**
  189. * A wrapper function for importing the user specified OBO file into Chado by
  190. * specifying the remote URL of the OBO. It requires that the file be in OBO v1.2
  191. * compatible format. This function is typically executed via the Tripal jobs
  192. * management after a user submits a job via the Load Onotloies form.
  193. *
  194. * @param $obo_name
  195. * The name of the OBO (typially the ontology or controlled vocabulary name)
  196. * @param $url
  197. * The remote URL of the OBO file.
  198. * @param $job_id
  199. * The job_id of the job from the Tripal jobs management system.
  200. * @param $is_new
  201. * Set to TRUE if this is a new ontology that does not yet exist in the
  202. * tripal_cv_obo table. If TRUE the OBO will be added to the table.
  203. *
  204. * @ingroup tripal_obo_loader
  205. */
  206. function tripal_cv_load_obo_v1_2_url($obo_name, $url, $jobid = NULL, $is_new = TRUE) {
  207. $newcvs = array();
  208. // first download the OBO
  209. $temp = tempnam(sys_get_temp_dir(), 'obo_');
  210. print "Downloading URL $url, saving to $temp\n";
  211. $url_fh = fopen($url, "r");
  212. $obo_fh = fopen($temp, "w");
  213. if (!$url_fh) {
  214. tripal_cv_obo_quiterror("Unable to download the remote OBO file at $url. Could a firewall be blocking outgoing connections? " .
  215. " if you are unable to download the file you may manually downlod the OBO file and use the web interface to " .
  216. " specify the location of the file on your server.");
  217. }
  218. while (!feof($url_fh)) {
  219. fwrite($obo_fh, fread($url_fh, 255), 255);
  220. }
  221. fclose($url_fh);
  222. fclose($obo_fh);
  223. if ($is_new) {
  224. tripal_insert_obo($obo_name, $url);
  225. }
  226. // second, parse the OBO
  227. $success = tripal_cv_load_obo_v1_2($temp, $jobid, $newcvs);
  228. if ($success) {
  229. // update the cvtermpath table
  230. tripal_cv_load_update_cvtermpath($newcvs, $jobid);
  231. print "Done\n";
  232. }
  233. // now remove the temp file
  234. unlink($temp);
  235. }
  236. /**
  237. * A function for executing the cvtermpath function of Chado. This function
  238. * populates the cvtermpath table of Chado for quick lookup of term
  239. * relationships
  240. *
  241. * @param $newcvs
  242. * An associative array of controlled vocabularies to update. The key must be
  243. * the name of the vocabulary and the value the cv_id from the cv table of chado.
  244. * @param $jobid
  245. * The job_id of the job from the Tripal jobs management system.
  246. *
  247. * @ingroup tripal_obo_loader
  248. */
  249. function tripal_cv_load_update_cvtermpath($newcvs, $jobid) {
  250. print "\nUpdating cvtermpath table. This may take a while...\n";
  251. foreach ($newcvs as $namespace => $cvid) {
  252. tripal_update_cvtermpath($cvid, $jobid);
  253. }
  254. }
  255. /**
  256. * Imports a given OBO file into Chado. This function is usually called by
  257. * one of three wrapper functions: tripal_cv_load_obo_v1_2_id,
  258. * tripal_cv_load_obo_v1_2_file or tirpal_cv_load_obo_v1_2_url. But, it can
  259. * be called directly if the full path to an OBO file is available on the
  260. * file system.
  261. *
  262. * @param $flie
  263. * The full path to the OBO file on the file system
  264. * @param $jobid
  265. * The job_id of the job from the Tripal jobs management system.
  266. * @param $newcvs
  267. * An empty array passed by reference that upon return will contain the list
  268. * of newly added vocabularies. The key will contain the CV name and the
  269. * value the new cv_id
  270. *
  271. *
  272. * @ingroup tripal_obo_loader
  273. */
  274. function tripal_cv_load_obo_v1_2($file, $jobid = NULL, &$newcvs) {
  275. $transaction = db_transaction();
  276. print "\nNOTE: Loading of this OBO file is performed using a database transaction. \n" .
  277. "If the load fails or is terminated prematurely then the entire set of \n" .
  278. "insertions/updates is rolled back and will not be found in the database\n\n";
  279. try {
  280. $header = array();
  281. // make sure our temporary table exists
  282. $ret = array();
  283. // empty the temp table
  284. $sql = "DELETE FROM {tripal_obo_temp}";
  285. chado_query($sql);
  286. print "Step 1: Preloading File $file\n";
  287. // make sure we have an 'internal' and a '_global' database
  288. if (!tripal_insert_db(array('name' => 'internal'))) {
  289. tripal_cv_obo_quiterror("Cannot add 'internal' database");
  290. }
  291. if (!tripal_insert_db(array('name' => '_global'))) {
  292. tripal_cv_obo_quiterror("Cannot add '_global' database");
  293. }
  294. // parse the obo file
  295. $default_db = tripal_cv_obo_parse($file, $header, $jobid);
  296. // add the CV for this ontology to the database. The v1.2 definition
  297. // specifies a 'default-namespace' to be used if a 'namespace' is not
  298. // present for each stanza. Some ontologies have adopted the v1.4 method
  299. // in their v1.2 files and not including it.
  300. if (array_key_exists('default-namespace', $header)) {
  301. $defaultcv = tripal_insert_cv($header['default-namespace'][0], '');
  302. if (!$defaultcv) {
  303. tripal_cv_obo_quiterror('Cannot add namespace ' . $header['default-namespace'][0]);
  304. }
  305. $newcvs[$header['default-namespace'][0]] = $defaultcv->cv_id;
  306. }
  307. // if the 'default-namespace' is missing
  308. else {
  309. // look to see if an 'ontology' key is present. It is part of the v1.4
  310. // specification so it shouldn't be in the file, but just in case
  311. if (array_key_exists('ontology', $header)) {
  312. $defaultcv = tripal_insert_cv(strtoupper($header['ontology'][0]), '');
  313. if (!$defaultcv) {
  314. tripal_cv_obo_quiterror('Cannot add namespace ' . strtoupper($header['ontology'][0]));
  315. }
  316. $newcvs[strtoupper(strtoupper($header['ontology'][0]))] = $defaultcv->cv_id;
  317. }
  318. else {
  319. $defaultcv = tripal_insert_cv('_global', '');
  320. $newcvs['_global'] = $defaultcv->cv_id;
  321. }
  322. watchdog('t_obo_loader', "This OBO is missing the 'default-namespace' header. It is not possible to determine which vocabulary terms without a 'namespace' key should go. Instead, those terms will be placed in the '%vocab' vocabulary.",
  323. array('%vocab' => $defaultcv->name), WATCHDOG_WARNING);
  324. }
  325. // add any typedefs to the vocabulary first
  326. print "\nStep 2: Loading type defs...\n";
  327. tripal_cv_obo_load_typedefs($defaultcv, $newcvs, $default_db, $jobid);
  328. // next add terms to the vocabulary
  329. print "\nStep 3: Loading terms...\n";
  330. if (!tripal_cv_obo_process_terms($defaultcv, $jobid, $newcvs, $default_db)) {
  331. tripal_cv_obo_quiterror('Cannot add terms from this ontology');
  332. }
  333. }
  334. catch (Exception $e) {
  335. $transaction->rollback();
  336. print "\n"; // make sure we start errors on new line
  337. print "FAILED. Rolling back database changes...\n";
  338. watchdog_exception('T_obo_loader', $e);
  339. return FALSE;
  340. }
  341. return TRUE;
  342. }
  343. /**
  344. * Immediately terminates loading of the OBO file.
  345. *
  346. * @param $message
  347. * The error message to present to the user
  348. *
  349. * @ingroup tripal_obo_loader
  350. */
  351. function tripal_cv_obo_quiterror($message) {
  352. tripal_report_error("T_obo_loader", TRIPAL_ERROR, $message, array());
  353. exit;
  354. }
  355. /**
  356. * OBO files are divided into a typedefs terms section and vocabulary terms section.
  357. * This function loads the typedef terms from the OBO.
  358. *
  359. * @param $defaultcv
  360. * A database object containing a record from the cv table for the
  361. * default controlled vocabulary
  362. * @param $newcvs
  363. * An associative array of controlled vocabularies for this OBO. The key must be
  364. * the name of the vocabulary and the value the cv_id from the cv table of chado.
  365. * @param $default_db
  366. * The name of the default database.
  367. * @param $jobid
  368. * The job_id of the job from the Tripal jobs management system.
  369. *
  370. * @ingroup tripal_obo_loader
  371. */
  372. function tripal_cv_obo_load_typedefs($defaultcv, $newcvs, $default_db, $jobid) {
  373. $sql = "SELECT * FROM {tripal_obo_temp} WHERE type = 'Typedef' ";
  374. $typedefs = chado_query($sql);
  375. $sql = "
  376. SELECT count(*) as num_terms
  377. FROM {tripal_obo_temp}
  378. WHERE type = 'Typedef'
  379. ";
  380. $result = chado_query($sql)->fetchObject();
  381. $count = $result->num_terms;
  382. // calculate the interval for updates
  383. $interval = intval($count * 0.0001);
  384. if ($interval < 1) {
  385. $interval = 1;
  386. }
  387. $i = 0;
  388. foreach ($typedefs as $typedef) {
  389. $term = unserialize(base64_decode($typedef->stanza));
  390. // update the job status every interval
  391. if ($jobid and $i % $interval == 0) {
  392. $complete = ($i / $count) * 33.33333333;
  393. tripal_set_job_progress($jobid, intval($complete + 33.33333333));
  394. printf("%d of %d records. (%0.2f%%) Memory: %s bytes\r", $i, $count, $complete * 3, number_format(memory_get_usage()));
  395. }
  396. tripal_cv_obo_process_term($term, $defaultcv->name, 1, $newcvs, $default_db);
  397. $i++;
  398. }
  399. // set the final status
  400. if ($jobid) {
  401. if ($count > 0) {
  402. $complete = ($i / $count) * 33.33333333;
  403. }
  404. else {
  405. $complete = 33.33333333;
  406. }
  407. tripal_set_job_progress($jobid, intval($complete + 33.33333333));
  408. printf("%d of %d records. (%0.2f%%) Memory: %s bytes\r", $i, $count, $complete * 3, number_format(memory_get_usage()));
  409. }
  410. return 1;
  411. }
  412. /**
  413. * OBO files are divided into a typedefs section and a terms section. This
  414. * function loads the typedef terms from the OBO.
  415. *
  416. * @param $defaultcv
  417. * A database object containing a record from the cv table for the
  418. * default controlled vocabulary
  419. * @param $jobid
  420. * The job_id of the job from the Tripal jobs management system.
  421. * @param $newcvs
  422. * An associative array of controlled vocabularies for this OBO. The key must be
  423. * the name of the vocabulary and the value the cv_id from the cv table of chado.
  424. * @param $default_db
  425. * The name of the default database.
  426. * @ingroup tripal_obo_loader
  427. */
  428. function tripal_cv_obo_process_terms($defaultcv, $jobid = NULL, &$newcvs, $default_db) {
  429. $i = 0;
  430. // iterate through each term from the OBO file and add it
  431. $sql = "
  432. SELECT * FROM {tripal_obo_temp}
  433. WHERE type = 'Term'
  434. ORDER BY id
  435. ";
  436. $terms = chado_query($sql);
  437. $sql = "
  438. SELECT count(*) as num_terms
  439. FROM {tripal_obo_temp}
  440. WHERE type = 'Term'
  441. ";
  442. $result = chado_query($sql)->fetchObject();
  443. $count = $result->num_terms;
  444. // calculate the interval for updates
  445. $interval = intval($count * 0.0001);
  446. if ($interval < 1) {
  447. $interval = 1;
  448. }
  449. foreach ($terms as $t) {
  450. $term = unserialize(base64_decode($t->stanza));
  451. // update the job status every interval
  452. if ($jobid and $i % $interval == 0) {
  453. $complete = ($i / $count) * 33.33333333;
  454. tripal_set_job_progress($jobid, intval($complete + 66.666666));
  455. printf("%d of %d records. (%0.2f%%) Memory: %s bytes\r", $i, $count, $complete * 3, number_format(memory_get_usage()));
  456. }
  457. // add/update this term
  458. if (!tripal_cv_obo_process_term($term, $defaultcv->name, 0, $newcvs, $default_db)) {
  459. tripal_cv_obo_quiterror("Failed to process terms from the ontology");
  460. }
  461. $i++;
  462. }
  463. // set the final status
  464. if ($jobid) {
  465. if ($count > 0) {
  466. $complete = ($i / $count) * 33.33333333;
  467. }
  468. else {
  469. $complete = 33.33333333;
  470. }
  471. tripal_set_job_progress($jobid, intval($complete + 66.666666));
  472. printf("%d of %d records. (%0.2f%%) Memory: %s bytes\r", $i, $count, $complete * 3, number_format(memory_get_usage()));
  473. }
  474. return 1;
  475. }
  476. /**
  477. * Uses the provided term array to add/update information to Chado about the
  478. * term including the term, dbxref, synonyms, properties, and relationships.
  479. *
  480. * @param $term
  481. * An array representing the cvterm.
  482. * @param $defaultcv
  483. * The name of the default controlled vocabulary
  484. * @is_relationship
  485. * Set to 1 if this term is a relationship term
  486. * @default_db
  487. * The name of the default database.
  488. *
  489. * @ingroup tripal_obo_loader
  490. */
  491. function tripal_cv_obo_process_term($term, $defaultcv, $is_relationship = 0, &$newcvs, $default_db) {
  492. // make sure we have a namespace for this term
  493. if (!array_key_exists('namespace', $term) and !($defaultcv or $defaultcv == '')) {
  494. tripal_cv_obo_quiterror("Cannot add the term: no namespace defined. " . $term['id'][0]);
  495. }
  496. // construct the term array for sending to the tripal_cv_add_cvterm function
  497. // for adding a new cvterm
  498. $t = array();
  499. $t['id'] = $term['id'][0];
  500. $t['name'] = $term['name'][0];
  501. if (array_key_exists('def', $term)) {
  502. $t['definition'] = $term['def'][0];
  503. }
  504. if (array_key_exists('subset', $term)) {
  505. $t['subset'] = $term['subset'][0];
  506. }
  507. if (array_key_exists('namespace', $term)) {
  508. $t['namespace'] = $term['namespace'][0];
  509. }
  510. if (array_key_exists('is_obsolete', $term)) {
  511. $t['is_obsolete'] = $term['is_obsolete'][0];
  512. }
  513. $t['cv_name'] = $defaultcv;
  514. $t['is_relationship'] = $is_relationship;
  515. $t['db_name'] = $default_db;
  516. // add the cvterm
  517. $cvterm = tripal_insert_cvterm($t, array('update_existing' => TRUE));
  518. if (!$cvterm) {
  519. tripal_cv_obo_quiterror("Cannot add the term " . $term['id'][0]);
  520. }
  521. if (array_key_exists('namespace', $term)) {
  522. $newcvs[$term['namespace'][0]] = $cvterm->cv_id;
  523. }
  524. // now handle other properites
  525. if (array_key_exists('is_anonymous', $term)) {
  526. //print "WARNING: unhandled tag: is_anonymous\n";
  527. }
  528. if (array_key_exists('alt_id', $term)) {
  529. foreach ($term['alt_id'] as $alt_id) {
  530. if (!tripal_cv_obo_add_cvterm_dbxref($cvterm, $alt_id)) {
  531. tripal_cv_obo_quiterror("Cannot add alternate id $alt_id");
  532. }
  533. }
  534. }
  535. if (array_key_exists('subset', $term)) {
  536. //print "WARNING: unhandled tag: subset\n";
  537. }
  538. // add synonyms for this cvterm
  539. if (array_key_exists('synonym', $term)) {
  540. if (!tripal_cv_obo_add_synonyms($term, $cvterm)) {
  541. tripal_cv_obo_quiterror("Cannot add synonyms");
  542. }
  543. }
  544. // reformat the deprecated 'exact_synonym, narrow_synonym, and broad_synonym'
  545. // types to be of the v1.2 standard
  546. if (array_key_exists('exact_synonym', $term) or array_key_exists('narrow_synonym', $term) or array_key_exists('broad_synonym', $term)) {
  547. if (array_key_exists('exact_synonym', $term)) {
  548. foreach ($term['exact_synonym'] as $synonym) {
  549. $new = preg_replace('/^\s*(\".+?\")(.*?)$/', '$1 EXACT $2', $synonym);
  550. $term['synonym'][] = $new;
  551. }
  552. }
  553. if (array_key_exists('narrow_synonym', $term)) {
  554. foreach ($term['narrow_synonym'] as $synonym) {
  555. $new = preg_replace('/^\s*(\".+?\")(.*?)$/', '$1 NARROW $2', $synonym);
  556. $term['synonym'][] = $new;
  557. }
  558. }
  559. if (array_key_exists('broad_synonym', $term)) {
  560. foreach ($term['broad_synonym'] as $synonym) {
  561. $new = preg_replace('/^\s*(\".+?\")(.*?)$/', '$1 BROAD $2', $synonym);
  562. $term['synonym'][] = $new;
  563. }
  564. }
  565. if (!tripal_cv_obo_add_synonyms($term, $cvterm)) {
  566. tripal_cv_obo_quiterror("Cannot add/update synonyms");
  567. }
  568. }
  569. // add the comment to the cvtermprop table
  570. if (array_key_exists('comment', $term)) {
  571. $comments = $term['comment'];
  572. $j = 0;
  573. foreach ($comments as $comment) {
  574. if (!tripal_cv_obo_add_cvterm_prop($cvterm, 'comment', $comment, $j)) {
  575. tripal_cv_obo_quiterror("Cannot add/update cvterm property");
  576. }
  577. $j++;
  578. }
  579. }
  580. // add any other external dbxrefs
  581. if (array_key_exists('xref', $term)) {
  582. foreach ($term['xref'] as $xref) {
  583. if (!tripal_cv_obo_add_cvterm_dbxref($cvterm, $xref)) {
  584. tripal_cv_obo_quiterror("Cannot add/update cvterm database reference (dbxref).");
  585. }
  586. }
  587. }
  588. if (array_key_exists('xref_analog', $term)) {
  589. foreach ($term['xref_analog'] as $xref) {
  590. if (!tripal_cv_obo_add_cvterm_dbxref($cvterm, $xref)) {
  591. tripal_cv_obo_quiterror("Cannot add/update cvterm database reference (dbxref).");
  592. }
  593. }
  594. }
  595. if (array_key_exists('xref_unk', $term)) {
  596. foreach ($term['xref_unk'] as $xref) {
  597. if (!tripal_cv_obo_add_cvterm_dbxref($cvterm, $xref)) {
  598. tripal_cv_obo_quiterror("Cannot add/update cvterm database reference (dbxref).");
  599. }
  600. }
  601. }
  602. // add is_a relationships for this cvterm
  603. if (array_key_exists('is_a', $term)) {
  604. foreach ($term['is_a'] as $is_a) {
  605. if (!tripal_cv_obo_add_relationship($cvterm, $defaultcv, 'is_a', $is_a, $is_relationship, $default_db)) {
  606. tripal_cv_obo_quiterror("Cannot add relationship is_a: $is_a");
  607. }
  608. }
  609. }
  610. if (array_key_exists('intersection_of', $term)) {
  611. //print "WARNING: unhandled tag: intersection_of\n";
  612. }
  613. if (array_key_exists('union_of', $term)) {
  614. //print "WARNING: unhandled tag: union_on\n";
  615. }
  616. if (array_key_exists('disjoint_from', $term)) {
  617. //print "WARNING: unhandled tag: disjoint_from\n";
  618. }
  619. if (array_key_exists('relationship', $term)) {
  620. foreach ($term['relationship'] as $value) {
  621. $rel = preg_replace('/^(.+?)\s.+?$/', '\1', $value);
  622. $object = preg_replace('/^.+?\s(.+?)$/', '\1', $value);
  623. if (!tripal_cv_obo_add_relationship($cvterm, $defaultcv, $rel, $object, $is_relationship, $default_db)) {
  624. tripal_cv_obo_quiterror("Cannot add relationship $rel: $object");
  625. }
  626. }
  627. }
  628. if (array_key_exists('replaced_by', $term)) {
  629. //print "WARNING: unhandled tag: replaced_by\n";
  630. }
  631. if (array_key_exists('consider', $term)) {
  632. //print "WARNING: unhandled tag: consider\n";
  633. }
  634. if (array_key_exists('use_term', $term)) {
  635. //print "WARNING: unhandled tag: user_term\n";
  636. }
  637. if (array_key_exists('builtin', $term)) {
  638. //print "WARNING: unhandled tag: builtin\n";
  639. }
  640. return 1;
  641. }
  642. /**
  643. * Adds a cvterm relationship
  644. *
  645. * @param $cvterm
  646. * A database object for the cvterm
  647. * @param $rel
  648. * The relationship name
  649. * @param $objname
  650. * The relationship term name
  651. * @param $defaultcv
  652. * A database object containing a record from the cv table for the
  653. * default controlled vocabulary
  654. * @object_is_relationship
  655. * Set to 1 if this term is a relationship term
  656. * @default_db
  657. * The name of the default database.
  658. *
  659. * @ingroup tripal_obo_loader
  660. */
  661. function tripal_cv_obo_add_relationship($cvterm, $defaultcv, $rel,
  662. $objname, $object_is_relationship = 0, $default_db = 'OBO_REL') {
  663. // make sure the relationship cvterm exists
  664. $term = array(
  665. 'name' => $rel,
  666. 'id' => "$default_db:$rel",
  667. 'definition' => '',
  668. 'is_obsolete' => 0,
  669. 'cv_name' => $defaultcv,
  670. 'is_relationship' => TRUE,
  671. 'db_naame' => $default_db
  672. );
  673. $relcvterm = tripal_insert_cvterm($term, array('update_existing' => FALSE));
  674. if (!$relcvterm) {
  675. // if the relationship term couldn't be found in the default_db provided
  676. // then do on more check to find it in the relationship ontology
  677. $term = array(
  678. 'name' => $rel,
  679. 'id' => "OBO_REL:$rel",
  680. 'definition' => '',
  681. 'is_obsolete' => 0,
  682. 'cv_name' => $defaultcv,
  683. 'is_relationship' => TRUE,
  684. 'db_name' => 'OBO_REL'
  685. );
  686. $relcvterm = tripal_insert_cvterm($term, array('update_existing' => FALSE));
  687. if (!$relcvterm) {
  688. tripal_cv_obo_quiterror("Cannot find the relationship term in the current ontology or in the relationship ontology: $rel\n");
  689. }
  690. }
  691. // get the object term
  692. $oterm = tripal_cv_obo_get_term($objname);
  693. if (!$oterm) {
  694. tripal_cv_obo_quiterror("Could not find object term $objname\n");
  695. }
  696. $objterm = array();
  697. $objterm['id'] = $oterm['id'][0];
  698. $objterm['name'] = $oterm['name'][0];
  699. if (array_key_exists('def', $oterm)) {
  700. $objterm['definition'] = $oterm['def'][0];
  701. }
  702. if (array_key_exists('subset', $oterm)) {
  703. $objterm['subset'] = $oterm['subset'][0];
  704. }
  705. if (array_key_exists('namespace', $oterm)) {
  706. $objterm['namespace'] = $oterm['namespace'][0];
  707. }
  708. if (array_key_exists('is_obsolete', $oterm)) {
  709. $objterm['is_obsolete'] = $oterm['is_obsolete'][0];
  710. }
  711. $objterm['cv_name' ] = $defaultcv;
  712. $objterm['is_relationship'] = $object_is_relationship;
  713. $objterm['db_name'] = $default_db;
  714. $objcvterm = tripal_insert_cvterm($objterm, array('update_existing' => TRUE));
  715. if (!$objcvterm) {
  716. tripal_cv_obo_quiterror("Cannot add cvterm " . $oterm['name'][0]);
  717. }
  718. // check to see if the cvterm_relationship already exists, if not add it
  719. $values = array(
  720. 'type_id' => $relcvterm->cvterm_id,
  721. 'subject_id' => $cvterm->cvterm_id,
  722. 'object_id' => $objcvterm->cvterm_id
  723. );
  724. $result = chado_select_record('cvterm_relationship', array('*'), $values);
  725. if (count($result) == 0) {
  726. $options = array('return_record' => FALSE);
  727. $success = chado_insert_record('cvterm_relationship', $values, $options);
  728. if (!$success) {
  729. tripal_cv_obo_quiterror("Cannot add term relationship: '$cvterm->name' $rel '$objcvterm->name'");
  730. }
  731. }
  732. return TRUE;
  733. }
  734. /**
  735. * Retreives the term array from the temp loading table for a given term id.
  736. *
  737. * @param id
  738. * The id of the term to retrieve
  739. *
  740. * @ingroup tripal_obo_loader
  741. */
  742. function tripal_cv_obo_get_term($id) {
  743. $values = array('id' => $id);
  744. $result = chado_select_record('tripal_obo_temp', array('stanza'), $values);
  745. if (count($result) == 0) {
  746. return FALSE;
  747. }
  748. return unserialize(base64_decode($result[0]->stanza));
  749. }
  750. /**
  751. * Adds the synonyms to a term
  752. *
  753. * @param term
  754. * An array representing the cvterm. It must have a 'synonym' key/value pair.
  755. * @param cvterm
  756. * The database object of the cvterm to which the synonym will be added.
  757. *
  758. * @ingroup tripal_obo_loader
  759. */
  760. function tripal_cv_obo_add_synonyms($term, $cvterm) {
  761. // make sure we have a 'synonym_type' vocabulary
  762. $syncv = tripal_insert_cv('synonym_type', 'A vocabulary added by the Tripal CV module OBO loader for storing synonym types.');
  763. // now add the synonyms
  764. if (array_key_exists('synonym', $term)) {
  765. foreach ($term['synonym'] as $synonym) {
  766. // separate out the synonym definition and the synonym type
  767. $def = preg_replace('/^\s*"(.*)"\s*.*$/', '\1', $synonym);
  768. // the scope will be 'EXACT', etc...
  769. $scope = drupal_strtolower(preg_replace('/^.*"\s+(.*?)\s+.*$/', '\1', $synonym));
  770. if (!$scope) { // if no scope then default to 'exact'
  771. $scope = 'exact';
  772. }
  773. // make sure the synonym type exists in the 'synonym_type' vocabulary
  774. $values = array(
  775. 'name' => $scope,
  776. 'cv_id' => array(
  777. 'name' => 'synonym_type',
  778. ),
  779. );
  780. $options = array('is_duplicate' => 1);
  781. $results = chado_select_record('cvterm', array('*'), $values, $options);
  782. // if it doesn't exist then add it
  783. if (!$results) {
  784. // build a 'term' object so we can add the missing term
  785. $term = array(
  786. 'name' => $scope,
  787. 'id' => "internal:$scope",
  788. 'definition' => '',
  789. 'is_obsolete' => 0,
  790. 'cv_name' => $syncv->name,
  791. 'is_relationship' => FALSE
  792. );
  793. $syntype = tripal_insert_cvterm($term, array('update_existing' => TRUE));
  794. if (!$syntype) {
  795. tripal_cv_obo_quiterror("Cannot add synonym type: internal:$scope");
  796. }
  797. }
  798. else {
  799. $syntype = $results[0];
  800. }
  801. // make sure the synonym doesn't already exists
  802. $values = array(
  803. 'cvterm_id' => $cvterm->cvterm_id,
  804. 'synonym' => $def
  805. );
  806. $results = chado_select_record('cvtermsynonym', array('*'), $values);
  807. if (count($results) == 0) {
  808. $values = array(
  809. 'cvterm_id' => $cvterm->cvterm_id,
  810. 'synonym' => $def,
  811. 'type_id' => $syntype->cvterm_id
  812. );
  813. $options = array('return_record' => FALSE);
  814. $success = chado_insert_record('cvtermsynonym', $values, $options);
  815. if (!$success) {
  816. tripal_cv_obo_quiterror("Failed to insert the synonym for term: $name ($def)");
  817. }
  818. }
  819. // now add the dbxrefs for the synonym if we have a comma in the middle
  820. // of a description then this will cause problems when splitting os lets
  821. // just change it so it won't mess up our splitting and then set it back
  822. // later.
  823. /**
  824. $synonym = preg_replace('/(".*?),\s(.*?")/','$1,_$2',$synonym);
  825. $dbxrefs = preg_split("/, /",preg_replace('/^.*\[(.*?)\]$/','\1',$synonym));
  826. foreach ($dbxrefs as $dbxref) {
  827. $dbxref = preg_replace('/,_/',", ",$dbxref);
  828. if ($dbxref) {
  829. tripal_cv_obo_add_cvterm_dbxref($syn,$dbxref);
  830. }
  831. }
  832. */
  833. }
  834. }
  835. return TRUE;
  836. }
  837. /**
  838. * Parse the OBO file and populate the templ loading table
  839. *
  840. * @param $file
  841. * The path on the file system where the ontology can be found
  842. * @param $header
  843. * An array passed by reference that will be populated with the header
  844. * information from the OBO file
  845. * @param $jobid
  846. * The job_id of the job from the Tripal jobs management system.
  847. *
  848. * @ingroup tripal_obo_loader
  849. */
  850. function tripal_cv_obo_parse($obo_file, &$header, $jobid) {
  851. $in_header = 1;
  852. $stanza = array();
  853. $default_db = '_global';
  854. $line_num = 0;
  855. $num_read = 0;
  856. $intv_read = 0;
  857. $filesize = filesize($obo_file);
  858. $interval = intval($filesize * 0.01);
  859. if ($interval < 1) {
  860. $interval = 1;
  861. }
  862. // iterate through the lines in the OBO file and parse the stanzas
  863. $fh = fopen($obo_file, 'r');
  864. while ($line = fgets($fh)) {
  865. $line_num++;
  866. $size = drupal_strlen($line);
  867. $num_read += $size;
  868. $intv_read += $size;
  869. $line = trim($line);
  870. // update the job status every 1% features
  871. if ($jobid and $intv_read >= $interval) {
  872. $percent = sprintf("%.2f", ($num_read / $filesize) * 100);
  873. print "Parsing Line $line_num (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes.\r";
  874. tripal_set_job_progress($jobid, intval(($num_read / $filesize) * 33.33333333));
  875. $intv_read = 0;
  876. }
  877. // remove newlines
  878. $line = rtrim($line);
  879. // remove any special characters that may be hiding
  880. $line = preg_replace('/[^(\x20-\x7F)]*/', '', $line);
  881. // skip empty lines
  882. if (strcmp($line, '') == 0) {
  883. continue;
  884. }
  885. //remove comments from end of lines
  886. $line = preg_replace('/^(.*?)\!.*$/', '\1', $line); // TODO: if the explamation is escaped
  887. // at the first stanza we're out of header
  888. if (preg_match('/^\s*\[/', $line)) {
  889. $in_header = 0;
  890. // store the stanza we just finished reading
  891. if (sizeof($stanza) > 0) {
  892. // add the term to the temp table
  893. $values = array(
  894. 'id' => $stanza['id'][0],
  895. 'stanza' => base64_encode(serialize($stanza)),
  896. 'type' => $type,
  897. );
  898. $success = chado_insert_record('tripal_obo_temp', $values);
  899. if (!$success) {
  900. tripal_report_error('T_obo_loader', "ERROR: Cannot insert stanza into temporary table.", array(), 'error');
  901. exit;
  902. }
  903. }
  904. // get the stanza type: Term, Typedef or Instance
  905. $type = preg_replace('/^\s*\[\s*(.+?)\s*\]\s*$/', '\1', $line);
  906. // start fresh with a new array
  907. $stanza = array();
  908. continue;
  909. }
  910. // break apart the line into the tag and value but ignore any escaped colons
  911. preg_replace("/\\:/", "|-|-|", $line); // temporarily replace escaped colons
  912. $pair = explode(":", $line, 2);
  913. $tag = $pair[0];
  914. $value = ltrim(rtrim($pair[1]));// remove surrounding spaces
  915. // if this is the ID then look for the default DB
  916. $matches = array();
  917. if ($tag == 'id' and preg_match('/^(.+?):.*$/', $value, $matches)) {
  918. $default_db = $matches[1];
  919. }
  920. $tag = preg_replace("/\|-\|-\|/", "\:", $tag); // return the escaped colon
  921. $value = preg_replace("/\|-\|-\|/", "\:", $value);
  922. if ($in_header) {
  923. if (!array_key_exists($tag, $header)) {
  924. $header[$tag] = array();
  925. }
  926. $header[$tag][] = $value;
  927. }
  928. else {
  929. if (!array_key_exists($tag, $stanza)) {
  930. $stanza[$tag] = array();
  931. }
  932. $stanza[$tag][] = $value;
  933. }
  934. }
  935. // now add the last term in the file
  936. if (sizeof($stanza) > 0) {
  937. $values = array(
  938. 'id' => $stanza['id'][0],
  939. 'stanza' => base64_encode(serialize($stanza)),
  940. 'type' => $type,
  941. );
  942. chado_insert_record('tripal_obo_temp', $values);
  943. if (!$success) {
  944. tripal_report_error('T_obo_loader', "ERROR: Cannot insert stanza into temporary table.", array(), 'error');
  945. exit;
  946. }
  947. $percent = sprintf("%.2f", ($num_read / $filesize) * 100);
  948. print "Parsing Line $line_num (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes.\r";
  949. tripal_set_job_progress($jobid, intval(($num_read / $filesize) * 33.33333333));
  950. }
  951. return $default_db;
  952. }
  953. /**
  954. * Adds a database reference to a cvterm
  955. *
  956. * @param cvterm
  957. * The database object of the cvterm to which the synonym will be added.
  958. * @param xref
  959. * The cross refernce. It should be of the form from the OBO specification
  960. *
  961. * @ingroup tripal_obo_loader
  962. */
  963. function tripal_cv_obo_add_cvterm_dbxref($cvterm, $xref) {
  964. $dbname = preg_replace('/^(.+?):.*$/', '$1', $xref);
  965. $accession = preg_replace('/^.+?:\s*(.*?)(\{.+$|\[.+$|\s.+$|\".+$|$)/', '$1', $xref);
  966. $description = preg_replace('/^.+?\"(.+?)\".*?$/', '$1', $xref);
  967. $dbxrefs = preg_replace('/^.+?\[(.+?)\].*?$/', '$1', $xref);
  968. if (!$accession) {
  969. tripal_cv_obo_quiterror();
  970. tripal_report_error("T_obo_loader", TRIPAL_WARNING, "Cannot add a dbxref without an accession: '$xref'", NULL);
  971. return FALSE;
  972. }
  973. // if the xref is a database link, handle that specially
  974. if (strcmp($dbname, 'http') == 0) {
  975. $accession = $xref;
  976. $dbname = 'URL';
  977. }
  978. // add the database
  979. $db = tripal_insert_db(array('name' => $dbname));
  980. if (!$db) {
  981. tripal_cv_obo_quiterror("Cannot find database '$dbname' in Chado.");
  982. }
  983. // now add the dbxref
  984. $dbxref = tripal_cv_obo_add_dbxref($db->db_id, $accession, '', $description);
  985. if (!$dbxref) {
  986. tripal_cv_obo_quiterror("Cannot find or add the database reference (dbxref)");
  987. }
  988. // finally add the cvterm_dbxref but first check to make sure it exists
  989. $values = array(
  990. 'cvterm_id' => $cvterm->cvterm_id,
  991. 'dbxref_id' => $dbxref->dbxref_id,
  992. );
  993. $result = chado_select_record('cvterm_dbxref', array('*'), $values);
  994. if (count($result) == 0) {
  995. $ins_options = array('return_record' => FALSE);
  996. $result = chado_insert_record('cvterm_dbxref', $values, $ins_options);
  997. if (!$result) {
  998. tripal_cv_obo_quiterror("Cannot add cvterm_dbxref: $xref");
  999. return FALSE;
  1000. }
  1001. }
  1002. return TRUE;
  1003. }
  1004. /**
  1005. * Adds a property to a cvterm
  1006. *
  1007. * @param cvterm
  1008. * A database object for the cvterm to which properties will be added
  1009. * @param $property
  1010. * The name of the property to add
  1011. * @param $value
  1012. * The value of the property
  1013. * @param rank
  1014. * The rank of the property
  1015. *
  1016. * @ingroup tripal_obo_loader
  1017. */
  1018. function tripal_cv_obo_add_cvterm_prop($cvterm, $property, $value, $rank) {
  1019. // make sure the 'cvterm_property_type' CV exists
  1020. $cv = tripal_insert_cv('cvterm_property_type', '');
  1021. if (!$cv) {
  1022. tripal_cv_obo_quiterror("Cannot add/find cvterm_property_type cvterm");
  1023. }
  1024. // get the property type cvterm. If it doesn't exist then we want to add it
  1025. $values = array(
  1026. 'name' => $property,
  1027. 'cv_id' => $cv->cv_id,
  1028. );
  1029. $results = chado_select_record('cvterm', array('*'), $values);
  1030. if (count($results) == 0) {
  1031. $term = array(
  1032. 'name' => $property,
  1033. 'id' => "internal:$property",
  1034. 'definition' => '',
  1035. 'is_obsolete' => 0,
  1036. 'cv_name' => $cv->name,
  1037. 'is_relationship' => FALSE,
  1038. );
  1039. $cvproptype = tripal_insert_cvterm($term, array('update_existing' => FALSE));
  1040. if (!$cvproptype) {
  1041. tripal_cv_obo_quiterror("Cannot add cvterm property: internal:$property");
  1042. return FALSE;
  1043. }
  1044. }
  1045. else {
  1046. $cvproptype = $results[0];
  1047. }
  1048. // remove any properties that currently exist for this term. We'll reset them
  1049. if ($rank == 0) {
  1050. $values = array('cvterm_id' => $cvterm->cvterm_id);
  1051. $success = chado_delete_record('cvtermprop', $values);
  1052. if (!$success) {
  1053. tripal_cv_obo_quiterror("Could not remove existing properties to update property $property for term\n");
  1054. return FALSE;
  1055. }
  1056. }
  1057. // now add the property
  1058. $values = array(
  1059. 'cvterm_id' => $cvterm->cvterm_id,
  1060. 'type_id' => $cvproptype->cvterm_id,
  1061. 'value' => $value,
  1062. 'rank' => $rank,
  1063. );
  1064. $options = array('return_record' => FALSE);
  1065. $result = chado_insert_record('cvtermprop', $values, $options);
  1066. if (!$result) {
  1067. tripal_cv_obo_quiterror("Could not add property $property for term\n");
  1068. return FALSE;
  1069. }
  1070. return TRUE;
  1071. }
  1072. /**
  1073. * Adds a database cross reference to a cvterm
  1074. *
  1075. * @param db_id
  1076. * The database ID of the cross reference
  1077. * @param accession
  1078. * The cross reference's accession
  1079. * @param $version
  1080. * The version of the dbxref
  1081. * @param $description
  1082. * The description of the cross reference
  1083. *
  1084. * @ingroup tripal_obo_loader
  1085. */
  1086. function tripal_cv_obo_add_dbxref($db_id, $accession, $version='', $description='') {
  1087. // check to see if the dbxref exists if not, add it
  1088. $values = array(
  1089. 'db_id' => $db_id,
  1090. 'accession' => $accession,
  1091. );
  1092. $result = chado_select_record('dbxref', array('dbxref_id'), $values);
  1093. if (count($result) == 0) {
  1094. $ins_values = array(
  1095. 'db_id' => $db_id,
  1096. 'accession' => $accession,
  1097. 'version' => $version,
  1098. 'description' => $description,
  1099. );
  1100. $ins_options = array('return_record' => FALSE);
  1101. $result = chado_insert_record('dbxref', $ins_values, $ins_options);
  1102. if (!$result) {
  1103. tripal_cv_obo_quiterror("Failed to insert the dbxref record $accession");
  1104. return FALSE;
  1105. }
  1106. $result = chado_select_record('dbxref', array('dbxref_id'), $values, $options);
  1107. }
  1108. return $result[0];
  1109. }