data__sequence_coordinates.inc

File

tripal_chado/includes/TripalFields/data__sequence_coordinates/data__sequence_coordinates.inc
View source
  1. <?php
  2. class data__sequence_coordinates 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 = 'Sequence Coordinates';
  12. // The default description for this field.
  13. public static $description = 'Locations on reference sequences where the feature is located.';
  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' => 'data',
  24. // The name of the term.
  25. 'term_name' => 'sequence_coordinates',
  26. // The unique ID (i.e. accession) of the term.
  27. 'term_accession' => '2012',
  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 = 'data__sequence_coordinates_widget';
  35. // The default formatter for this field.
  36. public static $default_formatter = 'data__sequence_coordinates_formatter';
  37. // --------------------------------------------------------------------------
  38. // PROTECTED CLASS MEMBERS -- DO NOT OVERRIDE
  39. // --------------------------------------------------------------------------
  40. // An array containing details about the field. The format of this array
  41. // is the same as that returned by field_info_fields()
  42. protected $field;
  43. // An array containing details about an instance of the field. A field does
  44. // not have to have an instance. But if dealing with an instance (such as
  45. // when using the widgetForm, formatterSettingsForm, etc.) it should be set.
  46. protected $instance;
  47. /**
  48. * @see TripalField::elementInfo()
  49. */
  50. public function elementInfo() {
  51. $field_term = $this->getFieldTermID();
  52. $reference_term = 'data:3002';
  53. $fmin_term = chado_get_semweb_term('featureloc', 'fmin');
  54. $fmax_term = chado_get_semweb_term('featureloc', 'fmax');
  55. $strand_term = chado_get_semweb_term('featureloc', 'strand');
  56. $phase_term = chado_get_semweb_term('featureloc', 'phase');
  57. return array(
  58. $field_term => array(
  59. 'operations' => array(),
  60. 'sortable' => FALSE,
  61. 'searchable' => FALSE,
  62. 'label' => 'Location Coordinates',
  63. 'help' => 'The locations on other genomic sequences where this record has been aligned.',
  64. 'type' => 'xs:complexType',
  65. 'readonly' => TRUE,
  66. 'elements' => array(
  67. $reference_term => array(
  68. 'searchable' => TRUE,
  69. 'label' => 'Location Reference Name',
  70. 'help' => 'The genomic feature on which this feature is localized.',
  71. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  72. 'sortable' => TRUE,
  73. 'type' => 'xs:string',
  74. 'readonly' => TRUE,
  75. 'required' => FALSE,
  76. ),
  77. $fmin_term => array(
  78. 'searchable' => TRUE,
  79. 'label' => 'Location Start Position',
  80. 'help' => 'The start position',
  81. 'type' => 'numeric',
  82. 'operations' => array('eq', 'gt', 'lt', 'gte' ,'lte'),
  83. 'sortable' => TRUE,
  84. 'type' => 'xs:integer',
  85. 'readonly' => TRUE,
  86. 'required' => FALSE,
  87. ),
  88. $fmax_term => array(
  89. 'searchable' => TRUE,
  90. 'label' => 'Location End Position',
  91. 'help' => 'The end position',
  92. 'type' => 'numeric',
  93. 'operations' => array('eq', 'gt', 'lt', 'gte' ,'lte'),
  94. 'sortable' => TRUE,
  95. 'type' => 'xs:integer',
  96. 'readonly' => TRUE,
  97. 'required' => FALSE,
  98. ),
  99. $phase_term => array(
  100. 'searchable' => TRUE,
  101. 'type' => 'numeric',
  102. 'label' => 'Location Phase',
  103. 'help' => 'The phase of the feature (applicable only to coding sequences).',
  104. 'operations' => array('eq', 'gt', 'lt', 'gte' ,'lte'),
  105. 'sortable' => TRUE,
  106. 'type' => 'xs:integer',
  107. 'readonly' => TRUE,
  108. 'required' => FALSE,
  109. ),
  110. $strand_term => array(
  111. 'searchable' => TRUE,
  112. 'label' => 'Location Strand',
  113. 'help' => 'The orientation of this feature where it is localized',
  114. 'operations' => array('eq', 'gt', 'lt', 'gte' ,'lte'),
  115. 'sortable' => FALSE,
  116. 'type' => 'xs:string',
  117. 'readonly' => TRUE,
  118. 'required' => FALSE,
  119. ),
  120. ),
  121. ),
  122. );
  123. }
  124. /**
  125. * @see ChadoField::query()
  126. */
  127. public function query($query, $condition) {
  128. $alias = $this->field['field_name'];
  129. $operator = $condition['operator'];
  130. $field_term_id = $this->getFieldTermID();
  131. $reference_term = $field_term_id . ',' . 'data:3002';
  132. $fmin_term = $field_term_id . ',' . chado_get_semweb_term('featureloc', 'fmin');
  133. $fmax_term = $field_term_id . ',' . chado_get_semweb_term('featureloc', 'fmax');
  134. $strand_term = $field_term_id . ',' . chado_get_semweb_term('featureloc', 'strand');
  135. $phase_term = $field_term_id . ',' . chado_get_semweb_term('featureloc', 'phase');
  136. // Join to the organism table for this field.
  137. $this->queryJoinOnce($query, 'featureloc', $alias, "base.feature_id = $alias.feature_id");
  138. if ($condition['column'] == $reference_term) {
  139. $salias = $alias . '_src';
  140. $this->queryJoinOnce($query, 'feature', $salias, "$alias.srcfeature_id = $salias.feature_id");
  141. $query->condition("$salias.name", $condition['value'], $operator);
  142. }
  143. if ($condition['column'] == $strand_term) {
  144. $strand = '';
  145. if ($condition['value'] == '-') {
  146. $strand = -1;
  147. }
  148. if ($condition['value'] == '+') {
  149. $strand = 1;
  150. }
  151. $query->condition("$alias.strand", $strand, $operator);
  152. }
  153. if ($condition['column'] == $fmin_term) {
  154. $query->condition("$alias.fmin", $condition['value'] - 1, $operator);
  155. }
  156. if ($condition['column'] == $fmax_term) {
  157. $query->condition("$alias.fmax", $condition['value'], $operator);
  158. }
  159. if ($condition['column'] == $phase_term) {
  160. $query->condition("$alias.phase", $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_term_id = $this->getFieldTermID();
  169. $reference_term = $field_term_id . ',' . 'data:3002';
  170. $fmin_term = $field_term_id . ',' . chado_get_semweb_term('featureloc', 'fmin');
  171. $fmax_term = $field_term_id . ',' . chado_get_semweb_term('featureloc', 'fmax');
  172. $strand_term = $field_term_id . ',' . chado_get_semweb_term('featureloc', 'strand');
  173. $phase_term = $field_term_id . ',' . chado_get_semweb_term('featureloc', 'phase');
  174. $this->queryJoinOnce($query, 'featureloc', $alias, "base.feature_id = $alias.feature_id", "LEFT OUTER");
  175. if ($order['column'] == $reference_term) {
  176. $salias = $alias . '_src';
  177. $this->queryJoinOnce($query, 'feature', $salias, "$alias.srcfeature_id = $salias.feature_id", "LEFT OUTER");
  178. $query->orderBy("$salias.name", $order['direction']);
  179. }
  180. if ($order['column'] == $fmin_term) {
  181. $query->orderBy("$alias.fmin", $order['direction']);
  182. }
  183. if ($order['column'] == $fmax_term) {
  184. $query->orderBy("$alias.fmax", $order['direction']);
  185. }
  186. if ($order['column'] == $phase_term) {
  187. $query->orderBy("$alias.phase", $order['direction']);
  188. }
  189. }
  190. /**
  191. *
  192. * @see TripalField::load()
  193. */
  194. public function load($entity) {
  195. $field_name = $this->field['field_name'];
  196. $feature = $entity->chado_record;
  197. $num_seqs = 0;
  198. $description = 'schema:description';
  199. $reference_term = 'data:3002';
  200. $fmin_term = chado_get_semweb_term('featureloc', 'fmin');
  201. $fmax_term = chado_get_semweb_term('featureloc', 'fmax');
  202. $strand_term = chado_get_semweb_term('featureloc', 'strand');
  203. $phase_term = chado_get_semweb_term('featureloc', 'phase');
  204. $options = array(
  205. 'return_array' => TRUE,
  206. 'order_by' => array('rank' => 'ASC'),
  207. );
  208. $feature = chado_expand_var($feature, 'table', 'featureloc', $options);
  209. // Set some defauls for the empty record
  210. $entity->{$field_name}['und'][0] = array(
  211. 'value' => '',
  212. );
  213. // Get the featureloc records that this feature is aligned to.
  214. $aligned = $feature->featureloc->feature_id;
  215. if ($aligned) {
  216. foreach ($aligned as $index => $featureloc) {
  217. $srcfeature = $featureloc->srcfeature_id->name;
  218. $strand = '';
  219. if ($featureloc->strand == 1) {
  220. $strand = '+';
  221. }
  222. else {
  223. $strand = '-';
  224. }
  225. $fmin = $featureloc->fmin + 1;
  226. $fmax = $featureloc->fmax;
  227. $entity->{$field_name}['und'][0] = array(
  228. 'value' => array(
  229. $description => $srcfeature . ':' . $fmin . '-' . $fmax . $strand,
  230. $reference_term => $srcfeature,
  231. $fmin_term => $fmin,
  232. $fmax_term => $fmax,
  233. $strand_term => $strand,
  234. $phase_term => $featureloc->phase,
  235. ),
  236. );
  237. $sentity_id = chado_get_record_entity_by_table('feature_id', $featureloc->srcfeature_id->feature_id);
  238. if ($sentity_id) {
  239. $entity->{$field_name}['und'][0]['value']['entity'] = 'TripalEntity:' . $sentity_id;
  240. }
  241. }
  242. }
  243. }
  244. }