chado_linker__featureloc.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. class chado_linker__featureloc extends TripalField {
  3. /**
  4. * @see TripalField::field_info()
  5. */
  6. public function field_info() {
  7. return array(
  8. 'label' => t('Aligned Locations'),
  9. 'description' => t('Locations on landmark sequences where the feature is aligned.'),
  10. 'default_widget' => 'chado_linker__featureloc_widget',
  11. 'default_formatter' => 'chado_linker__featureloc_formatter',
  12. 'settings' => array(),
  13. 'storage' => array(
  14. 'type' => 'field_chado_storage',
  15. 'module' => 'tripal_chado',
  16. 'active' => TRUE
  17. ),
  18. );
  19. }
  20. /**
  21. * @see TripalField::attach_info()
  22. */
  23. public function attach_info($entity_type, $bundle, $target) {
  24. $field_info = array();
  25. $table_name = $target['data_table'];
  26. $type_table = $target['type_table'];
  27. $type_field = $target['field'];
  28. $cv_id = $target['cv_id'];
  29. $cvterm_id = $target['cvterm_id'];
  30. // If this is not the feature table then we don't want to attach.
  31. if ($table_name != 'feature') {
  32. return $field_info;
  33. }
  34. $schema = chado_get_schema('featureloc');
  35. $pkey = $schema['primary key'][0];
  36. // Initialize the field array.
  37. $field_info = array(
  38. 'field_name' => 'featureloc',
  39. 'field_type' => 'chado_linker__featureloc',
  40. 'widget_type' => 'chado_linker__featureloc_widget',
  41. 'widget_settings' => array('display_label' => 1),
  42. 'description' => '',
  43. 'label' => 'Aligned Locations',
  44. 'is_required' => 0,
  45. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  46. 'storage' => 'field_chado_storage',
  47. 'field_settings' => array(
  48. 'chado_table' => 'featureloc',
  49. 'chado_column' => $pkey,
  50. 'base_table' => 'feature',
  51. 'semantic_web' => array(
  52. 'name' => '',
  53. 'accession' => '',
  54. 'ns' => '',
  55. 'nsurl' => '',
  56. ),
  57. ),
  58. );
  59. return $field_info;
  60. }
  61. /**
  62. * @see TripalField::widget_info()
  63. */
  64. public function widget_info() {
  65. return array(
  66. 'label' => t('Aligned Locations'),
  67. 'field types' => array('chado_linker__featureloc')
  68. );
  69. }
  70. /**
  71. * @see TripalField::formatter_info()
  72. */
  73. public function formatter_info() {
  74. return array(
  75. 'label' => t('Aligned Locations'),
  76. 'field types' => array('chado_linker__featureloc'),
  77. 'settings' => array(
  78. ),
  79. );
  80. }
  81. /**
  82. * @see TripalField::formatter_settings_summary()
  83. */
  84. public function formatter_settings_summary($field, $instance,
  85. $view_mode) {
  86. }
  87. /**
  88. * @see TripalField::formatter_settings_form()
  89. */
  90. public function formatter_settings_form($field, $instance,
  91. $view_mode, $form, &$form_state) {
  92. }
  93. /**
  94. * @see TripalField::formatter_view()
  95. */
  96. function formatter_view(&$element, $entity_type, $entity,
  97. $field, $instance, $langcode, $items, $display) {
  98. // Get the settings
  99. $settings = $display['settings'];
  100. $rows = array();
  101. $headers = array('Aligned Feature' ,'Feature Type', 'Alignment Location');
  102. foreach ($items as $delta => $item) {
  103. $alignment = $item['value'];
  104. $feature_name = $alignment['name'];
  105. $feature_loc = '';
  106. $strand = '.';
  107. if ($alignment['strand'] == -1) {
  108. $strand = '-';
  109. }
  110. elseif ($alignment['strand'] == 1) {
  111. $strand = '+';
  112. }
  113. // if this is a match then make the other location
  114. if(array_key_exists('right_feature', $alignment)){
  115. $rstrand = '.';
  116. if ($alignment['right_strand'] == -1) {
  117. $rstrand = '-';
  118. }
  119. elseif ($alignment['right_strand'] == 1) {
  120. $rstrand = '+';
  121. }
  122. $feature_loc = $feature['name'] .":". ($alignment['fmin'] + 1) . ".." . $alignment['fmax'] . " " . $strand;
  123. $feature_loc .= "<br>" . $alignment['name'] .":". ($alignment['right_fmin'] + 1) . ".." . $alignment['right_fmax'] . " " . $rstrand;
  124. }
  125. else {
  126. $feature_loc = $alignment['name'] .":". ($alignment['fmin'] + 1) . ".." . $alignment['fmax'] . " " . $strand;
  127. }
  128. $rows[] = array(
  129. $feature_name,
  130. $alignment['type'],
  131. $feature_loc
  132. );
  133. }
  134. // the $table array contains the headers and rows array as well as other
  135. // options for controlling the display of the table. Additional
  136. // documentation can be found here:
  137. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  138. $table = array(
  139. 'header' => $headers,
  140. 'rows' => $rows,
  141. 'attributes' => array(
  142. 'id' => 'tripal_feature-table-alignments',
  143. 'class' => 'tripal-data-table'
  144. ),
  145. 'sticky' => FALSE,
  146. 'caption' => '',
  147. 'colgroups' => array(),
  148. 'empty' => '',
  149. );
  150. // once we have our table array structure defined, we call Drupal's theme_table()
  151. // function to generate the table.
  152. $element[$delta] = array(
  153. '#type' => 'markup',
  154. '#markup' => theme_table($table),
  155. );
  156. }
  157. /**
  158. * @see TripalField::load()
  159. */
  160. function load($field, $entity, $details) {
  161. $record = $details['record'];
  162. $settings = $field['settings'];
  163. $field_name = $field['field_name'];
  164. $field_type = $field['type'];
  165. $options = array(
  166. 'return_array' => TRUE,
  167. 'include_fk' => array(
  168. 'srcfeature_id' => array(
  169. 'type_id' => 1,
  170. ),
  171. 'feature_id' => array(
  172. 'type_id' => 1
  173. ),
  174. )
  175. );
  176. $feature = chado_expand_var($record, 'table', 'featureloc', $options);
  177. // Get alignments as child
  178. $cfeaturelocs = $feature->featureloc->feature_id;
  179. if (!$cfeaturelocs) {
  180. $cfeaturelocs = array();
  181. }
  182. // Get alignment as parent
  183. $pfeaturelocs = $feature->featureloc->srcfeature_id;
  184. if (!$pfeaturelocs) {
  185. $pfeaturelocs = array();
  186. }
  187. // Get matched alignments (those with an itermediate 'match' or 'EST_match', etc
  188. $mfeaturelocs = $this->get_matched_alignments($feature);
  189. $feature->matched_featurelocs = $mfeaturelocs;
  190. // Combine all three alignments into a single array for printing together in
  191. // a single list
  192. $alignments = array();
  193. foreach ($pfeaturelocs as $featureloc) {
  194. // if type is a 'match' then ignore it. We will handle those below
  195. if (preg_match('/(^match$|^.*?_match|match_part)$/', $featureloc->feature_id->type_id->name)) {
  196. continue;
  197. }
  198. $alignments[] = array(
  199. 'name' => $featureloc->feature_id->name,
  200. 'type' => $featureloc->feature_id->type_id->name,
  201. 'fmin' => $featureloc->fmin,
  202. 'fmax' => $featureloc->fmax,
  203. 'phase' => $featureloc->phase,
  204. 'strand' => $featureloc->strand,
  205. );
  206. }
  207. foreach ($cfeaturelocs as $featureloc) {
  208. // if type is a 'match' then ignore it. We will handle those below
  209. if (preg_match('/(^match$|^.*?_match|match_part)$/', $featureloc->feature_id->type_id->name)) {
  210. continue;
  211. }
  212. $alignments[] = array(
  213. 'name' => $featureloc->srcfeature_id->name,
  214. 'type' => $featureloc->srcfeature_id->type_id->name,
  215. 'fmin' => $featureloc->fmin,
  216. 'is_fmin_partial' => $featureloc->is_fmin_partial,
  217. 'fmax' => $featureloc->fmax,
  218. 'is_fmax_partial' => $featureloc->is_fmax_partial,
  219. 'phase' => $featureloc->phase,
  220. 'strand' => $featureloc->strand,
  221. );
  222. }
  223. // in matching features, the left feature is always the feature
  224. // provided to this function.
  225. foreach ($mfeaturelocs as $featureloc) {
  226. // get more information about the right feature
  227. $select = array('feature_id' => $featureloc->right_srcfeature_id);
  228. $rfeature = chado_generate_var('feature', $select);
  229. // now add to the list
  230. $alignments[] = array(
  231. 'name' => $rfeature->name,
  232. 'type' => $rfeature->type_id->name,
  233. 'fmin' => $featureloc->left_fmin,
  234. 'is_fmin_partial' => $featureloc->left_is_fmin_partial,
  235. 'fmax' => $featureloc->left_fmax,
  236. 'is_fmax_partial' => $featureloc->left_is_fmax_partial,
  237. 'phase' => $featureloc->left_phase,
  238. 'strand' => $featureloc->left_strand,
  239. 'right_fmin' => $featureloc->right_fmin,
  240. 'right_is_fmin_partial' => $featureloc->right_is_fmin_partial,
  241. 'right_fmax' => $featureloc->right_fmax,
  242. 'right_is_fmax_partial' => $featureloc->right_is_fmax_partial,
  243. 'right_phase' => $featureloc->right_phase,
  244. 'right_strand' => $featureloc->right_strand,
  245. );
  246. }
  247. $i = 0;
  248. foreach ($alignments as $alignment) {
  249. $entity->{$field_name}['und'][$i]['value'] = $alignment;
  250. $i++;
  251. }
  252. }
  253. /**
  254. * This function is for features that align through an intermediate such
  255. * as 'EST_match' or 'match'. This occurs in the case where two sequences
  256. * align but where one does not align perfectly. Some ESTs may be in a contig
  257. * but not all of the EST. Portions may overhang and not be included in the
  258. * consensus if quality is bad.
  259. * For example:
  260. * Feature 1: Contig --------------------
  261. * Feature 2: EST_match -------
  262. * Feature 3: EST ---------
  263. *
  264. * The feature provided to the function will always be the feature 1. The
  265. * featureloc columns prefixed with 'right' (e.g. right_fmin) belong to the
  266. * alignment of feature 3 with feature 2
  267. *
  268. * Features may align to more than one feature and are not matches. We do
  269. * not want to include these, so we have to filter on the SO terms:
  270. * match, or %_match
  271. *
  272. * @ingroup tripal_feature
  273. */
  274. private function get_matched_alignments($feature) {
  275. $sql = "
  276. SELECT
  277. FL1.featureloc_id as left_featureloc_id,
  278. FL1.srcfeature_id as left_srcfeature_id,
  279. FL1.feature_id as left_feature_id,
  280. FL1.fmin as left_fmin,
  281. FL1.is_fmin_partial as left_is_fmin_partial,
  282. FL1.fmax as left_fmax,
  283. FL1.is_fmax_partial as left_is_fmax_partial,
  284. FL1.strand as left_strand,
  285. FL1.phase as left_phase,
  286. FL1.locgroup as left_locgroup,
  287. FL1.rank as left_rank,
  288. FL2.featureloc_id as right_featureloc_id,
  289. FL2.srcfeature_id as right_srcfeature_id,
  290. FL2.feature_id as right_feature_id,
  291. FL2.fmin as right_fmin,
  292. FL2.is_fmin_partial as right_is_fmin_partial,
  293. FL2.fmax as right_fmax,
  294. FL2.is_fmax_partial as right_is_fmax_partial,
  295. FL2.strand as right_strand,
  296. FL2.phase as right_phase,
  297. FL2.locgroup as right_locgroup,
  298. FL2.rank as right_rank
  299. FROM {feature} F1
  300. INNER JOIN {featureloc} FL1 on FL1.srcfeature_id = F1.feature_id
  301. INNER JOIN {feature} F2 on FL1.feature_id = F2.feature_id
  302. INNER JOIN {featureloc} FL2 on FL2.feature_id = F2.feature_id
  303. INNER JOIN {cvterm} CVT2 on F2.type_id = CVT2.cvterm_id
  304. WHERE
  305. F1.feature_id = :feature_id AND
  306. (CVT2.name = 'match' or CVT2.name like '%_match')
  307. ORDER BY FL1.fmin
  308. ";
  309. $results = chado_query($sql, array(':feature_id' => $feature->feature_id));
  310. // iterate through the results and add them to our featurelocs array
  311. $featurelocs = array();
  312. while ($fl = $results->fetchObject()) {
  313. // ignore featurelocs where the left and right srcfeature is the same
  314. if (strcmp($fl->left_srcfeature_id, $fl->right_srcfeature_id) == 0) {
  315. continue;
  316. }
  317. $featurelocs[] = $fl ;
  318. }
  319. return $featurelocs;
  320. }
  321. /**
  322. * @see TripalField::widget_form()
  323. */
  324. function widget_form(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
  325. }
  326. }
  327. /**
  328. * Callback function for validating the chado_linker_featureloc_widget.
  329. */
  330. function chado_linker__featureloc_widget_validate($element, &$form_state) {
  331. }
  332. /**
  333. * Validation function for the chado_linker_featureloc_formatter_settings_form.
  334. */
  335. function chado_linker__featureloc_formatter_settings_form_validate(&$form, &$form_state) {
  336. // Place here as an example for validating the settings form.
  337. }