chado_linker__featureloc.inc 14 KB

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