chado_linker__relationship.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. class chado_linker__relationship extends TripalField {
  3. /**
  4. * @see TripalField::field_info()
  5. */
  6. function field_info() {
  7. return array(
  8. 'label' => t('Relationships'),
  9. 'description' => t('Relationships between records.'),
  10. 'default_widget' => 'chado_linker__relationship_widget',
  11. 'default_formatter' => 'chado_linker__relationship_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::attach_info()
  22. */
  23. function attach_info($entity_type, $bundle, $target) {
  24. $field_info = array();
  25. $table_name = $target['data_table'];
  26. $type_table = $target['type_table'];
  27. $type_field = $target['field'];
  28. $cv_id = $target['cv_id'];
  29. $cvterm_id = $target['cvterm_id'];
  30. // If the linker table does not exists then we don't want to add attach.
  31. $rel_table = $table_name . '_relationship';
  32. if (!chado_table_exists($rel_table)) {
  33. return $field_info;
  34. }
  35. $schema = chado_get_schema($rel_table);
  36. $pkey = $schema['primary key'][0];
  37. // Initialize the field array.
  38. $field_info = array(
  39. 'field_name' => $table_name . '__relationship',
  40. 'field_type' => 'chado_linker__relationship',
  41. 'widget_type' => 'chado_linker__relationship_widget',
  42. 'widget_settings' => array('display_label' => 1),
  43. 'description' => 'A generic field for displaying relationships between data types',
  44. 'label' => 'Relationships',
  45. 'is_required' => 0,
  46. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  47. 'storage' => 'field_chado_storage',
  48. 'field_settings' => array(
  49. 'chado_table' => $rel_table,
  50. 'chado_column' => $pkey,
  51. 'base_table' => $table_name,
  52. 'semantic_web' => array(
  53. 'name' => 'relationship',
  54. 'accession' => 'relationship',
  55. 'ns' => 'local',
  56. 'nsurl' => '',
  57. ),
  58. ),
  59. );
  60. return $field_info;
  61. }
  62. /**
  63. * @see TripalField::widget_info()
  64. */
  65. function widget_info() {
  66. return array(
  67. 'label' => t('Relationship Settings'),
  68. 'field types' => array('chado_linker__relationship')
  69. );
  70. }
  71. /**
  72. * @see TripalField::formatter_info()
  73. */
  74. function formatter_info() {
  75. return array(
  76. 'label' => t('Relationships'),
  77. 'field types' => array('chado_linker__relationship'),
  78. 'settings' => array(
  79. ),
  80. );
  81. }
  82. /**
  83. * @see TripalField::formatter_view()
  84. */
  85. function formatter_view(&$element, $entity_type, $entity,
  86. $field, $instance, $langcode, $items, $display) {
  87. // Get the settings
  88. $settings = $display['settings'];
  89. $rows = array();
  90. $headers = array('Subject' ,'Type', 'Object');
  91. $headers = array('Relationship');
  92. $this_name = $entity->chado_record->name;
  93. if (property_exists($entity->chado_record, 'uniquename')) {
  94. $this_name = $entity->chado_record->uniquename;
  95. }
  96. foreach ($items as $delta => $item) {
  97. $subject_name = $item['value']['subject']['name'];
  98. $subject_type = $item['value']['subject']['type'];
  99. $object_name = $item['value']['object']['name'];
  100. $object_type = $item['value']['object']['type'];
  101. $phrase = $item['value']['phrase'];
  102. // Handle some special cases.
  103. // For mRNA objects we don't want to show the CDS, exons, 5' UTR, etc.
  104. // we want to show the parent gene and the protein.
  105. if ($object_type == 'mRNA' and ($subject_type != 'polypeptide')) {
  106. continue;
  107. }
  108. if ($subject_type == 'mRNA' and ($object_type != 'gene')) {
  109. continue;
  110. }
  111. if (array_key_exists('entity_id', $item['value']['object'])) {
  112. $object_entity_id = $item['value']['object']['entity_id'];
  113. if ($object_entity_id != $entity->id) {
  114. $link = l($object_name, 'bio_data/' . $object_entity_id);
  115. $phrase = preg_replace("/$object_name/", $link, $phrase);
  116. }
  117. }
  118. if (array_key_exists('entity_id', $item['value']['subject'])) {
  119. $subject_entity_id = $item['value']['subject']['entity_id'];
  120. if ($subject_entity_id != $entity->id) {
  121. $link = l($subject_name, 'bio_data/' . $subject_entity_id);
  122. $phrase = preg_replace("/$subject_name/", $link, $phrase);
  123. }
  124. }
  125. $phrase = preg_replace("/$subject_type/", "<b>$subject_type</b>", $phrase);
  126. $phrase = preg_replace("/$object_type/", "<b>$object_type</b>", $phrase);
  127. $rows[] = array($phrase);
  128. }
  129. // the $table array contains the headers and rows array as well as other
  130. // options for controlling the display of the table. Additional
  131. // documentation can be found here:
  132. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  133. $table = array(
  134. 'header' => $headers,
  135. 'rows' => $rows,
  136. 'attributes' => array(
  137. 'id' => 'tripal_feature-table-alignments',
  138. 'class' => 'tripal-data-table'
  139. ),
  140. 'sticky' => FALSE,
  141. 'caption' => '',
  142. 'colgroups' => array(),
  143. 'empty' => '',
  144. );
  145. // once we have our table array structure defined, we call Drupal's theme_table()
  146. // function to generate the table.
  147. $element[$delta] = array(
  148. '#type' => 'markup',
  149. '#markup' => theme_table($table),
  150. );
  151. }
  152. /**
  153. * @see TripalField::load()
  154. */
  155. function load($field, $entity, $details) {
  156. $settings = $field['settings'];
  157. $record = $details['record'];
  158. $field_name = $field['field_name'];
  159. $field_type = $field['type'];
  160. $field_table = $field['settings']['chado_table'];
  161. $field_column = $field['settings']['chado_column'];
  162. $base_table = $field['settings']['base_table'];
  163. // Expand the object to include the relationships.
  164. $options = array(
  165. 'return_array' => 1,
  166. 'order_by' => array('rank' => 'ASC'),
  167. // we don't want to fully recurse we only need information about the
  168. // relationship type and the object and subject (including feature type
  169. // and organism)
  170. 'include_fk' => array(
  171. 'type_id' => 1,
  172. 'object_id' => array(
  173. 'type_id' => 1,
  174. 'organism_id' => 1
  175. ),
  176. 'subject_id' => array(
  177. 'type_id' => 1,
  178. 'organism_id' => 1
  179. ),
  180. ),
  181. );
  182. $rel_table = $base_table . '_relationship';
  183. $record = chado_expand_var($record, 'table', $rel_table, $options);
  184. $srelationships = $record->$rel_table->subject_id;
  185. $orelationships = $record->$rel_table->object_id;
  186. $i = 0;
  187. if ($orelationships) {
  188. foreach ($orelationships as $relationship) {
  189. $rel_acc = $relationship->type_id->dbxref_id->db_id->name . ':' . $relationship->type_id->dbxref_id->accession;
  190. $rel_type = $relationship->type_id->name;
  191. $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
  192. $verb = $this->get_rel_verb($rel_type_clean);
  193. $subject_name = $relationship->subject_id->name;
  194. $subject_type = $relationship->subject_id->type_id->name;
  195. $object_name = $relationship->object_id->name;
  196. $object_type = $relationship->object_id->type_id->name;
  197. $entity->{$field_name}['und'][$i]['value'] = array(
  198. '@type' => $rel_acc,
  199. 'subject' => array(
  200. '@type' => $relationship->subject_id->type_id->dbxref_id->db_id->name .
  201. ':' . $relationship->subject_id->type_id->dbxref_id->accession,
  202. 'type' => $subject_type,
  203. 'name' => $subject_name,
  204. ),
  205. 'type' => $relationship->type_id->name,
  206. 'object' => array(
  207. 'entity_id' => $entity->id,
  208. 'entity_type' => 'TripalEntity',
  209. 'type' => $object_type,
  210. 'name' => $object_name,
  211. )
  212. );
  213. if (property_exists($relationship->subject_id, 'uniquename')) {
  214. $subject_name = $relationship->subject_id->uniquename;
  215. $entity->{$field_name}['und'][$i]['value']['subject']['name'] = $subject_name;
  216. }
  217. if (property_exists($relationship->object_id, 'uniquename')) {
  218. $object_name = $relationship->object_id->uniquename;
  219. $entity->{$field_name}['und'][$i]['value']['object']['name'] = $object_name;
  220. }
  221. if (property_exists($relationship->subject_id, 'entity_id')) {
  222. $entity_id = $relationship->subject_id->entity_id;
  223. $entity->{$field_name}['und'][$i]['value']['subject']['entity_id'] = $entity_id;
  224. $entity->{$field_name}['und'][$i]['value']['subject']['entity_type'] = 'TripalEntity';
  225. }
  226. $entity->{$field_name}['und'][$i]['value']['phrase'] = 'The ' . $subject_type . ', ' .
  227. $subject_name . ', ' . $verb . ' ' . $rel_type_clean . ' this ' .
  228. $object_type . '.';
  229. $i++;
  230. }
  231. }
  232. if ($srelationships) {
  233. foreach ($srelationships as $relationship) {
  234. $rel_acc = $relationship->type_id->dbxref_id->db_id->name . ':' . $relationship->type_id->dbxref_id->accession;
  235. $rel_type = $relationship->type_id->name;
  236. $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
  237. $verb = $this->get_rel_verb($rel_type_clean);
  238. $subject_name = $relationship->subject_id->name;
  239. $subject_type = $relationship->subject_id->type_id->name;
  240. $object_name = $relationship->object_id->name;
  241. $object_type = $relationship->object_id->type_id->name;
  242. $entity->{$field_name}['und'][$i]['value'] = array(
  243. '@type' => $rel_acc,
  244. 'subject' => array(
  245. 'type' => $subject_type,
  246. 'name' => $subject_name,
  247. 'entity_id' => $entity->id,
  248. 'entity_type' => 'TripalEntity',
  249. ),
  250. 'type' => $relationship->type_id->name,
  251. 'object' => array(
  252. '@type' => $relationship->object_id->type_id->dbxref_id->db_id->name .
  253. ':' . $relationship->subject_id->type_id->dbxref_id->accession,
  254. 'type' => $object_type,
  255. 'name' => $oject_name,
  256. )
  257. );
  258. if (property_exists($relationship->subject_id, 'uniquename')) {
  259. $subject_name = $relationship->subject_id->uniquename;
  260. $entity->{$field_name}['und'][$i]['value']['subject']['name'] = $subject_name;
  261. }
  262. if (property_exists($relationship->object_id, 'uniquename')) {
  263. $object_name = $relationship->object_id->uniquename;
  264. $entity->{$field_name}['und'][$i]['value']['object']['name'] = $object_name;
  265. }
  266. if (property_exists($relationship->object_id, 'entity_id')) {
  267. $entity_id = $relationship->object_id->entity_id;
  268. $entity->{$field_name}['und'][$i]['value']['object']['entity_id'] = $entity_id;
  269. $entity->{$field_name}['und'][$i]['value']['object']['entity_type'] = 'TripalEntity';
  270. }
  271. $entity->{$field_name}['und'][$i]['value']['phrase'] = 'This ' .
  272. $subject_type . ' ' . $verb . ' ' . $rel_type_clean . ' the ' .
  273. $object_type . ', ' . $object_name . '.';
  274. $i++;
  275. }
  276. }
  277. }
  278. private function get_rel_verb($rel_type) {
  279. $verb = '';
  280. switch ($rel_type) {
  281. case 'integral part of':
  282. case 'instance of':
  283. $verb = 'is an';
  284. break;
  285. case 'proper part of':
  286. case 'transformation of':
  287. case 'genome of':
  288. case 'part of':
  289. $verb = 'is a';
  290. case 'position of':
  291. case 'sequence of':
  292. case 'variant of':
  293. $verb = 'is a';
  294. break;
  295. case 'derives from':
  296. case 'connects on':
  297. case 'contains':
  298. case 'finishes':
  299. case 'guides':
  300. case 'has origin':
  301. case 'has part':
  302. case 'has quality':
  303. case 'is consecutive sequence of':
  304. case 'maximally overlaps':
  305. case 'overlaps':
  306. case 'starts':
  307. $verb = '';
  308. break;
  309. default:
  310. $verb = 'is';
  311. }
  312. return $verb;
  313. }
  314. }