sio__annotation.inc 14 KB

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