tripal_chado.obo_loader.inc 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  1. <?php
  2. /**
  3. * @file
  4. * Functions to aid in loading ontologies into the chado cv module
  5. */
  6. /**
  7. * @defgroup tripal_obo_loader Ontology Loader
  8. * @ingroup tripal_chado
  9. * @{
  10. * Functions to aid in loading ontologies into the chado cv module
  11. * @}
  12. */
  13. /**
  14. * Provides the form to load an already existing controlled
  15. * Vocabulary into chado
  16. *
  17. * @param $form
  18. * The form array
  19. * @param $form_state
  20. * The form state array
  21. *
  22. * @return
  23. * The form array with new additions
  24. *
  25. * @ingroup tripal_obo_loader
  26. */
  27. function tripal_cv_obo_form($form, &$form_state) {
  28. // get a list of db from chado for user to choose
  29. $sql = "SELECT * FROM {tripal_cv_obo} ORDER BY name";
  30. $results = db_query($sql);
  31. $obos = array();
  32. $obos[] = 'Select a Vocabulary';
  33. foreach ($results as $obo) {
  34. $obos[$obo->obo_id] = $obo->name;
  35. }
  36. $obo_id = '';
  37. if (array_key_exists('values', $form_state)) {
  38. $obo_id = array_key_exists('obo_id', $form_state['values']) ? $form_state['values']['obo_id'] : '';
  39. }
  40. $form['instructions']['info'] = array(
  41. '#type' => 'item',
  42. '#markup' => t('This page allows you to load vocabularies and ontologies
  43. that are in OBO format. Once loaded, the terms from these
  44. vocabularies can be used to create content.
  45. You may use the form below to either reload a vocabulary that is already
  46. loaded (as when new updates to that vocabulary are available) or load a new
  47. vocabulary.'),
  48. );
  49. $form['obo_existing'] = array(
  50. '#type' => 'fieldset',
  51. '#title' => t('Use a Saved Ontology OBO Reference'),
  52. '#prefix' => '<span id="obo-existing-fieldset">',
  53. '#suffix' => '</span>'
  54. );
  55. $form['obo_existing']['existing_instructions']= array(
  56. '#type' => 'item',
  57. '#markup' => t('The vocabularies listed in the select box below have bene pre-populated
  58. upon installation of Tripal or have been previously loaded. Select one to edit
  59. its settings or submit for loading. You may reload any vocabulary that has
  60. already been loaded to retrieve any new updates.'),
  61. );
  62. $form['obo_existing']['obo_id'] = array(
  63. '#title' => t('Ontology OBO File Reference'),
  64. '#type' => 'select',
  65. '#options' => $obos,
  66. '#ajax' => array(
  67. 'callback' => 'tripal_cv_obo_form_ajax_callback',
  68. 'wrapper' => 'obo-existing-fieldset',
  69. ),
  70. '#description' => t('Select a vocabulary to import.')
  71. );
  72. // If the user has selected an OBO ID then get the form elements for
  73. // updating.
  74. if ($obo_id) {
  75. $uobo_name = '';
  76. $uobo_url = '';
  77. $uobo_file = '';
  78. $vocab = db_select('tripal_cv_obo', 't')
  79. ->fields('t', array('name', 'path'))
  80. ->condition('obo_id', $obo_id)
  81. ->execute()
  82. ->fetchObject();
  83. $uobo_name = $vocab->name;
  84. if (preg_match('/^http/', $vocab->path)) {
  85. $uobo_url = $vocab->path;
  86. }
  87. else {
  88. $uobo_file = trim($vocab->path);
  89. $matches = array();
  90. if (preg_match('/\{(.*?)\}/', $uobo_file, $matches)) {
  91. $modpath = drupal_get_path('module', $matches[1]);
  92. $uobo_file = '/' . preg_replace('/\{.*?\}/', $modpath, $uobo_file);
  93. }
  94. }
  95. // We don't want the previous value to remain. We want the new default to
  96. // show up, so remove the input values
  97. unset($form_state['input']['uobo_name']);
  98. unset($form_state['input']['uobo_url']);
  99. unset($form_state['input']['uobo_file']);
  100. $form['obo_existing']['uobo_name']= array(
  101. '#type' => 'textfield',
  102. '#title' => t('Vocabulary Name'),
  103. '#description' => t('Please provide a name for this vocabulary. After upload, this name will appear in the drop down
  104. list above for use again later.'),
  105. '#default_value' => $uobo_name,
  106. );
  107. $form['obo_existing']['uobo_url']= array(
  108. '#type' => 'textfield',
  109. '#title' => t('Remote URL'),
  110. '#description' => t('Please enter a URL for the online OBO file. The file will be downloaded and parsed.
  111. (e.g. http://www.obofoundry.org/ro/ro.obo'),
  112. '#default_value' => $uobo_url,
  113. );
  114. $form['obo_existing']['uobo_file']= array(
  115. '#type' => 'textfield',
  116. '#title' => t('Local File'),
  117. '#description' => t('Please enter the file system path for an OBO
  118. definition file. If entering a path relative to
  119. the Drupal installation you may use a relative path that excludes the
  120. Drupal installation directory (e.g. sites/default/files/xyz.obo). Note
  121. that Drupal relative paths have no preceeding slash.
  122. Otherwise, please provide the full path on the filesystem. The path
  123. must be accessible to the web server on which this Drupal instance is running.'),
  124. '#default_value' => $uobo_file,
  125. );
  126. $form['obo_existing']['update_obo_details'] = array(
  127. '#type' => 'submit',
  128. '#value' => 'Update Ontology Details',
  129. '#name' => 'update_obo_details'
  130. );
  131. $form['obo_existing']['update_load_obo'] = array(
  132. '#type' => 'submit',
  133. '#value' => 'Load Vocabulary',
  134. '#name' => 'update_load_obo'
  135. );
  136. }
  137. $form['obo_new'] = array(
  138. '#type' => 'fieldset',
  139. '#title' => t('Add a New Ontology OBO Reference'),
  140. '#collapsible' => TRUE,
  141. '#collapsed' => TRUE,
  142. );
  143. $form['obo_new']['path_instructions']= array(
  144. '#value' => t('Provide the name and path for the OBO file. If the vocabulary OBO file
  145. is stored local to the server provide a file name. If the vocabulry is stored remotely,
  146. provide a URL. Only provide a URL or a local file, not both.'),
  147. );
  148. $form['obo_new']['obo_name']= array(
  149. '#type' => 'textfield',
  150. '#title' => t('New Vocabulary Name'),
  151. '#description' => t('Please provide a name for this vocabulary. After upload, this name will appear in the drop down
  152. list above for use again later.'),
  153. );
  154. $form['obo_new']['obo_url']= array(
  155. '#type' => 'textfield',
  156. '#title' => t('Remote URL'),
  157. '#description' => t('Please enter a URL for the online OBO file. The file will be downloaded and parsed.
  158. (e.g. http://www.obofoundry.org/ro/ro.obo'),
  159. );
  160. $form['obo_new']['obo_file']= array(
  161. '#type' => 'textfield',
  162. '#title' => t('Local File'),
  163. '#description' => t('Please enter the file system path for an OBO
  164. definition file. If entering a path relative to
  165. the Drupal installation you may use a relative path that excludes the
  166. Drupal installation directory (e.g. sites/default/files/xyz.obo). Note
  167. that Drupal relative paths have no preceeding slash.
  168. Otherwise, please provide the full path on the filesystem. The path
  169. must be accessible to the web server on which this Drupal instance is running.'),
  170. );
  171. $form['obo_new']['add_new_obo'] = array(
  172. '#type' => 'submit',
  173. '#value' => t('Add this vocabulary'),
  174. '#name' => 'add_new_obo',
  175. );
  176. $form['#redirect'] = 'admin/tripal/tripal_chado/obo_loader';
  177. return $form;
  178. }
  179. /**
  180. *
  181. * @param $form
  182. * @param $form_state
  183. */
  184. function tripal_cv_obo_form_validate($form, &$form_state) {
  185. $obo_id = $form_state['values']['obo_id'];
  186. $obo_name = trim($form_state['values']['obo_name']);
  187. $obo_url = trim($form_state['values']['obo_url']);
  188. $obo_file = trim($form_state['values']['obo_file']);
  189. $uobo_name = array_key_exists('uobo_name', $form_state['values']) ? trim($form_state['values']['uobo_name']) : '';
  190. $uobo_url = array_key_exists('uobo_url', $form_state['values']) ? trim($form_state['values']['uobo_url']) : '';
  191. $uobo_file = array_key_exists('uobo_file', $form_state['values']) ? trim($form_state['values']['uobo_file']) : '';
  192. // Make sure if the name is changed it doesn't conflict with another OBO.
  193. if ($form_state['clicked_button']['#name'] == 'update_obo_details' or
  194. $form_state['clicked_button']['#name'] == 'update_load_obo') {
  195. // Get the current record
  196. $vocab = db_select('tripal_cv_obo', 't')
  197. ->fields('t', array('obo_id', 'name', 'path'))
  198. ->condition('name', $uobo_name)
  199. ->execute()
  200. ->fetchObject();
  201. if ($vocab and $vocab->obo_id != $obo_id) {
  202. form_set_error('uobo_name', 'The vocabulary name must be different from existing vocabularies');
  203. }
  204. // Make sure the file exists. First check if it is a relative path
  205. $dfile = $_SERVER['DOCUMENT_ROOT'] . base_path() . $uobo_file;
  206. if (!file_exists($dfile)) {
  207. if (!file_exists($uobo_file)) {
  208. form_set_error('uobo_file', 'The specified path does not exist or cannot be read.');
  209. }
  210. }
  211. if (!$uobo_url and !$uobo_file) {
  212. form_set_error('uobo_url', 'Please provide a URL or a path for the vocabulary.');
  213. }
  214. if ($uobo_url and $uobo_file) {
  215. form_set_error('uobo_url', 'Please provide only a URL or a path for the vocabulary, but not both.');
  216. }
  217. }
  218. if ($form_state['clicked_button']['#name'] == 'add_new_obo') {
  219. // Get the current record
  220. $vocab = db_select('tripal_cv_obo', 't')
  221. ->fields('t', array('obo_id', 'name', 'path'))
  222. ->condition('name', $obo_name)
  223. ->execute()
  224. ->fetchObject();
  225. if ($vocab) {
  226. form_set_error('obo_name', 'The vocabulary name must be different from existing vocabularies');
  227. }
  228. // Make sure the file exists. First check if it is a relative path
  229. $dfile = $_SERVER['DOCUMENT_ROOT'] . base_path() . $obo_file;
  230. if (!file_exists($dfile)) {
  231. if (!file_exists($obo_file)) {
  232. form_set_error('obo_file', 'The specified path does not exist or cannot be read.');
  233. }
  234. }
  235. if (!$obo_url and !$obo_file) {
  236. form_set_error('obo_url', 'Please provide a URL or a path for the vocabulary.');
  237. }
  238. if ($obo_url and $obo_file) {
  239. form_set_error('obo_url', 'Please provide only a URL or a path for the vocabulary, but not both.');
  240. }
  241. }
  242. }
  243. /**
  244. * The submit function for the load ontology form. It registers a
  245. * tripal job to import the user specified ontology file
  246. *
  247. * @param $form
  248. * The form array
  249. * @param $form_state
  250. * The form state array
  251. *
  252. *
  253. * @ingroup tripal_obo_loader
  254. */
  255. function tripal_cv_obo_form_submit($form, &$form_state) {
  256. $obo_id = $form_state['values']['obo_id'];
  257. $obo_name = trim($form_state['values']['obo_name']);
  258. $obo_url = trim($form_state['values']['obo_url']);
  259. $obo_file = trim($form_state['values']['obo_file']);
  260. $uobo_name = array_key_exists('uobo_name', $form_state['values']) ? trim($form_state['values']['uobo_name']) : '';
  261. $uobo_url = array_key_exists('uobo_url', $form_state['values']) ? trim($form_state['values']['uobo_url']) : '';
  262. $uobo_file = array_key_exists('uobo_file', $form_state['values']) ? trim($form_state['values']['uobo_file']) : '';
  263. // If the user requested to alter the details then do that.
  264. if ($form_state['clicked_button']['#name'] == 'update_obo_details' or
  265. $form_state['clicked_button']['#name'] == 'update_load_obo') {
  266. $success = db_update('tripal_cv_obo')
  267. ->fields(array(
  268. 'name' => $uobo_name,
  269. 'path' => $uobo_url ? $uobo_url : $uobo_file,
  270. ))
  271. ->condition('obo_id', $obo_id)
  272. ->execute();
  273. if ($success) {
  274. drupal_set_message(t("The vocabulary %vocab has been updated.", array('%vocab' => $uobo_name)));
  275. }
  276. else {
  277. drupal_set_message(t("The vocabulary %vocab could not be updated.", array('%vocab' => $uobo_name)), 'error');
  278. }
  279. }
  280. // If the user requested to update and load then we've already handled the
  281. // update now we just need to load.
  282. if ($form_state['clicked_button']['#name'] == 'update_load_obo') {
  283. tripal_submit_obo_job(array('obo_id' => $obo_id));
  284. }
  285. if ($form_state['clicked_button']['#name'] == 'add_new_obo') {
  286. $success = db_insert('tripal_cv_obo')
  287. ->fields(array(
  288. 'name' => $obo_name,
  289. 'path' => $obo_url ? $obo_url : $obo_file,
  290. ))
  291. ->execute();
  292. if ($success) {
  293. drupal_set_message(t("The vocabulary %vocab has been added.", array('%vocab' => $obo_name)));
  294. }
  295. else {
  296. drupal_set_message(t("The vocabulary %vocab could not be added.", array('%vocab' => $obo_name)), 'error');
  297. }
  298. }
  299. }
  300. /**
  301. * A wrapper function for importing the user specified OBO file into Chado by
  302. * specifying the obo_id of the OBO. It requires that the file be in OBO v1.2
  303. * compatible format. This function is typically executed via the Tripal jobs
  304. * management after a user submits a job via the Load Onotloies form.
  305. *
  306. * @param $obo_id
  307. * An obo_id from the tripal_cv_obo file that specifies which OBO file to import
  308. * @param $job_id
  309. * The job_id of the job from the Tripal jobs management system.
  310. *
  311. * @ingroup tripal_obo_loader
  312. */
  313. function tripal_chado_load_obo_v1_2_id($obo_id, $jobid = NULL) {
  314. // Get the OBO reference.
  315. $sql = "SELECT * FROM {tripal_cv_obo} WHERE obo_id = :obo_id";
  316. $obo = db_query($sql, array(':obo_id' => $obo_id))->fetchObject();
  317. // Convert the module name to the real path if present
  318. if (preg_match("/\{(.*?)\}/", $obo->path, $matches)) {
  319. $module = $matches[1];
  320. $path = drupal_realpath(drupal_get_path('module', $module));
  321. $obo->path = preg_replace("/\{.*?\}/", $path, $obo->path);
  322. }
  323. // if the reference is for a remote URL then run the URL processing function
  324. if (preg_match("/^https:\/\//", $obo->path) or
  325. preg_match("/^http:\/\//", $obo->path) or
  326. preg_match("/^ftp:\/\//", $obo->path)) {
  327. tripal_chado_load_obo_v1_2_url($obo->name, $obo->path, $jobid, 0);
  328. }
  329. // if the reference is for a local file then run the file processing function
  330. else {
  331. // check to see if the file is located local to Drupal
  332. $dfile = $_SERVER['DOCUMENT_ROOT'] . base_path() . $obo->path;
  333. if (file_exists($dfile)) {
  334. tripal_chado_load_obo_v1_2_file($obo->name, $dfile , $jobid, 0);
  335. }
  336. // if not local to Drupal, the file must be someplace else, just use
  337. // the full path provided
  338. else {
  339. if (file_exists($obo->path)) {
  340. tripal_chado_load_obo_v1_2_file($obo->name, $obo->path, $jobid, 0);
  341. }
  342. else {
  343. print "ERROR: could not find OBO file: '$obo->path'\n";
  344. }
  345. }
  346. }
  347. }
  348. /**
  349. * A wrapper function for importing the user specified OBO file into Chado by
  350. * specifying the filename and path of the OBO. It requires that the file be in OBO v1.2
  351. * compatible format. This function is typically executed via the Tripal jobs
  352. * management after a user submits a job via the Load Onotloies form.
  353. *
  354. * @param $obo_name
  355. * The name of the OBO (typially the ontology or controlled vocabulary name)
  356. * @param $file
  357. * The path on the file system where the ontology can be found
  358. * @param $job_id
  359. * The job_id of the job from the Tripal jobs management system.
  360. * @param $is_new
  361. * Set to TRUE if this is a new ontology that does not yet exist in the
  362. * tripal_cv_obo table. If TRUE the OBO will be added to the table.
  363. *
  364. * @ingroup tripal_obo_loader
  365. */
  366. function tripal_chado_load_obo_v1_2_file($obo_name, $file, $jobid = NULL, $is_new = TRUE) {
  367. $newcvs = array();
  368. if ($is_new) {
  369. tripal_insert_obo($obo_name, $file);
  370. }
  371. $success = tripal_chado_load_obo_v1_2($file, $jobid, $newcvs);
  372. if ($success) {
  373. // update the cvtermpath table
  374. tripal_chado_load_update_cvtermpath($newcvs, $jobid);
  375. print "\nDone\n";
  376. }
  377. }
  378. /**
  379. * A wrapper function for importing the user specified OBO file into Chado by
  380. * specifying the remote URL of the OBO. It requires that the file be in OBO v1.2
  381. * compatible format. This function is typically executed via the Tripal jobs
  382. * management after a user submits a job via the Load Onotloies form.
  383. *
  384. * @param $obo_name
  385. * The name of the OBO (typially the ontology or controlled vocabulary name)
  386. * @param $url
  387. * The remote URL of the OBO file.
  388. * @param $job_id
  389. * The job_id of the job from the Tripal jobs management system.
  390. * @param $is_new
  391. * Set to TRUE if this is a new ontology that does not yet exist in the
  392. * tripal_cv_obo table. If TRUE the OBO will be added to the table.
  393. *
  394. * @ingroup tripal_obo_loader
  395. */
  396. function tripal_chado_load_obo_v1_2_url($obo_name, $url, $jobid = NULL, $is_new = TRUE) {
  397. $newcvs = array();
  398. // first download the OBO
  399. $temp = tempnam(sys_get_temp_dir(), 'obo_');
  400. print "Downloading URL $url, saving to $temp\n";
  401. $url_fh = fopen($url, "r");
  402. $obo_fh = fopen($temp, "w");
  403. if (!$url_fh) {
  404. tripal_cv_obo_quiterror("Unable to download the remote OBO file at $url. Could a firewall be blocking outgoing connections? " .
  405. " if you are unable to download the file you may manually downlod the OBO file and use the web interface to " .
  406. " specify the location of the file on your server.");
  407. }
  408. while (!feof($url_fh)) {
  409. fwrite($obo_fh, fread($url_fh, 255), 255);
  410. }
  411. fclose($url_fh);
  412. fclose($obo_fh);
  413. if ($is_new) {
  414. tripal_insert_obo($obo_name, $url);
  415. }
  416. // second, parse the OBO
  417. $success = tripal_chado_load_obo_v1_2($temp, $jobid, $newcvs);
  418. if ($success) {
  419. // update the cvtermpath table
  420. tripal_chado_load_update_cvtermpath($newcvs, $jobid);
  421. print "Done\n";
  422. }
  423. // now remove the temp file
  424. unlink($temp);
  425. }
  426. /**
  427. * A function for executing the cvtermpath function of Chado. This function
  428. * populates the cvtermpath table of Chado for quick lookup of term
  429. * relationships
  430. *
  431. * @param $newcvs
  432. * An associative array of controlled vocabularies to update. The key must be
  433. * the name of the vocabulary and the value the cv_id from the cv table of chado.
  434. * @param $jobid
  435. * The job_id of the job from the Tripal jobs management system.
  436. *
  437. * @ingroup tripal_obo_loader
  438. */
  439. function tripal_chado_load_update_cvtermpath($newcvs, $jobid) {
  440. print "\nUpdating cvtermpath table. This may take a while...\n";
  441. foreach ($newcvs as $namespace => $cvid) {
  442. tripal_update_cvtermpath($cvid, $jobid);
  443. }
  444. }
  445. /**
  446. * Imports a given OBO file into Chado. This function is usually called by
  447. * one of three wrapper functions: tripal_chado_load_obo_v1_2_id,
  448. * tripal_chado_load_obo_v1_2_file or tirpal_cv_load_obo_v1_2_url. But, it can
  449. * be called directly if the full path to an OBO file is available on the
  450. * file system.
  451. *
  452. * @param $flie
  453. * The full path to the OBO file on the file system
  454. * @param $jobid
  455. * The job_id of the job from the Tripal jobs management system.
  456. * @param $newcvs
  457. * An empty array passed by reference that upon return will contain the list
  458. * of newly added vocabularies. The key will contain the CV name and the
  459. * value the new cv_id
  460. *
  461. *
  462. * @ingroup tripal_obo_loader
  463. */
  464. function tripal_chado_load_obo_v1_2($file, $jobid = NULL, &$newcvs) {
  465. $transaction = db_transaction();
  466. print "\nNOTE: Loading of this OBO file is performed using a database transaction. \n" .
  467. "If the load fails or is terminated prematurely then the entire set of \n" .
  468. "insertions/updates is rolled back and will not be found in the database\n\n";
  469. try {
  470. $header = array();
  471. // make sure our temporary table exists
  472. $ret = array();
  473. // empty the temp table
  474. $sql = "DELETE FROM {tripal_obo_temp}";
  475. chado_query($sql);
  476. print "Step 1: Preloading File $file\n";
  477. // parse the obo file
  478. $default_db = tripal_cv_obo_parse($file, $header, $jobid);
  479. // add the CV for this ontology to the database. The v1.2 definition
  480. // specifies a 'default-namespace' to be used if a 'namespace' is not
  481. // present for each stanza. Some ontologies have adopted the v1.4 method
  482. // in their v1.2 files and not including it.
  483. if (array_key_exists('default-namespace', $header)) {
  484. $defaultcv = tripal_insert_cv($header['default-namespace'][0], '');
  485. if (!$defaultcv) {
  486. tripal_cv_obo_quiterror('Cannot add namespace ' . $header['default-namespace'][0]);
  487. }
  488. $newcvs[$header['default-namespace'][0]] = $defaultcv->cv_id;
  489. }
  490. // if the 'default-namespace' is missing
  491. else {
  492. // look to see if an 'ontology' key is present. It is part of the v1.4
  493. // specification so it shouldn't be in the file, but just in case
  494. if (array_key_exists('ontology', $header)) {
  495. $defaultcv = tripal_insert_cv(strtoupper($header['ontology'][0]), '');
  496. if (!$defaultcv) {
  497. tripal_cv_obo_quiterror('Cannot add namespace ' . strtoupper($header['ontology'][0]));
  498. }
  499. $newcvs[strtoupper(strtoupper($header['ontology'][0]))] = $defaultcv->cv_id;
  500. }
  501. else {
  502. tripal_cv_obo_quiterror("Could not find a namespace for this OBO file.");
  503. }
  504. watchdog('t_obo_loader', "This OBO is missing the 'default-namespace' header. It is not possible to determine which vocabulary terms without a 'namespace' key should go. Instead, those terms will be placed in the '%vocab' vocabulary.",
  505. array('%vocab' => $defaultcv->name), WATCHDOG_WARNING);
  506. }
  507. // add any typedefs to the vocabulary first
  508. print "\nStep 2: Loading type defs...\n";
  509. tripal_cv_obo_load_typedefs($defaultcv, $newcvs, $default_db, $jobid);
  510. // next add terms to the vocabulary
  511. print "\nStep 3: Loading terms...\n";
  512. if (!tripal_cv_obo_process_terms($defaultcv, $jobid, $newcvs, $default_db)) {
  513. tripal_cv_obo_quiterror('Cannot add terms from this ontology');
  514. }
  515. }
  516. catch (Exception $e) {
  517. $transaction->rollback();
  518. print "\n"; // make sure we start errors on new line
  519. print "FAILED. Rolling back database changes...\n";
  520. watchdog_exception('T_obo_loader', $e);
  521. return FALSE;
  522. }
  523. return TRUE;
  524. }
  525. /**
  526. * Immediately terminates loading of the OBO file.
  527. *
  528. * @param $message
  529. * The error message to present to the user
  530. *
  531. * @ingroup tripal_obo_loader
  532. */
  533. function tripal_cv_obo_quiterror($message) {
  534. tripal_report_error("T_obo_loader", TRIPAL_ERROR, $message, array());
  535. exit;
  536. }
  537. /**
  538. * OBO files are divided into a typedefs terms section and vocabulary terms section.
  539. * This function loads the typedef terms from the OBO.
  540. *
  541. * @param $defaultcv
  542. * A database object containing a record from the cv table for the
  543. * default controlled vocabulary
  544. * @param $newcvs
  545. * An associative array of controlled vocabularies for this OBO. The key must be
  546. * the name of the vocabulary and the value the cv_id from the cv table of chado.
  547. * @param $default_db
  548. * The name of the default database.
  549. * @param $jobid
  550. * The job_id of the job from the Tripal jobs management system.
  551. *
  552. * @ingroup tripal_obo_loader
  553. */
  554. function tripal_cv_obo_load_typedefs($defaultcv, $newcvs, $default_db, $jobid) {
  555. $sql = "SELECT * FROM {tripal_obo_temp} WHERE type = 'Typedef' ";
  556. $typedefs = chado_query($sql);
  557. $sql = "
  558. SELECT count(*) as num_terms
  559. FROM {tripal_obo_temp}
  560. WHERE type = 'Typedef'
  561. ";
  562. $result = chado_query($sql)->fetchObject();
  563. $count = $result->num_terms;
  564. // calculate the interval for updates
  565. $interval = intval($count * 0.0001);
  566. if ($interval < 1) {
  567. $interval = 1;
  568. }
  569. $i = 0;
  570. foreach ($typedefs as $typedef) {
  571. $term = unserialize(base64_decode($typedef->stanza));
  572. // update the job status every interval
  573. if ($jobid and $i % $interval == 0) {
  574. $complete = ($i / $count) * 33.33333333;
  575. tripal_set_job_progress($jobid, intval($complete + 33.33333333));
  576. printf("%d of %d records. (%0.2f%%) Memory: %s bytes\r", $i, $count, $complete * 3, number_format(memory_get_usage()));
  577. }
  578. tripal_cv_obo_process_term($term, $defaultcv->name, 1, $newcvs, $default_db);
  579. $i++;
  580. }
  581. // set the final status
  582. if ($jobid) {
  583. if ($count > 0) {
  584. $complete = ($i / $count) * 33.33333333;
  585. }
  586. else {
  587. $complete = 33.33333333;
  588. }
  589. tripal_set_job_progress($jobid, intval($complete + 33.33333333));
  590. printf("%d of %d records. (%0.2f%%) Memory: %s bytes\r", $i, $count, $complete * 3, number_format(memory_get_usage()));
  591. }
  592. return 1;
  593. }
  594. /**
  595. * OBO files are divided into a typedefs section and a terms section. This
  596. * function loads the typedef terms from the OBO.
  597. *
  598. * @param $defaultcv
  599. * A database object containing a record from the cv table for the
  600. * default controlled vocabulary
  601. * @param $jobid
  602. * The job_id of the job from the Tripal jobs management system.
  603. * @param $newcvs
  604. * An associative array of controlled vocabularies for this OBO. The key must be
  605. * the name of the vocabulary and the value the cv_id from the cv table of chado.
  606. * @param $default_db
  607. * The name of the default database.
  608. * @ingroup tripal_obo_loader
  609. */
  610. function tripal_cv_obo_process_terms($defaultcv, $jobid = NULL, &$newcvs, $default_db) {
  611. $i = 0;
  612. // iterate through each term from the OBO file and add it
  613. $sql = "
  614. SELECT * FROM {tripal_obo_temp}
  615. WHERE type = 'Term'
  616. ORDER BY id
  617. ";
  618. $terms = chado_query($sql);
  619. $sql = "
  620. SELECT count(*) as num_terms
  621. FROM {tripal_obo_temp}
  622. WHERE type = 'Term'
  623. ";
  624. $result = chado_query($sql)->fetchObject();
  625. $count = $result->num_terms;
  626. // calculate the interval for updates
  627. $interval = intval($count * 0.0001);
  628. if ($interval < 1) {
  629. $interval = 1;
  630. }
  631. foreach ($terms as $t) {
  632. $term = unserialize(base64_decode($t->stanza));
  633. // update the job status every interval
  634. if ($jobid and $i % $interval == 0) {
  635. $complete = ($i / $count) * 33.33333333;
  636. tripal_set_job_progress($jobid, intval($complete + 66.666666));
  637. printf("%d of %d records. (%0.2f%%) Memory: %s bytes\r", $i, $count, $complete * 3, number_format(memory_get_usage()));
  638. }
  639. // add/update this term
  640. if (!tripal_cv_obo_process_term($term, $defaultcv->name, 0, $newcvs, $default_db)) {
  641. tripal_cv_obo_quiterror("Failed to process terms from the ontology");
  642. }
  643. $i++;
  644. }
  645. // set the final status
  646. if ($jobid) {
  647. if ($count > 0) {
  648. $complete = ($i / $count) * 33.33333333;
  649. }
  650. else {
  651. $complete = 33.33333333;
  652. }
  653. tripal_set_job_progress($jobid, intval($complete + 66.666666));
  654. printf("%d of %d records. (%0.2f%%) Memory: %s bytes\r", $i, $count, $complete * 3, number_format(memory_get_usage()));
  655. }
  656. return 1;
  657. }
  658. /**
  659. * Uses the provided term array to add/update information to Chado about the
  660. * term including the term, dbxref, synonyms, properties, and relationships.
  661. *
  662. * @param $term
  663. * An array representing the cvterm.
  664. * @param $defaultcv
  665. * The name of the default controlled vocabulary
  666. * @is_relationship
  667. * Set to 1 if this term is a relationship term
  668. * @default_db
  669. * The name of the default database.
  670. *
  671. * @ingroup tripal_obo_loader
  672. */
  673. function tripal_cv_obo_process_term($term, $defaultcv, $is_relationship = 0, &$newcvs, $default_db) {
  674. // make sure we have a namespace for this term
  675. if (!array_key_exists('namespace', $term) and !($defaultcv or $defaultcv == '')) {
  676. tripal_cv_obo_quiterror("Cannot add the term: no namespace defined. " . $term['id'][0]);
  677. }
  678. // construct the term array for sending to the tripal_chado_add_cvterm function
  679. // for adding a new cvterm
  680. $t = array();
  681. $t['id'] = $term['id'][0];
  682. $t['name'] = $term['name'][0];
  683. if (array_key_exists('def', $term)) {
  684. $t['definition'] = $term['def'][0];
  685. }
  686. if (array_key_exists('subset', $term)) {
  687. $t['subset'] = $term['subset'][0];
  688. }
  689. if (array_key_exists('namespace', $term)) {
  690. $t['namespace'] = $term['namespace'][0];
  691. }
  692. if (array_key_exists('is_obsolete', $term)) {
  693. $t['is_obsolete'] = $term['is_obsolete'][0];
  694. }
  695. $t['cv_name'] = $defaultcv;
  696. $t['is_relationship'] = $is_relationship;
  697. $t['db_name'] = $default_db;
  698. // add the cvterm
  699. $cvterm = tripal_insert_cvterm($t, array('update_existing' => TRUE));
  700. if (!$cvterm) {
  701. tripal_cv_obo_quiterror("Cannot add the term " . $term['id'][0]);
  702. }
  703. if (array_key_exists('namespace', $term)) {
  704. $newcvs[$term['namespace'][0]] = $cvterm->cv_id;
  705. }
  706. // now handle other properites
  707. if (array_key_exists('is_anonymous', $term)) {
  708. //print "WARNING: unhandled tag: is_anonymous\n";
  709. }
  710. if (array_key_exists('alt_id', $term)) {
  711. foreach ($term['alt_id'] as $alt_id) {
  712. if (!tripal_cv_obo_add_cvterm_dbxref($cvterm, $alt_id)) {
  713. tripal_cv_obo_quiterror("Cannot add alternate id $alt_id");
  714. }
  715. }
  716. }
  717. if (array_key_exists('subset', $term)) {
  718. //print "WARNING: unhandled tag: subset\n";
  719. }
  720. // add synonyms for this cvterm
  721. if (array_key_exists('synonym', $term)) {
  722. if (!tripal_cv_obo_add_synonyms($term, $cvterm)) {
  723. tripal_cv_obo_quiterror("Cannot add synonyms");
  724. }
  725. }
  726. // reformat the deprecated 'exact_synonym, narrow_synonym, and broad_synonym'
  727. // types to be of the v1.2 standard
  728. if (array_key_exists('exact_synonym', $term) or array_key_exists('narrow_synonym', $term) or array_key_exists('broad_synonym', $term)) {
  729. if (array_key_exists('exact_synonym', $term)) {
  730. foreach ($term['exact_synonym'] as $synonym) {
  731. $new = preg_replace('/^\s*(\".+?\")(.*?)$/', '$1 EXACT $2', $synonym);
  732. $term['synonym'][] = $new;
  733. }
  734. }
  735. if (array_key_exists('narrow_synonym', $term)) {
  736. foreach ($term['narrow_synonym'] as $synonym) {
  737. $new = preg_replace('/^\s*(\".+?\")(.*?)$/', '$1 NARROW $2', $synonym);
  738. $term['synonym'][] = $new;
  739. }
  740. }
  741. if (array_key_exists('broad_synonym', $term)) {
  742. foreach ($term['broad_synonym'] as $synonym) {
  743. $new = preg_replace('/^\s*(\".+?\")(.*?)$/', '$1 BROAD $2', $synonym);
  744. $term['synonym'][] = $new;
  745. }
  746. }
  747. if (!tripal_cv_obo_add_synonyms($term, $cvterm)) {
  748. tripal_cv_obo_quiterror("Cannot add/update synonyms");
  749. }
  750. }
  751. // add the comment to the cvtermprop table
  752. if (array_key_exists('comment', $term)) {
  753. $comments = $term['comment'];
  754. $j = 0;
  755. foreach ($comments as $comment) {
  756. if (!tripal_cv_obo_add_cvterm_prop($cvterm, 'comment', $comment, $j)) {
  757. tripal_cv_obo_quiterror("Cannot add/update cvterm property");
  758. }
  759. $j++;
  760. }
  761. }
  762. // add any other external dbxrefs
  763. if (array_key_exists('xref', $term)) {
  764. foreach ($term['xref'] as $xref) {
  765. if (!tripal_cv_obo_add_cvterm_dbxref($cvterm, $xref)) {
  766. tripal_cv_obo_quiterror("Cannot add/update cvterm database reference (dbxref).");
  767. }
  768. }
  769. }
  770. if (array_key_exists('xref_analog', $term)) {
  771. foreach ($term['xref_analog'] as $xref) {
  772. if (!tripal_cv_obo_add_cvterm_dbxref($cvterm, $xref)) {
  773. tripal_cv_obo_quiterror("Cannot add/update cvterm database reference (dbxref).");
  774. }
  775. }
  776. }
  777. if (array_key_exists('xref_unk', $term)) {
  778. foreach ($term['xref_unk'] as $xref) {
  779. if (!tripal_cv_obo_add_cvterm_dbxref($cvterm, $xref)) {
  780. tripal_cv_obo_quiterror("Cannot add/update cvterm database reference (dbxref).");
  781. }
  782. }
  783. }
  784. // add is_a relationships for this cvterm
  785. if (array_key_exists('is_a', $term)) {
  786. foreach ($term['is_a'] as $is_a) {
  787. if (!tripal_cv_obo_add_relationship($cvterm, $defaultcv, 'is_a', $is_a, $is_relationship, $default_db)) {
  788. tripal_cv_obo_quiterror("Cannot add relationship is_a: $is_a");
  789. }
  790. }
  791. }
  792. if (array_key_exists('intersection_of', $term)) {
  793. //print "WARNING: unhandled tag: intersection_of\n";
  794. }
  795. if (array_key_exists('union_of', $term)) {
  796. //print "WARNING: unhandled tag: union_on\n";
  797. }
  798. if (array_key_exists('disjoint_from', $term)) {
  799. //print "WARNING: unhandled tag: disjoint_from\n";
  800. }
  801. if (array_key_exists('relationship', $term)) {
  802. foreach ($term['relationship'] as $value) {
  803. $rel = preg_replace('/^(.+?)\s.+?$/', '\1', $value);
  804. $object = preg_replace('/^.+?\s(.+?)$/', '\1', $value);
  805. if (!tripal_cv_obo_add_relationship($cvterm, $defaultcv, $rel, $object, $is_relationship, $default_db)) {
  806. tripal_cv_obo_quiterror("Cannot add relationship $rel: $object");
  807. }
  808. }
  809. }
  810. if (array_key_exists('replaced_by', $term)) {
  811. //print "WARNING: unhandled tag: replaced_by\n";
  812. }
  813. if (array_key_exists('consider', $term)) {
  814. //print "WARNING: unhandled tag: consider\n";
  815. }
  816. if (array_key_exists('use_term', $term)) {
  817. //print "WARNING: unhandled tag: user_term\n";
  818. }
  819. if (array_key_exists('builtin', $term)) {
  820. //print "WARNING: unhandled tag: builtin\n";
  821. }
  822. return 1;
  823. }
  824. /**
  825. * Adds a cvterm relationship
  826. *
  827. * @param $cvterm
  828. * A database object for the cvterm
  829. * @param $rel
  830. * The relationship name
  831. * @param $objname
  832. * The relationship term name
  833. * @param $defaultcv
  834. * A database object containing a record from the cv table for the
  835. * default controlled vocabulary
  836. * @object_is_relationship
  837. * Set to 1 if this term is a relationship term
  838. * @default_db
  839. * The name of the default database.
  840. *
  841. * @ingroup tripal_obo_loader
  842. */
  843. function tripal_cv_obo_add_relationship($cvterm, $defaultcv, $rel,
  844. $objname, $object_is_relationship = 0, $default_db = 'OBO_REL') {
  845. // make sure the relationship cvterm exists
  846. $term = array(
  847. 'name' => $rel,
  848. 'id' => "$default_db:$rel",
  849. 'definition' => '',
  850. 'is_obsolete' => 0,
  851. 'cv_name' => $defaultcv,
  852. 'is_relationship' => TRUE,
  853. 'db_naame' => $default_db
  854. );
  855. $relcvterm = tripal_insert_cvterm($term, array('update_existing' => FALSE));
  856. if (!$relcvterm) {
  857. // if the relationship term couldn't be found in the default_db provided
  858. // then do on more check to find it in the relationship ontology
  859. $term = array(
  860. 'name' => $rel,
  861. 'id' => "OBO_REL:$rel",
  862. 'definition' => '',
  863. 'is_obsolete' => 0,
  864. 'cv_name' => $defaultcv,
  865. 'is_relationship' => TRUE,
  866. 'db_name' => 'OBO_REL'
  867. );
  868. $relcvterm = tripal_insert_cvterm($term, array('update_existing' => FALSE));
  869. if (!$relcvterm) {
  870. tripal_cv_obo_quiterror("Cannot find the relationship term in the current ontology or in the relationship ontology: $rel\n");
  871. }
  872. }
  873. // get the object term
  874. $oterm = tripal_cv_obo_get_term($objname);
  875. if (!$oterm) {
  876. tripal_cv_obo_quiterror("Could not find object term $objname\n");
  877. }
  878. $objterm = array();
  879. $objterm['id'] = $oterm['id'][0];
  880. $objterm['name'] = $oterm['name'][0];
  881. if (array_key_exists('def', $oterm)) {
  882. $objterm['definition'] = $oterm['def'][0];
  883. }
  884. if (array_key_exists('subset', $oterm)) {
  885. $objterm['subset'] = $oterm['subset'][0];
  886. }
  887. if (array_key_exists('namespace', $oterm)) {
  888. $objterm['namespace'] = $oterm['namespace'][0];
  889. }
  890. if (array_key_exists('is_obsolete', $oterm)) {
  891. $objterm['is_obsolete'] = $oterm['is_obsolete'][0];
  892. }
  893. $objterm['cv_name' ] = $defaultcv;
  894. $objterm['is_relationship'] = $object_is_relationship;
  895. $objterm['db_name'] = $default_db;
  896. $objcvterm = tripal_insert_cvterm($objterm, array('update_existing' => TRUE));
  897. if (!$objcvterm) {
  898. tripal_cv_obo_quiterror("Cannot add cvterm " . $oterm['name'][0]);
  899. }
  900. // check to see if the cvterm_relationship already exists, if not add it
  901. $values = array(
  902. 'type_id' => $relcvterm->cvterm_id,
  903. 'subject_id' => $cvterm->cvterm_id,
  904. 'object_id' => $objcvterm->cvterm_id
  905. );
  906. $result = chado_select_record('cvterm_relationship', array('*'), $values);
  907. if (count($result) == 0) {
  908. $options = array('return_record' => FALSE);
  909. $success = chado_insert_record('cvterm_relationship', $values, $options);
  910. if (!$success) {
  911. tripal_cv_obo_quiterror("Cannot add term relationship: '$cvterm->name' $rel '$objcvterm->name'");
  912. }
  913. }
  914. return TRUE;
  915. }
  916. /**
  917. * Retreives the term array from the temp loading table for a given term id.
  918. *
  919. * @param id
  920. * The id of the term to retrieve
  921. *
  922. * @ingroup tripal_obo_loader
  923. */
  924. function tripal_cv_obo_get_term($id) {
  925. $values = array('id' => $id);
  926. $result = chado_select_record('tripal_obo_temp', array('stanza'), $values);
  927. if (count($result) == 0) {
  928. return FALSE;
  929. }
  930. return unserialize(base64_decode($result[0]->stanza));
  931. }
  932. /**
  933. * Adds the synonyms to a term
  934. *
  935. * @param term
  936. * An array representing the cvterm. It must have a 'synonym' key/value pair.
  937. * @param cvterm
  938. * The database object of the cvterm to which the synonym will be added.
  939. *
  940. * @ingroup tripal_obo_loader
  941. */
  942. function tripal_cv_obo_add_synonyms($term, $cvterm) {
  943. // make sure we have a 'synonym_type' vocabulary
  944. $syncv = tripal_insert_cv(
  945. 'synonym_type',
  946. 'A local vocabulary added for synonym types.'
  947. );
  948. // now add the synonyms
  949. if (array_key_exists('synonym', $term)) {
  950. foreach ($term['synonym'] as $synonym) {
  951. // separate out the synonym definition and the synonym type
  952. $def = preg_replace('/^\s*"(.*)"\s*.*$/', '\1', $synonym);
  953. // the scope will be 'EXACT', etc...
  954. $scope = drupal_strtolower(preg_replace('/^.*"\s+(.*?)\s+.*$/', '\1', $synonym));
  955. if (!$scope) { // if no scope then default to 'exact'
  956. $scope = 'exact';
  957. }
  958. // make sure the synonym type exists in the 'synonym_type' vocabulary
  959. $values = array(
  960. 'name' => $scope,
  961. 'cv_id' => array(
  962. 'name' => 'synonym_type',
  963. ),
  964. );
  965. $syntype = tripal_get_cvterm($values);
  966. // if it doesn't exist then add it
  967. if (!$syntype) {
  968. // build a 'term' object so we can add the missing term
  969. $term = array(
  970. 'name' => $scope,
  971. 'id' => "synonym_type:$scope",
  972. 'definition' => '',
  973. 'is_obsolete' => 0,
  974. 'cv_name' => $syncv->name,
  975. 'is_relationship' => FALSE
  976. );
  977. $syntype = tripal_insert_cvterm($term, array('update_existing' => TRUE));
  978. if (!$syntype) {
  979. tripal_cv_obo_quiterror("Cannot add synonym type: internal:$scope");
  980. }
  981. }
  982. // make sure the synonym doesn't already exists
  983. $values = array(
  984. 'cvterm_id' => $cvterm->cvterm_id,
  985. 'synonym' => $def
  986. );
  987. $results = chado_select_record('cvtermsynonym', array('*'), $values);
  988. if (count($results) == 0) {
  989. $values = array(
  990. 'cvterm_id' => $cvterm->cvterm_id,
  991. 'synonym' => $def,
  992. 'type_id' => $syntype->cvterm_id
  993. );
  994. $options = array('return_record' => FALSE);
  995. $success = chado_insert_record('cvtermsynonym', $values, $options);
  996. if (!$success) {
  997. tripal_cv_obo_quiterror("Failed to insert the synonym for term: $name ($def)");
  998. }
  999. }
  1000. // now add the dbxrefs for the synonym if we have a comma in the middle
  1001. // of a description then this will cause problems when splitting os lets
  1002. // just change it so it won't mess up our splitting and then set it back
  1003. // later.
  1004. /**
  1005. $synonym = preg_replace('/(".*?),\s(.*?")/','$1,_$2',$synonym);
  1006. $dbxrefs = preg_split("/, /",preg_replace('/^.*\[(.*?)\]$/','\1',$synonym));
  1007. foreach ($dbxrefs as $dbxref) {
  1008. $dbxref = preg_replace('/,_/',", ",$dbxref);
  1009. if ($dbxref) {
  1010. tripal_cv_obo_add_cvterm_dbxref($syn,$dbxref);
  1011. }
  1012. }
  1013. */
  1014. }
  1015. }
  1016. return TRUE;
  1017. }
  1018. /**
  1019. * Parse the OBO file and populate the templ loading table
  1020. *
  1021. * @param $file
  1022. * The path on the file system where the ontology can be found
  1023. * @param $header
  1024. * An array passed by reference that will be populated with the header
  1025. * information from the OBO file
  1026. * @param $jobid
  1027. * The job_id of the job from the Tripal jobs management system.
  1028. *
  1029. * @ingroup tripal_obo_loader
  1030. */
  1031. function tripal_cv_obo_parse($obo_file, &$header, $jobid) {
  1032. $in_header = 1;
  1033. $stanza = array();
  1034. $default_db = '';
  1035. $line_num = 0;
  1036. $num_read = 0;
  1037. $intv_read = 0;
  1038. $filesize = filesize($obo_file);
  1039. $interval = intval($filesize * 0.01);
  1040. if ($interval < 1) {
  1041. $interval = 1;
  1042. }
  1043. // iterate through the lines in the OBO file and parse the stanzas
  1044. $fh = fopen($obo_file, 'r');
  1045. while ($line = fgets($fh)) {
  1046. $line_num++;
  1047. $size = drupal_strlen($line);
  1048. $num_read += $size;
  1049. $intv_read += $size;
  1050. $line = trim($line);
  1051. // update the job status every 1% features
  1052. if ($jobid and $intv_read >= $interval) {
  1053. $percent = sprintf("%.2f", ($num_read / $filesize) * 100);
  1054. print "Parsing Line $line_num (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes.\r";
  1055. tripal_set_job_progress($jobid, intval(($num_read / $filesize) * 33.33333333));
  1056. $intv_read = 0;
  1057. }
  1058. // remove newlines
  1059. $line = rtrim($line);
  1060. // remove any special characters that may be hiding
  1061. $line = preg_replace('/[^(\x20-\x7F)]*/', '', $line);
  1062. // skip empty lines
  1063. if (strcmp($line, '') == 0) {
  1064. continue;
  1065. }
  1066. //remove comments from end of lines
  1067. $line = preg_replace('/^(.*?)\!.*$/', '\1', $line); // TODO: if the explamation is escaped
  1068. // at the first stanza we're out of header
  1069. if (preg_match('/^\s*\[/', $line)) {
  1070. $in_header = 0;
  1071. // store the stanza we just finished reading
  1072. if (sizeof($stanza) > 0) {
  1073. // add the term to the temp table
  1074. $values = array(
  1075. 'id' => $stanza['id'][0],
  1076. 'stanza' => base64_encode(serialize($stanza)),
  1077. 'type' => $type,
  1078. );
  1079. $success = chado_insert_record('tripal_obo_temp', $values);
  1080. if (!$success) {
  1081. tripal_report_error('T_obo_loader', "ERROR: Cannot insert stanza into temporary table.", array(), 'error');
  1082. exit;
  1083. }
  1084. }
  1085. // get the stanza type: Term, Typedef or Instance
  1086. $type = preg_replace('/^\s*\[\s*(.+?)\s*\]\s*$/', '\1', $line);
  1087. // start fresh with a new array
  1088. $stanza = array();
  1089. continue;
  1090. }
  1091. // break apart the line into the tag and value but ignore any escaped colons
  1092. preg_replace("/\\:/", "|-|-|", $line); // temporarily replace escaped colons
  1093. $pair = explode(":", $line, 2);
  1094. $tag = $pair[0];
  1095. $value = ltrim(rtrim($pair[1]));// remove surrounding spaces
  1096. // if this is the ID then look for the default DB
  1097. $matches = array();
  1098. if ($tag == 'id' and preg_match('/^(.+?):.*$/', $value, $matches)) {
  1099. $default_db = $matches[1];
  1100. }
  1101. $tag = preg_replace("/\|-\|-\|/", "\:", $tag); // return the escaped colon
  1102. $value = preg_replace("/\|-\|-\|/", "\:", $value);
  1103. if ($in_header) {
  1104. if (!array_key_exists($tag, $header)) {
  1105. $header[$tag] = array();
  1106. }
  1107. $header[$tag][] = $value;
  1108. }
  1109. else {
  1110. if (!array_key_exists($tag, $stanza)) {
  1111. $stanza[$tag] = array();
  1112. }
  1113. $stanza[$tag][] = $value;
  1114. }
  1115. }
  1116. // now add the last term in the file
  1117. if (sizeof($stanza) > 0) {
  1118. $values = array(
  1119. 'id' => $stanza['id'][0],
  1120. 'stanza' => base64_encode(serialize($stanza)),
  1121. 'type' => $type,
  1122. );
  1123. chado_insert_record('tripal_obo_temp', $values);
  1124. if (!$success) {
  1125. tripal_report_error('T_obo_loader', "ERROR: Cannot insert stanza into temporary table.", array(), 'error');
  1126. exit;
  1127. }
  1128. $percent = sprintf("%.2f", ($num_read / $filesize) * 100);
  1129. print "Parsing Line $line_num (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes.\r";
  1130. tripal_set_job_progress($jobid, intval(($num_read / $filesize) * 33.33333333));
  1131. }
  1132. return $default_db;
  1133. }
  1134. /**
  1135. * Adds a database reference to a cvterm
  1136. *
  1137. * @param cvterm
  1138. * The database object of the cvterm to which the synonym will be added.
  1139. * @param xref
  1140. * The cross refernce. It should be of the form from the OBO specification
  1141. *
  1142. * @ingroup tripal_obo_loader
  1143. */
  1144. function tripal_cv_obo_add_cvterm_dbxref($cvterm, $xref) {
  1145. $dbname = preg_replace('/^(.+?):.*$/', '$1', $xref);
  1146. $accession = preg_replace('/^.+?:\s*(.*?)(\{.+$|\[.+$|\s.+$|\".+$|$)/', '$1', $xref);
  1147. $description = preg_replace('/^.+?\"(.+?)\".*?$/', '$1', $xref);
  1148. $dbxrefs = preg_replace('/^.+?\[(.+?)\].*?$/', '$1', $xref);
  1149. if (!$accession) {
  1150. tripal_cv_obo_quiterror();
  1151. tripal_report_error("T_obo_loader", TRIPAL_WARNING, "Cannot add a dbxref without an accession: '$xref'", NULL);
  1152. return FALSE;
  1153. }
  1154. // if the xref is a database link, handle that specially
  1155. if (strcmp($dbname, 'http') == 0) {
  1156. $accession = $xref;
  1157. $dbname = 'URL';
  1158. }
  1159. // add the database
  1160. $db = tripal_insert_db(array('name' => $dbname));
  1161. if (!$db) {
  1162. tripal_cv_obo_quiterror("Cannot find database '$dbname' in Chado.");
  1163. }
  1164. // now add the dbxref
  1165. $dbxref = tripal_cv_obo_add_dbxref($db->db_id, $accession, '', $description);
  1166. if (!$dbxref) {
  1167. tripal_cv_obo_quiterror("Cannot find or add the database reference (dbxref)");
  1168. }
  1169. // finally add the cvterm_dbxref but first check to make sure it exists
  1170. $values = array(
  1171. 'cvterm_id' => $cvterm->cvterm_id,
  1172. 'dbxref_id' => $dbxref->dbxref_id,
  1173. );
  1174. $result = chado_select_record('cvterm_dbxref', array('*'), $values);
  1175. if (count($result) == 0) {
  1176. $ins_options = array('return_record' => FALSE);
  1177. $result = chado_insert_record('cvterm_dbxref', $values, $ins_options);
  1178. if (!$result) {
  1179. tripal_cv_obo_quiterror("Cannot add cvterm_dbxref: $xref");
  1180. return FALSE;
  1181. }
  1182. }
  1183. return TRUE;
  1184. }
  1185. /**
  1186. * Adds a property to a cvterm
  1187. *
  1188. * @param cvterm
  1189. * A database object for the cvterm to which properties will be added
  1190. * @param $property
  1191. * The name of the property to add
  1192. * @param $value
  1193. * The value of the property
  1194. * @param rank
  1195. * The rank of the property
  1196. *
  1197. * @ingroup tripal_obo_loader
  1198. */
  1199. function tripal_cv_obo_add_cvterm_prop($cvterm, $property, $value, $rank) {
  1200. // make sure the 'cvterm_property_type' CV exists
  1201. $cv = tripal_insert_cv('cvterm_property_type', '');
  1202. if (!$cv) {
  1203. tripal_cv_obo_quiterror("Cannot add/find cvterm_property_type cvterm");
  1204. }
  1205. // get the property type cvterm. If it doesn't exist then we want to add it
  1206. $values = array(
  1207. 'name' => $property,
  1208. 'cv_id' => $cv->cv_id,
  1209. );
  1210. $results = chado_select_record('cvterm', array('*'), $values);
  1211. if (count($results) == 0) {
  1212. $term = array(
  1213. 'name' => $property,
  1214. 'id' => "internal:$property",
  1215. 'definition' => '',
  1216. 'is_obsolete' => 0,
  1217. 'cv_name' => $cv->name,
  1218. 'is_relationship' => FALSE,
  1219. );
  1220. $cvproptype = tripal_insert_cvterm($term, array('update_existing' => FALSE));
  1221. if (!$cvproptype) {
  1222. tripal_cv_obo_quiterror("Cannot add cvterm property: internal:$property");
  1223. return FALSE;
  1224. }
  1225. }
  1226. else {
  1227. $cvproptype = $results[0];
  1228. }
  1229. // remove any properties that currently exist for this term. We'll reset them
  1230. if ($rank == 0) {
  1231. $values = array('cvterm_id' => $cvterm->cvterm_id);
  1232. $success = chado_delete_record('cvtermprop', $values);
  1233. if (!$success) {
  1234. tripal_cv_obo_quiterror("Could not remove existing properties to update property $property for term\n");
  1235. return FALSE;
  1236. }
  1237. }
  1238. // now add the property
  1239. $values = array(
  1240. 'cvterm_id' => $cvterm->cvterm_id,
  1241. 'type_id' => $cvproptype->cvterm_id,
  1242. 'value' => $value,
  1243. 'rank' => $rank,
  1244. );
  1245. $options = array('return_record' => FALSE);
  1246. $result = chado_insert_record('cvtermprop', $values, $options);
  1247. if (!$result) {
  1248. tripal_cv_obo_quiterror("Could not add property $property for term\n");
  1249. return FALSE;
  1250. }
  1251. return TRUE;
  1252. }
  1253. /**
  1254. * Adds a database cross reference to a cvterm
  1255. *
  1256. * @param db_id
  1257. * The database ID of the cross reference
  1258. * @param accession
  1259. * The cross reference's accession
  1260. * @param $version
  1261. * The version of the dbxref
  1262. * @param $description
  1263. * The description of the cross reference
  1264. *
  1265. * @ingroup tripal_obo_loader
  1266. */
  1267. function tripal_cv_obo_add_dbxref($db_id, $accession, $version='', $description='') {
  1268. // check to see if the dbxref exists if not, add it
  1269. $values = array(
  1270. 'db_id' => $db_id,
  1271. 'accession' => $accession,
  1272. );
  1273. $result = chado_select_record('dbxref', array('dbxref_id'), $values);
  1274. if (count($result) == 0) {
  1275. $ins_values = array(
  1276. 'db_id' => $db_id,
  1277. 'accession' => $accession,
  1278. 'version' => $version,
  1279. 'description' => $description,
  1280. );
  1281. $ins_options = array('return_record' => FALSE);
  1282. $result = chado_insert_record('dbxref', $ins_values, $ins_options);
  1283. if (!$result) {
  1284. tripal_cv_obo_quiterror("Failed to insert the dbxref record $accession");
  1285. return FALSE;
  1286. }
  1287. $result = chado_select_record('dbxref', array('dbxref_id'), $values, $options);
  1288. }
  1289. return $result[0];
  1290. }
  1291. function tripal_cv_obo_form_ajax_callback($form, $form_state) {
  1292. return $form['obo_existing'];
  1293. }