chado_linker__featureloc.inc 14 KB

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