obo_loader.inc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. <?php
  2. /**
  3. * @file
  4. * Tripal Ontology Loader
  5. *
  6. * @defgroup tripal_obo_loader Tripal Ontology Loader
  7. * @ingroup tripal_cv
  8. */
  9. /**
  10. *
  11. * @ingroup tripal_obo_loader
  12. */
  13. function tripal_cv_load_obo_v1_2_id($obo_id, $jobid = NULL) {
  14. // get the OBO reference
  15. $sql = "SELECT * FROM {tripal_cv_obo} WHERE obo_id = %d";
  16. $obo = db_fetch_object(db_query($sql, $obo_id));
  17. // if the reference is for a remote URL then run the URL processing function
  18. if (preg_match("/^http:\/\//", $obo->path) or preg_match("/^ftp:\/\//", $obo->path)) {
  19. tripal_cv_load_obo_v1_2_url($obo->name, $obo->path, $jobid, 0);
  20. }
  21. // if the reference is for a local file then run the file processing function
  22. else {
  23. // check to see if the file is located local to Drupal
  24. $dfile = $_SERVER['DOCUMENT_ROOT'] . base_path() . $obo->path;
  25. if (file_exists($dfile)) {
  26. tripal_cv_load_obo_v1_2_file($obo->name, $dfile , $jobid, 0);
  27. }
  28. // if not local to Drupal, the file must be someplace else, just use
  29. // the full path provided
  30. else {
  31. if (file_exists($obo->path)) {
  32. tripal_cv_load_obo_v1_2_file($obo->name, $obo->path, $jobid, 0);
  33. }
  34. else {
  35. print "ERROR: counld not find OBO file: '$obo->path'\n";
  36. }
  37. }
  38. }
  39. }
  40. /**
  41. *
  42. * @ingroup tripal_obo_loader
  43. */
  44. function tripal_cv_load_obo_v1_2_file($obo_name, $file, $jobid = NULL, $is_new = TRUE) {
  45. $newcvs = array();
  46. tripal_cv_load_obo_v1_2($file, $jobid, $newcvs);
  47. if ($is_new) {
  48. tripal_cv_load_obo_add_ref($obo_name, $file);
  49. }
  50. // update the cvtermpath table
  51. tripal_cv_load_update_cvtermpath($newcvs, $jobid);
  52. print "Ontology Sucessfully loaded!\n";
  53. }
  54. /**
  55. *
  56. * @ingroup tripal_obo_loader
  57. */
  58. function tripal_cv_load_obo_v1_2_url($obo_name, $url, $jobid = NULL, $is_new = TRUE) {
  59. $newcvs = array();
  60. // first download the OBO
  61. $temp = tempnam(sys_get_temp_dir(), 'obo_');
  62. print "Downloading URL $url, saving to $temp\n";
  63. $url_fh = fopen($url, "r");
  64. $obo_fh = fopen($temp, "w");
  65. if (!$url_fh) {
  66. tripal_cv_obo_quiterror("Unable to download the remote OBO file at $url. Could a firewall be blocking outgoing connections? ".
  67. " if you are unable to download the file you may manually downlod the OBO file and use the web interface to ".
  68. " specify the location of the file on your server.");
  69. }
  70. while (!feof($url_fh)) {
  71. fwrite($obo_fh, fread($url_fh, 255), 255);
  72. }
  73. fclose($url_fh);
  74. fclose($obo_fh);
  75. // second, parse the OBO
  76. tripal_cv_load_obo_v1_2($temp, $jobid, $newcvs);
  77. // now remove the temp file
  78. unlink($temp);
  79. if ($is_new) {
  80. tripal_cv_load_obo_add_ref($obo_name, $url);
  81. }
  82. // update the cvtermpath table
  83. tripal_cv_load_update_cvtermpath($newcvs, $jobid);
  84. print "Ontology Sucessfully loaded!\n";
  85. }
  86. /**
  87. *
  88. * @ingroup tripal_obo_loader
  89. */
  90. function tripal_cv_load_update_cvtermpath($newcvs, $jobid) {
  91. print "\nUpdating cvtermpath table. This may take a while...\n";
  92. foreach ($newcvs as $namespace => $cvid) {
  93. tripal_cv_update_cvtermpath($cvid, $jobid);
  94. }
  95. }
  96. /**
  97. * Add the obo to the tripal_cv_obo table in the Drupal database
  98. */
  99. function tripal_cv_load_obo_add_ref($name, $path) {
  100. $isql = "INSERT INTO {tripal_cv_obo} (name,path) VALUES ('%s','%s')";
  101. db_query($isql, $name, $path);
  102. }
  103. /**
  104. *
  105. * @ingroup tripal_obo_loader
  106. */
  107. function tripal_cv_load_obo_v1_2($file, $jobid = NULL, &$newcvs) {
  108. // we need to get a persistent connection. If one exists this function
  109. // will not recreate it, but if not it will create one and store it in
  110. // a Drupal variable for reuse later.
  111. $connection = tripal_db_persistent_chado();
  112. $header = array();
  113. $obo = array();
  114. print "Opening File $file\n";
  115. // make sure we have an 'internal' and a '_global' database
  116. if (!tripal_db_add_db('internal')) {
  117. tripal_cv_obo_quiterror("Cannot add 'internal' database");
  118. }
  119. if (!tripal_db_add_db('_global')) {
  120. tripal_cv_obo_quiterror("Cannot add '_global' database");
  121. }
  122. // parse the obo file
  123. $default_db = tripal_cv_obo_parse($file, $obo, $header);
  124. // add the CV for this ontology to the database
  125. $defaultcv = tripal_cv_add_cv($header['default-namespace'][0], '');
  126. if (!$defaultcv) {
  127. tripal_cv_obo_quiterror('Cannot add namespace ' . $header['default-namespace'][0]);
  128. }
  129. $newcvs[$header['default-namespace'][0]] = $defaultcv->cv_id;
  130. // add any typedefs to the vocabulary first
  131. $typedefs = $obo['Typedef'];
  132. foreach ($typedefs as $typedef) {
  133. $t = array();
  134. $t['id'] = $typedef['id'][0];
  135. $t['name'] = $typedef['name'][0];
  136. $t['def'] = $typedef['def'][0];
  137. $t['subset'] = $typedef['subset'][0];
  138. tripal_cv_obo_process_term($t, $defaultcv->name, $obo, 1, $newcvs, $default_db);
  139. }
  140. // next add terms to the vocabulary
  141. $terms = $obo['Term'];
  142. if (!tripal_cv_obo_process_terms($terms, $defaultcv->name, $obo, $jobid, $newcvs, $default_db)) {
  143. tripal_cv_obo_quiterror('Cannot add terms from this ontology');
  144. }
  145. return;
  146. }
  147. /**
  148. *
  149. * @ingroup tripal_obo_loader
  150. */
  151. function tripal_cv_obo_quiterror($message) {
  152. watchdog("T_obo_loader", $message, array(), WATCHDOG_ERROR);;
  153. exit;
  154. }
  155. /**
  156. *
  157. * @ingroup tripal_obo_loader
  158. */
  159. function tripal_cv_obo_process_terms($terms, $defaultcv, $obo, $jobid = NULL, &$newcvs, $default_db) {
  160. $i = 0;
  161. $count = sizeof($terms);
  162. $interval = intval($count * 0.01);
  163. if ($interval > 1) {
  164. $interval = 1;
  165. }
  166. // iterate through each term from the OBO file and add it
  167. print "Loading terms...\n";
  168. foreach ($terms as $term) {
  169. // update the job status every 1% terms
  170. if ($jobid and $i % $interval == 0) {
  171. $complete = ($i / $count) * 100;
  172. tripal_job_set_progress($jobid, intval($complete));
  173. printf("%d of %d records. (%0.2f%%) memory: %d\r", $i, $count, $complete, memory_get_usage());
  174. }
  175. // add/update this term
  176. $t = array();
  177. $t['id'] = $term['id'][0];
  178. $t['name'] = $term['name'][0];
  179. $t['def'] = $term['def'][0];
  180. $t['subset'] = $term['subset'][0];
  181. $t['namespace'] = $term['namespace'][0];
  182. $t['is_obsolete'] = $term['is_obsolete'][0];
  183. if (!tripal_cv_obo_process_term($t, $defaultcv, $obo, 0, $newcvs, $default_db)) {
  184. tripal_cv_obo_quiterror("Failed to process terms from the ontology");
  185. }
  186. $i++;
  187. }
  188. // set the final status
  189. if ($jobid) {
  190. $complete = ($i / $count) * 100;
  191. tripal_job_set_progress($jobid, intval($complete));
  192. printf("%d of %d records. (%0.2f%%) memory: %d\r", $i, $count, $complete, memory_get_usage());
  193. }
  194. return 1;
  195. }
  196. /**
  197. *
  198. * @ingroup tripal_obo_loader
  199. */
  200. function tripal_cv_obo_process_term($term, $defaultcv, $obo, $is_relationship = 0, &$newcvs, $default_db) {
  201. // add the cvterm
  202. $cvterm = tripal_cv_add_cvterm($term, $defaultcv, $is_relationship, 1, $default_db);
  203. if (!$cvterm) {
  204. tripal_cv_obo_quiterror("Cannot add the term " . $term['id']);
  205. }
  206. if ($term['namespace']) {
  207. $newcvs[$term['namespace']] = $cvterm->cv_id;
  208. }
  209. // now handle other properites
  210. if (isset($term['is_anonymous'])) {
  211. //print "WARNING: unhandled tag: is_anonymous\n";
  212. }
  213. if (isset($term['alt_id'])) {
  214. foreach ($term['alt_id'] as $alt_id) {
  215. if (!tripal_cv_obo_add_cvterm_dbxref($cvterm, $alt_id)) {
  216. tripal_cv_obo_quiterror("Cannot add alternate id $alt_id");
  217. }
  218. }
  219. }
  220. if (isset($term['subset'])) {
  221. //print "WARNING: unhandled tag: subset\n";
  222. }
  223. // add synonyms for this cvterm
  224. if (isset($term['synonym'])) {
  225. if (!tripal_cv_obo_add_synonyms($term, $cvterm)) {
  226. tripal_cv_obo_quiterror("Cannot add synonyms");
  227. }
  228. }
  229. // reformat the deprecated 'exact_synonym, narrow_synonym, and broad_synonym'
  230. // types to be of the v1.2 standard
  231. if (isset($term['exact_synonym']) or isset($term['narrow_synonym']) or isset($term['broad_synonym'])) {
  232. if (isset($term['exact_synonym'])) {
  233. foreach ($term['exact_synonym'] as $synonym) {
  234. $new = preg_replace('/^\s*(\".+?\")(.*?)$/', '$1 EXACT $2', $synonym);
  235. $term['synonym'][] = $new;
  236. }
  237. }
  238. if (isset($term['narrow_synonym'])) {
  239. foreach ($term['narrow_synonym'] as $synonym) {
  240. $new = preg_replace('/^\s*(\".+?\")(.*?)$/', '$1 NARROW $2', $synonym);
  241. $term['synonym'][] = $new;
  242. }
  243. }
  244. if (isset($term['broad_synonym'])) {
  245. foreach ($term['broad_synonym'] as $synonym) {
  246. $new = preg_replace('/^\s*(\".+?\")(.*?)$/', '$1 BROAD $2', $synonym);
  247. $term['synonym'][] = $new;
  248. }
  249. }
  250. if (!tripal_cv_obo_add_synonyms($term, $cvterm)) {
  251. tripal_cv_obo_quiterror("Cannot add/update synonyms");
  252. }
  253. }
  254. // add the comment to the cvtermprop table
  255. if (isset($term['comment'])) {
  256. $comments = $term['comment'];
  257. $j = 0;
  258. foreach ($comments as $comment) {
  259. if (!tripal_cv_obo_add_cvterm_prop($cvterm, 'comment', $comment, $j)) {
  260. tripal_cv_obo_quiterror("Cannot add/update cvterm property");
  261. }
  262. $j++;
  263. }
  264. }
  265. // add any other external dbxrefs
  266. if (isset($term['xref'])) {
  267. foreach ($term['xref'] as $xref) {
  268. if (!tripal_cv_obo_add_cvterm_dbxref($cvterm, $xref)) {
  269. tripal_cv_obo_quiterror("Cannot add/update cvterm database reference (dbxref).");
  270. }
  271. }
  272. }
  273. if (isset($term['xref_analog'])) {
  274. foreach ($term['xref_analog'] as $xref) {
  275. if (!tripal_cv_obo_add_cvterm_dbxref($cvterm, $xref)) {
  276. tripal_cv_obo_quiterror("Cannot add/update cvterm database reference (dbxref).");
  277. }
  278. }
  279. }
  280. if (isset($term['xref_unk'])) {
  281. foreach ($term['xref_unk'] as $xref) {
  282. if (!tripal_cv_obo_add_cvterm_dbxref($cvterm, $xref)) {
  283. tripal_cv_obo_quiterror("Cannot add/update cvterm database reference (dbxref).");
  284. }
  285. }
  286. }
  287. // add is_a relationships for this cvterm
  288. if (isset($term['is_a'])) {
  289. foreach ($term['is_a'] as $is_a) {
  290. if (!tripal_cv_obo_add_relationship($cvterm, $defaultcv, $obo, 'is_a', $is_a, $is_relationship)) {
  291. tripal_cv_obo_quiterror("Cannot add relationship is_a: $is_a");
  292. }
  293. }
  294. }
  295. if (isset($term['intersection_of'])) {
  296. //print "WARNING: unhandled tag: intersection_of\n";
  297. }
  298. if (isset($term['union_of'])) {
  299. //print "WARNING: unhandled tag: union_on\n";
  300. }
  301. if (isset($term['disjoint_from'])) {
  302. //print "WARNING: unhandled tag: disjoint_from\n";
  303. }
  304. if (isset($term['relationship'])) {
  305. foreach ($term['relationship'] as $value) {
  306. $rel = preg_replace('/^(.+?)\s.+?$/', '\1', $value);
  307. $object = preg_replace('/^.+?\s(.+?)$/', '\1', $value);
  308. if (!tripal_cv_obo_add_relationship($cvterm, $defaultcv, $obo, $rel, $object, $is_relationship)) {
  309. tripal_cv_obo_quiterror("Cannot add relationship $rel: $object");
  310. }
  311. }
  312. }
  313. if (isset($term['replaced_by'])) {
  314. //print "WARNING: unhandled tag: replaced_by\n";
  315. }
  316. if (isset($term['consider'])) {
  317. //print "WARNING: unhandled tag: consider\n";
  318. }
  319. if (isset($term['use_term'])) {
  320. //print "WARNING: unhandled tag: user_term\n";
  321. }
  322. if (isset($term['builtin'])) {
  323. //print "WARNING: unhandled tag: builtin\n";
  324. }
  325. return 1;
  326. }
  327. /**
  328. * Add a cvterm relationship
  329. *
  330. * @ingroup tripal_obo_loader
  331. */
  332. function tripal_cv_obo_add_relationship($cvterm, $defaultcv, $obo, $rel, $objname, $object_is_relationship = 0) {
  333. // make sure the relationship cvterm exists
  334. $term = array(
  335. 'name' => array($rel),
  336. 'id' => array($rel),
  337. 'definition' => array(''),
  338. 'is_obsolete' => array(0),
  339. );
  340. $relcvterm = tripal_cv_add_cvterm($term, $defaultcv, 1, 0);
  341. if (!$relcvterm) {
  342. tripal_cv_obo_quiterror("Cannot find or insert the relationship term: $rel\n");
  343. }
  344. // get the object term
  345. $objterm = tripal_cv_obo_get_term($obo, $objname);
  346. if (!$objterm) {
  347. tripal_cv_obo_quiterror("Could not find object term $objname\n");
  348. }
  349. $objcvterm = tripal_cv_add_cvterm($objterm, $defaultcv, $object_is_relationship, 1);
  350. if (!$objcvterm) {
  351. tripal_cv_obo_quiterror("Cannot add/find cvterm");
  352. }
  353. // check to see if the cvterm_relationship already exists, if not add it
  354. $values = array(
  355. 'type_id' => $relcvterm->cvterm_id,
  356. 'subject_id' => $cvterm->cvterm_id,
  357. 'object_id' => $objcvterm->cvterm_id
  358. );
  359. $options = array('statement_name' => 'sel_cvtermrelationship_tysuob');
  360. $result = tripal_core_chado_select('cvterm_relationship', array('*'), $values, $options);
  361. if (count($result) == 0) {
  362. $options = array('statement_name' => 'ins_cvtermrelationship_tysuob');
  363. $sql = "INSERT INTO {cvterm_relationship} ".
  364. "(type_id,subject_id,object_id) VALUES (%d,%d,%d)";
  365. $success = tripal_core_chado_insert('cvterm_relationship', $values, $options);
  366. if (!$success) {
  367. tripal_cv_obo_quiterror("Cannot add term relationship: '$cvterm->name' $rel '$objcvterm->name'");
  368. }
  369. }
  370. return TRUE;
  371. }
  372. /**
  373. *
  374. * @ingroup tripal_obo_loader
  375. */
  376. function tripal_cv_obo_get_term($obo, $id) {
  377. foreach ($obo as $type) {
  378. foreach ($type as $term) {
  379. $accession = $term['id'][0];
  380. if (strcmp($accession, $id)==0) {
  381. return $term;
  382. }
  383. }
  384. }
  385. return FALSE;
  386. }
  387. /**
  388. *
  389. * @ingroup tripal_obo_loader
  390. */
  391. function tripal_cv_obo_add_synonyms($term, $cvterm) {
  392. // make sure we have a 'synonym_type' vocabulary
  393. $syncv = tripal_cv_add_cv('synonym_type');
  394. // now add the synonyms
  395. if (isset($term['synonym'])) {
  396. foreach ($term['synonym'] as $synonym) {
  397. // separate out the synonym definition and the synonym type
  398. $def = preg_replace('/^\s*"(.*)"\s*.*$/', '\1', $synonym);
  399. $type = drupal_strtolower(preg_replace('/^.*"\s+(.*?)\s+.*$/', '\1', $synonym));
  400. // make sure the synonym type exists in the 'synonym_type' vocabulary
  401. $values = array(
  402. 'name' => $type,
  403. 'cv_id' => array(
  404. 'name' => 'synonym_type',
  405. ),
  406. );
  407. $options = array('statement_name' => 'sel_cvterm_nacv');
  408. $results = tripal_core_chado_select('cvterm', array('*'), $values, $options);
  409. // if it doesn't exist then add it
  410. if (count($results) == 0) {
  411. // build a 'term' object so we can add the missing term
  412. $term = array(
  413. 'name' => array($type),
  414. 'id' => array("internal:$type"),
  415. 'definition' => array(''),
  416. 'is_obsolete' => array(0),
  417. );
  418. $syntype = tripal_cv_add_cvterm($term, $syncv, 0, 1);
  419. if (!$syntype) {
  420. tripal_cv_obo_quiterror("Cannot add synonym type: internal:$type");
  421. }
  422. }
  423. // make sure the synonym doesn't already exists
  424. $values = array(
  425. 'cvterm_id' => $cvterm->cvterm_id,
  426. 'synonym' => $def
  427. );
  428. $options = array('statement_name' => 'sel_cvtermsynonym_cvsy');
  429. $results = tripal_core_chado_select('cvtermsynonym', array('*'), $values, $options);
  430. if (count($results) == 0) {
  431. $values = array(
  432. 'cvterm_id' => $cvterm->cvterm_id,
  433. 'synonym' => $def,
  434. 'type_id' => $syntype->cvterm_id
  435. );
  436. $options = array('statement_name' => 'ins_cvtermsynonym_cvsy');
  437. $success = tripal_core_chado_insert('cvtermsynonym', $values, $options);
  438. if (!$success) {
  439. tripal_cv_obo_quiterror("Failed to insert the synonym for term: $name ($def)");
  440. }
  441. }
  442. // now add the dbxrefs for the synonym if we have a comma in the middle
  443. // of a description then this will cause problems when splitting os lets
  444. // just change it so it won't mess up our splitting and then set it back
  445. // later.
  446. /**
  447. $synonym = preg_replace('/(".*?),\s(.*?")/','$1,_$2',$synonym);
  448. $dbxrefs = preg_split("/, /",preg_replace('/^.*\[(.*?)\]$/','\1',$synonym));
  449. foreach ($dbxrefs as $dbxref) {
  450. $dbxref = preg_replace('/,_/',", ",$dbxref);
  451. if ($dbxref) {
  452. tripal_cv_obo_add_cvterm_dbxref($syn,$dbxref);
  453. }
  454. }
  455. */
  456. }
  457. }
  458. return TRUE;
  459. }
  460. /**
  461. * Actually parse the OBO file
  462. *
  463. * @ingroup tripal_obo_loader
  464. */
  465. function tripal_cv_obo_parse($obo_file, &$obo, &$header) {
  466. $i = 0;
  467. $in_header = 1;
  468. $stanza = array();
  469. $default_db = '_global';
  470. // iterate through the lines in the OBO file and parse the stanzas
  471. $fh = fopen($obo_file, 'r');
  472. while ($line = fgets($fh)) {
  473. $i++;
  474. // remove newlines
  475. $line = rtrim($line);
  476. // remove any special characters that may be hiding
  477. $line = preg_replace('/[^(\x20-\x7F)]*/', '', $line);
  478. // skip empty lines
  479. if (strcmp($line, '') == 0) {
  480. continue;
  481. }
  482. //remove comments from end of lines
  483. $line = preg_replace('/^(.*?)\!.*$/', '\1', $line); // TODO: if the explamation is escaped
  484. if (preg_match('/^\s*\[/', $line)) { // at the first stanza we're out of header
  485. $in_header = 0;
  486. // load the stanza we just finished reading
  487. if (sizeof($stanza) > 0) {
  488. if (!isset($obo[$type])) {
  489. $obo[$type] = array();
  490. }
  491. if (!isset($obo[$type][$stanza['id'][0]])) {
  492. $obo[$type][$stanza['id'][0]] = $stanza;
  493. }
  494. else {
  495. array_merge($obo[$type][$stanza['id'][0]], $stanza);
  496. }
  497. }
  498. // get the stanza type: Term, Typedef or Instance
  499. $type = preg_replace('/^\s*\[\s*(.+?)\s*\]\s*$/', '\1', $line);
  500. // start fresh with a new array
  501. $stanza = array();
  502. continue;
  503. }
  504. // break apart the line into the tag and value but ignore any escaped colons
  505. preg_replace("/\\:/", "|-|-|", $line); // temporarily replace escaped colons
  506. $pair = explode(":", $line, 2);
  507. $tag = $pair[0];
  508. $value = ltrim(rtrim($pair[1]));// remove surrounding spaces
  509. // look for the default DB
  510. $matches = array();
  511. if ($tag == 'id' and preg_match('/^(.+?):.*$/', $value, $matches)) {
  512. $default_db = $matches[1];
  513. }
  514. $tag = preg_replace("/\|-\|-\|/", "\:", $tag); // return the escaped colon
  515. $value = preg_replace("/\|-\|-\|/", "\:", $value);
  516. if ($in_header) {
  517. if (!isset($header[$tag])) {
  518. $header[$tag] = array();
  519. }
  520. $header[$tag][] = $value;
  521. }
  522. else {
  523. if (!isset($stanza[$tag])) {
  524. $stanza[$tag] = array();
  525. }
  526. $stanza[$tag][] = $value;
  527. }
  528. }
  529. // now add the last term in the file
  530. if (sizeof($stanza) > 0) {
  531. if (!isset($obo[$type])) {
  532. $obo[$type] = array();
  533. }
  534. if (!isset($obo[$type][$stanza['id'][0]])) {
  535. $obo[$type][$stanza['id'][0]] = $stanza;
  536. }
  537. else {
  538. array_merge($obo[$type][$stanza['id'][0]], $stanza);
  539. }
  540. }
  541. return $default_db;
  542. }
  543. /**
  544. * Add database reference to cvterm
  545. *
  546. * @ingroup tripal_obo_loader
  547. */
  548. function tripal_cv_obo_add_cvterm_dbxref($cvterm, $xref) {
  549. $dbname = preg_replace('/^(.+?):.*$/', '$1', $xref);
  550. $accession = preg_replace('/^.+?:\s*(.*?)(\{.+$|\[.+$|\s.+$|\".+$|$)/', '$1', $xref);
  551. $description = preg_replace('/^.+?\"(.+?)\".*?$/', '$1', $xref);
  552. $dbxrefs = preg_replace('/^.+?\[(.+?)\].*?$/', '$1', $xref);
  553. if (!$accession) {
  554. tripal_cv_obo_quiterror();
  555. watchdog("T_obo_loader", "Cannot add a dbxref without an accession: '$xref'", NULL, WATCHDOG_WARNING);
  556. return FALSE;
  557. }
  558. // if the xref is a database link, handle that specially
  559. if (strcmp($dbname, 'http') == 0) {
  560. $accession = $xref;
  561. $dbname = 'URL';
  562. }
  563. // add the database
  564. $db = tripal_db_add_db($dbname);
  565. if (!$db) {
  566. tripal_cv_obo_quiterror("Cannot find database '$dbname' in Chado.");
  567. }
  568. // now add the dbxref
  569. $dbxref = tripal_cv_obo_add_dbxref($db->db_id, $accession, '', $description);
  570. if (!$dbxref) {
  571. tripal_cv_obo_quiterror("Cannot find or add the database reference (dbxref)");
  572. }
  573. // finally add the cvterm_dbxref but first check to make sure it exists
  574. $sql = "SELECT * from {cvterm_dbxref} WHERE cvterm_id = %d and dbxref_id = %d";
  575. if (!db_fetch_object(db_query($sql, $cvterm->cvterm_id, $dbxref->dbxref_id))) {
  576. $sql = "INSERT INTO {cvterm_dbxref} (cvterm_id,dbxref_id)".
  577. "VALUES (%d,%d)";
  578. if (!db_query($sql, $cvterm->cvterm_id, $dbxref->dbxref_id)) {
  579. tripal_cv_obo_quiterror("Cannot add cvterm_dbxref: $xref");
  580. }
  581. }
  582. return TRUE;
  583. }
  584. /**
  585. * Add property to CVterm
  586. * @ingroup tripal_obo_loader
  587. */
  588. function tripal_cv_obo_add_cvterm_prop($cvterm, $property, $value, $rank) {
  589. // make sure the 'cvterm_property_type' CV exists
  590. $cv = tripal_cv_add_cv('cvterm_property_type', '');
  591. if (!$cv) {
  592. tripal_cv_obo_quiterror("Cannot add/find cvterm_property_type cvterm");
  593. }
  594. // get the property type cvterm. If it doesn't exist then we want to add it
  595. $sql = "
  596. SELECT *
  597. FROM {cvterm} CVT INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  598. WHERE CVT.name = '%s' and CV.name = '%s'
  599. ";
  600. $cvproptype = db_fetch_object(db_query($sql, $property, 'cvterm_property_type'));
  601. if (!$cvproptype) {
  602. $term = array(
  603. 'name' => array($property),
  604. 'id' => array("internal:$property"),
  605. 'definition' => array(''),
  606. 'is_obsolete' => array(0),
  607. );
  608. $cvproptype = tripal_cv_add_cvterm($term, $cv, 0, 0);
  609. if (!$cvproptype) {
  610. tripal_cv_obo_quiterror("Cannot add cvterm property: internal:$property");
  611. }
  612. }
  613. // remove any properties that currently exist for this term. We'll reset them
  614. if ($rank == 0) {
  615. $sql = "DELETE FROM {cvtermprop} WHERE cvterm_id = %d";
  616. db_query($sql, $cvterm->cvterm_id);
  617. }
  618. // now add the property
  619. $sql = "INSERT INTO {cvtermprop} (cvterm_id,type_id,value,rank) ".
  620. "VALUES (%d, %d, '%s',%d)";
  621. if (!db_query($sql, $cvterm->cvterm_id, $cvproptype->cvterm_id, $value, $rank)) {
  622. tripal_cv_obo_quiterror("Could not add property $property for term\n");
  623. }
  624. return TRUE;
  625. }
  626. /**
  627. * Add Database Reference
  628. * @ingroup tripal_obo_loader
  629. */
  630. function tripal_cv_obo_add_dbxref($db_id, $accession, $version='', $description='') {
  631. // check to see if the dbxref exists if not, add it
  632. $dbxsql = "SELECT dbxref_id FROM {dbxref} WHERE db_id = %d and accession = '%s'";
  633. $dbxref = db_fetch_object(db_query($dbxsql, $db_id, $accession));
  634. if (!$dbxref) {
  635. $sql = "
  636. INSERT INTO {dbxref} (db_id, accession, version, description)
  637. VALUES (%d,'%s','%s','%s')
  638. ";
  639. if (!db_query($sql, $db_id, $accession, $version, $description)) {
  640. tripal_cv_obo_quiterror("Failed to insert the dbxref record $accession");
  641. }
  642. print "Added Dbxref accession: $accession\n";
  643. $dbxref = db_fetch_object(db_query($dbxsql, $db_id, $accession));
  644. }
  645. return $dbxref;
  646. }