OBOImporter.inc 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529
  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. print_r($cv_id);
  374. print_r("\n");
  375. $this->logMessage("- Loading paths for @vocab", array('@vocab' => $namespace));
  376. tripal_update_cvtermpath($cvid);
  377. }
  378. }
  379. /**
  380. * A wrapper function for importing the user specified OBO file into Chado by
  381. * specifying the obo_id of the OBO. It requires that the file be in OBO v1.2
  382. * compatible format. This function is typically executed via the Tripal jobs
  383. * management after a user submits a job via the Load Onotloies form.
  384. *
  385. * @param $obo_id
  386. * An obo_id from the tripal_cv_obo file that specifies which OBO file to import
  387. * @ingroup tripal_obo_loader
  388. */
  389. private function loadOBO_v1_2_id($obo) {
  390. // Convert the module name to the real path if present
  391. if (preg_match("/\{(.*?)\}/", $obo->path, $matches)) {
  392. $module = $matches[1];
  393. $path = drupal_realpath(drupal_get_path('module', $module));
  394. $obo->path = preg_replace("/\{.*?\}/", $path, $obo->path);
  395. }
  396. // if the reference is for a remote URL then run the URL processing function
  397. if (preg_match("/^https:\/\//", $obo->path) or
  398. preg_match("/^http:\/\//", $obo->path) or
  399. preg_match("/^ftp:\/\//", $obo->path)) {
  400. $this->loadOBO_v1_2_url($obo->name, $obo->path, 0);
  401. }
  402. // if the reference is for a local file then run the file processing function
  403. else {
  404. // check to see if the file is located local to Drupal
  405. $dfile = $_SERVER['DOCUMENT_ROOT'] . base_path() . $obo->path;
  406. if (file_exists($dfile)) {
  407. $this->loadOBO_v1_2_file($obo->name, $dfile, 0);
  408. }
  409. // if not local to Drupal, the file must be someplace else, just use
  410. // the full path provided
  411. else {
  412. if (file_exists($obo->path)) {
  413. $this->loadOBO_v1_2_file($obo->name, $obo->path, 0);
  414. }
  415. else {
  416. print "ERROR: could not find OBO file: '$obo->path'\n";
  417. }
  418. }
  419. }
  420. }
  421. /**
  422. * A wrapper function for importing the user specified OBO file into Chado by
  423. * specifying the filename and path of the OBO. It requires that the file be in OBO v1.2
  424. * compatible format. This function is typically executed via the Tripal jobs
  425. * management after a user submits a job via the Load Onotloies form.
  426. *
  427. * @param $obo_name
  428. * The name of the OBO (typially the ontology or controlled vocabulary name)
  429. * @param $file
  430. * The path on the file system where the ontology can be found
  431. * @param $is_new
  432. * Set to TRUE if this is a new ontology that does not yet exist in the
  433. * tripal_cv_obo table. If TRUE the OBO will be added to the table.
  434. *
  435. * @ingroup tripal_obo_loader
  436. */
  437. private function loadOBO_v1_2_file($obo_name, $file, $is_new = TRUE) {
  438. if ($is_new) {
  439. tripal_insert_obo($obo_name, $file);
  440. }
  441. $success = $this->loadOBO_v1_2($file, $obo_name);
  442. }
  443. /**
  444. * A wrapper function for importing the user specified OBO file into Chado by
  445. * specifying the remote URL of the OBO. It requires that the file be in OBO v1.2
  446. * compatible format. This function is typically executed via the Tripal jobs
  447. * management after a user submits a job via the Load Onotloies form.
  448. *
  449. * @param $obo_name
  450. * The name of the OBO (typially the ontology or controlled vocabulary name)
  451. * @param $url
  452. * The remote URL of the OBO file.
  453. * @param $is_new
  454. * Set to TRUE if this is a new ontology that does not yet exist in the
  455. * tripal_cv_obo table. If TRUE the OBO will be added to the table.
  456. *
  457. * @ingroup tripal_obo_loader
  458. */
  459. private function loadOBO_v1_2_url($obo_name, $url, $is_new = TRUE) {
  460. // first download the OBO
  461. $temp = tempnam(sys_get_temp_dir(), 'obo_');
  462. print "Downloading URL $url, saving to $temp\n";
  463. $url_fh = fopen($url, "r");
  464. $obo_fh = fopen($temp, "w");
  465. if (!$url_fh) {
  466. throw new Exception("Unable to download the remote OBO file at $url. Could a firewall be blocking outgoing connections? " .
  467. " if you are unable to download the file you may manually downlod the OBO file and use the web interface to " .
  468. " specify the location of the file on your server.");
  469. }
  470. while (!feof($url_fh)) {
  471. fwrite($obo_fh, fread($url_fh, 255), 255);
  472. }
  473. fclose($url_fh);
  474. fclose($obo_fh);
  475. if ($is_new) {
  476. tripal_insert_obo($obo_name, $url);
  477. }
  478. // second, parse the OBO
  479. $this->loadOBO_v1_2($temp, $obo_name);
  480. // now remove the temp file
  481. unlink($temp);
  482. }
  483. /**
  484. * Imports a given OBO file into Chado. This function is usually called by
  485. * one of three wrapper functions: loadOBO_v1_2_id,
  486. * loadOBO_v1_2_file or tirpal_cv_load_obo_v1_2_url. But, it can
  487. * be called directly if the full path to an OBO file is available on the
  488. * file system.
  489. *
  490. * @param $flie
  491. * The full path to the OBO file on the file system
  492. *
  493. * @ingroup tripal_obo_loader
  494. */
  495. private function loadOBO_v1_2($file, $obo_name) {
  496. $header = array();
  497. $ret = array();
  498. // Empty the temp table.
  499. $sql = "DELETE FROM {tripal_obo_temp}";
  500. chado_query($sql);
  501. $this->logMessage("Step 1: Preloading File $file...");
  502. // parse the obo file
  503. $default_db = $this->parse($file, $header);
  504. // Add the CV for this ontology to the database. The v1.2 definition
  505. // specifies a 'default-namespace' to be used if a 'namespace' is not
  506. // present for each stanza. Some ontologies have adopted the v1.4 method
  507. // in their v1.2 files and not including it.
  508. if (array_key_exists('default-namespace', $header)) {
  509. $defaultcv = tripal_insert_cv($header['default-namespace'][0], '');
  510. if (!$defaultcv) {
  511. throw new Exception('Cannot add namespace ' . $header['default-namespace'][0]);
  512. }
  513. $this->newcvs[$header['default-namespace'][0]] = $defaultcv->cv_id;
  514. }
  515. // If the 'default-namespace' is missing.
  516. else {
  517. // Grab the first term accession from the file and get the short name for the cv
  518. $fh = fopen($file, 'r');
  519. while ($line = fgets($fh)) {
  520. // Grab the first item's id info to break apart.
  521. if (preg_match('/^\s*\[/', $line)) {
  522. $stanza = TRUE;
  523. continue;
  524. }
  525. if ($stanza === TRUE && (substr($line, 0, 3) === "id:")) {
  526. $parts = explode(':', $line);
  527. $short_name = strtolower($parts[1]);
  528. $short_name = preg_replace('/\s+/', '', $short_name);
  529. break;
  530. }
  531. }
  532. fclose($fh);
  533. // Check if the EBI ontology search has this ontology:
  534. try {
  535. $results = $this->oboEbiLookup($short_name, 'ontology');
  536. if (array_key_exists('default-namespace', $results['config']['annotations'])) {
  537. $results = $results['config']['annotations']['default-namespace'];
  538. if (is_array($results)) {
  539. $results = $results[0];
  540. }
  541. }
  542. elseif (array_key_exists('namespace', $results['config'])) {
  543. $results = $results['config']['namespace'];
  544. }
  545. else {
  546. $results = $short_name;
  547. }
  548. $defaultcv = tripal_insert_cv(strtoupper($results), '');
  549. $this->newcvs[$defaultcv->name] = $defaultcv->cv_id;
  550. }
  551. catch (Exception $e) {
  552. watchdog_exception('OBOImporter no such accession found in EBI Ontology Search', $e);
  553. }
  554. if (empty($defaultcv)) {
  555. throw new Exception("Could not find a namespace for this OBO file: $file");
  556. }
  557. $this->logMessage("This OBO is missing the 'default-namespace' header. It " .
  558. "is not possible to determine which vocabulary terms without a 'namespace' key " .
  559. "should go. Instead, those terms will be placed in the '!vocab' vocabulary.",
  560. array('!vocab' => $defaultcv->name), TRIPAL_WARNING);
  561. }
  562. // Add any typedefs to the vocabulary first.
  563. $this->logMessage("Step 2: Loading type defs...");
  564. $this->loadTypeDefs($defaultcv, $default_db);
  565. // Next add terms to the vocabulary.
  566. $this->logMessage("Step 3: Loading terms...");
  567. if (!$this->processTerms($defaultcv, $default_db)) {
  568. throw new Exception('Cannot add terms from this ontology');
  569. }
  570. }
  571. /**
  572. * OBO files are divided into a typedefs terms section and vocabulary terms section.
  573. * This function loads the typedef terms from the OBO.
  574. *
  575. * @param $defaultcv
  576. * A database object containing a record from the cv table for the
  577. * default controlled vocabulary
  578. * @param $default_db
  579. * The name of the default database.
  580. *
  581. * @ingroup tripal_obo_loader
  582. */
  583. private function loadTypeDefs($defaultcv, $default_db) {
  584. $sql = "SELECT * FROM {tripal_obo_temp} WHERE type = 'Typedef' ";
  585. $typedefs = chado_query($sql);
  586. $sql = "
  587. SELECT count(*) as num_terms
  588. FROM {tripal_obo_temp}
  589. WHERE type = 'Typedef'
  590. ";
  591. $result = chado_query($sql)->fetchObject();
  592. $count = $result->num_terms;
  593. $this->setTotalItems($count);
  594. $this->setItemsHandled(0);
  595. $i = 0;
  596. foreach ($typedefs as $typedef) {
  597. $this->setItemsHandled($i);
  598. $term = unserialize(base64_decode($typedef->stanza));
  599. $this->processTerm($term, $defaultcv->name, 1, $default_db);
  600. $i++;
  601. }
  602. $this->setItemsHandled($i);
  603. return 1;
  604. }
  605. /**
  606. * OBO files are divided into a typedefs section and a terms section.
  607. *
  608. * This function loads the typedef terms from the OBO.
  609. *
  610. * @param $defaultcv
  611. * A database object containing a record from the cv table for the
  612. * default controlled vocabulary
  613. * @param $default_db
  614. * The name of the default database.
  615. */
  616. private function processTerms($defaultcv, $default_db) {
  617. $i = 0;
  618. // Iterate through each term from the OBO file and add it.
  619. $sql = "
  620. SELECT * FROM {tripal_obo_temp}
  621. WHERE type = 'Term'
  622. ORDER BY id
  623. ";
  624. $terms = chado_query($sql);
  625. $sql = "
  626. SELECT count(*) as num_terms
  627. FROM {tripal_obo_temp}
  628. WHERE type = 'Term'
  629. ";
  630. $result = chado_query($sql)->fetchObject();
  631. $count = $result->num_terms;
  632. $this->setTotalItems($count);
  633. $this->setItemsHandled(0);
  634. // Iterate through the terms.
  635. foreach ($terms as $t) {
  636. $term = unserialize(base64_decode($t->stanza));
  637. $this->setItemsHandled($i);
  638. // Add/update this term.
  639. if (!$this->processTerm($term, $defaultcv->name, 0, $default_db)) {
  640. throw new Exception("Failed to process terms from the ontology");
  641. }
  642. $i++;
  643. }
  644. $this->setItemsHandled($i);
  645. return 1;
  646. }
  647. /**
  648. * Uses the provided term array to add/update information to Chado about the
  649. * term including the term, dbxref, synonyms, properties, and relationships.
  650. *
  651. * @param $term
  652. * An array representing the cvterm.
  653. * @param $defaultcv
  654. * The name of the default controlled vocabulary
  655. * @is_relationship
  656. * Set to 1 if this term is a relationship term
  657. * @default_db
  658. * The name of the default database.
  659. *
  660. * @ingroup tripal_obo_loader
  661. */
  662. private function processTerm($term, $defaultcv, $is_relationship = 0, $default_db) {
  663. // make sure we have a namespace for this term
  664. if (!array_key_exists('namespace', $term) and !($defaultcv or $defaultcv == '')) {
  665. throw new Exception("Cannot add the term: no namespace defined. " . $term['id'][0]);
  666. }
  667. // construct the term array for sending to the tripal_chado_add_cvterm function
  668. // for adding a new cvterm
  669. $t = array();
  670. $t['id'] = $term['id'][0];
  671. $t['name'] = $term['name'][0];
  672. if (array_key_exists('def', $term)) {
  673. $t['definition'] = $term['def'][0];
  674. }
  675. if (array_key_exists('subset', $term)) {
  676. $t['subset'] = $term['subset'][0];
  677. }
  678. if (array_key_exists('namespace', $term)) {
  679. $t['namespace'] = $term['namespace'][0];
  680. }
  681. if (array_key_exists('is_obsolete', $term)) {
  682. $t['is_obsolete'] = $term['is_obsolete'][0];
  683. }
  684. // Check that the default_cv is in the cv table.
  685. $sql = "
  686. SELECT CV.name
  687. FROM {cv} CV
  688. WHERE CV.name = '$defaultcv'
  689. ";
  690. $results = chado_query($sql)->fetchObject();
  691. if (!$results){
  692. //The controlled vocabulary is not in the cv term table and needs to be added.
  693. $ontology_info = $this->oboEbiLookup($defaultcv, 'ontology');
  694. if (!empty($ontology_info)){
  695. if (array_key_exists('default-namespace', $ontology_info['config']['annotations'])) {
  696. $results = $ontology_info['config']['annotations']['default-namespace'];
  697. }
  698. elseif (array_key_exists('namespace', $ontology_info['config'])) {
  699. $results = $ontology_info['config']['namespace'];
  700. }
  701. $cv_returned = tripal_insert_cv($results[0], '');
  702. // If name && definition are both empty then look up the term from the ontology you just loaded.
  703. if($cv_returned) {
  704. $defaultcv = $cv_returned;
  705. }
  706. }
  707. }
  708. $t['cv_name'] = $defaultcv;
  709. $t['is_relationship'] = $is_relationship;
  710. $t['db_name'] = $default_db;
  711. // add the cvterm
  712. $cvterm = tripal_insert_cvterm($t, array('update_existing' => TRUE));
  713. if (!$cvterm) {
  714. throw new Exception("Cannot add the term " . $term['id'][0]);
  715. }
  716. // Remove any relationships that this term already has (in case it was
  717. // updated) and we'll re-add them.
  718. $sql = "
  719. DELETE FROM {cvterm_relationship} CVTR
  720. WHERE CVTR.subject_id = :cvterm_id
  721. ";
  722. chado_query($sql, array(':cvterm_id' => $cvterm->cvterm_id));
  723. if (array_key_exists('namespace', $term)) {
  724. $this->newcvs[$term['namespace'][0]] = $cvterm->cv_id;
  725. }
  726. // now handle other properites
  727. if (array_key_exists('is_anonymous', $term)) {
  728. //print "WARNING: unhandled tag: is_anonymous\n";
  729. }
  730. if (array_key_exists('alt_id', $term)) {
  731. foreach ($term['alt_id'] as $alt_id) {
  732. if (!$this->addCvtermDbxref($cvterm, $alt_id)) {
  733. throw new Exception("Cannot add alternate id $alt_id");
  734. }
  735. }
  736. }
  737. if (array_key_exists('subset', $term)) {
  738. //print "WARNING: unhandled tag: subset\n";
  739. }
  740. // add synonyms for this cvterm
  741. if (array_key_exists('synonym', $term)) {
  742. if (!$this->addSynonym($term, $cvterm)) {
  743. throw new Exception("Cannot add synonyms");
  744. }
  745. }
  746. // reformat the deprecated 'exact_synonym, narrow_synonym, and broad_synonym'
  747. // types to be of the v1.2 standard
  748. if (array_key_exists('exact_synonym', $term) or array_key_exists('narrow_synonym', $term) or array_key_exists('broad_synonym', $term)) {
  749. if (array_key_exists('exact_synonym', $term)) {
  750. foreach ($term['exact_synonym'] as $synonym) {
  751. $new = preg_replace('/^\s*(\".+?\")(.*?)$/', '$1 EXACT $2', $synonym);
  752. $term['synonym'][] = $new;
  753. }
  754. }
  755. if (array_key_exists('narrow_synonym', $term)) {
  756. foreach ($term['narrow_synonym'] as $synonym) {
  757. $new = preg_replace('/^\s*(\".+?\")(.*?)$/', '$1 NARROW $2', $synonym);
  758. $term['synonym'][] = $new;
  759. }
  760. }
  761. if (array_key_exists('broad_synonym', $term)) {
  762. foreach ($term['broad_synonym'] as $synonym) {
  763. $new = preg_replace('/^\s*(\".+?\")(.*?)$/', '$1 BROAD $2', $synonym);
  764. $term['synonym'][] = $new;
  765. }
  766. }
  767. if (!$this->addSynonym($term, $cvterm)) {
  768. throw new Exception("Cannot add/update synonyms");
  769. }
  770. }
  771. // add the comment to the cvtermprop table
  772. if (array_key_exists('comment', $term)) {
  773. $comments = $term['comment'];
  774. $j = 0;
  775. foreach ($comments as $comment) {
  776. if (!$this->addCvtermProp($cvterm, 'comment', $comment, $j)) {
  777. throw new Exception("Cannot add/update cvterm property");
  778. }
  779. $j++;
  780. }
  781. }
  782. // add any other external dbxrefs
  783. if (array_key_exists('xref', $term)) {
  784. foreach ($term['xref'] as $xref) {
  785. if (!$this->addCvtermDbxref($cvterm, $xref)) {
  786. throw new Exception("Cannot add/update cvterm database reference (dbxref).");
  787. }
  788. }
  789. }
  790. if (array_key_exists('xref_analog', $term)) {
  791. foreach ($term['xref_analog'] as $xref) {
  792. if (!$this->addCvtermDbxref($cvterm, $xref)) {
  793. throw new Exception("Cannot add/update cvterm database reference (dbxref).");
  794. }
  795. }
  796. }
  797. if (array_key_exists('xref_unk', $term)) {
  798. foreach ($term['xref_unk'] as $xref) {
  799. if (!$this->addCvtermDbxref($cvterm, $xref)) {
  800. throw new Exception("Cannot add/update cvterm database reference (dbxref).");
  801. }
  802. }
  803. }
  804. // add is_a relationships for this cvterm
  805. if (array_key_exists('is_a', $term)) {
  806. foreach ($term['is_a'] as $is_a) {
  807. if (!$this->addRelationship($cvterm, $defaultcv, 'is_a', $is_a, $is_relationship, $default_db)) {
  808. throw new Exception("Cannot add relationship is_a: $is_a");
  809. }
  810. }
  811. }
  812. if (array_key_exists('intersection_of', $term)) {
  813. //print "WARNING: unhandled tag: intersection_of\n";
  814. }
  815. if (array_key_exists('union_of', $term)) {
  816. //print "WARNING: unhandled tag: union_on\n";
  817. }
  818. if (array_key_exists('disjoint_from', $term)) {
  819. //print "WARNING: unhandled tag: disjoint_from\n";
  820. }
  821. if (array_key_exists('relationship', $term)) {
  822. foreach ($term['relationship'] as $value) {
  823. $rel = preg_replace('/^(.+?)\s.+?$/', '\1', $value);
  824. $object = preg_replace('/^.+?\s(.+?)$/', '\1', $value);
  825. if (!$this->addRelationship($cvterm, $defaultcv, $rel, $object, $is_relationship, $default_db)) {
  826. throw new Exception("Cannot add relationship $rel: $object");
  827. }
  828. }
  829. }
  830. if (array_key_exists('replaced_by', $term)) {
  831. //print "WARNING: unhandled tag: replaced_by\n";
  832. }
  833. if (array_key_exists('consider', $term)) {
  834. //print "WARNING: unhandled tag: consider\n";
  835. }
  836. if (array_key_exists('use_term', $term)) {
  837. //print "WARNING: unhandled tag: user_term\n";
  838. }
  839. if (array_key_exists('builtin', $term)) {
  840. //print "WARNING: unhandled tag: builtin\n";
  841. }
  842. return 1;
  843. }
  844. /**
  845. * Adds a cvterm relationship
  846. *
  847. * @param $cvterm
  848. * A database object for the cvterm
  849. * @param $rel
  850. * The relationship name
  851. * @param $objname
  852. * The relationship term name
  853. * @param $defaultcv
  854. * A database object containing a record from the cv table for the
  855. * default controlled vocabulary
  856. * @object_is_relationship
  857. * Set to 1 if this term is a relationship term
  858. * @default_db
  859. * The name of the default database.
  860. *
  861. * @ingroup tripal_obo_loader
  862. */
  863. private function addRelationship($cvterm, $defaultcv, $rel,
  864. $objname, $object_is_relationship = 0, $default_db = 'OBO_REL') {
  865. // If an accession was passed we need to see if we can find the actual label.
  866. if (strpos($rel, ':')) {
  867. $pair = explode(":", $rel);
  868. $ontology_id = $pair[0];
  869. $accession_num = $pair[1];
  870. if (is_numeric($accession_num)) {
  871. $results = $this->oboEbiLookup($rel, 'query');
  872. if (!empty($results)) {
  873. if (array_key_exists('docs', $results)){
  874. if(!empty($results['docs'])) {
  875. $rel = $results['docs']['label'];
  876. }
  877. else {
  878. // The first search doesn't work, so let's try a broader one.
  879. $results = $this->oboEbiLookup($rel, 'query-non-local');
  880. if (!empty($results)) {
  881. if (array_key_exists('docs', $results)){
  882. if(!empty($results['docs'])) {
  883. $accession = $rel;
  884. $accession_underscore = str_replace(":", "_", $accession);
  885. foreach ($results['docs'] as $item) {
  886. if ($item['label'] != $accession && $item['label'] != $accession_underscore) {
  887. //Found the first place a label is other than the accession is used, so take
  888. // that info and then end the loop.
  889. $rel = $item['label'];
  890. break;
  891. }
  892. }
  893. }
  894. }
  895. }
  896. }
  897. }
  898. }
  899. }
  900. }
  901. // Make sure the relationship cvterm exists.
  902. $term = array(
  903. 'name' => $rel,
  904. 'id' => "$default_db:$rel",
  905. 'definition' => '',
  906. 'is_obsolete' => 0,
  907. 'cv_name' => $defaultcv,
  908. 'is_relationship' => TRUE,
  909. 'db_name' => $default_db
  910. );
  911. $relcvterm = tripal_insert_cvterm($term, array('update_existing' => FALSE));
  912. if (!$relcvterm) {
  913. // If the relationship term couldn't be found in the default_db provided
  914. // then do on more check to find it in the relationship ontology
  915. $term = array(
  916. 'name' => $rel,
  917. 'id' => "OBO_REL:$rel",
  918. 'definition' => '',
  919. 'is_obsolete' => 0,
  920. 'cv_name' => $defaultcv,
  921. 'is_relationship' => TRUE,
  922. 'db_name' => 'OBO_REL'
  923. );
  924. $relcvterm = tripal_insert_cvterm($term, array('update_existing' => FALSE));
  925. if (!$relcvterm) {
  926. throw new Exception("Cannot find the relationship term in the current ontology or in the relationship ontology: $rel\n");
  927. }
  928. }
  929. // Get the object term.
  930. $oterm = $this->getTerm($objname);
  931. if (!$oterm) {
  932. throw new Exception("Could not find object term $objname\n");
  933. }
  934. $objterm = array();
  935. $objterm['id'] = $oterm['id'][0];
  936. $objterm['name'] = $oterm['name'][0];
  937. if (array_key_exists('def', $oterm)) {
  938. $objterm['definition'] = $oterm['def'][0];
  939. }
  940. if (array_key_exists('subset', $oterm)) {
  941. $objterm['subset'] = $oterm['subset'][0];
  942. }
  943. if (array_key_exists('namespace', $oterm)) {
  944. $objterm['namespace'] = $oterm['namespace'][0];
  945. }
  946. if (array_key_exists('is_obsolete', $oterm)) {
  947. $objterm['is_obsolete'] = $oterm['is_obsolete'][0];
  948. }
  949. $objterm['cv_name' ] = $defaultcv;
  950. $objterm['is_relationship'] = $object_is_relationship;
  951. $objterm['db_name'] = $default_db;
  952. $objcvterm = tripal_insert_cvterm($objterm, array('update_existing' => TRUE));
  953. if (!$objcvterm) {
  954. throw new Exception("Cannot add cvterm " . $oterm['name'][0]);
  955. }
  956. // check to see if the cvterm_relationship already exists, if not add it
  957. $values = array(
  958. 'type_id' => $relcvterm->cvterm_id,
  959. 'subject_id' => $cvterm->cvterm_id,
  960. 'object_id' => $objcvterm->cvterm_id
  961. );
  962. $result = chado_select_record('cvterm_relationship', array('*'), $values);
  963. if (count($result) == 0) {
  964. $options = array('return_record' => FALSE);
  965. $success = chado_insert_record('cvterm_relationship', $values, $options);
  966. if (!$success) {
  967. throw new Exception("Cannot add term relationship: '$cvterm->name' $rel '$objcvterm->name'");
  968. }
  969. }
  970. return TRUE;
  971. }
  972. /**
  973. * Retrieves the term array from the temp loading table for a given term id.
  974. *
  975. * @param id
  976. * The id of the term to retrieve
  977. *
  978. * @ingroup tripal_obo_loader
  979. */
  980. private function getTerm($id) {
  981. $values = array('id' => $id);
  982. $result = chado_select_record('tripal_obo_temp', array('stanza'), $values);
  983. if (count($result) == 0) {
  984. return FALSE;
  985. }
  986. return unserialize(base64_decode($result[0]->stanza));
  987. }
  988. /**
  989. * Adds the synonyms to a term
  990. *
  991. * @param term
  992. * An array representing the cvterm. It must have a 'synonym' key/value pair.
  993. * @param cvterm
  994. * The database object of the cvterm to which the synonym will be added.
  995. *
  996. * @ingroup tripal_obo_loader
  997. */
  998. private function addSynonym($term, $cvterm) {
  999. // make sure we have a 'synonym_type' vocabulary
  1000. $syncv = tripal_insert_cv(
  1001. 'synonym_type',
  1002. 'A local vocabulary added for synonym types.'
  1003. );
  1004. // now add the synonyms
  1005. if (array_key_exists('synonym', $term)) {
  1006. foreach ($term['synonym'] as $synonym) {
  1007. // separate out the synonym definition and the synonym type
  1008. $def = preg_replace('/^\s*"(.*)"\s*.*$/', '\1', $synonym);
  1009. // the scope will be 'EXACT', etc...
  1010. $scope = drupal_strtolower(preg_replace('/^.*"\s+(.*?)\s+.*$/', '\1', $synonym));
  1011. if (!$scope) { // if no scope then default to 'exact'
  1012. $scope = 'exact';
  1013. }
  1014. // make sure the synonym type exists in the 'synonym_type' vocabulary
  1015. $values = array(
  1016. 'name' => $scope,
  1017. 'cv_id' => array(
  1018. 'name' => 'synonym_type',
  1019. ),
  1020. );
  1021. $syntype = tripal_get_cvterm($values);
  1022. // if it doesn't exist then add it
  1023. if (!$syntype) {
  1024. // build a 'term' object so we can add the missing term
  1025. $term = array(
  1026. 'name' => $scope,
  1027. 'id' => "synonym_type:$scope",
  1028. 'definition' => '',
  1029. 'is_obsolete' => 0,
  1030. 'cv_name' => $syncv->name,
  1031. 'is_relationship' => FALSE
  1032. );
  1033. $syntype = tripal_insert_cvterm($term, array('update_existing' => TRUE));
  1034. if (!$syntype) {
  1035. throw new Exception("Cannot add synonym type: internal:$scope");
  1036. }
  1037. }
  1038. // make sure the synonym doesn't already exists
  1039. $values = array(
  1040. 'cvterm_id' => $cvterm->cvterm_id,
  1041. 'synonym' => $def
  1042. );
  1043. $results = chado_select_record('cvtermsynonym', array('*'), $values);
  1044. if (count($results) == 0) {
  1045. $values = array(
  1046. 'cvterm_id' => $cvterm->cvterm_id,
  1047. 'synonym' => $def,
  1048. 'type_id' => $syntype->cvterm_id
  1049. );
  1050. $options = array('return_record' => FALSE);
  1051. $success = chado_insert_record('cvtermsynonym', $values, $options);
  1052. if (!$success) {
  1053. throw new Exception("Failed to insert the synonym for term: $cvterm->name ($def)");
  1054. }
  1055. }
  1056. // now add the dbxrefs for the synonym if we have a comma in the middle
  1057. // of a description then this will cause problems when splitting os lets
  1058. // just change it so it won't mess up our splitting and then set it back
  1059. // later.
  1060. /**
  1061. $synonym = preg_replace('/(".*?),\s(.*?")/','$1,_$2',$synonym);
  1062. $dbxrefs = preg_split("/, /",preg_replace('/^.*\[(.*?)\]$/','\1',$synonym));
  1063. foreach ($dbxrefs as $dbxref) {
  1064. $dbxref = preg_replace('/,_/',", ",$dbxref);
  1065. if ($dbxref) {
  1066. $this->addCvtermDbxref($syn,$dbxref);
  1067. }
  1068. }
  1069. */
  1070. }
  1071. }
  1072. return TRUE;
  1073. }
  1074. /**
  1075. * Parse the OBO file and populate the templ loading table
  1076. *
  1077. * @param $file
  1078. * The path on the file system where the ontology can be found
  1079. * @param $header
  1080. * An array passed by reference that will be populated with the header
  1081. * information from the OBO file
  1082. *
  1083. * @ingroup tripal_obo_loader
  1084. */
  1085. private function parse($obo_file, &$header) {
  1086. $in_header = 1;
  1087. $stanza = array();
  1088. $default_db = '';
  1089. $line_num = 0;
  1090. $num_read = 0;
  1091. $type = '';
  1092. $filesize = filesize($obo_file);
  1093. $this->setTotalItems($filesize);
  1094. $this->setItemsHandled(0);
  1095. // iterate through the lines in the OBO file and parse the stanzas
  1096. $fh = fopen($obo_file, 'r');
  1097. while ($line = fgets($fh)) {
  1098. $line_num++;
  1099. $size = drupal_strlen($line);
  1100. $num_read += $size;
  1101. $line = trim($line);
  1102. $this->setItemsHandled($num_read);
  1103. // remove newlines
  1104. $line = rtrim($line);
  1105. // remove any special characters that may be hiding
  1106. $line = preg_replace('/[^(\x20-\x7F)]*/', '', $line);
  1107. // skip empty lines
  1108. if (strcmp($line, '') == 0) {
  1109. continue;
  1110. }
  1111. //remove comments from end of lines
  1112. $line = preg_replace('/^(.*?)\!.*$/', '\1', $line); // TODO: if the explamation is escaped
  1113. // at the first stanza we're out of header
  1114. if (preg_match('/^\s*\[/', $line)) {
  1115. $in_header = 0;
  1116. // store the stanza we just finished reading
  1117. if (sizeof($stanza) > 0) {
  1118. // add the term to the temp table
  1119. $values = array(
  1120. 'id' => $stanza['id'][0],
  1121. 'stanza' => base64_encode(serialize($stanza)),
  1122. 'type' => $type,
  1123. );
  1124. $success = chado_insert_record('tripal_obo_temp', $values);
  1125. if (!$success) {
  1126. throw new Exception("Cannot insert stanza into temporary table.");
  1127. }
  1128. }
  1129. // get the stanza type: Term, Typedef or Instance
  1130. $type = preg_replace('/^\s*\[\s*(.+?)\s*\]\s*$/', '\1', $line);
  1131. // start fresh with a new array
  1132. $stanza = array();
  1133. continue;
  1134. }
  1135. // break apart the line into the tag and value but ignore any escaped colons
  1136. preg_replace("/\\:/", "|-|-|", $line); // temporarily replace escaped colons
  1137. $pair = explode(":", $line, 2);
  1138. $tag = $pair[0];
  1139. $value = ltrim(rtrim($pair[1]));// remove surrounding spaces
  1140. // if this is the ID then look for the default DB
  1141. $matches = array();
  1142. if ($tag == 'id' and preg_match('/^(.+?):.*$/', $value, $matches)) {
  1143. $default_db = $matches[1];
  1144. }
  1145. $tag = preg_replace("/\|-\|-\|/", "\:", $tag); // return the escaped colon
  1146. $value = preg_replace("/\|-\|-\|/", "\:", $value);
  1147. if ($in_header) {
  1148. if (!array_key_exists($tag, $header)) {
  1149. $header[$tag] = array();
  1150. }
  1151. $header[$tag][] = $value;
  1152. }
  1153. else {
  1154. if (!array_key_exists($tag, $stanza)) {
  1155. $stanza[$tag] = array();
  1156. }
  1157. $stanza[$tag][] = $value;
  1158. }
  1159. }
  1160. // now add the last term in the file
  1161. if (sizeof($stanza) > 0) {
  1162. $values = array(
  1163. 'id' => $stanza['id'][0],
  1164. 'stanza' => base64_encode(serialize($stanza)),
  1165. 'type' => $type,
  1166. );
  1167. chado_insert_record('tripal_obo_temp', $values);
  1168. if (!$success) {
  1169. throw new Exception("Cannot insert stanza into temporary table.");
  1170. }
  1171. $this->setItemsHandled($num_read);
  1172. }
  1173. return $default_db;
  1174. }
  1175. /**
  1176. * Adds a database reference to a cvterm
  1177. *
  1178. * @param cvterm
  1179. * The database object of the cvterm to which the synonym will be added.
  1180. * @param xref
  1181. * The cross refernce. It should be of the form from the OBO specification
  1182. *
  1183. * @ingroup tripal_obo_loader
  1184. */
  1185. private function addCvtermDbxref($cvterm, $xref) {
  1186. $dbname = preg_replace('/^(.+?):.*$/', '$1', $xref);
  1187. $accession = preg_replace('/^.+?:\s*(.*?)(\{.+$|\[.+$|\s.+$|\".+$|$)/', '$1', $xref);
  1188. $description = preg_replace('/^.+?\"(.+?)\".*?$/', '$1', $xref);
  1189. $dbxrefs = preg_replace('/^.+?\[(.+?)\].*?$/', '$1', $xref);
  1190. if (!$accession) {
  1191. throw new Exception("Cannot add a dbxref without an accession: '$xref'");
  1192. }
  1193. // if the xref is a database link, handle that specially
  1194. if (strcmp($dbname, 'http') == 0) {
  1195. $accession = $xref;
  1196. $dbname = 'URL';
  1197. }
  1198. // add the database
  1199. $db = tripal_insert_db(array('name' => $dbname));
  1200. if (!$db) {
  1201. throw new Exception("Cannot find database '$dbname' in Chado.");
  1202. }
  1203. // now add the dbxref
  1204. $dbxref = $this->addDbxref($db->db_id, $accession, '', $description);
  1205. if (!$dbxref) {
  1206. throw new Exception("Cannot find or add the database reference (dbxref)");
  1207. }
  1208. // finally add the cvterm_dbxref but first check to make sure it exists
  1209. $values = array(
  1210. 'cvterm_id' => $cvterm->cvterm_id,
  1211. 'dbxref_id' => $dbxref->dbxref_id,
  1212. );
  1213. $result = chado_select_record('cvterm_dbxref', array('*'), $values);
  1214. if (count($result) == 0) {
  1215. $ins_options = array('return_record' => FALSE);
  1216. $result = chado_insert_record('cvterm_dbxref', $values, $ins_options);
  1217. if (!$result) {
  1218. throw new Exception("Cannot add cvterm_dbxref: $xref");
  1219. }
  1220. }
  1221. return TRUE;
  1222. }
  1223. /**
  1224. * Adds a property to a cvterm
  1225. *
  1226. * @param cvterm
  1227. * A database object for the cvterm to which properties will be added
  1228. * @param $property
  1229. * The name of the property to add
  1230. * @param $value
  1231. * The value of the property
  1232. * @param rank
  1233. * The rank of the property
  1234. *
  1235. * @ingroup tripal_obo_loader
  1236. */
  1237. private function addCvtermProp($cvterm, $property, $value, $rank) {
  1238. // make sure the 'cvterm_property_type' CV exists
  1239. $cv = tripal_insert_cv('cvterm_property_type', '');
  1240. if (!$cv) {
  1241. throw new Exception("Cannot add/find cvterm_property_type cvterm");
  1242. }
  1243. // get the property type cvterm. If it doesn't exist then we want to add it
  1244. $values = array(
  1245. 'name' => $property,
  1246. 'cv_id' => $cv->cv_id,
  1247. );
  1248. $results = chado_select_record('cvterm', array('*'), $values);
  1249. if (count($results) == 0) {
  1250. $term = array(
  1251. 'name' => $property,
  1252. 'id' => "internal:$property",
  1253. 'definition' => '',
  1254. 'is_obsolete' => 0,
  1255. 'cv_name' => $cv->name,
  1256. 'is_relationship' => FALSE,
  1257. );
  1258. $cvproptype = tripal_insert_cvterm($term, array('update_existing' => FALSE));
  1259. if (!$cvproptype) {
  1260. throw new Exception("Cannot add cvterm property: internal:$property");
  1261. }
  1262. }
  1263. else {
  1264. $cvproptype = $results[0];
  1265. }
  1266. // remove any properties that currently exist for this term. We'll reset them
  1267. if ($rank == 0) {
  1268. $values = array('cvterm_id' => $cvterm->cvterm_id);
  1269. $success = chado_delete_record('cvtermprop', $values);
  1270. if (!$success) {
  1271. throw new Exception("Could not remove existing properties to update property $property for term\n");
  1272. }
  1273. }
  1274. // now add the property
  1275. $values = array(
  1276. 'cvterm_id' => $cvterm->cvterm_id,
  1277. 'type_id' => $cvproptype->cvterm_id,
  1278. 'value' => $value,
  1279. 'rank' => $rank,
  1280. );
  1281. $options = array('return_record' => FALSE);
  1282. $result = chado_insert_record('cvtermprop', $values, $options);
  1283. if (!$result) {
  1284. throw new Exception("Could not add property $property for term\n");
  1285. }
  1286. return TRUE;
  1287. }
  1288. /**
  1289. * Adds a database cross reference to a cvterm
  1290. *
  1291. * @param db_id
  1292. * The database ID of the cross reference
  1293. * @param accession
  1294. * The cross reference's accession
  1295. * @param $version
  1296. * The version of the dbxref
  1297. * @param $description
  1298. * The description of the cross reference
  1299. *
  1300. * @ingroup tripal_obo_loader
  1301. */
  1302. private function addDbxref($db_id, $accession, $version='', $description='') {
  1303. // check to see if the dbxref exists if not, add it
  1304. $values = array(
  1305. 'db_id' => $db_id,
  1306. 'accession' => $accession,
  1307. );
  1308. $result = chado_select_record('dbxref', array('dbxref_id'), $values);
  1309. if (count($result) == 0) {
  1310. $ins_values = array(
  1311. 'db_id' => $db_id,
  1312. 'accession' => $accession,
  1313. 'version' => $version,
  1314. 'description' => $description,
  1315. );
  1316. $ins_options = array('return_record' => FALSE);
  1317. $result = chado_insert_record('dbxref', $ins_values, $ins_options);
  1318. if (!$result) {
  1319. throw new Exception("Failed to insert the dbxref record $accession");
  1320. }
  1321. $result = chado_select_record('dbxref', array('dbxref_id'), $values);
  1322. }
  1323. return $result[0];
  1324. }
  1325. /**
  1326. * API call to Ontology Lookup Service provided by
  1327. * https://www.ebi.ac.uk/ols/docs/api#resources-terms
  1328. *
  1329. * @param ontology
  1330. * The OLS ontology id e.g. go
  1331. * @param accession
  1332. * Accession term for query
  1333. *
  1334. * @ingroup tripal_obo_loader
  1335. */
  1336. private function oboEbiLookup($accession, $type_of_search) {
  1337. //Grab just the ontology from the $accession.
  1338. $parts = explode(':', $accession);
  1339. $ontology = strtolower($parts[0]);
  1340. $ontology = preg_replace('/\s+/', '', $ontology);
  1341. if ($type_of_search == 'ontology') {
  1342. $options = array();
  1343. $full_url = 'http://www.ebi.ac.uk/ols/api/ontologies/' . $ontology;
  1344. $response = drupal_http_request($full_url, $options);
  1345. if(!empty($response)){
  1346. $response = drupal_json_decode($response->data);
  1347. }
  1348. }
  1349. elseif ($type_of_search == 'term') {
  1350. //The IRI of the terms, this value must be double URL encoded
  1351. $iri = urlencode(urlencode("http://purl.obolibrary.org/obo/" . str_replace(':' , '_', $accession)));
  1352. $options = array();
  1353. $full_url = 'http://www.ebi.ac.uk/ols/api/ontologies/' . $ontology . '/' . 'terms/' . $iri;
  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') {
  1360. $options = array();
  1361. $full_url = 'http://www.ebi.ac.uk/ols/api/search?q=' . $accession . '&queryFields=obo_id&local=true';
  1362. $response = drupal_http_request($full_url, $options);
  1363. if(!empty($response)){
  1364. $response = drupal_json_decode($response->data);
  1365. }
  1366. }
  1367. elseif($type_of_search == 'query-non-local') {
  1368. $options = array();
  1369. $full_url = 'http://www.ebi.ac.uk/ols/api/search?q=' . $accession . '&queryFields=obo_id';
  1370. $response = drupal_http_request($full_url, $options);
  1371. if(!empty($response)){
  1372. $response = drupal_json_decode($response->data);
  1373. }
  1374. }
  1375. return $response;
  1376. }
  1377. //drush php-eval "module_load_include('inc', 'tripal_chado', 'sites/all/modules/tripal/tripal_chado/includes/TripalImporter'); oboEbiLookup('BFO:0000050')"
  1378. }
  1379. /**
  1380. * Ajax callback for the OBOImporter::form() function.
  1381. */
  1382. function tripal_cv_obo_form_ajax_callback($form, $form_state) {
  1383. return $form['class_elements']['obo_existing'];
  1384. }