obo_loader.php 28 KB

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