obo_loader.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. <?php
  2. /**
  3. * @defgroup tripal_obo_loader Tripal Ontology Loader
  4. * @ingroup tripal_cv
  5. */
  6. /**
  7. *
  8. * @ingroup tripal_obo_loader
  9. */
  10. function tripal_cv_load_obo_v1_2_id($obo_id,$jobid = NULL){
  11. // get the OBO reference
  12. $sql = "SELECT * FROM {tripal_cv_obo} WHERE obo_id = %d";
  13. $obo = db_fetch_object(db_query($sql,$obo_id));
  14. // if the reference is for a remote URL then run the URL processing function
  15. if(preg_match("/^http:\/\//",$obo->path) or preg_match("/^ftp:\/\//",$obo->path)){
  16. tripal_cv_load_obo_v1_2_url($obo->name,$obo->path,$jobid,0);
  17. }
  18. // if the reference is for a local file then run the file processing function
  19. else {
  20. // check to see if the file is located local to Drupal
  21. $dfile = $_SERVER['DOCUMENT_ROOT'] . base_path() . $obo->path;
  22. if(file_exists($dfile)){
  23. tripal_cv_load_obo_v1_2_file($obo->name,$dfile,$jobid,0);
  24. }
  25. // if not local to Drupal, the file must be someplace else, just use
  26. // the full path provided
  27. else{
  28. if(file_exists($obo->path)){
  29. tripal_cv_load_obo_v1_2_file($obo->name,$obo->path,$jobid,0);
  30. } else {
  31. print "ERROR: counld not find OBO file: '$obo->path'\n";
  32. }
  33. }
  34. }
  35. }
  36. /**
  37. *
  38. * @ingroup tripal_obo_loader
  39. */
  40. function tripal_cv_load_obo_v1_2_file($obo_name,$file,$jobid = NULL,$is_new = 1){
  41. $newcvs = array();
  42. tripal_cv_load_obo_v1_2($file,$jobid,$newcvs);
  43. if($is_new){
  44. tripal_cv_load_obo_add_ref($obo_name,$file);
  45. }
  46. // update the cvtermpath table
  47. tripal_cv_load_update_cvtermpath($newcvs,$jobid);
  48. print "Ontology Sucessfully loaded!\n";
  49. }
  50. /**
  51. *
  52. * @ingroup tripal_obo_loader
  53. */
  54. function tripal_cv_load_obo_v1_2_url($obo_name,$url,$jobid = NULL,$is_new = 1){
  55. $newcvs = array();
  56. // first download the OBO
  57. $temp = tempnam(sys_get_temp_dir(),'obo_');
  58. print "Downloading URL $url, saving to $temp\n";
  59. $url_fh = fopen($url,"r");
  60. $obo_fh = fopen($temp,"w");
  61. while(!feof($url_fh)){
  62. fwrite($obo_fh,fread($url_fh,255),255);
  63. }
  64. fclose($url_fh);
  65. fclose($obo_fh);
  66. // second, parse the OBO
  67. tripal_cv_load_obo_v1_2($temp,$jobid,$newcvs);
  68. // now remove the temp file
  69. unlink($temp);
  70. if($is_new){
  71. tripal_cv_load_obo_add_ref($obo_name,$url);
  72. }
  73. // update the cvtermpath table
  74. tripal_cv_load_update_cvtermpath($newcvs,$jobid);
  75. print "Ontology Sucessfully loaded!\n";
  76. }
  77. /**
  78. *
  79. * @ingroup tripal_obo_loader
  80. */
  81. function tripal_cv_load_update_cvtermpath($newcvs,$jobid){
  82. print "\nUpdating cvtermpath table. This may take a while...\n";
  83. foreach($newcvs as $namespace => $cvid){
  84. tripal_cv_update_cvtermpath($cvid, $jobid);
  85. }
  86. }
  87. /**
  88. *
  89. */
  90. function tripal_cv_load_obo_add_ref($name,$path){
  91. $isql = "INSERT INTO tripal_cv_obo (name,path) VALUES ('%s','%s')";
  92. db_query($isql,$name,$path);
  93. }
  94. /**
  95. *
  96. * @ingroup tripal_obo_loader
  97. */
  98. function tripal_cv_load_obo_v1_2($file,$jobid = NULL,&$newcvs) {
  99. global $previous_db;
  100. $header = array();
  101. $obo = array();
  102. print "Opening File $file\n";
  103. // set the search path
  104. $previous_db = tripal_db_set_active('chado');
  105. // make sure we have an 'internal' and a '_global' database
  106. if(!tripal_cv_obo_add_db('internal')){
  107. tripal_cv_obo_quiterror("Cannot add 'internal' database");
  108. }
  109. if(!tripal_cv_obo_add_db('_global')){
  110. tripal_cv_obo_quiterror("Cannot add '_global' database");
  111. }
  112. // parse the obo file
  113. tripal_cv_obo_parse($file,$obo,$header);
  114. // add the CV for this ontology to the database
  115. $defaultcv = tripal_cv_obo_add_cv($header['default-namespace'][0],'');
  116. if(!$defaultcv){
  117. tripal_cv_obo_quiterror('Cannot add namespace ' . $header['default-namespace'][0]);
  118. }
  119. $newcvs[$header['default-namespace'][0]] = $defaultcv->cv_id;
  120. // add any typedefs to the vocabulary first
  121. $typedefs = $obo['Typedef'];
  122. foreach($typedefs as $typedef){
  123. tripal_cv_obo_process_term($typedef,$defaultcv,$obo,1,$newcvs);
  124. }
  125. // next add terms to the vocabulary
  126. $terms = $obo['Term'];
  127. if(!tripal_cv_obo_process_terms($terms,$defaultcv,$obo,$jobid,$newcvs)){
  128. tripal_cv_obo_quiterror('Cannot add terms from this ontology');
  129. }
  130. return tripal_cv_obo_loader_done();
  131. }
  132. /**
  133. *
  134. * @ingroup tripal_obo_loader
  135. */
  136. function tripal_cv_obo_quiterror ($message){
  137. print "ERROR: $message\n";
  138. db_query("set search_path to public");
  139. exit;
  140. }
  141. /**
  142. *
  143. * @ingroup tripal_obo_loader
  144. */
  145. function tripal_cv_obo_loader_done (){
  146. // return the search path to normal
  147. tripal_db_set_active($previous_db);
  148. db_query("set search_path to public");
  149. return '';
  150. }
  151. /**
  152. *
  153. * @ingroup tripal_obo_loader
  154. */
  155. function tripal_cv_obo_process_terms($terms,$defaultcv,$obo,$jobid=null,&$newcvs){
  156. $i = 0;
  157. $count = sizeof($terms);
  158. $interval = intval($count * 0.01);
  159. foreach ($terms as $term){
  160. // update the job status every 1% terms
  161. if($jobid and $i % $interval == 0){
  162. tripal_job_set_progress($jobid,intval(($i/$count)*50)); // we mulitply by 50 because parsing and loacing cvterms
  163. // is only the first half. The other half is updating
  164. } // the cvtermpath table.
  165. if(!tripal_cv_obo_process_term($term,$defaultcv,$obo,0,$newcvs)){
  166. tripal_cv_obo_quiterror("Failed to process terms from the ontology");
  167. }
  168. $i++;
  169. }
  170. return 1;
  171. }
  172. /**
  173. *
  174. * @ingroup tripal_obo_loader
  175. */
  176. function tripal_cv_obo_process_term($term,$defaultcv,$obo,$is_relationship=0,&$newcvs){
  177. // add the cvterm
  178. $cvterm = tripal_cv_obo_add_cvterm($term,$defaultcv,$is_relationship,1);
  179. if(!$cvterm){
  180. tripal_cv_obo_quiterror("Cannot add the term " . $term['id'][0]);
  181. }
  182. if($term['namespace'][0]){
  183. $newcvs[$term['namespace'][0]] = $cvterm->cv_id;
  184. }
  185. // now handle other properites
  186. if(isset($term['is_anonymous'])){
  187. print "WARNING: unhandled tag: is_anonymous\n";
  188. }
  189. if(isset($term['alt_id'])){
  190. foreach($term['alt_id'] as $alt_id){
  191. if(!tripal_cv_obo_add_cvterm_dbxref($cvterm,$alt_id)){
  192. tripal_cv_obo_quiterror("Cannot add alternate id $alt_id");
  193. }
  194. }
  195. }
  196. if(isset($term['subset'])){
  197. print "WARNING: unhandled tag: subset\n";
  198. }
  199. // add synonyms for this cvterm
  200. if(isset($term['synonym'])){
  201. if(!tripal_cv_obo_add_synonyms($term,$cvterm)){
  202. tripal_cv_obo_quiterror("Cannot add synonyms");
  203. }
  204. }
  205. // reformat the deprecated 'exact_synonym, narrow_synonym, and broad_synonym'
  206. // types to be of the v1.2 standard
  207. if(isset($term['exact_synonym']) or isset($term['narrow_synonym']) or isset($term['broad_synonym'])){
  208. if(isset($term['exact_synonym'])){
  209. foreach ($term['exact_synonym'] as $synonym){
  210. $new = preg_replace('/^\s*(\".+?\")(.*?)$/','$1 EXACT $2',$synonym);
  211. $term['synonym'][] = $new;
  212. }
  213. }
  214. if(isset($term['narrow_synonym'])){
  215. foreach ($term['narrow_synonym'] as $synonym){
  216. $new = preg_replace('/^\s*(\".+?\")(.*?)$/','$1 NARROW $2',$synonym);
  217. $term['synonym'][] = $new;
  218. }
  219. }
  220. if(isset($term['broad_synonym'])){
  221. foreach ($term['broad_synonym'] as $synonym){
  222. $new = preg_replace('/^\s*(\".+?\")(.*?)$/','$1 BROAD $2',$synonym);
  223. $term['synonym'][] = $new;
  224. }
  225. }
  226. if(!tripal_cv_obo_add_synonyms($term,$cvterm)){
  227. tripal_cv_obo_quiterror("Cannot add/update synonyms");
  228. }
  229. }
  230. // add the comment to the cvtermprop table
  231. if(isset($term['comment'])){
  232. $comments = $term['comment'];
  233. $j = 0;
  234. foreach($comments as $comment){
  235. if(!tripal_cv_obo_add_cvterm_prop($cvterm,'comment',$comment,$j)){
  236. tripal_cv_obo_quiterror("Cannot add/update cvterm property");
  237. }
  238. $j++;
  239. }
  240. }
  241. // add any other external dbxrefs
  242. if(isset($term['xref'])){
  243. foreach($term['xref'] as $xref){
  244. if(!tripal_cv_obo_add_cvterm_dbxref($cvterm,$xref)){
  245. tripal_cv_obo_quiterror("Cannot add/update cvterm database reference (dbxref).");
  246. }
  247. }
  248. }
  249. if(isset($term['xref_analog'])){
  250. foreach($term['xref_analog'] as $xref){
  251. if(!tripal_cv_obo_add_cvterm_dbxref($cvterm,$xref)){
  252. tripal_cv_obo_quiterror("Cannot add/update cvterm database reference (dbxref).");
  253. }
  254. }
  255. }
  256. if(isset($term['xref_unk'])){
  257. foreach($term['xref_unk'] as $xref){
  258. if(!tripal_cv_obo_add_cvterm_dbxref($cvterm,$xref)){
  259. tripal_cv_obo_quiterror("Cannot add/update cvterm database reference (dbxref).");
  260. }
  261. }
  262. }
  263. // add is_a relationships for this cvterm
  264. if(isset($term['is_a'])){
  265. foreach($term['is_a'] as $is_a){
  266. if(!tripal_cv_obo_add_relationship($cvterm,$defaultcv,$obo,'is_a',$is_a,$is_relationship)){
  267. tripal_cv_obo_quiterror("Cannot add relationship is_a: $is_a");
  268. }
  269. }
  270. }
  271. if(isset($term['intersection_of'])){
  272. print "WARNING: unhandled tag: intersection_of\n";
  273. }
  274. if(isset($term['union_of'])){
  275. print "WARNING: unhandled tag: union_on\n";
  276. }
  277. if(isset($term['disjoint_from'])){
  278. print "WARNING: unhandled tag: disjoint_from\n";
  279. }
  280. if(isset($term['relationship'])){
  281. foreach($term['relationship'] as $value){
  282. $rel = preg_replace('/^(.+?)\s.+?$/','\1',$value);
  283. $object = preg_replace('/^.+?\s(.+?)$/','\1',$value);
  284. if(!tripal_cv_obo_add_relationship($cvterm,$defaultcv,$obo,$rel,$object,$is_relationship)){
  285. tripal_cv_obo_quiterror("Cannot add relationship $rel: $object");
  286. }
  287. }
  288. }
  289. if(isset($term['replaced_by'])){
  290. print "WARNING: unhandled tag: replaced_by\n";
  291. }
  292. if(isset($term['consider'])){
  293. print "WARNING: unhandled tag: consider\n";
  294. }
  295. if(isset($term['use_term'])){
  296. print "WARNING: unhandled tag: user_term\n";
  297. }
  298. if(isset($term['builtin'])){
  299. print "WARNING: unhandled tag: builtin\n";
  300. }
  301. return 1;
  302. }
  303. /**
  304. *
  305. * @ingroup tripal_obo_loader
  306. */
  307. function tripal_cv_obo_add_db($dbname){
  308. $db_sql = "SELECT * FROM {db} WHERE name ='%s'";
  309. $db = db_fetch_object(db_query($db_sql,$dbname));
  310. if(!$db){
  311. if(!db_query("INSERT INTO {db} (name) VALUES ('%s')",$dbname)){
  312. tripal_cv_obo_quiterror("Cannot create '$dbname' db in Chado.");
  313. }
  314. $db = db_fetch_object(db_query($db_sql,$dbname));
  315. }
  316. return $db;
  317. }
  318. /**
  319. *
  320. * @ingroup tripal_obo_loader
  321. */
  322. function tripal_cv_obo_add_cv($name,$comment){
  323. // see if the CV (default-namespace) exists already in the database
  324. $vocab = $name;
  325. $remark = $comment;
  326. $cv_sql = "SELECT * FROM {cv} WHERE name = '%s'";
  327. $cv = db_fetch_object(db_query($cv_sql,$vocab));
  328. // if the CV exists then update it, otherwise insert
  329. if(!$cv){
  330. $sql = "INSERT INTO {cv} (name,definition) VALUES ('%s','%s')";
  331. if(!db_query($sql,$vocab,$remark)){
  332. tripal_cv_obo_quiterror("Failed to create the CV record");
  333. }
  334. $cv = db_fetch_object(db_query($cv_sql,$vocab));
  335. } else {
  336. $sql = "UPDATE {cv} SET definition = '%s' WHERE name ='%s'";
  337. if(!db_query($sql,$remark,$vocab)){
  338. tripal_cv_obo_quiterror("Failed to update the CV record");
  339. }
  340. $cv = db_fetch_object(db_query($cv_sql,$vocab));
  341. }
  342. return $cv;
  343. }
  344. /**
  345. *
  346. * @ingroup tripal_obo_loader
  347. */
  348. function tripal_cv_obo_add_relationship($cvterm,$defaultcv,$obo,$rel,$objname,$object_is_relationship=0){
  349. // make sure the relationship cvterm exists
  350. $term = array(
  351. 'name' => array($rel),
  352. 'id' => array($rel),
  353. 'definition' => array(''),
  354. 'is_obsolete' => array(0),
  355. );
  356. $relcvterm = tripal_cv_obo_add_cvterm($term,$defaultcv,1,0);
  357. if(!$relcvterm){
  358. tripal_cv_obo_quiterror("Cannot find or insert the relationship term: $rel\n");
  359. }
  360. // get the object term
  361. $objterm = tripal_cv_obo_get_term($obo,$objname);
  362. if(!$objterm) {
  363. tripal_cv_obo_quiterror("Could not find object term $objname\n");
  364. }
  365. $objcvterm = tripal_cv_obo_add_cvterm($objterm,$defaultcv,$object_is_relationship,1);
  366. if(!$objcvterm){
  367. tripal_cv_obo_quiterror("Cannot add/find cvterm");
  368. }
  369. // check to see if the cvterm_relationship already exists, if not add it
  370. $cvrsql = "SELECT * FROM {cvterm_relationship} WHERE type_id = %d and subject_id = %d and object_id = %d";
  371. if(!db_fetch_object(db_query($cvrsql,$relcvterm->cvterm_id,$cvterm->cvterm_id,$objcvterm->cvterm_id))){
  372. $sql = "INSERT INTO {cvterm_relationship} ".
  373. "(type_id,subject_id,object_id) VALUES (%d,%d,%d)";
  374. if(!db_query($sql,$relcvterm->cvterm_id,$cvterm->cvterm_id,$objcvterm->cvterm_id)){
  375. tripal_cv_obo_quiterror("Cannot add term relationship: '$cvterm->name' $rel '$objcvterm->name'");
  376. }
  377. }
  378. return 1;
  379. }
  380. /**
  381. *
  382. * @ingroup tripal_obo_loader
  383. */
  384. function tripal_cv_obo_get_term($obo,$id){
  385. foreach ($obo as $type){
  386. foreach ($type as $term){
  387. $accession = $term['id'][0];
  388. if(strcmp($accession,$id)==0){
  389. return $term;
  390. }
  391. }
  392. }
  393. return;
  394. }
  395. /**
  396. *
  397. * @ingroup tripal_obo_loader
  398. */
  399. function tripal_cv_obo_add_synonyms($term,$cvterm){
  400. // make sure we have a 'synonym_type' vocabulary
  401. $sql = "SELECT * FROM {cv} WHERE name='synonym_type'";
  402. $syncv = db_fetch_object(db_query($sql));
  403. if(!$syncv){
  404. $sql = "INSERT INTO {cv} (name,definition) VALUES ('synonym_type','')";
  405. if(!db_query($sql)){
  406. tripal_cv_obo_quiterror("Failed to add the synonyms type vocabulary");
  407. }
  408. $syncv = db_fetch_object(db_query($sql));
  409. }
  410. // now add the synonyms
  411. if(isset($term['synonym'])){
  412. foreach($term['synonym'] as $synonym){
  413. // separate out the synonym definition and the synonym type
  414. $def = preg_replace('/^\s*"(.*)"\s*.*$/','\1',$synonym);
  415. $type = strtolower(preg_replace('/^.*"\s+(.*?)\s+.*$/','\1',$synonym));
  416. // make sure the synonym type exists in the 'synonym_type' vocabulary
  417. $cvtsql = "
  418. SELECT *
  419. FROM {cvterm} CVT
  420. INNER JOIN {cv} CV ON CVT.cv_id = CV.cv_id
  421. WHERE CVT.name = '%s' and CV.name = '%s'
  422. ";
  423. $syntype = db_fetch_object(db_query($cvtsql,$type,'synonym_type'));
  424. if(!$syntype){
  425. // build a 'term' object so we can add the missing term
  426. $term = array(
  427. 'name' => array($type),
  428. 'id' => array("internal:$type"),
  429. 'definition' => array(''),
  430. 'is_obsolete' => array(0),
  431. );
  432. $syntype = tripal_cv_obo_add_cvterm($term,$syncv,0,1);
  433. if(!$syntype){
  434. tripal_cv_obo_quiterror("Cannot add synonym type: internal:$type");
  435. }
  436. }
  437. // make sure the synonym doesn't already exists
  438. $sql = "
  439. SELECT *
  440. FROM {cvtermsynonym}
  441. WHERE cvterm_id = %d and synonym = '%s'
  442. ";
  443. $syn = db_fetch_object(db_query($sql,$cvterm->cvterm_id,$def));
  444. if(!$syn){
  445. $sql = "INSERT INTO {cvtermsynonym} (cvterm_id,synonym,type_id)
  446. VALUES(%d,'%s',%d)";
  447. if(!db_query($sql,$cvterm->cvterm_id,$def,$syntype->cvterm_id)){
  448. tripal_cv_obo_quiterror("Failed to insert the synonym for term: $name ($def)");
  449. }
  450. }
  451. // now add the dbxrefs for the synonym if we have a comma in the middle
  452. // of a description then this will cause problems when splitting os lets
  453. // just change it so it won't mess up our splitting and then set it back
  454. // later.
  455. // $synonym = preg_replace('/(".*?),\s(.*?")/','$1,_$2',$synonym);
  456. // $dbxrefs = preg_split("/, /",preg_replace('/^.*\[(.*?)\]$/','\1',$synonym));
  457. // foreach($dbxrefs as $dbxref){
  458. // $dbxref = preg_replace('/,_/',", ",$dbxref);
  459. // if($dbxref){
  460. // tripal_cv_obo_add_cvterm_dbxref($syn,$dbxref);
  461. // }
  462. // }
  463. }
  464. }
  465. return 1;
  466. }
  467. /**
  468. *
  469. * @ingroup tripal_obo_loader
  470. */
  471. function tripal_cv_obo_parse($obo_file,&$obo,&$header){
  472. $i = 0;
  473. $in_header = 1;
  474. $stanza = array();
  475. // iterate through the lines in the OBO file and parse the stanzas
  476. $fh = fopen($obo_file,'r');
  477. while($line = fgets($fh)) {
  478. $i++;
  479. // remove newlines
  480. $line = rtrim($line);
  481. // skip empty lines
  482. if(strcmp($line,'')==0) { continue; }
  483. //remove comments from end of lines
  484. $line = preg_replace('/^(.*?)\!.*$/','\1',$line); // TODO: if the explamation is escaped
  485. if(preg_match('/^\s*\[/',$line)){ // at the first stanza we're out of header
  486. $in_header = 0;
  487. // load the stanza we just finished reading
  488. if(sizeof($stanza) > 0){
  489. if(!isset($obo[$type])){
  490. $obo[$type] = array();
  491. }
  492. if(!isset($obo[$type][$stanza['id'][0]])){
  493. $obo[$type][$stanza['id'][0]] = $stanza;
  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. $tag = preg_replace("/\|-\|-\|/","\:",$tag); // return the escaped colon
  510. $value = preg_replace("/\|-\|-\|/","\:",$value);
  511. if($in_header){
  512. if(!isset($header[$tag])){
  513. $header[$tag] = array();
  514. }
  515. $header[$tag][] = $value;
  516. } else {
  517. if(!isset($stanza[$tag])){
  518. $stanza[$tag] = array();
  519. }
  520. $stanza[$tag][] = $value;
  521. }
  522. }
  523. // now add the last term in the file
  524. if(sizeof($stanza) > 0){
  525. if(!isset($obo[$type])){
  526. $obo[$type] = array();
  527. }
  528. if(!isset($obo[$type][$stanza['id'][0]])){
  529. $obo[$type][$stanza['id'][0]] = $stanza;
  530. } else {
  531. array_merge($obo[$type][$stanza['id'][0]],$stanza);
  532. }
  533. }
  534. }
  535. /**
  536. *
  537. * @ingroup tripal_obo_loader
  538. */
  539. function tripal_cv_obo_add_cvterm_dbxref($cvterm,$xref){
  540. $dbname = preg_replace('/^(.+?):.*$/','$1',$xref);
  541. $accession = preg_replace('/^.+?:\s*(.*?)(\{.+$|\[.+$|\s.+$|\".+$|$)/','$1',$xref);
  542. $description = preg_replace('/^.+?\"(.+?)\".*?$/','$1',$xref);
  543. $dbxrefs = preg_replace('/^.+?\[(.+?)\].*?$/','$1',$xref);
  544. if(!$accession){
  545. tripal_cv_obo_quiterror();
  546. watchdog('tripal_cv',"Cannot add a dbxref without an accession: '$xref'"
  547. ,NULL,WATCHDOG_WARNING);
  548. return 0;
  549. }
  550. // if the xref is a database link, handle that specially
  551. if(strcmp($dbname,'http')==0){
  552. $accession = $xref;
  553. $dbname = 'URL';
  554. }
  555. // check to see if the database exists
  556. $db = tripal_cv_obo_add_db($dbname);
  557. if(!$db){
  558. tripal_cv_obo_quiterror("Cannot find database '$dbname' in Chado.");
  559. }
  560. // now add the dbxref
  561. $dbxref = tripal_cv_obo_add_dbxref($db->db_id,$accession,'',$description);
  562. if(!$dbxref){
  563. tripal_cv_obo_quiterror("Cannot find or add the database reference (dbxref)");
  564. }
  565. // finally add the cvterm_dbxref but first check to make sure it exists
  566. $sql = "SELECT * from {cvterm_dbxref} WHERE cvterm_id = %d and dbxref_id = %d";
  567. if(!db_fetch_object(db_query($sql,$cvterm->cvterm_id,$dbxref->dbxref_id))){
  568. $sql = "INSERT INTO {cvterm_dbxref} (cvterm_id,dbxref_id)".
  569. "VALUES (%d,%d)";
  570. if(!db_query($sql,$cvterm->cvterm_id,$dbxref->dbxref_id)){
  571. tripal_cv_obo_quiterror("Cannot add cvterm_dbxref: $xref");
  572. }
  573. }
  574. return 1;
  575. }
  576. /**
  577. *
  578. * @ingroup tripal_obo_loader
  579. */
  580. function tripal_cv_obo_add_cvterm_prop($cvterm,$property,$value,$rank){
  581. // make sure the 'cvterm_property_type' CV exists
  582. $cv = tripal_cv_obo_add_cv('cvterm_property_type','');
  583. if(!$cv){
  584. tripal_cv_obo_quiterror("Cannot add/find cvterm_property_type cvterm");
  585. }
  586. // get the property type cvterm. If it doesn't exist then we want to add it
  587. $sql = "
  588. SELECT *
  589. FROM {cvterm} CVT INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  590. WHERE CVT.name = '%s' and CV.name = '%s'
  591. ";
  592. $cvproptype = db_fetch_object(db_query($sql,$property,'cvterm_property_type'));
  593. if(!$cvproptype){
  594. $term = array(
  595. 'name' => array($property),
  596. 'id' => array("internal:$property"),
  597. 'definition' => array(''),
  598. 'is_obsolete' => array(0),
  599. );
  600. $cvproptype = tripal_cv_obo_add_cvterm($term,$cv,0,0);
  601. if(!$cvproptype){
  602. tripal_cv_obo_quiterror("Cannot add cvterm property: internal:$property");
  603. }
  604. }
  605. // remove any properties that currently exist for this term. We'll reset them
  606. if($rank == 0){
  607. $sql = "DELETE FROM {cvtermprop} WHERE cvterm_id = %d";
  608. db_query($sql,$cvterm->cvterm_id);
  609. }
  610. // now add the property
  611. $sql = "INSERT INTO {cvtermprop} (cvterm_id,type_id,value,rank) ".
  612. "VALUES (%d, %d, '%s',%d)";
  613. if(!db_query($sql,$cvterm->cvterm_id,$cvproptype->cvterm_id,$value,$rank)){
  614. tripal_cv_obo_quiterror("Could not add property $property for term\n");
  615. }
  616. return 1;
  617. }
  618. /**
  619. *
  620. * @ingroup tripal_obo_loader
  621. */
  622. function tripal_cv_obo_add_cvterm($term,$defaultcv,$is_relationship = 0,$update = 1){
  623. // get the term properties
  624. $id = $term['id'][0];
  625. $name = $term['name'][0];
  626. $cvname = $term['namespace'][0];
  627. $definition = preg_replace('/^\"(.*)\"/','\1',$term['def'][0]);
  628. $is_obsolete = 0;
  629. if(isset($term['is_obsolete'][0]) and strcmp($term['is_obsolete'][0],'true')==0){
  630. $is_obsolete = 1;
  631. }
  632. if(!$cvname){
  633. $cvname = $defaultcv->name;
  634. }
  635. // make sure the CV name exists
  636. $cv = tripal_cv_obo_add_cv($cvname,'');
  637. if(!$cv){
  638. tripal_cv_obo_quiterror("Cannot find namespace '$cvname' when adding/updating $id");
  639. }
  640. // this SQL statement will be used a lot to find a cvterm so just set it
  641. // here for easy reference below.
  642. $cvtermsql = "SELECT CVT.name, CVT.cvterm_id, DB.name as dbname, DB.db_id
  643. FROM {cvterm} CVT
  644. INNER JOIN {dbxref} DBX on CVT.dbxref_id = DBX.dbxref_id
  645. INNER JOIN {db} DB on DBX.db_id = DB.db_id
  646. INNER JOIN {cv} CV on CV.cv_id = CVT.cv_id
  647. WHERE CVT.name = '%s' and DB.name = '%s'";
  648. // get the accession and the database from the cvterm
  649. if(preg_match('/^.+?:.*$/',$id)){
  650. $accession = preg_replace('/^.+?:(.*)$/','\1',$id);
  651. $dbname = preg_replace('/^(.+?):.*$/','\1',$id);
  652. }
  653. if($is_relationship and !$dbname){
  654. $accession = $id;
  655. // because this is a relationship cvterm first check to see if it
  656. // exists in the relationship ontology. If it does then return the cvterm.
  657. // If not then set the dbname to _global and we'll add it or find it there
  658. $cvterm = db_fetch_object(db_query($cvtermsql,$name,'OBO_REL'));
  659. if($cvterm){
  660. return $cvterm;
  661. } else {
  662. // next check if this term is in the _global ontology. If it is then
  663. // return it no matter what the original CV
  664. $dbname = '_global';
  665. $cvterm = db_fetch_object(db_query($cvtermsql,$name,$dbname));
  666. if($cvterm){
  667. return $cvterm;
  668. }
  669. }
  670. }
  671. if(!$is_relationship and !$dbname){
  672. tripal_cv_obo_quiterror("A database identifier is missing from the term: $id");
  673. }
  674. // check to see if the database exists.
  675. $db = tripal_cv_obo_add_db($dbname);
  676. if(!$db){
  677. tripal_cv_obo_quiterror("Cannot find database '$dbname' in Chado.");
  678. }
  679. // if the cvterm doesn't exist then add it otherwise just update it
  680. $cvterm = db_fetch_object(db_query($cvtermsql,$name,$dbname));
  681. if(!$cvterm){
  682. // check to see if the dbxref exists if not, add it
  683. $dbxref = tripal_cv_obo_add_dbxref($db->db_id,$accession);
  684. if(!$dbxref){
  685. tripal_cv_obo_quiterror("Failed to find or insert the dbxref record for cvterm, $name (id: $accession), for database $dbname");
  686. }
  687. // check to see if the dbxref already has an entry in the cvterm table
  688. $sql = "SELECT * FROM {cvterm} WHERE dbxref_id = %d";
  689. $check = db_fetch_object(db_query($sql,$dbxref->dbxref_id));
  690. if(!$check){
  691. // now add the cvterm
  692. $sql = "
  693. INSERT INTO {cvterm} (cv_id, name, definition, dbxref_id,
  694. is_obsolete, is_relationshiptype)
  695. VALUES (%d,'%s','%s',%d,%d,%d)
  696. ";
  697. if(!db_query($sql,$cv->cv_id,$name,$definition,
  698. $dbxref->dbxref_id,$is_obsolete,$is_relationship)){
  699. if(!$is_relationship){
  700. tripal_cv_obo_quiterror("Failed to insert the term: $name ($dbname)");
  701. } else {
  702. tripal_cv_obo_quiterror("Failed to insert the relationship term: $name (cv: " . $cvname . " db: $dbname)");
  703. }
  704. }
  705. }
  706. elseif($check and strcmp($check->name,$name)!=0){
  707. // this dbxref_id alrady exists in the database but the name is
  708. // different. We will trust that the OBO is correct and that there
  709. // has been a name change for this dbxref, so we'll update
  710. $sql = "
  711. UPDATE {cvterm} SET name = '%s', definition = '%s',
  712. is_obsolete = %d, is_relationshiptype = %d
  713. ";
  714. if(!db_query($sql,$name,$definition,$is_obsolete,$is_relationship)){
  715. if(!$is_relationship){
  716. tripal_cv_obo_quiterror("Failed to update the term: $name ($dbname)");
  717. } else {
  718. tripal_cv_obo_quiterror("Failed to update the relationship term: $name (cv: " . $cvname . " db: $dbname)");
  719. }
  720. }
  721. }
  722. elseif($check and strcmp($check->name,$name)==0){
  723. // this entry already exists. We're good, so do nothing
  724. }
  725. $cvterm = db_fetch_object(db_query($cvtermsql,$name,$dbname));
  726. if(!$is_relationship){
  727. print "Added CV term: $name ($dbname)\n";
  728. } else {
  729. print "Added relationship CV term: $name ($dbname)\n";
  730. }
  731. }
  732. elseif($update) { // update the cvterm
  733. $sql = "
  734. UPDATE {cvterm} SET name='%s', definition='%s',
  735. is_obsolete = %d, is_relationshiptype = %d
  736. WHERE cvterm_id = %d
  737. ";
  738. if(!db_query($sql,$term['name'][0],$definition,
  739. $is_obsolete,$is_relationship,$cvterm->cvterm_id)){
  740. tripal_cv_obo_quiterror("Failed to update the term: $name");
  741. }
  742. $cvterm = db_fetch_object(db_query($cvtermsql,$name,$dbname));
  743. if(!$is_relationship){
  744. print "Updated CV term: $name ($dbname)\n";
  745. } else {
  746. print "Updated relationship CV term: $name ($dbname)\n";
  747. }
  748. }
  749. // return the cvterm
  750. return $cvterm;
  751. }
  752. /**
  753. *
  754. * @ingroup tripal_obo_loader
  755. */
  756. function tripal_cv_obo_add_dbxref($db_id,$accession,$version='',$description=''){
  757. // check to see if the dbxref exists if not, add it
  758. $dbxsql = "SELECT dbxref_id FROM {dbxref} WHERE db_id = %d and accession = '%s'";
  759. $dbxref = db_fetch_object(db_query($dbxsql,$db_id,$accession));
  760. if(!$dbxref){
  761. $sql = "
  762. INSERT INTO {dbxref} (db_id, accession, version, description)
  763. VALUES (%d,'%s','%s','%s')
  764. ";
  765. if(!db_query($sql,$db_id,$accession,$version,$description)){
  766. tripal_cv_obo_quiterror("Failed to insert the dbxref record $accession");
  767. }
  768. print "Added Dbxref accession: $accession\n";
  769. $dbxref = db_fetch_object(db_query($dbxsql,$db_id,$accession));
  770. }
  771. return $dbxref;
  772. }