chado_linker__featureloc.inc 14 KB

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