sio__annotation.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. public function validate($entity_type, $entity, $langcode, $items, &$errors) {
  43. $field_name = $this->field['field_name'];
  44. foreach ($items as $delta => $item) {
  45. // Get the term that matches.
  46. $cvterm_name = $item['cvterm_name'];
  47. $cv_id = $item['cv_id'];
  48. if($cvterm_name and $cv_id) {
  49. $cvterm = chado_generate_var('cvterm', array(
  50. 'cv_id' => $cv_id,
  51. 'name' => $cvterm_name,
  52. ));
  53. if (!$cvterm) {
  54. $errors[$field_name][$langcode][$delta][] = array(
  55. 'message' => t("Cannot find a term that matches the term name and vocabulary."),
  56. 'error' => 'cvterm_name'
  57. );
  58. }
  59. }
  60. }
  61. }
  62. /**
  63. * @see TripalField::elementInfo()
  64. */
  65. public function elementInfo() {
  66. $field_table = $this->instance['settings']['chado_table'];
  67. $schema = chado_get_schema($field_table);
  68. $vocabulary_term = tripal_get_chado_semweb_term('cvterm', 'cv_id');
  69. $accession_term = tripal_get_chado_semweb_term('dbxref', 'accession');
  70. $definition_term = tripal_get_chado_semweb_term('cvterm', 'definition');
  71. $field_term = $this->getFieldTermID();
  72. $info = array(
  73. $field_term => array(
  74. 'operations' => array(),
  75. 'sortable' => FALSE,
  76. 'searchable' => FALSE,
  77. 'type' => 'array',
  78. 'elements' => array(
  79. $vocabulary_term => array(
  80. 'sortable' => TRUE,
  81. 'searchable' => TRUE,
  82. 'label' => 'Annotation Term Vocabulary',
  83. ),
  84. $accession_term => array(
  85. 'sortable' => TRUE,
  86. 'searchable' => TRUE,
  87. 'label' => 'Annotation Term Accession',
  88. ),
  89. $definition_term => array(
  90. 'sortable' => TRUE,
  91. 'searchable' => TRUE,
  92. 'label' => 'Annotation Term Description',
  93. ),
  94. ),
  95. ),
  96. );
  97. if (array_key_exists('is_not', $schema['fields'])) {
  98. $negation_term = tripal_get_chado_semweb_term($field_table, 'is_not');
  99. $info[$field_term]['elements'][$negation_term] = array(
  100. 'sortable' => FALSE,
  101. 'searchable' => FALSE,
  102. 'label' => 'Annotation Term Negates',
  103. );
  104. }
  105. if (array_key_exists('rank', $schema['fields'])) {
  106. $rank_term = tripal_get_chado_semweb_term($field_table, 'rank');
  107. $info[$field_term]['elements'][$rank_term] = array(
  108. 'sortable' => FALSE,
  109. 'searchable' => FALSE,
  110. 'label' => 'Annotation Term Rank',
  111. 'type' => 'numeric'
  112. );
  113. }
  114. if (array_key_exists('pub_id', $schema['fields'])) {
  115. }
  116. return $info;
  117. }
  118. /**
  119. * @see ChadoField::query()
  120. */
  121. public function query($query, $condition) {
  122. $alias = $this->field['field_name'];
  123. $operator = $condition['operator'];
  124. $field_table = $this->instance['settings']['chado_table'];
  125. $base_table = $this->instance['settings']['base_table'];
  126. $schema = chado_get_schema($field_table);
  127. $pkey = $schema['primary key'][0];
  128. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  129. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  130. $field_term_id = $this->getFieldTermID();
  131. $vocabulary_term = $field_term_id . ',' . tripal_get_chado_semweb_term('cvterm', 'cv_id');
  132. $accession_term = $field_term_id . ',' . tripal_get_chado_semweb_term('dbxref', 'accession');
  133. $definition_term = $field_term_id . ',' . tripal_get_chado_semweb_term('cvterm', 'definition');
  134. // Join to the xxx_cvterm table for this field.
  135. $this->queryJoinOnce($query, $field_table, $alias, "base.$fkey_rcolumn = $alias.$fkey_lcolumn");
  136. if ($condition['column'] == $vocabulary_term) {
  137. $this->queryJoinOnce($query, 'cvterm', $alias . '_cvterm', $alias . ".cvterm_id = " . $alias . "_cvterm.cvterm_id");
  138. $this->queryJoinOnce($query, 'dbxref', $alias . '_dbxref', $alias . "_cvterm.dbxref_id = " . $alias . "_dbxref.dbxref_id");
  139. $this->queryJoinOnce($query, 'db', $alias . '_db', $alias . "_db.db_id = " . $alias . "_dbxref.db_id");
  140. $query->condition($alias . '_db.name', $condition['value'], $operator);
  141. }
  142. if ($condition['column'] == $accession_term) {
  143. $this->queryJoinOnce($query, 'cvterm', $alias . '_cvterm', $alias . ".cvterm_id = " . $alias . "_cvterm.cvterm_id");
  144. $this->queryJoinOnce($query, 'dbxref', $alias . '_dbxref', $alias . "_cvterm.dbxref_id = " . $alias . "_dbxref.dbxref_id");
  145. $query->condition($alias . '_dbxref.accession', $condition['value'], $operator);
  146. }
  147. if ($condition['column'] == $definition_term) {
  148. $this->queryJoinOnce($query, 'cvterm', $alias . '_cvterm', $alias . ".cvterm_id = " . $alias . "_cvterm.cvterm_id");
  149. $query->condition($alias . '_cvterm.definition', $condition['value'], $operator);
  150. }
  151. }
  152. /**
  153. * @see ChadoField::queryOrder()
  154. */
  155. public function queryOrder($query, $order) {
  156. $alias = $this->field['field_name'];
  157. $field_table = $this->instance['settings']['chado_table'];
  158. $base_table = $this->instance['settings']['base_table'];
  159. $schema = chado_get_schema($field_table);
  160. $pkey = $schema['primary key'][0];
  161. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  162. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  163. $field_term_id = $this->getFieldTermID();
  164. $vocabulary_term = $field_term_id . ',' . tripal_get_chado_semweb_term('cvterm', 'cv_id');
  165. $accession_term = $field_term_id . ',' . tripal_get_chado_semweb_term('dbxref', 'accession');
  166. $definition_term = $field_term_id . ',' . tripal_get_chado_semweb_term('cvterm', 'definition');
  167. // Join to the xxx_cvterm table for this field.
  168. $this->queryJoinOnce($query, $field_table, $alias, "base.$fkey_rcolumn = $alias.$fkey_lcolumn");
  169. if ($order['column'] == $vocabulary_term) {
  170. $this->queryJoinOnce($query, 'cvterm', $alias . '_cvterm', $alias . ".cvterm_id = " . $alias . "_cvterm.cvterm_id", "LEFT OUTER");
  171. $this->queryJoinOnce($query, 'dbxref', $alias . '_dbxref', $alias . "_cvterm.dbxref_id = " . $alias . "_dbxref.dbxref_id", "LEFT OUTER");
  172. $this->queryJoinOnce($query, 'db', $alias . '_db', $alias . "_db.db_id = " . $alias . "_dbxref.db_id", "LEFT OUTER");
  173. $query->orderBy($alias . "_db.name", $order['direction']);
  174. }
  175. if ($order['column'] == $accession_term) {
  176. $this->queryJoinOnce($query, 'cvterm', $alias . '_cvterm', $alias . ".cvterm_id = " . $alias . "_cvterm.cvterm_id", "LEFT OUTER");
  177. $this->queryJoinOnce($query, 'dbxref', $alias . '_dbxref', $alias . "_cvterm.dbxref_id = " . $alias . "_dbxref.dbxref_id", "LEFT OUTER");
  178. $query->orderBy($alias . "_dbxref.accession", $order['direction']);
  179. }
  180. if ($order['column'] == $definition_term) {
  181. $this->queryJoinOnce($query, 'cvterm', $alias . '_cvterm', $alias . ".cvterm_id = " . $alias . "_cvterm.cvterm_id");
  182. $query->orderBy($alias . "_cvterm.definition", $order['direction']);
  183. }
  184. }
  185. /**
  186. *
  187. * @see TripalField::load()
  188. */
  189. public function load($entity) {
  190. $field_name = $this->field['field_name'];
  191. $field_type = $this->field['type'];
  192. $field_table = $this->instance['settings']['chado_table'];
  193. $field_column = $this->instance['settings']['chado_column'];
  194. $base_table = $this->instance['settings']['base_table'];
  195. // Get the FK that links to the base record.
  196. $schema = chado_get_schema($field_table);
  197. $pkey = $schema['primary key'][0];
  198. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  199. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  200. $vocabulary = tripal_get_chado_semweb_term('cvterm', 'cv_id');
  201. $accession = tripal_get_chado_semweb_term('dbxref', 'accession');
  202. $definition = tripal_get_chado_semweb_term('cvterm', 'definition');
  203. if (array_key_exists('is_not', $schema['fields'])) {
  204. $negation = tripal_get_chado_semweb_term($field_table, 'is_not');
  205. }
  206. if (array_key_exists('rank', $schema['fields'])) {
  207. $rank_term = tripal_get_chado_semweb_term($field_table, 'rank');
  208. }
  209. // Set some defaults for the empty record.
  210. $chado_record = $entity->chado_record;
  211. $entity->{$field_name}['und'][0] = array(
  212. 'value' => '',
  213. 'chado-' . $field_table . '__' . $pkey => '',
  214. 'chado-' . $field_table . '__' . $fkey_lcolumn => $chado_record->$fkey_rcolumn,
  215. 'chado-' . $field_table . '__cvterm_id' => '',
  216. );
  217. if (array_key_exists('is_not', $schema['fields'])) {
  218. $entity->{$field_name}['und'][0]['chado-' . $field_table . '__is_not'] = '';
  219. }
  220. if (array_key_exists('rank', $schema['fields'])) {
  221. $entity->{$field_name}['und'][0]['chado-' . $field_table . '__rank'] = '';
  222. }
  223. if (array_key_exists('pub_id', $schema['fields'])) {
  224. $entity->{$field_name}['und'][0]['chado-' . $field_table . '__pub_id'] = '';
  225. }
  226. // Get the annotations associated with this base record for this fields type.
  227. $columns = array('*');
  228. $match = array(
  229. $fkey_lcolumn => $chado_record->$fkey_rcolumn,
  230. );
  231. $order_by = array($pkey => 'ASC');
  232. if (array_key_exists('rank', $schema['fields'])) {
  233. $order_by = array('rank' => 'ASC');
  234. }
  235. $options = array(
  236. 'return_array' => TRUE,
  237. 'order_by' => $order_by
  238. );
  239. $fcvterms = chado_select_record($field_table, $columns, $match, $options);
  240. for ($i = 0; $i < count($fcvterms); $i++) {
  241. $linker = $fcvterms[$i];
  242. $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $linker->cvterm_id));
  243. $entity->{$field_name}['und'][$i] = array(
  244. 'value' => array(
  245. $vocabulary => $cvterm->dbxref_id->db_id->name,
  246. $accession => $cvterm->dbxref_id->accession,
  247. $definition => $cvterm->definition
  248. ),
  249. 'chado-' . $field_table . '__' . $pkey => $linker->$pkey,
  250. 'chado-' . $field_table . '__' . $fkey_lcolumn => $linker->$fkey_lcolumn,
  251. 'chado-' . $field_table . '__' . 'cvterm_id' => $linker->cvterm_id,
  252. );
  253. if (array_key_exists('is_not', $schema['fields'])) {
  254. $entity->{$field_name}['und'][$i]['value'][$negation] = $linker->is_not;
  255. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__is_not'] = $linker->is_not;
  256. }
  257. if (array_key_exists('rank', $schema['fields'])) {
  258. $entity->{$field_name}['und'][$i]['value'][$rank_term] = $linker->rank;
  259. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__rank'] = $linker->rank;
  260. }
  261. if (array_key_exists('pub_id', $schema['fields'])) {
  262. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__pub_id'] = $linker->pub_id;
  263. }
  264. }
  265. }
  266. }
  267. /**
  268. * Theme function for the dbxref_id_widget.
  269. *
  270. * @param $variables
  271. */
  272. function theme_chado_linker__cvterm_widget($variables) {
  273. $element = $variables['element'];
  274. // These two fields were added to the widget to help identify the fields
  275. // for layout.
  276. $table_name = $element['#table_name'];
  277. $fkey = $element['#fkey_field'];
  278. $layout = "
  279. <div class=\"annotation-cvterm-widget\">
  280. <div class=\"annotation-cvterm-widget-item\">" .
  281. drupal_render($element['cv__cv_id']) . "
  282. </div>
  283. <div class=\"annotation-cvterm-widget-item\">" .
  284. drupal_render($element['cvterm__name']) . "
  285. </div>
  286. <div class=\"annotation-cvterm-widget-item\">" .
  287. drupal_render($element['pub']) . "
  288. </div>
  289. <div class=\"annotation-cvterm-widget-item\">" .
  290. drupal_render($element['chado-' . $table_name . '__is_not']) . "
  291. </div>
  292. </div>
  293. ";
  294. return $layout;
  295. }
  296. /**
  297. * An Ajax callback for the dbxref widget.
  298. */
  299. function chado_linker__cvterm_widget_form_ajax_callback($form, $form_state) {
  300. $field_name = $form_state['triggering_element']['#parents'][0];
  301. $delta = $form_state['triggering_element']['#parents'][2];
  302. return $form[$field_name]['und'][$delta];
  303. }