tripal_cv.obo_loader.inc 40 KB

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