chado_linker__relationship.inc 13 KB

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