ogi__location_on_map.inc

File

tripal_chado/includes/TripalFields/ogi__location_on_map/ogi__location_on_map.inc
View source
  1. <?php
  2. class ogi__location_on_map 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 = 'Location on Map';
  12. // The default description for this field.
  13. public static $description = 'Map position of a sequence.';
  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' => 'OGI',
  24. // The name of the term.
  25. 'term_name' => 'location on map',
  26. // The unique ID (i.e. accession) of the term.
  27. 'term_accession' => '0000021',
  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 = 'ogi__location_on_map_widget';
  35. // The default formatter for this field.
  36. public static $default_formatter = 'ogi__location_on_map_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. $map_term = 'data:1274';
  53. $name_term = chado_get_semweb_term('featuremap', 'name');
  54. $description_term = chado_get_semweb_term('featuremap', 'description');
  55. $mappos_term = chado_get_semweb_term('featurepos', 'mappos');
  56. $ref_feature_term = chado_get_semweb_term('featurepos', 'map_feature_id');
  57. $ref_feature_name = chado_get_semweb_term('feature', 'name');
  58. $ref_feature_id = chado_get_semweb_term('feature', 'uniquename');
  59. $ref_feature_type = 'rdfs:type';
  60. return array(
  61. $field_term => array(
  62. 'sortable' => FALSE,
  63. 'searchable' => FALSE,
  64. 'type' => 'xs:complexType',
  65. 'readonly' => TRUE,
  66. 'elements' => array(
  67. $map_term => array(
  68. 'searchable' => FALSE,
  69. 'sortable' => FALSE,
  70. 'type' => 'xs:complexType',
  71. 'readonly' => TRUE,
  72. 'required' => FALSE,
  73. 'elements' => array(
  74. $name_term => array(
  75. 'label' => 'Map Name',
  76. 'help' => 'The name of the map.',
  77. 'searchable' => TRUE,
  78. 'sortable' => TRUE,
  79. 'type' => 'xs:string',
  80. 'readonly' => TRUE,
  81. 'required' => FALSE,
  82. ),
  83. $description_term => array(
  84. 'label' => 'Map Description',
  85. 'help' => 'A description of the map.',
  86. 'searchable' => TRUE,
  87. 'sortable' => FALSE,
  88. 'type' => 'xs:string',
  89. 'readonly' => TRUE,
  90. 'required' => FALSE,
  91. ),
  92. 'entity' => array(
  93. 'searchable' => FALSE,
  94. 'sortable' => FALSE,
  95. ),
  96. ),
  97. ),
  98. $ref_feature_term => array(
  99. 'searchable' => FALSE,
  100. 'sortable' => FALSE,
  101. 'type' => 'xs:complexType',
  102. 'readonly' => TRUE,
  103. 'required' => FALSE,
  104. 'elements' => array(
  105. $ref_feature_name => array(
  106. 'label' => 'Map Reference Feature Name',
  107. 'help' => 'The genomic or genetic feature Nameof the map on which this feature is mapped.',
  108. 'searchable' => TRUE,
  109. 'sortable' => TRUE,
  110. 'type' => 'xs:string',
  111. 'readonly' => TRUE,
  112. 'required' => FALSE,
  113. ),
  114. $ref_feature_id=> array(
  115. 'label' => 'Map Reference Feature Identifier',
  116. 'help' => 'The genomic or genetic feature of the map on which this feature is mapped.',
  117. 'searchable' => TRUE,
  118. 'sortable' => TRUE,
  119. 'type' => 'xs:string',
  120. 'readonly' => TRUE,
  121. 'required' => FALSE,
  122. ),
  123. $ref_feature_type=> array(
  124. 'label' => 'Map Reference Feature Type',
  125. 'help' => 'The type of genomic or genetic feature of the map on which this feature is mapped.',
  126. 'searchable' => TRUE,
  127. 'sortable' => TRUE,
  128. 'type' => 'xs:string',
  129. 'readonly' => TRUE,
  130. 'required' => FALSE,
  131. ),
  132. ),
  133. ),
  134. $mappos_term => array(
  135. 'label' => 'Map Reference Position',
  136. 'help' => 'Maps may use different coordinate systems. This indicates the type of coordinate.',
  137. 'searchable' => TRUE,
  138. 'sortable' => TRUE,
  139. 'type' => 'xs:string',
  140. 'readonly' => TRUE,
  141. 'required' => FALSE,
  142. ),
  143. ),
  144. ),
  145. );
  146. }
  147. /**
  148. * @see ChadoField::query()
  149. */
  150. public function query($query, $condition) {
  151. $alias = $this->field['field_name'];
  152. $operator = $condition['operator'];
  153. // Map details.
  154. $field_term_id = $this->getFieldTermID();
  155. $map_term = 'data:1274';
  156. $name_term = $field_term_id . ',' . $map_term . ',' . chado_get_semweb_term('featuremap', 'name');
  157. $description_term = $field_term_id . ',' . $map_term . ',' . chado_get_semweb_term('featuremap', 'description');
  158. $mappos_term = $field_term_id . ',' . chado_get_semweb_term('featurepos', 'mappos');
  159. // Reference sequence details.
  160. $ref_feature_term = chado_get_semweb_term('featurepos', 'map_feature_id');
  161. $ref_feature_name = $field_term_id . ',' . $ref_feature_term . ',' .chado_get_semweb_term('feature', 'name');
  162. $ref_feature_id = $field_term_id . ',' . $ref_feature_term . ',' .chado_get_semweb_term('feature', 'uniquename');
  163. $ref_feature_type = $field_term_id . ',' . $ref_feature_term . ',' .'rdfs:type';
  164. $this->queryJoinOnce($query, 'featurepos', $alias, "base.feature_id = $alias.feature_id");
  165. if ($condition['column'] == $name_term) {
  166. $this->queryJoinOnce($query, 'featuremap', $alias . '_map', $alias . '_map.featuremap_id = ' . $alias . '.featuremap_id');
  167. $query->condition($alias . '_map.name', $condition['value'], $operator);
  168. }
  169. if ($condition['column'] == $description_term) {
  170. $this->queryJoinOnce($query, 'featuremap', $alias . '_map', $alias . '_map.featuremap_id = ' . $alias . '.featuremap_id');
  171. $query->condition($alias . '_map.description', $condition['value'], $operator);
  172. }
  173. if ($condition['column'] == $mappos_term) {
  174. $query->condition($alias . '.mappos', $condition['value'], $operator);
  175. }
  176. if ($condition['column'] == $ref_feature_name) {
  177. $this->queryJoinOnce($query, 'feature', $alias . '_mapref', $alias . '_mapref.feature_id = ' . $alias . '.map_feature_id');
  178. $query->condition($alias . '_mapref.name', $condition['value'], $operator);
  179. }
  180. if ($condition['column'] == $ref_feature_id) {
  181. $this->queryJoinOnce($query, 'feature', $alias . '_mapref', $alias . '_mapref.feature_id = ' . $alias . '.map_feature_id');
  182. $query->condition($alias . '_mapref.uniquename', $condition['value'], $operator);
  183. }
  184. if ($condition['column'] == $ref_feature_type) {
  185. $this->queryJoinOnce($query, 'feature', $alias . '_mapref', $alias . '_mapref.feature_id = ' . $alias . '.map_feature_id');
  186. $this->queryJoinOnce($query, 'cvterm', $alias . '_mapref_cvterm', $alias . '_mapref_cvterm.cvterm_id = ' . $alias . '_mapref.type_id');
  187. $query->condition($alias . '_mapref_cvterm.name', $condition['value'], $operator);
  188. }
  189. }
  190. /**
  191. * @see ChadoField::queryOrder()
  192. */
  193. public function queryOrder($query, $order) {
  194. $alias = $this->field['field_name'];
  195. // Map details.
  196. $field_term_id = $this->getFieldTermID();
  197. $map_term = 'data:1274';
  198. $name_term = $field_term_id . ',' . $map_term . ',' . chado_get_semweb_term('featuremap', 'name');
  199. $description_term = $field_term_id . ',' . $map_term . ',' . chado_get_semweb_term('featuremap', 'description');
  200. $mappos_term = $field_term_id . ',' . chado_get_semweb_term('featurepos', 'mappos');
  201. // Reference sequence details.
  202. $ref_feature_term = chado_get_semweb_term('featurepos', 'map_feature_id');
  203. $ref_feature_name = $field_term_id . ',' . $ref_feature_term . ',' .chado_get_semweb_term('feature', 'name');
  204. $ref_feature_id = $field_term_id . ',' . $ref_feature_term . ',' .chado_get_semweb_term('feature', 'uniquename');
  205. $ref_feature_type = $field_term_id . ',' . $ref_feature_term . ',' .'rdfs:type';
  206. $this->queryJoinOnce($query, 'featurepos', $alias, "base.feature_id = $alias.feature_id", 'LEFT OUTER');
  207. if ($order['column'] == $name_term) {
  208. $this->queryJoinOnce($query, 'featuremap', $alias . '_map', $alias . '_map.featuremap_id = ' . $alias . '.featuremap_id', 'LEFT OUTER');
  209. $query->orderBy($alias . '_map.name', $order['direction']);
  210. }
  211. if ($order['column'] == $description_term) {
  212. $this->queryJoinOnce($query, 'featuremap', $alias . '_map', $alias . '_map.featuremap_id = ' . $alias . '.featuremap_id', 'LEFT OUTER');
  213. $query->orderBy($alias . '_map.description', $order['direction']);
  214. }
  215. if ($order['column'] == $mappos_term) {
  216. $query->orderBy($alias . '.mappos', $order['direction']);
  217. }
  218. if ($order['column'] == $ref_feature_name) {
  219. $this->queryJoinOnce($query, 'feature', $alias . '_mapref', $alias . '_mapref.feature_id = ' . $alias . '.map_feature_id', 'LEFT OUTER');
  220. $query->orderBy($alias . '_mapref.name', $order['direction']);
  221. }
  222. if ($order['column'] == $ref_feature_id) {
  223. $this->queryJoinOnce($query, 'feature', $alias . '_mapref', $alias . '_mapref.feature_id = ' . $alias . '.map_feature_id', 'LEFT OUTER');
  224. $query->orderBy($alias . '_mapref.uniquename', $order['direction']);
  225. }
  226. if ($order['column'] == $ref_feature_type) {
  227. $this->queryJoinOnce($query, 'feature', $alias . '_mapref', $alias . '_mapref.feature_id = ' . $alias . '.map_feature_id', 'LEFT OUTER');
  228. $this->queryJoinOnce($query, 'cvterm', $alias . '_mapref_cvterm', $alias . '_mapref_cvterm.cvterm_id = ' . $alias . '_mapref.type_id', 'LEFT OUTER');
  229. $query->orderBy($alias . '_mapref_cvterm.name', $order['direction']);
  230. }
  231. }
  232. /**
  233. *
  234. * @see TripalField::load()
  235. */
  236. public function load($entity) {
  237. $record = $entity->chado_record;
  238. $settings = $this->field['settings'];
  239. $field_name = $this->field['field_name'];
  240. $field_type = $this->field['type'];
  241. // Map details
  242. $map_term = 'data:1274';
  243. $name_term = chado_get_semweb_term('featuremap', 'name');
  244. $description_term = chado_get_semweb_term('featuremap', 'description');
  245. $mappos_term = chado_get_semweb_term('featurepos', 'mappos');
  246. // Reference sequence details.
  247. $ref_feature_term = chado_get_semweb_term('featurepos', 'map_feature_id');
  248. $ref_feature_name = chado_get_semweb_term('feature', 'name');
  249. $ref_feature_id = chado_get_semweb_term('feature', 'uniquename');
  250. $ref_feature_type = 'rdfs:type';
  251. // Set some defaults for the empty record.
  252. $entity->{$field_name}['und'][0] = array(
  253. 'value' => array(),
  254. );
  255. // Add the featurepos records to our $record.
  256. $options = array('return_array' => TRUE,
  257. 'include_fk' => array(
  258. 'map_feature_id' => array(
  259. 'type_id' => 1,
  260. ),
  261. 'feature_id' => array(
  262. 'type_id' => 1
  263. ),
  264. ),
  265. );
  266. $feature = chado_expand_var($record, 'table', 'featurepos', $options);
  267. // Iterate through the positions and add them to our values.
  268. $i = 0;
  269. if (isset($feature->featurepos->feature_id)) {
  270. foreach ($feature->featurepos->feature_id AS $featurepos) {
  271. // Get details about the map
  272. $featuremap = chado_generate_var('featuremap', array('featuremap_id' => $featurepos->featuremap_id));
  273. $entity->{$field_name}['und'][$i]['value'] = array (
  274. // Map.
  275. $map_term => array(
  276. $name_term => $featuremap->name,
  277. $description_term => $featuremap->description,
  278. ),
  279. $ref_feature_term => array(
  280. $ref_feature_name => $featurepos->map_feature_id->name,
  281. $ref_feature_id => $featurepos->map_feature_id->uniquename,
  282. $ref_feature_type => $featurepos->map_feature_id->type_id->name,
  283. ),
  284. $mappos_term => $featurepos->mappos
  285. );
  286. if (property_exists($featuremap, 'entity_id')) {
  287. $entity->{$field_name}['und'][$i]['value'][$map_term]['entity'] = 'TripalEntity:' . $featuremap->entity_id;
  288. }
  289. if (property_exists($featurepos->map_feature_id, 'entity_id')) {
  290. $entity->{$field_name}['und'][$i]['value'][$map_term]['entity'] = 'TripalEntity:' . $featurepos->map_feature_id->entity_id;
  291. }
  292. $i++;
  293. }
  294. }
  295. }
  296. }