sio__annotation.inc

File

tripal_chado/includes/TripalFields/sio__annotation/sio__annotation.inc
View source
  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 lable 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 access within
  15. // the instanceSettingsForm. When the instanceSettingsForm is submitted
  16. // then Drupal with automatically change these settings for the instnace.
  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 = array(
  22. // The short name for the vocabulary (e.g. shcema, 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', array(
  61. 'cv_id' => $cv_id,
  62. 'name' => $cvterm_name,
  63. ));
  64. if (!$cvterm) {
  65. $errors[$field_name][$langcode][$delta][] = array(
  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 = array(
  84. $field_term => array(
  85. 'operations' => array(),
  86. 'sortable' => FALSE,
  87. 'searchable' => FALSE,
  88. 'type' => 'xs:complexType',
  89. 'readonly' => FALSE,
  90. 'elements' => array(
  91. $vocabulary_term => array(
  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 => array(
  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 => array(
  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] = array(
  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] = array(
  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 = chado_get_semweb_term('cvterm', 'cv_id');
  227. $accession = chado_get_semweb_term('dbxref', 'accession');
  228. $definition = chado_get_semweb_term('cvterm', 'definition');
  229. if (array_key_exists('is_not', $schema['fields'])) {
  230. $negation = chado_get_semweb_term($field_table, 'is_not');
  231. }
  232. if (array_key_exists('rank', $schema['fields'])) {
  233. $rank_term = chado_get_semweb_term($field_table, 'rank');
  234. }
  235. // Set some defaults for the empty record.
  236. $chado_record = $entity->chado_record;
  237. $entity->{$field_name}['und'][0] = array(
  238. 'value' => '',
  239. 'chado-' . $field_table . '__' . $pkey => '',
  240. 'chado-' . $field_table . '__' . $fkey_lcolumn => $chado_record->$fkey_rcolumn,
  241. 'chado-' . $field_table . '__cvterm_id' => '',
  242. );
  243. if (array_key_exists('is_not', $schema['fields'])) {
  244. $entity->{$field_name}['und'][0]['chado-' . $field_table . '__is_not'] = '';
  245. }
  246. if (array_key_exists('rank', $schema['fields'])) {
  247. $entity->{$field_name}['und'][0]['chado-' . $field_table . '__rank'] = '';
  248. }
  249. if (array_key_exists('pub_id', $schema['fields'])) {
  250. $entity->{$field_name}['und'][0]['chado-' . $field_table . '__pub_id'] = '';
  251. }
  252. // Get the annotations associated with this base record for this fields type.
  253. $columns = array('*');
  254. $match = array(
  255. $fkey_lcolumn => $chado_record->$fkey_rcolumn,
  256. );
  257. $order_by = array($pkey => 'ASC');
  258. if (array_key_exists('rank', $schema['fields'])) {
  259. $order_by = array('rank' => 'ASC');
  260. }
  261. $options = array(
  262. 'return_array' => TRUE,
  263. 'order_by' => $order_by
  264. );
  265. $fcvterms = chado_select_record($field_table, $columns, $match, $options);
  266. for ($i = 0; $i < count($fcvterms); $i++) {
  267. $linker = $fcvterms[$i];
  268. $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $linker->cvterm_id));
  269. $entity->{$field_name}['und'][$i] = array(
  270. 'value' => array(
  271. $vocabulary => $cvterm->dbxref_id->db_id->name,
  272. $accession => $cvterm->dbxref_id->accession,
  273. $definition => isset($cvterm->definition) ? $cvterm->definition : ''
  274. ),
  275. 'chado-' . $field_table . '__' . $pkey => $linker->$pkey,
  276. 'chado-' . $field_table . '__' . $fkey_lcolumn => $linker->$fkey_lcolumn,
  277. 'chado-' . $field_table . '__' . 'cvterm_id' => $linker->cvterm_id,
  278. );
  279. if (array_key_exists('is_not', $schema['fields'])) {
  280. $entity->{$field_name}['und'][$i]['value'][$negation] = $linker->is_not;
  281. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__is_not'] = $linker->is_not;
  282. }
  283. if (array_key_exists('rank', $schema['fields'])) {
  284. $entity->{$field_name}['und'][$i]['value'][$rank_term] = $linker->rank;
  285. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__rank'] = $linker->rank;
  286. }
  287. if (array_key_exists('pub_id', $schema['fields'])) {
  288. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__pub_id'] = $linker->pub_id;
  289. }
  290. }
  291. }
  292. }
  293. /**
  294. * Theme function for the dbxref_id_widget.
  295. *
  296. * @param $variables
  297. */
  298. function theme_chado_linker__cvterm_widget($variables) {
  299. $element = $variables['element'];
  300. // These two fields were added to the widget to help identify the fields
  301. // for layout.
  302. $table_name = $element['#table_name'];
  303. $fkey = $element['#fkey_field'];
  304. $layout = "
  305. <div class=\"annotation-cvterm-widget\">
  306. <div class=\"annotation-cvterm-widget-item\">" .
  307. drupal_render($element['cv__cv_id']) . "
  308. </div>
  309. <div class=\"annotation-cvterm-widget-item\">" .
  310. drupal_render($element['cvterm__name']) . "
  311. </div>
  312. <div class=\"annotation-cvterm-widget-item\">" .
  313. drupal_render($element['pub']) . "
  314. </div>
  315. <div class=\"annotation-cvterm-widget-item\">" .
  316. drupal_render($element['chado-' . $table_name . '__is_not']) . "
  317. </div>
  318. </div>
  319. ";
  320. return $layout;
  321. }
  322. /**
  323. * An Ajax callback for the dbxref widget.
  324. */
  325. function chado_linker__cvterm_widget_form_ajax_callback($form, $form_state) {
  326. $field_name = $form_state['triggering_element']['#parents'][0];
  327. $delta = $form_state['triggering_element']['#parents'][2];
  328. return $form[$field_name]['und'][$delta];
  329. }