chado_linker__featureloc.inc 10 KB

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