obo_loader.inc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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. tripal_cv_obo_process_term($typedef, $defaultcv->name, $obo, 1, $newcvs, $default_db);
  134. }
  135. // next add terms to the vocabulary
  136. $terms = $obo['Term'];
  137. if (!tripal_cv_obo_process_terms($terms, $defaultcv->name, $obo, $jobid, $newcvs, $default_db)) {
  138. tripal_cv_obo_quiterror('Cannot add terms from this ontology');
  139. }
  140. return;
  141. }
  142. /**
  143. *
  144. * @ingroup tripal_obo_loader
  145. */
  146. function tripal_cv_obo_quiterror($message) {
  147. watchdog("T_obo_loader", $message, array(), WATCHDOG_ERROR);;
  148. exit;
  149. }
  150. /**
  151. *
  152. * @ingroup tripal_obo_loader
  153. */
  154. function tripal_cv_obo_process_terms($terms, $defaultcv, $obo, $jobid = NULL, &$newcvs, $default_db) {
  155. $i = 0;
  156. $count = count($terms);
  157. // if the number of terms is zero then simply return.
  158. // this can happen when an OBO file simply has typedef
  159. // entries and no actual terms.
  160. if($count == 0) {
  161. return 1;
  162. }
  163. // calculate the interval for updates
  164. $interval = intval($count * 0.01);
  165. if ($interval < 1) {
  166. $interval = 1;
  167. }
  168. // iterate through each term from the OBO file and add it
  169. print "Loading terms...\n";
  170. foreach ($terms as $term) {
  171. // update the job status every 1% terms
  172. if ($jobid and $i % $interval == 0) {
  173. $complete = ($i / $count) * 100;
  174. tripal_job_set_progress($jobid, intval($complete));
  175. printf("%d of %d records. (%0.2f%%) memory: %d\r", $i, $count, $complete, memory_get_usage());
  176. }
  177. // add/update this term
  178. if (!tripal_cv_obo_process_term($term, $defaultcv, $obo, 0, $newcvs, $default_db)) {
  179. tripal_cv_obo_quiterror("Failed to process terms from the ontology");
  180. }
  181. $i++;
  182. }
  183. // set the final status
  184. if ($jobid) {
  185. $complete = ($i / $count) * 100;
  186. tripal_job_set_progress($jobid, intval($complete));
  187. printf("%d of %d records. (%0.2f%%) memory: %d\r", $i, $count, $complete, memory_get_usage());
  188. }
  189. return 1;
  190. }
  191. /**
  192. *
  193. * @ingroup tripal_obo_loader
  194. */
  195. function tripal_cv_obo_process_term($term, $defaultcv, $obo, $is_relationship = 0, &$newcvs, $default_db) {
  196. // construct the term array for sending to the tripal_cv_add_cvterm function
  197. // for adding a new cvterm
  198. $t = array();
  199. $t['id'] = $term['id'][0];
  200. $t['name'] = $term['name'][0];
  201. $t['def'] = $term['def'][0];
  202. if (isset($term['subset'])) {
  203. $t['subset'] = $term['subset'][0];
  204. }
  205. if (isset($term['namespace'])) {
  206. $t['namespace'] = $term['namespace'][0];
  207. }
  208. if (isset($term['is_obsolete'])) {
  209. $t['is_obsolete'] = $term['is_obsolete'][0];
  210. }
  211. // add the cvterm
  212. $cvterm = tripal_cv_add_cvterm($t, $defaultcv, $is_relationship, 1, $default_db);
  213. if (!$cvterm) {
  214. tripal_cv_obo_quiterror("Cannot add the term " . $term['id']);
  215. }
  216. if ($term['namespace']) {
  217. $newcvs[$term['namespace']] = $cvterm->cv_id;
  218. }
  219. // now handle other properites
  220. if (isset($term['is_anonymous'])) {
  221. //print "WARNING: unhandled tag: is_anonymous\n";
  222. }
  223. if (isset($term['alt_id'])) {
  224. foreach ($term['alt_id'] as $alt_id) {
  225. if (!tripal_cv_obo_add_cvterm_dbxref($cvterm, $alt_id)) {
  226. tripal_cv_obo_quiterror("Cannot add alternate id $alt_id");
  227. }
  228. }
  229. }
  230. if (isset($term['subset'])) {
  231. //print "WARNING: unhandled tag: subset\n";
  232. }
  233. // add synonyms for this cvterm
  234. if (isset($term['synonym'])) {
  235. if (!tripal_cv_obo_add_synonyms($term, $cvterm)) {
  236. tripal_cv_obo_quiterror("Cannot add synonyms");
  237. }
  238. }
  239. // reformat the deprecated 'exact_synonym, narrow_synonym, and broad_synonym'
  240. // types to be of the v1.2 standard
  241. if (isset($term['exact_synonym']) or isset($term['narrow_synonym']) or isset($term['broad_synonym'])) {
  242. if (isset($term['exact_synonym'])) {
  243. foreach ($term['exact_synonym'] as $synonym) {
  244. $new = preg_replace('/^\s*(\".+?\")(.*?)$/', '$1 EXACT $2', $synonym);
  245. $term['synonym'][] = $new;
  246. }
  247. }
  248. if (isset($term['narrow_synonym'])) {
  249. foreach ($term['narrow_synonym'] as $synonym) {
  250. $new = preg_replace('/^\s*(\".+?\")(.*?)$/', '$1 NARROW $2', $synonym);
  251. $term['synonym'][] = $new;
  252. }
  253. }
  254. if (isset($term['broad_synonym'])) {
  255. foreach ($term['broad_synonym'] as $synonym) {
  256. $new = preg_replace('/^\s*(\".+?\")(.*?)$/', '$1 BROAD $2', $synonym);
  257. $term['synonym'][] = $new;
  258. }
  259. }
  260. if (!tripal_cv_obo_add_synonyms($term, $cvterm)) {
  261. tripal_cv_obo_quiterror("Cannot add/update synonyms");
  262. }
  263. }
  264. // add the comment to the cvtermprop table
  265. if (isset($term['comment'])) {
  266. $comments = $term['comment'];
  267. $j = 0;
  268. foreach ($comments as $comment) {
  269. if (!tripal_cv_obo_add_cvterm_prop($cvterm, 'comment', $comment, $j)) {
  270. tripal_cv_obo_quiterror("Cannot add/update cvterm property");
  271. }
  272. $j++;
  273. }
  274. }
  275. // add any other external dbxrefs
  276. if (isset($term['xref'])) {
  277. foreach ($term['xref'] as $xref) {
  278. if (!tripal_cv_obo_add_cvterm_dbxref($cvterm, $xref)) {
  279. tripal_cv_obo_quiterror("Cannot add/update cvterm database reference (dbxref).");
  280. }
  281. }
  282. }
  283. if (isset($term['xref_analog'])) {
  284. foreach ($term['xref_analog'] as $xref) {
  285. if (!tripal_cv_obo_add_cvterm_dbxref($cvterm, $xref)) {
  286. tripal_cv_obo_quiterror("Cannot add/update cvterm database reference (dbxref).");
  287. }
  288. }
  289. }
  290. if (isset($term['xref_unk'])) {
  291. foreach ($term['xref_unk'] as $xref) {
  292. if (!tripal_cv_obo_add_cvterm_dbxref($cvterm, $xref)) {
  293. tripal_cv_obo_quiterror("Cannot add/update cvterm database reference (dbxref).");
  294. }
  295. }
  296. }
  297. // add is_a relationships for this cvterm
  298. if (isset($term['is_a'])) {
  299. foreach ($term['is_a'] as $is_a) {
  300. if (!tripal_cv_obo_add_relationship($cvterm, $defaultcv, $obo, 'is_a', $is_a, $is_relationship, $default_db)) {
  301. tripal_cv_obo_quiterror("Cannot add relationship is_a: $is_a");
  302. }
  303. }
  304. }
  305. if (isset($term['intersection_of'])) {
  306. //print "WARNING: unhandled tag: intersection_of\n";
  307. }
  308. if (isset($term['union_of'])) {
  309. //print "WARNING: unhandled tag: union_on\n";
  310. }
  311. if (isset($term['disjoint_from'])) {
  312. //print "WARNING: unhandled tag: disjoint_from\n";
  313. }
  314. if (isset($term['relationship'])) {
  315. foreach ($term['relationship'] as $value) {
  316. $rel = preg_replace('/^(.+?)\s.+?$/', '\1', $value);
  317. $object = preg_replace('/^.+?\s(.+?)$/', '\1', $value);
  318. if (!tripal_cv_obo_add_relationship($cvterm, $defaultcv, $obo, $rel, $object, $is_relationship, $default_db)) {
  319. tripal_cv_obo_quiterror("Cannot add relationship $rel: $object");
  320. }
  321. }
  322. }
  323. if (isset($term['replaced_by'])) {
  324. //print "WARNING: unhandled tag: replaced_by\n";
  325. }
  326. if (isset($term['consider'])) {
  327. //print "WARNING: unhandled tag: consider\n";
  328. }
  329. if (isset($term['use_term'])) {
  330. //print "WARNING: unhandled tag: user_term\n";
  331. }
  332. if (isset($term['builtin'])) {
  333. //print "WARNING: unhandled tag: builtin\n";
  334. }
  335. return 1;
  336. }
  337. /**
  338. * Add a cvterm relationship
  339. *
  340. * @ingroup tripal_obo_loader
  341. */
  342. function tripal_cv_obo_add_relationship($cvterm, $defaultcv, $obo, $rel,
  343. $objname, $object_is_relationship = 0, $default_db = 'OBO_REL') {
  344. // make sure the relationship cvterm exists
  345. $term = array(
  346. 'name' => $rel,
  347. 'id' => "$default_db:$rel",
  348. 'definition' => '',
  349. 'is_obsolete' => 0,
  350. );
  351. $relcvterm = tripal_cv_add_cvterm($term, $defaultcv, 1, 0);
  352. if (!$relcvterm) {
  353. // if the relationship term couldn't be found in the default_db provided
  354. // then do on more check to find it in the relationship ontology
  355. $term = array(
  356. 'name' => $rel,
  357. 'id' => "OBO_REL:$rel",
  358. 'definition' => '',
  359. 'is_obsolete' => 0,
  360. );
  361. $relcvterm = tripal_cv_add_cvterm($term, $defaultcv, 1, 0);
  362. if (!$relcvterm) {
  363. tripal_cv_obo_quiterror("Cannot find the relationship term in the current ontology or in the relationship ontology: $rel\n");
  364. }
  365. }
  366. // get the object term
  367. $oterm = tripal_cv_obo_get_term($obo, $objname);
  368. if (!$oterm) {
  369. tripal_cv_obo_quiterror("Could not find object term $objname\n");
  370. }
  371. $objterm = array(
  372. 'name' => $oterm['name'][0],
  373. 'id' => $oterm['id'][0],
  374. 'definition' => $oterm['def'][0],
  375. 'is_obsolete' => $oterm['is_obsolete'][0],
  376. );
  377. $objcvterm = tripal_cv_add_cvterm($objterm, $defaultcv, $object_is_relationship, 1);
  378. if (!$objcvterm) {
  379. tripal_cv_obo_quiterror("Cannot add/find cvterm");
  380. }
  381. // check to see if the cvterm_relationship already exists, if not add it
  382. $values = array(
  383. 'type_id' => $relcvterm->cvterm_id,
  384. 'subject_id' => $cvterm->cvterm_id,
  385. 'object_id' => $objcvterm->cvterm_id
  386. );
  387. $options = array('statement_name' => 'sel_cvtermrelationship_tysuob');
  388. $result = tripal_core_chado_select('cvterm_relationship', array('*'), $values, $options);
  389. if (count($result) == 0) {
  390. $options = array('statement_name' => 'ins_cvtermrelationship_tysuob');
  391. $sql = "INSERT INTO {cvterm_relationship} ".
  392. "(type_id,subject_id,object_id) VALUES (%d,%d,%d)";
  393. $success = tripal_core_chado_insert('cvterm_relationship', $values, $options);
  394. if (!$success) {
  395. tripal_cv_obo_quiterror("Cannot add term relationship: '$cvterm->name' $rel '$objcvterm->name'");
  396. }
  397. }
  398. return TRUE;
  399. }
  400. /**
  401. *
  402. * @ingroup tripal_obo_loader
  403. */
  404. function tripal_cv_obo_get_term($obo, $id) {
  405. foreach ($obo as $type) {
  406. foreach ($type as $term) {
  407. $accession = $term['id'][0];
  408. if (strcmp($accession, $id)==0) {
  409. return $term;
  410. }
  411. }
  412. }
  413. return FALSE;
  414. }
  415. /**
  416. *
  417. * @ingroup tripal_obo_loader
  418. */
  419. function tripal_cv_obo_add_synonyms($term, $cvterm) {
  420. // make sure we have a 'synonym_type' vocabulary
  421. $syncv = tripal_cv_add_cv('synonym_type', 'A vocabulary added by the Tripal CV module OBO loader for storing synonym types.');
  422. // now add the synonyms
  423. if (isset($term['synonym'])) {
  424. foreach ($term['synonym'] as $synonym) {
  425. // separate out the synonym definition and the synonym type
  426. $def = preg_replace('/^\s*"(.*)"\s*.*$/', '\1', $synonym);
  427. // the scope will be 'EXACT', etc...
  428. $scope = drupal_strtolower(preg_replace('/^.*"\s+(.*?)\s+.*$/', '\1', $synonym));
  429. if (!$scope) { // if no scope then default to 'exact'
  430. $scope = 'exact';
  431. }
  432. // make sure the synonym type exists in the 'synonym_type' vocabulary
  433. $values = array(
  434. 'name' => $scope,
  435. 'cv_id' => array(
  436. 'name' => 'synonym_type',
  437. ),
  438. );
  439. $options = array('statement_name' => 'sel_cvterm_nacv', 'is_updlicate' => 1);
  440. $results = tripal_core_chado_select('cvterm', array('*'), $values, $options);
  441. // if it doesn't exist then add it
  442. if (!$results) {
  443. // build a 'term' object so we can add the missing term
  444. $term = array(
  445. 'name' => $scope,
  446. 'id' => "internal:$scope",
  447. 'definition' => '',
  448. 'is_obsolete' => 0,
  449. );
  450. $syntype = tripal_cv_add_cvterm($term, $syncv->name, 0, 1);
  451. if (!$syntype) {
  452. tripal_cv_obo_quiterror("Cannot add synonym type: internal:$scope");
  453. }
  454. }
  455. else {
  456. $syntype = $results[0];
  457. }
  458. // make sure the synonym doesn't already exists
  459. $values = array(
  460. 'cvterm_id' => $cvterm->cvterm_id,
  461. 'synonym' => $def
  462. );
  463. $options = array('statement_name' => 'sel_cvtermsynonym_cvsy');
  464. $results = tripal_core_chado_select('cvtermsynonym', array('*'), $values, $options);
  465. if (count($results) == 0) {
  466. $values = array(
  467. 'cvterm_id' => $cvterm->cvterm_id,
  468. 'synonym' => $def,
  469. 'type_id' => $syntype->cvterm_id
  470. );
  471. $options = array('statement_name' => 'ins_cvtermsynonym_cvsy');
  472. $success = tripal_core_chado_insert('cvtermsynonym', $values, $options);
  473. if (!$success) {
  474. tripal_cv_obo_quiterror("Failed to insert the synonym for term: $name ($def)");
  475. }
  476. }
  477. // now add the dbxrefs for the synonym if we have a comma in the middle
  478. // of a description then this will cause problems when splitting os lets
  479. // just change it so it won't mess up our splitting and then set it back
  480. // later.
  481. /**
  482. $synonym = preg_replace('/(".*?),\s(.*?")/','$1,_$2',$synonym);
  483. $dbxrefs = preg_split("/, /",preg_replace('/^.*\[(.*?)\]$/','\1',$synonym));
  484. foreach ($dbxrefs as $dbxref) {
  485. $dbxref = preg_replace('/,_/',", ",$dbxref);
  486. if ($dbxref) {
  487. tripal_cv_obo_add_cvterm_dbxref($syn,$dbxref);
  488. }
  489. }
  490. */
  491. }
  492. }
  493. return TRUE;
  494. }
  495. /**
  496. * Actually parse the OBO file
  497. *
  498. * @ingroup tripal_obo_loader
  499. */
  500. function tripal_cv_obo_parse($obo_file, &$obo, &$header) {
  501. $i = 0;
  502. $in_header = 1;
  503. $stanza = array();
  504. $default_db = '_global';
  505. // iterate through the lines in the OBO file and parse the stanzas
  506. $fh = fopen($obo_file, 'r');
  507. while ($line = fgets($fh)) {
  508. $i++;
  509. // remove newlines
  510. $line = rtrim($line);
  511. // remove any special characters that may be hiding
  512. $line = preg_replace('/[^(\x20-\x7F)]*/', '', $line);
  513. // skip empty lines
  514. if (strcmp($line, '') == 0) {
  515. continue;
  516. }
  517. //remove comments from end of lines
  518. $line = preg_replace('/^(.*?)\!.*$/', '\1', $line); // TODO: if the explamation is escaped
  519. if (preg_match('/^\s*\[/', $line)) { // at the first stanza we're out of header
  520. $in_header = 0;
  521. // load the stanza we just finished reading
  522. if (sizeof($stanza) > 0) {
  523. if (!isset($obo[$type])) {
  524. $obo[$type] = array();
  525. }
  526. if (!isset($obo[$type][$stanza['id'][0]])) {
  527. $obo[$type][$stanza['id'][0]] = $stanza;
  528. }
  529. else {
  530. array_merge($obo[$type][$stanza['id'][0]], $stanza);
  531. }
  532. }
  533. // get the stanza type: Term, Typedef or Instance
  534. $type = preg_replace('/^\s*\[\s*(.+?)\s*\]\s*$/', '\1', $line);
  535. // start fresh with a new array
  536. $stanza = array();
  537. continue;
  538. }
  539. // break apart the line into the tag and value but ignore any escaped colons
  540. preg_replace("/\\:/", "|-|-|", $line); // temporarily replace escaped colons
  541. $pair = explode(":", $line, 2);
  542. $tag = $pair[0];
  543. $value = ltrim(rtrim($pair[1]));// remove surrounding spaces
  544. // if this is the ID then look for the default DB
  545. $matches = array();
  546. if ($tag == 'id' and preg_match('/^(.+?):.*$/', $value, $matches)) {
  547. $default_db = $matches[1];
  548. }
  549. $tag = preg_replace("/\|-\|-\|/", "\:", $tag); // return the escaped colon
  550. $value = preg_replace("/\|-\|-\|/", "\:", $value);
  551. if ($in_header) {
  552. if (!isset($header[$tag])) {
  553. $header[$tag] = array();
  554. }
  555. $header[$tag][] = $value;
  556. }
  557. else {
  558. if (!isset($stanza[$tag])) {
  559. $stanza[$tag] = array();
  560. }
  561. $stanza[$tag][] = $value;
  562. }
  563. }
  564. // now add the last term in the file
  565. if (sizeof($stanza) > 0) {
  566. if (!isset($obo[$type])) {
  567. $obo[$type] = array();
  568. }
  569. if (!isset($obo[$type][$stanza['id'][0]])) {
  570. $obo[$type][$stanza['id'][0]] = $stanza;
  571. }
  572. else {
  573. array_merge($obo[$type][$stanza['id'][0]], $stanza);
  574. }
  575. }
  576. return $default_db;
  577. }
  578. /**
  579. * Add database reference to cvterm
  580. *
  581. * @ingroup tripal_obo_loader
  582. */
  583. function tripal_cv_obo_add_cvterm_dbxref($cvterm, $xref) {
  584. $dbname = preg_replace('/^(.+?):.*$/', '$1', $xref);
  585. $accession = preg_replace('/^.+?:\s*(.*?)(\{.+$|\[.+$|\s.+$|\".+$|$)/', '$1', $xref);
  586. $description = preg_replace('/^.+?\"(.+?)\".*?$/', '$1', $xref);
  587. $dbxrefs = preg_replace('/^.+?\[(.+?)\].*?$/', '$1', $xref);
  588. if (!$accession) {
  589. tripal_cv_obo_quiterror();
  590. watchdog("T_obo_loader", "Cannot add a dbxref without an accession: '$xref'", NULL, WATCHDOG_WARNING);
  591. return FALSE;
  592. }
  593. // if the xref is a database link, handle that specially
  594. if (strcmp($dbname, 'http') == 0) {
  595. $accession = $xref;
  596. $dbname = 'URL';
  597. }
  598. // add the database
  599. $db = tripal_db_add_db($dbname);
  600. if (!$db) {
  601. tripal_cv_obo_quiterror("Cannot find database '$dbname' in Chado.");
  602. }
  603. // now add the dbxref
  604. $dbxref = tripal_cv_obo_add_dbxref($db->db_id, $accession, '', $description);
  605. if (!$dbxref) {
  606. tripal_cv_obo_quiterror("Cannot find or add the database reference (dbxref)");
  607. }
  608. // finally add the cvterm_dbxref but first check to make sure it exists
  609. $sql = "SELECT * from {cvterm_dbxref} WHERE cvterm_id = %d and dbxref_id = %d";
  610. if (!db_fetch_object(db_query($sql, $cvterm->cvterm_id, $dbxref->dbxref_id))) {
  611. $sql = "INSERT INTO {cvterm_dbxref} (cvterm_id,dbxref_id)".
  612. "VALUES (%d,%d)";
  613. if (!db_query($sql, $cvterm->cvterm_id, $dbxref->dbxref_id)) {
  614. tripal_cv_obo_quiterror("Cannot add cvterm_dbxref: $xref");
  615. }
  616. }
  617. return TRUE;
  618. }
  619. /**
  620. * Add property to CVterm
  621. * @ingroup tripal_obo_loader
  622. */
  623. function tripal_cv_obo_add_cvterm_prop($cvterm, $property, $value, $rank) {
  624. // make sure the 'cvterm_property_type' CV exists
  625. $cv = tripal_cv_add_cv('cvterm_property_type', '');
  626. if (!$cv) {
  627. tripal_cv_obo_quiterror("Cannot add/find cvterm_property_type cvterm");
  628. }
  629. // get the property type cvterm. If it doesn't exist then we want to add it
  630. $sql = "
  631. SELECT *
  632. FROM {cvterm} CVT INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  633. WHERE CVT.name = '%s' and CV.name = '%s'
  634. ";
  635. $cvproptype = db_fetch_object(db_query($sql, $property, 'cvterm_property_type'));
  636. if (!$cvproptype) {
  637. $term = array(
  638. 'name' => $property,
  639. 'id' => "internal:$property",
  640. 'definition' => '',
  641. 'is_obsolete' => 0,
  642. );
  643. $cvproptype = tripal_cv_add_cvterm($term, $cv->name, 0, 0);
  644. if (!$cvproptype) {
  645. tripal_cv_obo_quiterror("Cannot add cvterm property: internal:$property");
  646. }
  647. }
  648. // remove any properties that currently exist for this term. We'll reset them
  649. if ($rank == 0) {
  650. $sql = "DELETE FROM {cvtermprop} WHERE cvterm_id = %d";
  651. db_query($sql, $cvterm->cvterm_id);
  652. }
  653. // now add the property
  654. $sql = "INSERT INTO {cvtermprop} (cvterm_id,type_id,value,rank) ".
  655. "VALUES (%d, %d, '%s',%d)";
  656. if (!db_query($sql, $cvterm->cvterm_id, $cvproptype->cvterm_id, $value, $rank)) {
  657. tripal_cv_obo_quiterror("Could not add property $property for term\n");
  658. }
  659. return TRUE;
  660. }
  661. /**
  662. * Add Database Reference
  663. * @ingroup tripal_obo_loader
  664. */
  665. function tripal_cv_obo_add_dbxref($db_id, $accession, $version='', $description='') {
  666. // check to see if the dbxref exists if not, add it
  667. $dbxsql = "SELECT dbxref_id FROM {dbxref} WHERE db_id = %d and accession = '%s'";
  668. $dbxref = db_fetch_object(db_query($dbxsql, $db_id, $accession));
  669. if (!$dbxref) {
  670. $sql = "
  671. INSERT INTO {dbxref} (db_id, accession, version, description)
  672. VALUES (%d,'%s','%s','%s')
  673. ";
  674. if (!db_query($sql, $db_id, $accession, $version, $description)) {
  675. tripal_cv_obo_quiterror("Failed to insert the dbxref record $accession");
  676. }
  677. $dbxref = db_fetch_object(db_query($dbxsql, $db_id, $accession));
  678. }
  679. return $dbxref;
  680. }