obo_loader.php 26 KB

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