chado_linker__featureloc.inc 10 KB

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