tripal_chado.semweb.inc 18 KB

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