chado_linker__featureloc.inc 13 KB

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