chado_linker__featureloc.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. /**
  3. * Implements hook_info() for fields.
  4. *
  5. * This is a hook provided by the tripal_chado module for offloading the
  6. * hook_field_info() hook for each field to specify.
  7. */
  8. function chado_linker__featureloc_info() {
  9. return array(
  10. 'label' => t('Alignments'),
  11. 'description' => t('Locations on landmark sequences where the feature is aligned.'),
  12. 'default_widget' => 'chado_linker__featureloc_widget',
  13. 'default_formatter' => 'chado_linker__featureloc_formatter',
  14. 'settings' => array(),
  15. 'storage' => array(
  16. 'type' => 'field_chado_storage',
  17. 'module' => 'tripal_chado',
  18. 'active' => TRUE
  19. ),
  20. );
  21. }
  22. /**
  23. * Implements hook_widget_info.
  24. *
  25. * This is a hook provided by the tripal_chado module for offloading
  26. * the hook_field_widget_info() hook for each field to specify.
  27. */
  28. function chado_linker__featureloc_widget_info() {
  29. return array(
  30. 'label' => t('Alignment Settings'),
  31. 'field types' => array('chado_linker__featureloc')
  32. );
  33. }
  34. /**
  35. * Implements hook_formatter_info.
  36. *
  37. * This is a hook provided by the tripal_chado module for
  38. * offloading the hook_field_formatter_info() for each field
  39. * to specify.
  40. *
  41. */
  42. function chado_linker__featureloc_formatter_info() {
  43. return array(
  44. 'label' => t('Alignments'),
  45. 'field types' => array('chado_linker__featureloc'),
  46. 'settings' => array(
  47. ),
  48. );
  49. }
  50. /**
  51. * Implements hook_formatter_settings_summary.
  52. *
  53. * This is a hook provided by the tripal_chado module for
  54. * offloading the hook_field_formatter_settings_summary() for each field
  55. * to specify.
  56. *
  57. */
  58. function chado_linker__featureloc_formatter_settings_summary($field, $instance,
  59. $view_mode) {
  60. }
  61. /**
  62. * Provides a settings form for the formatter.
  63. *
  64. * This is a hook provided by the tripal_chado module for
  65. * offloading the hook_field_formatter_settings_form() for each field
  66. * to specify.
  67. */
  68. function chado_linker__featureloc_formatter_settings_form($field, $instance,
  69. $view_mode, $form, &$form_state) {
  70. }
  71. /**
  72. * Validation function for the chado_linker_featureloc_formatter_settings_form.
  73. */
  74. function chado_linker__featureloc_formatter_settings_form_validate(&$form, &$form_state) {
  75. // Place here as an example for validating the settings form.
  76. }
  77. /**
  78. *
  79. */
  80. function chado_linker__featureloc_formatter(&$element, $entity_type, $entity,
  81. $field, $instance, $langcode, $items, $display) {
  82. // Get the settings
  83. $settings = $display['settings'];
  84. foreach ($items as $delta => $item) {
  85. $alignments = $item['featurelocs'];
  86. if (count($alignments) > 0){
  87. // the $headers array is an array of fields to use as the colum headers.
  88. // additional documentation can be found here
  89. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  90. $headers = array('Aligned Feature' ,'Feature Type', 'Alignment Location');
  91. // the $rows array contains an array of rows where each row is an array
  92. // of values for each column of the table in that row. Additional documentation
  93. // can be found here:
  94. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  95. $rows = array();
  96. foreach ($alignments as $alignment){
  97. $feature_name = $alignment->name;
  98. if (property_exists($alignment, 'nid')) {
  99. $feature_name = l($feature_name, "node/" . $alignment->nid);
  100. }
  101. $feature_loc = '';
  102. $strand = '.';
  103. if ($alignment->strand == -1) {
  104. $strand = '-';
  105. }
  106. elseif ($alignment->strand == 1) {
  107. $strand = '+';
  108. }
  109. // if this is a match then make the other location
  110. if(property_exists($alignment, 'right_feature')){
  111. $rstrand = '.';
  112. if ($alignment->right_strand == -1) {
  113. $rstrand = '-';
  114. }
  115. elseif ($alignment->right_strand == 1) {
  116. $rstrand = '+';
  117. }
  118. $feature_loc = $feature->name .":". ($alignment->fmin + 1) . ".." . $alignment->fmax . " " . $strand;
  119. $feature_loc .= "<br>" . $alignment->name .":". ($alignment->right_fmin + 1) . ".." . $alignment->right_fmax . " " . $rstrand;
  120. }
  121. else {
  122. $feature_loc = $alignment->name .":". ($alignment->fmin + 1) . ".." . $alignment->fmax . " " . $strand;
  123. }
  124. $rows[] = array(
  125. $feature_name,
  126. $alignment->type,
  127. $feature_loc
  128. );
  129. }
  130. // the $table array contains the headers and rows array as well as other
  131. // options for controlling the display of the table. Additional
  132. // documentation can be found here:
  133. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  134. $table = array(
  135. 'header' => $headers,
  136. 'rows' => $rows,
  137. 'attributes' => array(
  138. 'id' => 'tripal_feature-table-alignments',
  139. 'class' => 'tripal-data-table'
  140. ),
  141. 'sticky' => FALSE,
  142. 'caption' => '',
  143. 'colgroups' => array(),
  144. 'empty' => '',
  145. );
  146. // once we have our table array structure defined, we call Drupal's theme_table()
  147. // function to generate the table.
  148. $element[$delta] = array(
  149. '#type' => 'markup',
  150. '#markup' => theme_table($table),
  151. );
  152. }
  153. }
  154. }
  155. /**
  156. * Loads the field values with appropriate data.
  157. *
  158. * This function is called by the tripal_chado_field_storage_load() for
  159. * each property managed by the field_chado_storage storage type. This is
  160. * an optional hook function that is only needed if the field has
  161. * multiple form elements.
  162. */
  163. function chado_linker__featureloc_load($field, $entity, $base_table, $record) {
  164. $field_name = $field['field_name'];
  165. $options = array(
  166. 'return_array' => 1,
  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 = chado_linker__featureloc_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. $alignment = new stdClass();
  199. $alignment->record = $featureloc;
  200. $alignment->name = $featureloc->feature_id->name;
  201. $alignment->type = $featureloc->feature_id->type_id->name;
  202. $alignment->fmin = $featureloc->fmin;
  203. $alignment->fmax = $featureloc->fmax;
  204. $alignment->phase = $featureloc->phase;
  205. $alignment->strand = $featureloc->strand;
  206. $alignments[] = $alignment;
  207. if (property_exists($featureloc->feature_id, 'nid')) {
  208. $alignment->nid = $featureloc->feature_id->nid;
  209. }
  210. }
  211. foreach ($cfeaturelocs as $featureloc) {
  212. // if type is a 'match' then ignore it. We will handle those below
  213. if (preg_match('/(^match$|^.*?_match|match_part)$/', $featureloc->feature_id->type_id->name)) {
  214. continue;
  215. }
  216. $alignment = new stdClass();
  217. $alignment->record = $featureloc;
  218. $alignment->name = $featureloc->srcfeature_id->name;
  219. $alignment->type = $featureloc->srcfeature_id->type_id->name;
  220. $alignment->fmin = $featureloc->fmin;
  221. $alignment->is_fmin_partial = $featureloc->is_fmin_partial;
  222. $alignment->fmax = $featureloc->fmax;
  223. $alignment->is_fmax_partial = $featureloc->is_fmax_partial;
  224. $alignment->phase = $featureloc->phase;
  225. $alignment->strand = $featureloc->strand;
  226. $alignments[] = $alignment;
  227. if (property_exists($featureloc->srcfeature_id, 'nid')) {
  228. $alignment->nid = $featureloc->srcfeature_id->nid;
  229. }
  230. }
  231. // in matching features, the left feature is always the feature
  232. // provided to this function.
  233. foreach ($mfeaturelocs as $featureloc) {
  234. // get more information about the right feature
  235. $select = array('feature_id' => $featureloc->right_srcfeature_id);
  236. $rfeature = chado_generate_var('feature', $select);
  237. // now add to the list
  238. $alignment = new stdClass();
  239. $alignment->record = $featureloc;
  240. $alignment->right_feature = $rfeature;
  241. $alignment->name = $rfeature->name;
  242. $alignment->type = $rfeature->type_id->name;
  243. $alignment->fmin = $featureloc->left_fmin;
  244. $alignment->is_fmin_partial = $featureloc->left_is_fmin_partial;
  245. $alignment->fmax = $featureloc->left_fmax;
  246. $alignment->is_fmax_partial = $featureloc->left_is_fmax_partial;
  247. $alignment->phase = $featureloc->left_phase;
  248. $alignment->strand = $featureloc->left_strand;
  249. $alignment->right_fmin = $featureloc->right_fmin;
  250. $alignment->right_is_fmin_partial = $featureloc->right_is_fmin_partial;
  251. $alignment->right_fmax = $featureloc->right_fmax;
  252. $alignment->right_is_fmax_partial = $featureloc->right_is_fmax_partial;
  253. $alignment->right_phase = $featureloc->right_phase;
  254. $alignment->right_strand = $featureloc->right_strand;
  255. $alignments[] = $alignment;
  256. if (property_exists($rfeature, 'nid')) {
  257. $alignment->nid = $rfeature->nid;
  258. }
  259. }
  260. $entity->{$field_name}['und'][0]['featurelocs'] = $alignments;
  261. }
  262. /**
  263. * This function is for features that align through an intermediate such
  264. * as 'EST_match' or 'match'. This occurs in the case where two sequences
  265. * align but where one does not align perfectly. Some ESTs may be in a contig
  266. * but not all of the EST. Portions may overhang and not be included in the
  267. * consensus if quality is bad.
  268. * For example:
  269. * Feature 1: Contig --------------------
  270. * Feature 2: EST_match -------
  271. * Feature 3: EST ---------
  272. *
  273. * The feature provided to the function will always be the feature 1. The
  274. * featureloc columns prefixed with 'right' (e.g. right_fmin) belong to the
  275. * alignment of feature 3 with feature 2
  276. *
  277. * Features may align to more than one feature and are not matches. We do
  278. * not want to include these, so we have to filter on the SO terms:
  279. * match, or %_match
  280. *
  281. * @ingroup tripal_feature
  282. */
  283. function chado_linker__featureloc_get_matched_alignments($feature) {
  284. $sql = "
  285. SELECT
  286. FL1.featureloc_id as left_featureloc_id,
  287. FL1.srcfeature_id as left_srcfeature_id,
  288. FL1.feature_id as left_feature_id,
  289. FL1.fmin as left_fmin,
  290. FL1.is_fmin_partial as left_is_fmin_partial,
  291. FL1.fmax as left_fmax,
  292. FL1.is_fmax_partial as left_is_fmax_partial,
  293. FL1.strand as left_strand,
  294. FL1.phase as left_phase,
  295. FL1.locgroup as left_locgroup,
  296. FL1.rank as left_rank,
  297. FL2.featureloc_id as right_featureloc_id,
  298. FL2.srcfeature_id as right_srcfeature_id,
  299. FL2.feature_id as right_feature_id,
  300. FL2.fmin as right_fmin,
  301. FL2.is_fmin_partial as right_is_fmin_partial,
  302. FL2.fmax as right_fmax,
  303. FL2.is_fmax_partial as right_is_fmax_partial,
  304. FL2.strand as right_strand,
  305. FL2.phase as right_phase,
  306. FL2.locgroup as right_locgroup,
  307. FL2.rank as right_rank
  308. FROM {feature} F1
  309. INNER JOIN {featureloc} FL1 on FL1.srcfeature_id = F1.feature_id
  310. INNER JOIN {feature} F2 on FL1.feature_id = F2.feature_id
  311. INNER JOIN {featureloc} FL2 on FL2.feature_id = F2.feature_id
  312. INNER JOIN {cvterm} CVT2 on F2.type_id = CVT2.cvterm_id
  313. WHERE
  314. F1.feature_id = :feature_id AND
  315. (CVT2.name = 'match' or CVT2.name like '%_match')
  316. ORDER BY FL1.fmin
  317. ";
  318. $results = chado_query($sql, array(':feature_id' => $feature->feature_id));
  319. // iterate through the results and add them to our featurelocs array
  320. $featurelocs = array();
  321. while ($fl = $results->fetchObject()) {
  322. // ignore featurelocs where the left and right srcfeature is the same
  323. if (strcmp($fl->left_srcfeature_id, $fl->right_srcfeature_id) == 0) {
  324. continue;
  325. }
  326. $featurelocs[] = $fl ;
  327. }
  328. return $featurelocs;
  329. }
  330. /**
  331. * Implements hook_ws_formatter().
  332. */
  333. function chado_linker__featureloc_ws_formatter(&$element, $entity_type, $entity,
  334. $field, $instance, $items) {
  335. }
  336. /**
  337. * Implements hook_widget().
  338. */
  339. function chado_linker__featureloc_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
  340. }
  341. /**
  342. * Callback function for validating the chado_linker_featureloc_widget.
  343. */
  344. function chado_linker__featureloc_widget_validate($element, &$form_state) {
  345. }