sio__annotation_formatter.inc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. class sio__annotation_formatter extends ChadoFieldFormatter {
  3. // The default lable for this field.
  4. public static $default_label = 'Chado Annotation';
  5. // The list of field types for which this formatter is appropriate.
  6. public static $field_types = array('chado_linker__cvterm');
  7. /**
  8. *
  9. * @see TripalFieldFormatter::view()
  10. */
  11. public function view(&$element, $entity_type, $entity, $langcode, $items, $display) {
  12. $headers = array('Term', 'Name', 'Definition');
  13. $rows = array();
  14. $field_table = $this->instance['settings']['chado_table'];
  15. $schema = chado_get_schema($field_table);
  16. $vocabulary_term = tripal_get_chado_semweb_term('cvterm', 'cv_id');
  17. $accession_term = tripal_get_chado_semweb_term('dbxref', 'accession');
  18. $definition_term = tripal_get_chado_semweb_term('cvterm', 'definition');
  19. $name_term = tripal_get_chado_semweb_term('cvterm', 'name');
  20. if (array_key_exists('is_not', $schema['fields'])) {
  21. $negation_term = chado_get_semweb_term($field_table, 'is_not');
  22. }
  23. $chado_table = $this->instance['settings']['chado_table'];
  24. foreach ($items as $delta => $item) {
  25. if (!empty($item['chado-' . $chado_table . '__cvterm_id'])) {
  26. $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $item['chado-' . $chado_table . '__cvterm_id']));
  27. $dbxref = $cvterm->dbxref_id;
  28. // Build the accession.
  29. $accession = $dbxref->db_id->name . ':' . $dbxref->accession;
  30. if ($dbxref->db_id->urlprefix) {
  31. $accession = l($accession, chado_get_dbxref_url($dbxref), array('attributes' => array('target' => '_blank')));
  32. }
  33. $row = array(
  34. $accession,
  35. $item['value'][$name_term],
  36. $item['value'][$definition_term],
  37. );
  38. if (array_key_exists('is_not', $schema['fields'])) {
  39. if ($negation_term == FALSE) {
  40. $row[1] = 'NOT ' . $row[1];
  41. }
  42. }
  43. $rows[] = $row;
  44. }
  45. }
  46. // Theme the results in a table.
  47. $caption = 'This record has the following annotations.';
  48. $table = array(
  49. 'header' => $headers,
  50. 'rows' => $rows,
  51. 'attributes' => array(
  52. 'id' => "$chado_table-table-terms",
  53. 'class' => 'tripal-data-table'
  54. ),
  55. 'caption' => $caption,
  56. 'sticky' => FALSE,
  57. 'colgroups' => array(),
  58. 'empty' => 'There are no annotations of this type',
  59. );
  60. if (count($items) > 0) {
  61. $element[0]['base'] = array(
  62. '#type' => 'markup',
  63. '#markup' => theme_table($table),
  64. );
  65. }
  66. $rows = $this->getAnnotationRows($child);
  67. $header = ['name', 'type', 'annotation'];
  68. $table = theme('table', ['rows' => $rows, 'header' => $header]);
  69. $element[0][$i]['annotation_table'] = [
  70. '#markup' => $table,
  71. '#title' => t("Annotations for !root", ['!root' => $name]),
  72. ];
  73. }
  74. /**
  75. * Get annotations for child features, from the data__sequence_features field.
  76. *
  77. * @param $data
  78. */
  79. private function getAnnotationRows($data) {
  80. $rows = [];
  81. $info = $data['info'];
  82. $children = $data['children'] ?? NULL;
  83. $annotations = $info->feature_cvterm;
  84. if ($annotations) {
  85. if (is_array($annotations)) {
  86. foreach ($annotations as $ann) {
  87. $annotation_name = $ann->cvterm_id->name;
  88. if ($ann->is_not) {
  89. $annotation_name = "is not " . $annotation_name;
  90. }
  91. $rows[] = [
  92. $info->uniquename,
  93. $info->type_id->name,
  94. $annotation_name,
  95. ];
  96. }
  97. }
  98. else {
  99. $annotation_name = $annotations->cvterm_id->name;
  100. if ($annotations->is_not) {
  101. $annotation_name = "is not " . $annotation_name;
  102. }
  103. $rows[] = [
  104. $info->uniquename,
  105. $info->type_id->name,
  106. $annotation_name,
  107. ];
  108. }
  109. }
  110. if ($children && !empty($children)) {
  111. foreach ($children as $child) {
  112. $rows = array_merge($this->getAnnotationRows($child), $rows);
  113. }
  114. }
  115. return $rows;
  116. }
  117. }