OBOImporter.inc 53 KB

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