sio__annotation.inc 16 KB

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