chado_linker__relationship.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. if (!$item['value']) {
  98. continue;
  99. }
  100. $subject_name = $item['value']['subject']['name'];
  101. $subject_type = $item['value']['subject']['type'];
  102. $object_name = $item['value']['object']['name'];
  103. $object_type = $item['value']['object']['type'];
  104. $phrase = $item['value']['phrase'];
  105. // Handle some special cases.
  106. // For mRNA objects we don't want to show the CDS, exons, 5' UTR, etc.
  107. // we want to show the parent gene and the protein.
  108. if ($object_type == 'mRNA' and ($subject_type != 'polypeptide')) {
  109. continue;
  110. }
  111. if ($subject_type == 'mRNA' and ($object_type != 'gene')) {
  112. continue;
  113. }
  114. $phrase = preg_replace("/$subject_type/", "<b>$subject_type</b>", $phrase);
  115. $phrase = preg_replace("/$object_type/", "<b>$object_type</b>", $phrase);
  116. if (array_key_exists('entity_id', $item['value']['object'])) {
  117. $object_entity_id = $item['value']['object']['entity_id'];
  118. if ($object_entity_id != $entity->id) {
  119. $link = l($object_name, 'bio_data/' . $object_entity_id);
  120. $phrase = preg_replace("/$object_name/", $link, $phrase);
  121. }
  122. }
  123. if (array_key_exists('entity_id', $item['value']['subject'])) {
  124. $subject_entity_id = $item['value']['subject']['entity_id'];
  125. if ($subject_entity_id != $entity->id) {
  126. $link = l($subject_name, 'bio_data/' . $subject_entity_id);
  127. $phrase = preg_replace("/$subject_name/", $link, $phrase);
  128. }
  129. }
  130. $rows[] = array($phrase);
  131. }
  132. // the $table array contains the headers and rows array as well as other
  133. // options for controlling the display of the table. Additional
  134. // documentation can be found here:
  135. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  136. $table = array(
  137. 'header' => $headers,
  138. 'rows' => $rows,
  139. 'attributes' => array(
  140. 'id' => 'tripal_feature-table-alignments',
  141. 'class' => 'tripal-data-table'
  142. ),
  143. 'sticky' => FALSE,
  144. 'caption' => '',
  145. 'colgroups' => array(),
  146. 'empty' => 'There are no relationships',
  147. );
  148. // once we have our table array structure defined, we call Drupal's theme_table()
  149. // function to generate the table.
  150. $element[$delta] = array(
  151. '#type' => 'markup',
  152. '#markup' => theme_table($table),
  153. );
  154. }
  155. /**
  156. * @see TripalField::load()
  157. */
  158. function load($field, $entity, $details) {
  159. $settings = $field['settings'];
  160. $record = $details['record'];
  161. $field_name = $field['field_name'];
  162. $field_type = $field['type'];
  163. $field_table = $field['settings']['chado_table'];
  164. $field_column = $field['settings']['chado_column'];
  165. $base_table = $field['settings']['base_table'];
  166. // Expand the object to include the relationships.
  167. $options = array(
  168. 'return_array' => 1,
  169. // we don't want to fully recurse we only need information about the
  170. // relationship type and the object and subject (including feature type
  171. // and organism)
  172. 'include_fk' => array(
  173. 'type_id' => 1,
  174. 'object_id' => array(
  175. 'type_id' => 1,
  176. 'organism_id' => 1
  177. ),
  178. 'subject_id' => array(
  179. 'type_id' => 1,
  180. 'organism_id' => 1
  181. ),
  182. ),
  183. );
  184. $rel_table = $base_table . '_relationship';
  185. $schema = chado_get_schema($rel_table);
  186. if (array_key_exists('rank', $schema['fields'])) {
  187. $options['order_by'] = array('rank' => 'ASC');
  188. }
  189. $record = chado_expand_var($record, 'table', $rel_table, $options);
  190. if (!$record->$rel_table) {
  191. return;
  192. }
  193. $srelationships = $record->$rel_table->subject_id;
  194. $orelationships = $record->$rel_table->object_id;
  195. $i = 0;
  196. if ($orelationships) {
  197. foreach ($orelationships as $relationship) {
  198. $rel_acc = $relationship->type_id->dbxref_id->db_id->name . ':' . $relationship->type_id->dbxref_id->accession;
  199. $rel_type = $relationship->type_id->name;
  200. $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
  201. $verb = $this->get_rel_verb($rel_type_clean);
  202. $subject_name = $relationship->subject_id->name;
  203. $subject_type = $relationship->subject_id->type_id->name;
  204. $object_name = $relationship->object_id->name;
  205. $object_type = $relationship->object_id->type_id->name;
  206. $entity->{$field_name}['und'][$i]['value'] = array(
  207. '@type' => $rel_acc,
  208. 'subject' => array(
  209. '@type' => $relationship->subject_id->type_id->dbxref_id->db_id->name .
  210. ':' . $relationship->subject_id->type_id->dbxref_id->accession,
  211. 'type' => $subject_type,
  212. 'name' => $subject_name,
  213. ),
  214. 'type' => $relationship->type_id->name,
  215. 'object' => array(
  216. '@type' => $relationship->object_id->type_id->dbxref_id->db_id->name .
  217. ':' . $relationship->object_id->type_id->dbxref_id->accession,
  218. // TODO the entity_id should not be here.... wherre to put it.
  219. 'entity_id' => $entity->id,
  220. 'entity_type' => 'TripalEntity',
  221. 'type' => $object_type,
  222. 'name' => $object_name,
  223. )
  224. );
  225. if (property_exists($relationship->subject_id, 'uniquename')) {
  226. $subject_name = $relationship->subject_id->uniquename;
  227. $entity->{$field_name}['und'][$i]['value']['subject']['name'] = $subject_name;
  228. }
  229. if (property_exists($relationship->object_id, 'uniquename')) {
  230. $object_name = $relationship->object_id->uniquename;
  231. $entity->{$field_name}['und'][$i]['value']['object']['name'] = $object_name;
  232. }
  233. if (property_exists($relationship->subject_id, 'entity_id')) {
  234. $entity_id = $relationship->subject_id->entity_id;
  235. $entity->{$field_name}['und'][$i]['value']['subject']['entity_id'] = $entity_id;
  236. $entity->{$field_name}['und'][$i]['value']['subject']['entity_type'] = 'TripalEntity';
  237. }
  238. $entity->{$field_name}['und'][$i]['value']['phrase'] = 'The ' . $subject_type . ', ' .
  239. $subject_name . ', ' . $verb . ' ' . $rel_type_clean . ' this ' .
  240. $object_type . '.';
  241. $i++;
  242. }
  243. }
  244. if ($srelationships) {
  245. foreach ($srelationships as $relationship) {
  246. $rel_acc = $relationship->type_id->dbxref_id->db_id->name . ':' . $relationship->type_id->dbxref_id->accession;
  247. $rel_type = $relationship->type_id->name;
  248. $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
  249. $verb = $this->get_rel_verb($rel_type_clean);
  250. $subject_name = $relationship->subject_id->name;
  251. $subject_type = $relationship->subject_id->type_id->name;
  252. $object_name = $relationship->object_id->name;
  253. $object_type = $relationship->object_id->type_id->name;
  254. $entity->{$field_name}['und'][$i]['value'] = array(
  255. '@type' => $rel_acc,
  256. 'subject' => array(
  257. '@type' => $relationship->subject_id->type_id->dbxref_id->db_id->name .
  258. ':' . $relationship->subject_id->type_id->dbxref_id->accession,
  259. 'type' => $subject_type,
  260. 'name' => $subject_name,
  261. 'entity_id' => $entity->id,
  262. 'entity_type' => 'TripalEntity',
  263. ),
  264. 'type' => $relationship->type_id->name,
  265. 'object' => array(
  266. '@type' => $relationship->object_id->type_id->dbxref_id->db_id->name .
  267. ':' . $relationship->object_id->type_id->dbxref_id->accession,
  268. 'type' => $object_type,
  269. 'name' => $object_name,
  270. )
  271. );
  272. if (property_exists($relationship->subject_id, 'uniquename')) {
  273. $subject_name = $relationship->subject_id->uniquename;
  274. $entity->{$field_name}['und'][$i]['value']['subject']['name'] = $subject_name;
  275. }
  276. if (property_exists($relationship->object_id, 'uniquename')) {
  277. $object_name = $relationship->object_id->uniquename;
  278. $entity->{$field_name}['und'][$i]['value']['object']['name'] = $object_name;
  279. }
  280. if (property_exists($relationship->object_id, 'entity_id')) {
  281. $entity_id = $relationship->object_id->entity_id;
  282. $entity->{$field_name}['und'][$i]['value']['object']['entity_id'] = $entity_id;
  283. $entity->{$field_name}['und'][$i]['value']['object']['entity_type'] = 'TripalEntity';
  284. }
  285. $entity->{$field_name}['und'][$i]['value']['phrase'] = 'This ' .
  286. $subject_type . ' ' . $verb . ' ' . $rel_type_clean . ' the ' .
  287. $object_type . ', ' . $object_name . '.';
  288. $i++;
  289. }
  290. }
  291. }
  292. private function get_rel_verb($rel_type) {
  293. $verb = '';
  294. switch ($rel_type) {
  295. case 'integral part of':
  296. case 'instance of':
  297. $verb = 'is an';
  298. break;
  299. case 'proper part of':
  300. case 'transformation of':
  301. case 'genome of':
  302. case 'part of':
  303. $verb = 'is a';
  304. case 'position of':
  305. case 'sequence of':
  306. case 'variant of':
  307. $verb = 'is a';
  308. break;
  309. case 'derives from':
  310. case 'connects on':
  311. case 'contains':
  312. case 'finishes':
  313. case 'guides':
  314. case 'has origin':
  315. case 'has part':
  316. case 'has quality':
  317. case 'is consecutive sequence of':
  318. case 'maximally overlaps':
  319. case 'overlaps':
  320. case 'starts':
  321. $verb = '';
  322. break;
  323. default:
  324. $verb = 'is';
  325. }
  326. return $verb;
  327. }
  328. }