tripal_chado.semweb.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. <?php
  2. /**
  3. * Adds defaults to the chado_semweb table.
  4. */
  5. function tripal_chado_populate_chado_semweb_table() {
  6. // Add in all tables and fields into the chado_semweb table.
  7. $chado_tables = chado_get_table_names(TRUE);
  8. foreach ($chado_tables as $chado_table) {
  9. tripal_add_chado_semweb_table($chado_table);
  10. }
  11. // Now set defaults!
  12. //
  13. // VOCABUARIES:
  14. // Add in vocabularies of terms that will be used for the semantic web
  15. //
  16. tripal_insert_db(array(
  17. 'name' => 'foaf',
  18. 'description' => 'Friend of a Friend. A dictionary of people-related terms that can be used in structured data).',
  19. 'url' => 'http://www.foaf-project.org/',
  20. 'urlprefix' => 'http://xmlns.com/foaf/spec/#',
  21. ));
  22. tripal_insert_cv('foaf','Friend of a Friend');
  23. tripal_insert_db(array(
  24. 'name' => 'schema',
  25. 'description' => 'Schema.org. Schema.org is sponsored by Google, Microsoft, Yahoo and Yandex. The vocabularies are developed by an open community process.',
  26. 'url' => 'https://schema.org/',
  27. 'urlprefix' => 'https://schema.org/',
  28. ));
  29. tripal_insert_cv('schema','Schema.org');
  30. tripal_insert_db(array(
  31. 'name' => 'TAXRANK',
  32. 'description' => 'Taxonomic rank vocabulary. A vocabulary of taxonomic ranks (species, family, phylum, etc).',
  33. 'url' => 'https://github.com/phenoscape/taxrank',
  34. 'urlprefix' => 'http://purl.obolibrary.org/obo/TAXRANK_',
  35. ));
  36. tripal_insert_cv('taxrank','Taxonomic rank vocabulary');
  37. tripal_insert_db(array(
  38. 'name' => 'NCBITaxon',
  39. 'description' => 'NCBI organismal classification. An ontology representation of the NCBI organismal taxonomy.',
  40. 'url' => 'http://www.berkeleybop.org/ontologies/ncbitaxon/',
  41. 'urlprefix' => 'http://purl.obolibrary.org/obo/ncbitaxon#',
  42. ));
  43. tripal_insert_cv('ncbitaxon','NCBI organismal classification');
  44. tripal_insert_db(array(
  45. 'name' => 'SWO',
  46. 'description' => 'Software Ontology. An ontology representation of the NCBI organismal taxonomy.',
  47. 'url' => 'http://theswo.sourceforge.net/',
  48. 'urlprefix' => '',
  49. ));
  50. tripal_insert_cv('swo','Software Ontology');
  51. tripal_insert_db(array(
  52. 'name' => 'IAO',
  53. 'description' => 'The Information Artifact Ontology (IAO) is a new ' .
  54. 'ontology of information entities, originally driven by work by the ' .
  55. 'OBI digital entity and realizable information entity branch.',
  56. 'url' => 'https://github.com/information-artifact-ontology/IAO/',
  57. 'urlprefix' => 'http://purl.obolibrary.org/obo/IAO_',
  58. ));
  59. tripal_insert_cv('IAO','Information Artifact Ontology');
  60. tripal_insert_db(array(
  61. 'name' => 'SBO',
  62. 'description' => 'Systems Biology. Terms commonly used in Systems Biology, and in particular in computational modeling.',
  63. 'url' => 'http://www.ebi.ac.uk/sbo/main/',
  64. 'urlprefix' => 'http://purl.obolibrary.org/obo/SBO_',
  65. ));
  66. tripal_insert_cv('sbo','Systems Biology');
  67. //
  68. // SET TERM DEFAULTS
  69. //
  70. $name = tripal_insert_cvterm(array(
  71. 'id' => 'schema:name',
  72. 'name' => 'name',
  73. 'cv_name' => 'schema',
  74. 'definition' => 'The name of the item.',
  75. ));
  76. tripal_associate_chado_semweb_term(NULL, 'uniquename', $name);
  77. tripal_associate_chado_semweb_term(NULL, 'name', $name);
  78. $alternate_name = tripal_insert_cvterm(array(
  79. 'id' => 'schema:alternateName',
  80. 'name' => 'alternateName',
  81. 'cv_name' => 'schema',
  82. 'definition' => 'The name of the item.',
  83. ));
  84. $description = tripal_insert_cvterm(array(
  85. 'id' => 'schema:description',
  86. 'name' => 'description',
  87. 'cv_name' => 'schema',
  88. 'definition' => 'A description of the item.',
  89. ));
  90. tripal_associate_chado_semweb_term(NULL, 'description', $description);
  91. $definition = tripal_insert_cvterm(array(
  92. 'id' => 'IAO:0000115',
  93. 'name' => 'definition',
  94. 'cv_name' => 'iao',
  95. 'definition' => 'The official OBI definition, explaining the meaning of ' .
  96. 'a class or property. Shall be Aristotelian, formalized and normalized. ' .
  97. 'Can be augmented with colloquial definitions.',
  98. ));
  99. tripal_associate_chado_semweb_term(NULL, 'definition', $definition);
  100. $comment = tripal_insert_cvterm(array(
  101. 'id' => 'schema:comment',
  102. 'name' => 'comment',
  103. 'cv_name' => 'schema',
  104. 'definition' => 'Comments, typically from users.',
  105. ));
  106. tripal_associate_chado_semweb_term(NULL, 'comment', $comment);
  107. $time_last_modified = tripal_insert_cvterm(array(
  108. 'id' => 'local:timelastmodified',
  109. 'name' => 'time_last_modified',
  110. 'cv_name' => 'local',
  111. 'definition' => 'The time at which a record for an item was first added.',
  112. ));
  113. tripal_associate_chado_semweb_term(NULL, 'timelastmodified', $time_last_modified);
  114. $time_accessioned = tripal_insert_cvterm(array(
  115. 'id' => 'local:timeaccessioned',
  116. 'name' => 'time_accessioned',
  117. 'cv_name' => 'local',
  118. 'definition' => 'The time at which a record for an item was last upated or modified.',
  119. ));
  120. tripal_associate_chado_semweb_term(NULL, 'timeaccessioned', $time_accessioned);
  121. $time_executed = tripal_insert_cvterm(array(
  122. 'id' => 'local:timeexecuted',
  123. 'name' => 'time_executed',
  124. 'cv_name' => 'local',
  125. 'definition' => 'The time at which a task was executed.',
  126. ));
  127. tripal_associate_chado_semweb_term(NULL, 'timeaccessioned', $time_executed);
  128. $dbxref = tripal_insert_cvterm(array(
  129. 'id' => 'SBO:0000554',
  130. 'name' => 'database cross reference',
  131. 'cv_name' => 'sbo',
  132. 'definition' => 'An annotation which directs one to information contained within a database.',
  133. ));
  134. $relationship = tripal_insert_cvterm(array(
  135. 'id' => 'SBO:0000374',
  136. 'name' => 'relationship',
  137. 'cv_name' => 'sbo',
  138. 'definition' => 'connectedness between entities and/or interactions representing their relatedness or influence. [ src_code:NR ]',
  139. ));
  140. //
  141. // ANALYSIS TABLE
  142. //
  143. $term = tripal_insert_cvterm(array(
  144. 'id' => 'SWO:0000001',
  145. 'name' => 'software',
  146. 'cv_name' => 'schema',
  147. 'definition' => 'Computer software, or generally just software, is any ' .
  148. 'set of machine-readable instructions (most often in the form of a ' .
  149. 'computer program) that conform to a given syntax (sometimes ' .
  150. 'referred to as a language) that is interpretable by a given ' .
  151. 'processor and that directs a computer\'s processor to perform ' .
  152. 'specific operations.',
  153. ));
  154. tripal_associate_chado_semweb_term('analysis', 'program', $term);
  155. $term = tripal_insert_cvterm(array(
  156. 'id' => 'IAO:0000129',
  157. 'name' => 'version number',
  158. 'cv_name' => 'IAO',
  159. 'definition' => 'A version number is an ' .
  160. 'information content entity which is a sequence of characters ' .
  161. 'borne by part of each of a class of manufactured products or its ' .
  162. 'packaging and indicates its order within a set of other products ' .
  163. 'having the same name.',
  164. ));
  165. tripal_associate_chado_semweb_term('analysis', 'programversion', $term);
  166. $term = tripal_insert_cvterm(array(
  167. 'id' => 'IAO:0000064',
  168. 'name' => 'algorithm',
  169. 'cv_name' => 'IAO',
  170. 'definition' => 'An algorithm is a set of instructions for performing a paticular calculation.',
  171. ));
  172. tripal_associate_chado_semweb_term('analysis', 'algorithm', $term);
  173. //
  174. // ORGANISM TABLE
  175. //
  176. $term = tripal_insert_cvterm(array(
  177. 'id' => 'TAXRANK:0000005',
  178. 'name' => 'genus',
  179. 'cv_name' => 'taxrank',
  180. ));
  181. tripal_associate_chado_semweb_term('organism', 'genus', $term);
  182. $term = tripal_insert_cvterm(array(
  183. 'id' => 'TAXRANK:0000006',
  184. 'name' => 'species',
  185. 'cv_name' => 'taxrank',
  186. ));
  187. tripal_associate_chado_semweb_term('organism', 'species', $term);
  188. $term = tripal_insert_cvterm(array(
  189. 'id' => 'TAXRANK:0000045',
  190. 'name' => 'infraspecies',
  191. 'cv_name' => 'taxrank',
  192. ));
  193. tripal_associate_chado_semweb_term('organism', 'infraspecific_name', $term);
  194. $term = tripal_insert_cvterm(array(
  195. 'id' => 'local:infraspecific_type',
  196. 'name' => 'infraspecific_type',
  197. 'definition' => 'The connector type for the infraspecific name',
  198. 'cv_name' => 'local',
  199. ));
  200. tripal_associate_chado_semweb_term('organism', 'type_id', $term);
  201. $term = tripal_insert_cvterm(array(
  202. 'id' => 'NCBITaxon:common_name',
  203. 'name' => 'common name',
  204. 'cv_name' => 'ncbitaxon',
  205. ));
  206. tripal_associate_chado_semweb_term('organism', 'common_name', $term);
  207. $term = tripal_insert_cvterm(array(
  208. 'id' => 'local:abbreviation',
  209. 'name' => 'abbreviation',
  210. 'cv_name' => 'local',
  211. ));
  212. tripal_associate_chado_semweb_term('organism', 'abbreviation', $term);
  213. //
  214. // FEATURE TABLE
  215. //
  216. tripal_associate_chado_semweb_term('feature', 'name', $alternate_name);
  217. //
  218. // PUB TABLE
  219. //
  220. tripal_associate_chado_semweb_term('pub', 'uniquename', $comment);
  221. //
  222. // STOCK TABLE
  223. //
  224. tripal_associate_chado_semweb_term('stock', 'name', $alternate_name);
  225. }
  226. /**
  227. * Adds defaults to the chado_semweb table.
  228. */
  229. function tripal_chado_semweb_form($form, &$form_state, $chado_table = NULL) {
  230. if (array_key_exists('values', $form_state)) {
  231. $chado_table = $form_state['values']['chado_table'];
  232. }
  233. $chado_tables = chado_get_table_names(TRUE);
  234. $chado_tables = array_merge(array('Select a Chado Table'), $chado_tables);
  235. $form['chado_table'] = array(
  236. '#type' => 'select',
  237. '#title' => 'Chado Table',
  238. '#description' => t('Select a chado table to set web services terms used for its columns.'),
  239. '#options' => $chado_tables,
  240. '#default_value' => $chado_table,
  241. '#ajax' => array(
  242. 'callback' => "tripal_chado_semweb_form_ajax_callback",
  243. 'wrapper' => "tripal-chado-semweb-form",
  244. 'effect' => 'fade',
  245. 'method' => 'replace'
  246. ),
  247. );
  248. // If the user has selected a chado table, then we need to
  249. // show the columns for setting terms.
  250. if ($chado_table) {
  251. $schema = chado_get_schema($chado_table);
  252. $pk = $schema['primary key'][0];
  253. $cv_default =
  254. db_select('tripal_cv_defaults', 'tc')
  255. ->fields('tc', array('field_name'))
  256. ->condition('table_name', $chado_table)
  257. ->execute()
  258. ->fetchField();
  259. $columns = $schema['fields'];
  260. $headers = array('Field Name', 'Term', 'Description', 'Action');
  261. $rows = array();
  262. foreach ($columns AS $column => $detail) {
  263. // Do not show column if it's the primary key or default cv
  264. if ($column != $pk && $column != $cv_default) {
  265. $cvterm_id =
  266. db_select('chado_semweb', 'cs')
  267. ->fields('cs', array('cvterm_id'))
  268. ->condition('chado_table', $chado_table)
  269. ->condition('chado_column', $column)
  270. ->execute()
  271. ->fetchField();
  272. $sw_term = '';
  273. $sw_desc = '';
  274. if($cvterm_id) {
  275. $term = tripal_get_cvterm(array(
  276. 'cvterm_id' => $cvterm_id
  277. ));
  278. $sw_term = $term->name;
  279. $sw_desc = $term->definition;
  280. }
  281. $rows[] = array(
  282. $column,
  283. $sw_term,
  284. $sw_desc,
  285. l('Edit', '/admin/tripal/storage/chado/semweb/edit/' . $chado_table . '/' . $column)
  286. );
  287. }
  288. }
  289. $output = theme('table', array(
  290. 'header' => $headers,
  291. 'rows' => $rows,
  292. ));
  293. $form['table'] = array(
  294. '#markup' => $output,
  295. '#title' => 'Table',
  296. '#description' => t('Please provide any filters for limiting
  297. the records. Only those that match the filters specified
  298. below will be published. To publish all records of this
  299. type, leave all filters blank.'),
  300. );
  301. }
  302. $form['#prefix'] = '<div id="tripal-chado-semweb-form">';
  303. $form['#suffix'] = '</div>';
  304. return $form;
  305. }
  306. /**
  307. * Adds defaults to the chado_semweb table.
  308. */
  309. function tripal_chado_semweb_edit_form($form, &$form_state, $table = NULL, $column = NULL) {
  310. $term_name = array_key_exists('values', $form_state) ? $form_state['values']['term_name'] : '';
  311. $form['chado_table'] = array(
  312. '#markup' => 'Term used for the <strong>' . t($column) . '</strong> column of the chado <strong>' . t($table) . '</strong> table:',
  313. );
  314. $form['table_name'] = array(
  315. '#type' => 'value',
  316. '#value' => $table
  317. );
  318. $form['column'] = array(
  319. '#type' => 'value',
  320. '#value' => $column
  321. );
  322. // If no term has been selected yet then provide the auto complete field.
  323. $form['term_name'] = array(
  324. '#title' => t('Term'),
  325. '#type' => 'textfield',
  326. '#description' => t("The content type must be the name of a term in
  327. a controlled vocabulary and the controlled vocabulary should
  328. already be loaded into Tripal. For example, to create a content
  329. type for storing 'genes', use the 'gene' term from the
  330. Sequence Ontology (SO)."),
  331. '#required' => TRUE,
  332. '#default_value' => $term_name,
  333. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/cvterm/",
  334. );
  335. $form['select_button'] = array(
  336. '#type' => 'button',
  337. '#value' => t('Lookup Term'),
  338. '#name' => 'select_cvterm',
  339. '#ajax' => array(
  340. 'callback' => "tripal_chado_semweb_form_ajax_callback",
  341. 'wrapper' => "tripal-chado-semweb-edit-form",
  342. 'effect' => 'fade',
  343. 'method' => 'replace'
  344. ),
  345. );
  346. if ($term_name) {
  347. $form['terms_list'] = array(
  348. '#type' => 'fieldset',
  349. '#title' => t('Matching Terms'),
  350. '#description' => t('Please select the term the best matches the
  351. content type you want to create. If the same term exists in
  352. multiple vocabularies you will see more than one option below.')
  353. );
  354. $match = array(
  355. 'name' => $term_name,
  356. );
  357. $terms = chado_generate_var('cvterm', $match, array('return_array' => TRUE));
  358. $terms = chado_expand_var($terms, 'field', 'cvterm.definition');
  359. $num_terms = 0;
  360. foreach ($terms as $term) {
  361. // Save the user a click by setting the default value as 1 if there's
  362. // only one matching term.
  363. $default = FALSE;
  364. $attrs = array();
  365. if ($num_terms == 0 and count($terms) == 1) {
  366. $default = TRUE;
  367. $attrs = array('checked' => 'checked');
  368. }
  369. $form['terms_list']['term-' . $term->cvterm_id] = array(
  370. '#type' => 'checkbox',
  371. '#title' => $term->name,
  372. '#default_value' => $default,
  373. '#attributes' => $attrs,
  374. '#description' => '<b>Vocabulary:</b> ' . $term->cv_id->name .
  375. '<br><b>Term: </b> ' . $term->dbxref_id->db_id->name . ':' . $term->dbxref_id->accession . '. ' .
  376. '<br><b>Definition:</b> ' . $term->definition,
  377. );
  378. $num_terms++;
  379. }
  380. if ($num_terms == 0) {
  381. $form['terms_list']['none'] = array(
  382. '#type' => 'item',
  383. '#markup' => '<i>' . t('There is no term that matches the entered text.') . '</i>'
  384. );
  385. }
  386. // Add in the button for the cases of no terms or too many.
  387. $form['submit_button'] = array(
  388. '#type' => 'submit',
  389. '#value' => t('Use this term'),
  390. '#name' => 'use_cvterm'
  391. );
  392. }
  393. $form['#prefix'] = '<div id = "tripal-chado-semweb-edit-form">';
  394. $form['#suffix'] = '</div>';
  395. return $form;
  396. }
  397. function tripal_chado_semweb_edit_form_validate($form, &$form_state) {
  398. if (array_key_exists('clicked_button', $form_state) && $form_state['clicked_button']['#name'] =='use_cvterm') {
  399. $cvterm_id = NULL;
  400. // Make sure we have a cvterm selected
  401. $num_selected = 0;
  402. foreach ($form_state['values'] as $key => $value) {
  403. $matches = array();
  404. if (preg_match("/^term-(\d+)$/", $key, $matches) and
  405. $form_state['values']['term-' . $matches[1]]) {
  406. $cvterm_id = $matches[1];
  407. $num_selected++;
  408. }
  409. }
  410. if ($num_selected == 0) {
  411. form_set_error('', 'Please select at least one term.');
  412. }
  413. else if ($num_selected > 1) {
  414. form_set_error('term-' . $cvterm_id, 'Please select only one term from the list below.');
  415. }
  416. else {
  417. $form_state['values']['#selected_cvterm_id'] = $cvterm_id;
  418. }
  419. }
  420. }
  421. function tripal_chado_semweb_edit_form_submit($form, &$form_state) {
  422. if (array_key_exists('clicked_button', $form_state) && $form_state['clicked_button']['#name'] =='use_cvterm') {
  423. $table_name = $form_state['values']['table_name'];
  424. $column = $form_state['values']['column'];
  425. $cvterm_id = $form_state['values']['#selected_cvterm_id'];
  426. // Check if there is already a record
  427. $record_id =
  428. db_select('chado_semweb', 'cs')
  429. ->fields('cs', array('chado_semweb_id'))
  430. ->condition('chado_table', $table_name)
  431. ->condition('chado_column', $column)
  432. ->execute()
  433. ->fetchField();
  434. // If the record exists, update it
  435. if ($record_id) {
  436. db_update('chado_semweb')
  437. ->fields(array(
  438. 'cvterm_id' => $cvterm_id
  439. ))
  440. ->condition('chado_semweb_id', $record_id)
  441. ->execute();
  442. }
  443. // Otherwise, insert a new record
  444. else {
  445. db_insert('chado_semweb')
  446. ->fields(array(
  447. 'chado_table' => $table_name,
  448. 'chado_column' => $column,
  449. 'cvterm_id' => $cvterm_id
  450. ))
  451. ->execute();
  452. }
  453. drupal_set_message('The term settings have been saved.');
  454. drupal_goto('/admin/tripal/storage/chado/semweb/' . $table_name);
  455. }
  456. }
  457. /**
  458. *
  459. */
  460. function tripal_chado_semweb_form_ajax_callback($form, $form_state) {
  461. return $form;
  462. }