chado_linker__featureloc.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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' => 'position_of',
  53. 'accession' => 'position_of',
  54. 'ns' => 'SO',
  55. 'nsurl' => 'http://www.sequenceontology.org/',
  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. if (!$item['value']) {
  104. continue;
  105. }
  106. $alignment = $item['value'];
  107. $feature_name = $alignment['name'];
  108. $feature_loc = '';
  109. $strand = '.';
  110. if ($alignment['strand'] == -1) {
  111. $strand = '-';
  112. }
  113. elseif ($alignment['strand'] == 1) {
  114. $strand = '+';
  115. }
  116. // if this is a match then make the other location
  117. if(array_key_exists('right_feature', $alignment)){
  118. $rstrand = '.';
  119. if ($alignment['right_strand'] == -1) {
  120. $rstrand = '-';
  121. }
  122. elseif ($alignment['right_strand'] == 1) {
  123. $rstrand = '+';
  124. }
  125. $feature_loc = $feature['name'] .":". ($alignment['fmin'] + 1) . ".." . $alignment['fmax'] . " " . $strand;
  126. $feature_loc .= "<br>" . $alignment['name'] .":". ($alignment['right_fmin'] + 1) . ".." . $alignment['right_fmax'] . " " . $rstrand;
  127. }
  128. else {
  129. $feature_loc = $alignment['name'] .":". ($alignment['fmin'] + 1) . ".." . $alignment['fmax'] . " " . $strand;
  130. }
  131. $rows[] = array(
  132. $feature_name,
  133. $alignment['type'],
  134. $feature_loc
  135. );
  136. }
  137. // the $table array contains the headers and rows array as well as other
  138. // options for controlling the display of the table. Additional
  139. // documentation can be found here:
  140. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  141. $table = array(
  142. 'header' => $headers,
  143. 'rows' => $rows,
  144. 'attributes' => array(
  145. 'id' => 'tripal_feature-table-alignments',
  146. 'class' => 'tripal-data-table'
  147. ),
  148. 'sticky' => FALSE,
  149. 'caption' => '',
  150. 'colgroups' => array(),
  151. 'empty' => '',
  152. );
  153. // once we have our table array structure defined, we call Drupal's theme_table()
  154. // function to generate the table.
  155. $element[$delta] = array(
  156. '#type' => 'markup',
  157. '#markup' => theme_table($table),
  158. );
  159. }
  160. /**
  161. * @see TripalField::load()
  162. */
  163. function load($field, $entity, $details) {
  164. $record = $details['record'];
  165. $settings = $field['settings'];
  166. $field_name = $field['field_name'];
  167. $field_type = $field['type'];
  168. // Set some defaults for the empty record.
  169. $entity->{$field_name}['und'][0] = array(
  170. 'value' => array(),
  171. );
  172. $options = array(
  173. 'return_array' => TRUE,
  174. 'include_fk' => array(
  175. 'srcfeature_id' => array(
  176. 'type_id' => 1,
  177. ),
  178. 'feature_id' => array(
  179. 'type_id' => 1
  180. ),
  181. )
  182. );
  183. $feature = chado_expand_var($record, 'table', 'featureloc', $options);
  184. // Get alignments as child
  185. $cfeaturelocs = $feature->featureloc->feature_id;
  186. if (!$cfeaturelocs) {
  187. $cfeaturelocs = array();
  188. }
  189. // Get alignment as parent
  190. $pfeaturelocs = $feature->featureloc->srcfeature_id;
  191. if (!$pfeaturelocs) {
  192. $pfeaturelocs = array();
  193. }
  194. // Get matched alignments (those with an itermediate 'match' or 'EST_match', etc
  195. $mfeaturelocs = $this->get_matched_alignments($feature);
  196. $feature->matched_featurelocs = $mfeaturelocs;
  197. // Combine all three alignments into a single array for printing together in
  198. // a single list
  199. $alignments = array();
  200. foreach ($pfeaturelocs as $featureloc) {
  201. // if type is a 'match' then ignore it. We will handle those below
  202. if (preg_match('/(^match$|^.*?_match|match_part)$/', $featureloc->feature_id->type_id->name)) {
  203. continue;
  204. }
  205. $alignments[] = array(
  206. 'name' => $featureloc->feature_id->name,
  207. 'type' => $featureloc->feature_id->type_id->name,
  208. 'fmin' => $featureloc->fmin,
  209. 'fmax' => $featureloc->fmax,
  210. 'phase' => $featureloc->phase,
  211. 'strand' => $featureloc->strand,
  212. );
  213. }
  214. foreach ($cfeaturelocs as $featureloc) {
  215. // if type is a 'match' then ignore it. We will handle those below
  216. if (preg_match('/(^match$|^.*?_match|match_part)$/', $featureloc->feature_id->type_id->name)) {
  217. continue;
  218. }
  219. $alignments[] = array(
  220. 'name' => $featureloc->srcfeature_id->name,
  221. 'type' => $featureloc->srcfeature_id->type_id->name,
  222. 'fmin' => $featureloc->fmin,
  223. 'is_fmin_partial' => $featureloc->is_fmin_partial,
  224. 'fmax' => $featureloc->fmax,
  225. 'is_fmax_partial' => $featureloc->is_fmax_partial,
  226. 'phase' => $featureloc->phase,
  227. 'strand' => $featureloc->strand,
  228. );
  229. }
  230. // in matching features, the left feature is always the feature
  231. // provided to this function.
  232. foreach ($mfeaturelocs as $featureloc) {
  233. // get more information about the right feature
  234. $select = array('feature_id' => $featureloc->right_srcfeature_id);
  235. $rfeature = chado_generate_var('feature', $select);
  236. // now add to the list
  237. $alignments[] = array(
  238. 'name' => $rfeature->name,
  239. 'type' => $rfeature->type_id->name,
  240. 'fmin' => $featureloc->left_fmin,
  241. 'is_fmin_partial' => $featureloc->left_is_fmin_partial,
  242. 'fmax' => $featureloc->left_fmax,
  243. 'is_fmax_partial' => $featureloc->left_is_fmax_partial,
  244. 'phase' => $featureloc->left_phase,
  245. 'strand' => $featureloc->left_strand,
  246. 'right_fmin' => $featureloc->right_fmin,
  247. 'right_is_fmin_partial' => $featureloc->right_is_fmin_partial,
  248. 'right_fmax' => $featureloc->right_fmax,
  249. 'right_is_fmax_partial' => $featureloc->right_is_fmax_partial,
  250. 'right_phase' => $featureloc->right_phase,
  251. 'right_strand' => $featureloc->right_strand,
  252. );
  253. }
  254. $i = 0;
  255. foreach ($alignments as $alignment) {
  256. $entity->{$field_name}['und'][$i]['value'] = $alignment;
  257. $i++;
  258. }
  259. }
  260. /**
  261. * This function is for features that align through an intermediate such
  262. * as 'EST_match' or 'match'. This occurs in the case where two sequences
  263. * align but where one does not align perfectly. Some ESTs may be in a contig
  264. * but not all of the EST. Portions may overhang and not be included in the
  265. * consensus if quality is bad.
  266. * For example:
  267. * Feature 1: Contig --------------------
  268. * Feature 2: EST_match -------
  269. * Feature 3: EST ---------
  270. *
  271. * The feature provided to the function will always be the feature 1. The
  272. * featureloc columns prefixed with 'right' (e.g. right_fmin) belong to the
  273. * alignment of feature 3 with feature 2
  274. *
  275. * Features may align to more than one feature and are not matches. We do
  276. * not want to include these, so we have to filter on the SO terms:
  277. * match, or %_match
  278. *
  279. * @ingroup tripal_feature
  280. */
  281. private function get_matched_alignments($feature) {
  282. $sql = "
  283. SELECT
  284. FL1.featureloc_id as left_featureloc_id,
  285. FL1.srcfeature_id as left_srcfeature_id,
  286. FL1.feature_id as left_feature_id,
  287. FL1.fmin as left_fmin,
  288. FL1.is_fmin_partial as left_is_fmin_partial,
  289. FL1.fmax as left_fmax,
  290. FL1.is_fmax_partial as left_is_fmax_partial,
  291. FL1.strand as left_strand,
  292. FL1.phase as left_phase,
  293. FL1.locgroup as left_locgroup,
  294. FL1.rank as left_rank,
  295. FL2.featureloc_id as right_featureloc_id,
  296. FL2.srcfeature_id as right_srcfeature_id,
  297. FL2.feature_id as right_feature_id,
  298. FL2.fmin as right_fmin,
  299. FL2.is_fmin_partial as right_is_fmin_partial,
  300. FL2.fmax as right_fmax,
  301. FL2.is_fmax_partial as right_is_fmax_partial,
  302. FL2.strand as right_strand,
  303. FL2.phase as right_phase,
  304. FL2.locgroup as right_locgroup,
  305. FL2.rank as right_rank
  306. FROM {feature} F1
  307. INNER JOIN {featureloc} FL1 on FL1.srcfeature_id = F1.feature_id
  308. INNER JOIN {feature} F2 on FL1.feature_id = F2.feature_id
  309. INNER JOIN {featureloc} FL2 on FL2.feature_id = F2.feature_id
  310. INNER JOIN {cvterm} CVT2 on F2.type_id = CVT2.cvterm_id
  311. WHERE
  312. F1.feature_id = :feature_id AND
  313. (CVT2.name = 'match' or CVT2.name like '%_match')
  314. ORDER BY FL1.fmin
  315. ";
  316. $results = chado_query($sql, array(':feature_id' => $feature->feature_id));
  317. // iterate through the results and add them to our featurelocs array
  318. $featurelocs = array();
  319. while ($fl = $results->fetchObject()) {
  320. // ignore featurelocs where the left and right srcfeature is the same
  321. if (strcmp($fl->left_srcfeature_id, $fl->right_srcfeature_id) == 0) {
  322. continue;
  323. }
  324. $featurelocs[] = $fl ;
  325. }
  326. return $featurelocs;
  327. }
  328. /**
  329. * @see TripalField::widget_form()
  330. */
  331. function widget_form(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
  332. }
  333. }
  334. /**
  335. * Callback function for validating the chado_linker_featureloc_widget.
  336. */
  337. function chado_linker__featureloc_widget_validate($element, &$form_state) {
  338. }
  339. /**
  340. * Validation function for the chado_linker_featureloc_formatter_settings_form.
  341. */
  342. function chado_linker__featureloc_formatter_settings_form_validate(&$form, &$form_state) {
  343. // Place here as an example for validating the settings form.
  344. }