chado_linker__featureloc.inc 13 KB

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