sio__annotation.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. class sio__annotation extends ChadoField {
  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. // The default label for this field.
  11. public static $default_label = 'Annotations';
  12. // The default description for this field.
  13. public static $description = 'This record is annotated with controlled vocabulary terms.';
  14. // Provide a list of instance specific settings. These can be accessed within
  15. // the instanceSettingsForm. When the instanceSettingsForm is submitted
  16. // then Drupal will automatically change these settings for the instance.
  17. // It is recommended to put settings at the instance level whenever possible.
  18. // If you override this variable in a child class be sure to replicate the
  19. // term_name, term_vocab, term_accession and term_fixed keys as these are
  20. // required for all TripalFields.
  21. public static $default_instance_settings = [
  22. // The short name for the vocabulary (e.g. schema, SO, GO, PATO, etc.).
  23. 'term_vocabulary' => 'SIO',
  24. // The name of the term.
  25. 'term_name' => 'annotation',
  26. // The unique ID (i.e. accession) of the term.
  27. 'term_accession' => '001166',
  28. // Set to TRUE if the site admin is allowed to change the term
  29. // type. This will create form elements when editing the field instance
  30. // to allow the site admin to change the term settings above.
  31. 'term_fixed' => FALSE,
  32. ];
  33. // The default widget for this field.
  34. public static $default_widget = 'chado_linker__cvterm_widget';
  35. // The default formatter for this field.
  36. public static $default_formatter = 'chado_linker__cvterm_formatter';
  37. // A boolean specifying that users should not be allowed to create
  38. // fields and instances of this field type through the UI. Such
  39. // fields can only be created programmatically with field_create_field()
  40. // and field_create_instance().
  41. public static $no_ui = FALSE;
  42. /**
  43. * @see TripalField::validate()
  44. */
  45. public function validate($entity_type, $entity, $langcode, $items, &$errors) {
  46. // If we don't have an entity then we don't want to validate. The case
  47. // where this could happen is when a user is editing the field settings
  48. // and trying to set a default value. In that case there's no entity and
  49. // we don't want to validate. There will always be an entity for creation
  50. // and update operations of a content type.
  51. if (!$entity) {
  52. return;
  53. }
  54. $field_name = $this->field['field_name'];
  55. foreach ($items as $delta => $item) {
  56. // Get the term that matches.
  57. $cvterm_name = $item['cvterm_name'];
  58. $cv_id = $item['cv_id'];
  59. if ($cvterm_name and $cv_id) {
  60. $cvterm = chado_generate_var('cvterm', [
  61. 'cv_id' => $cv_id,
  62. 'name' => $cvterm_name,
  63. ]);
  64. if (!$cvterm) {
  65. $errors[$field_name][$langcode][$delta][] = [
  66. 'message' => t("Cannot find a term that matches the term name and vocabulary."),
  67. 'error' => 'cvterm_name',
  68. ];
  69. }
  70. }
  71. }
  72. }
  73. /**
  74. * @see TripalField::elementInfo()
  75. */
  76. public function elementInfo() {
  77. $field_table = $this->instance['settings']['chado_table'];
  78. $schema = chado_get_schema($field_table);
  79. $vocabulary_term = chado_get_semweb_term('cvterm', 'cv_id');
  80. $accession_term = chado_get_semweb_term('dbxref', 'accession');
  81. $definition_term = chado_get_semweb_term('cvterm', 'definition');
  82. $field_term = $this->getFieldTermID();
  83. $info = [
  84. $field_term => [
  85. 'operations' => [],
  86. 'sortable' => FALSE,
  87. 'searchable' => FALSE,
  88. 'type' => 'xs:complexType',
  89. 'readonly' => FALSE,
  90. 'elements' => [
  91. $vocabulary_term => [
  92. 'sortable' => TRUE,
  93. 'searchable' => TRUE,
  94. 'label' => 'Annotation Term Vocabulary',
  95. 'type' => 'xs:string',
  96. 'readonly' => FALSE,
  97. 'required' => TRUE,
  98. ],
  99. $accession_term => [
  100. 'sortable' => TRUE,
  101. 'searchable' => TRUE,
  102. 'label' => 'Annotation Term Accession',
  103. 'type' => 'xs:string',
  104. 'readonly' => FALSE,
  105. 'required' => TRUE,
  106. ],
  107. $definition_term => [
  108. 'sortable' => TRUE,
  109. 'searchable' => TRUE,
  110. 'label' => 'Annotation Term Description',
  111. 'type' => 'xs:string',
  112. 'readonly' => FALSE,
  113. 'required' => FALSE,
  114. ],
  115. ],
  116. ],
  117. ];
  118. if (array_key_exists('is_not', $schema['fields'])) {
  119. $negation_term = chado_get_semweb_term($field_table, 'is_not');
  120. $info[$field_term]['elements'][$negation_term] = [
  121. 'sortable' => FALSE,
  122. 'searchable' => FALSE,
  123. 'label' => 'Annotation Term Negates',
  124. 'type' => 'xs:boolean',
  125. 'readonly' => FALSE,
  126. 'required' => FALSE,
  127. ];
  128. }
  129. if (array_key_exists('rank', $schema['fields'])) {
  130. $rank_term = chado_get_semweb_term($field_table, 'rank');
  131. $info[$field_term]['elements'][$rank_term] = [
  132. 'sortable' => FALSE,
  133. 'searchable' => FALSE,
  134. 'label' => 'Annotation Term Rank',
  135. 'type' => 'xs:integer',
  136. 'readonly' => FALSE,
  137. 'required' => FALSE,
  138. ];
  139. }
  140. if (array_key_exists('pub_id', $schema['fields'])) {
  141. }
  142. return $info;
  143. }
  144. /**
  145. * @see ChadoField::query()
  146. */
  147. public function query($query, $condition) {
  148. $alias = $this->field['field_name'];
  149. $operator = $condition['operator'];
  150. $field_table = $this->instance['settings']['chado_table'];
  151. $base_table = $this->instance['settings']['base_table'];
  152. $schema = chado_get_schema($field_table);
  153. $pkey = $schema['primary key'][0];
  154. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  155. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  156. $field_term_id = $this->getFieldTermID();
  157. $vocabulary_term = $field_term_id . ',' . chado_get_semweb_term('cvterm', 'cv_id');
  158. $accession_term = $field_term_id . ',' . chado_get_semweb_term('dbxref', 'accession');
  159. $definition_term = $field_term_id . ',' . chado_get_semweb_term('cvterm', 'definition');
  160. // Join to the xxx_cvterm table for this field.
  161. $this->queryJoinOnce($query, $field_table, $alias, "base.$fkey_rcolumn = $alias.$fkey_lcolumn");
  162. if ($condition['column'] == $vocabulary_term) {
  163. $this->queryJoinOnce($query, 'cvterm', $alias . '_cvterm', $alias . ".cvterm_id = " . $alias . "_cvterm.cvterm_id");
  164. $this->queryJoinOnce($query, 'dbxref', $alias . '_dbxref', $alias . "_cvterm.dbxref_id = " . $alias . "_dbxref.dbxref_id");
  165. $this->queryJoinOnce($query, 'db', $alias . '_db', $alias . "_db.db_id = " . $alias . "_dbxref.db_id");
  166. $query->condition($alias . '_db.name', $condition['value'], $operator);
  167. }
  168. if ($condition['column'] == $accession_term) {
  169. $this->queryJoinOnce($query, 'cvterm', $alias . '_cvterm', $alias . ".cvterm_id = " . $alias . "_cvterm.cvterm_id");
  170. $this->queryJoinOnce($query, 'dbxref', $alias . '_dbxref', $alias . "_cvterm.dbxref_id = " . $alias . "_dbxref.dbxref_id");
  171. $query->condition($alias . '_dbxref.accession', $condition['value'], $operator);
  172. }
  173. if ($condition['column'] == $definition_term) {
  174. $this->queryJoinOnce($query, 'cvterm', $alias . '_cvterm', $alias . ".cvterm_id = " . $alias . "_cvterm.cvterm_id");
  175. $query->condition($alias . '_cvterm.definition', $condition['value'], $operator);
  176. }
  177. }
  178. /**
  179. * @see ChadoField::queryOrder()
  180. */
  181. public function queryOrder($query, $order) {
  182. $alias = $this->field['field_name'];
  183. $field_table = $this->instance['settings']['chado_table'];
  184. $base_table = $this->instance['settings']['base_table'];
  185. $schema = chado_get_schema($field_table);
  186. $pkey = $schema['primary key'][0];
  187. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  188. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  189. $field_term_id = $this->getFieldTermID();
  190. $vocabulary_term = $field_term_id . ',' . chado_get_semweb_term('cvterm', 'cv_id');
  191. $accession_term = $field_term_id . ',' . chado_get_semweb_term('dbxref', 'accession');
  192. $definition_term = $field_term_id . ',' . chado_get_semweb_term('cvterm', 'definition');
  193. // Join to the xxx_cvterm table for this field.
  194. $this->queryJoinOnce($query, $field_table, $alias, "base.$fkey_rcolumn = $alias.$fkey_lcolumn");
  195. if ($order['column'] == $vocabulary_term) {
  196. $this->queryJoinOnce($query, 'cvterm', $alias . '_cvterm', $alias . ".cvterm_id = " . $alias . "_cvterm.cvterm_id", "LEFT OUTER");
  197. $this->queryJoinOnce($query, 'dbxref', $alias . '_dbxref', $alias . "_cvterm.dbxref_id = " . $alias . "_dbxref.dbxref_id", "LEFT OUTER");
  198. $this->queryJoinOnce($query, 'db', $alias . '_db', $alias . "_db.db_id = " . $alias . "_dbxref.db_id", "LEFT OUTER");
  199. $query->orderBy($alias . "_db.name", $order['direction']);
  200. }
  201. if ($order['column'] == $accession_term) {
  202. $this->queryJoinOnce($query, 'cvterm', $alias . '_cvterm', $alias . ".cvterm_id = " . $alias . "_cvterm.cvterm_id", "LEFT OUTER");
  203. $this->queryJoinOnce($query, 'dbxref', $alias . '_dbxref', $alias . "_cvterm.dbxref_id = " . $alias . "_dbxref.dbxref_id", "LEFT OUTER");
  204. $query->orderBy($alias . "_dbxref.accession", $order['direction']);
  205. }
  206. if ($order['column'] == $definition_term) {
  207. $this->queryJoinOnce($query, 'cvterm', $alias . '_cvterm', $alias . ".cvterm_id = " . $alias . "_cvterm.cvterm_id");
  208. $query->orderBy($alias . "_cvterm.definition", $order['direction']);
  209. }
  210. }
  211. /**
  212. *
  213. * @see TripalField::load()
  214. */
  215. public function load($entity) {
  216. $field_name = $this->field['field_name'];
  217. $field_type = $this->field['type'];
  218. $field_table = $this->instance['settings']['chado_table'];
  219. $field_column = $this->instance['settings']['chado_column'];
  220. $base_table = $this->instance['settings']['base_table'];
  221. // Get the FK that links to the base record.
  222. $schema = chado_get_schema($field_table);
  223. $pkey = $schema['primary key'][0];
  224. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  225. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  226. $vocabulary = tripal_get_chado_semweb_term('cvterm', 'cv_id');
  227. $accession = tripal_get_chado_semweb_term('dbxref', 'accession');
  228. $definition = tripal_get_chado_semweb_term('cvterm', 'definition');
  229. $name = tripal_get_chado_semweb_term('cvterm', 'name');
  230. if (array_key_exists('is_not', $schema['fields'])) {
  231. $negation = chado_get_semweb_term($field_table, 'is_not');
  232. }
  233. if (array_key_exists('rank', $schema['fields'])) {
  234. $rank_term = chado_get_semweb_term($field_table, 'rank');
  235. }
  236. // Set some defaults for the empty record.
  237. $chado_record = $entity->chado_record;
  238. $entity->{$field_name}['und'][0] = [
  239. 'value' => '',
  240. 'chado-' . $field_table . '__' . $pkey => '',
  241. 'chado-' . $field_table . '__' . $fkey_lcolumn => $chado_record->$fkey_rcolumn,
  242. 'chado-' . $field_table . '__cvterm_id' => '',
  243. ];
  244. if (array_key_exists('is_not', $schema['fields'])) {
  245. $entity->{$field_name}['und'][0]['chado-' . $field_table . '__is_not'] = '';
  246. }
  247. if (array_key_exists('rank', $schema['fields'])) {
  248. $entity->{$field_name}['und'][0]['chado-' . $field_table . '__rank'] = '';
  249. }
  250. if (array_key_exists('pub_id', $schema['fields'])) {
  251. $entity->{$field_name}['und'][0]['chado-' . $field_table . '__pub_id'] = '';
  252. }
  253. // Get the annotations associated with this base record for this fields type.
  254. $columns = ['*'];
  255. $match = [
  256. $fkey_lcolumn => $chado_record->$fkey_rcolumn,
  257. ];
  258. $order_by = [$pkey => 'ASC'];
  259. if (array_key_exists('rank', $schema['fields'])) {
  260. $order_by = ['rank' => 'ASC'];
  261. }
  262. $options = [
  263. 'return_array' => TRUE,
  264. 'order_by' => $order_by,
  265. ];
  266. $fcvterms = chado_select_record($field_table, $columns, $match, $options);
  267. for ($i = 0; $i < count($fcvterms); $i++) {
  268. $linker = $fcvterms[$i];
  269. $cvterm = chado_generate_var('cvterm', ['cvterm_id' => $linker->cvterm_id]);
  270. $entity->{$field_name}['und'][$i] = [
  271. 'value' => [
  272. $vocabulary => $cvterm->dbxref_id->db_id->name,
  273. $accession => $cvterm->dbxref_id->accession,
  274. $name => $cvterm->name,
  275. $definition => isset($cvterm->definition) ? $cvterm->definition : '',
  276. ],
  277. 'chado-' . $field_table . '__' . $pkey => $linker->$pkey,
  278. 'chado-' . $field_table . '__' . $fkey_lcolumn => $linker->$fkey_lcolumn,
  279. 'chado-' . $field_table . '__' . 'cvterm_id' => $linker->cvterm_id,
  280. ];
  281. if (array_key_exists('is_not', $schema['fields'])) {
  282. $entity->{$field_name}['und'][$i]['value'][$negation] = $linker->is_not;
  283. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__is_not'] = $linker->is_not;
  284. }
  285. if (array_key_exists('rank', $schema['fields'])) {
  286. $entity->{$field_name}['und'][$i]['value'][$rank_term] = $linker->rank;
  287. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__rank'] = $linker->rank;
  288. }
  289. if (array_key_exists('pub_id', $schema['fields'])) {
  290. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__pub_id'] = $linker->pub_id;
  291. }
  292. }
  293. }
  294. }
  295. /**
  296. * Theme function for the dbxref_id_widget.
  297. *
  298. * @param $variables
  299. */
  300. function theme_chado_linker__cvterm_widget($variables) {
  301. $element = $variables['element'];
  302. // These two fields were added to the widget to help identify the fields
  303. // for layout.
  304. $table_name = $element['#table_name'];
  305. $fkey = $element['#fkey_field'];
  306. $layout = "
  307. <div class=\"annotation-cvterm-widget\">
  308. <div class=\"annotation-cvterm-widget-item\">" .
  309. drupal_render($element['cv__cv_id']) . "
  310. </div>
  311. <div class=\"annotation-cvterm-widget-item\">" .
  312. drupal_render($element['cvterm__name']) . "
  313. </div>
  314. <div class=\"annotation-cvterm-widget-item\">" .
  315. drupal_render($element['pub']) . "
  316. </div>
  317. <div class=\"annotation-cvterm-widget-item\">" .
  318. drupal_render($element['chado-' . $table_name . '__is_not']) . "
  319. </div>
  320. </div>
  321. ";
  322. return $layout;
  323. }
  324. /**
  325. * An Ajax callback for the dbxref widget.
  326. */
  327. function chado_linker__cvterm_widget_form_ajax_callback($form, $form_state) {
  328. $field_name = $form_state['triggering_element']['#parents'][0];
  329. $delta = $form_state['triggering_element']['#parents'][2];
  330. return $form[$field_name]['und'][$delta];
  331. }