chado_linker__relationship.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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' => 'SBO:0000374',
  53. ),
  54. );
  55. return $field_info;
  56. }
  57. /**
  58. * @see TripalField::widget_info()
  59. */
  60. function widget_info() {
  61. return array(
  62. 'label' => t('Relationship Settings'),
  63. 'field types' => array('chado_linker__relationship')
  64. );
  65. }
  66. /**
  67. * @see TripalField::formatter_info()
  68. */
  69. function formatter_info() {
  70. return array(
  71. 'label' => t('Relationships'),
  72. 'field types' => array('chado_linker__relationship'),
  73. 'settings' => array(
  74. ),
  75. );
  76. }
  77. /**
  78. * @see TripalField::formatter_view()
  79. */
  80. function formatter_view(&$element, $entity_type, $entity,
  81. $field, $instance, $langcode, $items, $display) {
  82. // Get the settings
  83. $settings = $display['settings'];
  84. $rows = array();
  85. $headers = array('Subject' ,'Type', 'Object');
  86. $headers = array('Relationship');
  87. foreach ($items as $delta => $item) {
  88. if (!$item['value']) {
  89. continue;
  90. }
  91. $subject_name = $item['value']['subject']['name'];
  92. $subject_type = $item['value']['subject']['type'];
  93. $object_name = $item['value']['object']['name'];
  94. $object_type = $item['value']['object']['type'];
  95. $phrase = $item['value']['phrase'];
  96. // Handle some special cases.
  97. // For mRNA objects we don't want to show the CDS, exons, 5' UTR, etc.
  98. // we want to show the parent gene and the protein.
  99. if ($object_type == 'mRNA' and ($subject_type != 'polypeptide')) {
  100. continue;
  101. }
  102. if ($subject_type == 'mRNA' and ($object_type != 'gene')) {
  103. continue;
  104. }
  105. $phrase = preg_replace("/$subject_type/", "<b>$subject_type</b>", $phrase);
  106. $phrase = preg_replace("/$object_type/", "<b>$object_type</b>", $phrase);
  107. if (array_key_exists('entity_id', $item['value']['object'])) {
  108. $object_entity_id = $item['value']['object']['entity_id'];
  109. if ($object_entity_id != $entity->id) {
  110. $link = l($object_name, 'bio_data/' . $object_entity_id);
  111. $phrase = preg_replace("/$object_name/", $link, $phrase);
  112. }
  113. }
  114. if (array_key_exists('entity_id', $item['value']['subject'])) {
  115. $subject_entity_id = $item['value']['subject']['entity_id'];
  116. if ($subject_entity_id != $entity->id) {
  117. $link = l($subject_name, 'bio_data/' . $subject_entity_id);
  118. $phrase = preg_replace("/$subject_name/", $link, $phrase);
  119. }
  120. }
  121. $rows[] = array($phrase);
  122. }
  123. // the $table array contains the headers and rows array as well as other
  124. // options for controlling the display of the table. Additional
  125. // documentation can be found here:
  126. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  127. $table = array(
  128. 'header' => $headers,
  129. 'rows' => $rows,
  130. 'attributes' => array(
  131. 'id' => 'chado-linker--relationship-table',
  132. 'class' => 'tripal-data-table'
  133. ),
  134. 'sticky' => FALSE,
  135. 'caption' => '',
  136. 'colgroups' => array(),
  137. 'empty' => 'There are no relationships',
  138. );
  139. // once we have our table array structure defined, we call Drupal's theme_table()
  140. // function to generate the table.
  141. $element[$delta] = array(
  142. '#type' => 'markup',
  143. '#markup' => theme_table($table),
  144. );
  145. }
  146. /**
  147. * @see TripalField::widget_form()
  148. */
  149. function widget_form(&$widget, $form, $form_state, $field,
  150. $instance, $langcode, $items, $delta, $element) {
  151. $entity = $form['#entity'];
  152. $field_name = $field['field_name'];
  153. // Get the FK column that links to the base table.
  154. $table_name = $field['settings']['chado_table'];
  155. $base_table = $field['settings']['base_table'];
  156. $schema = chado_get_schema($table_name);
  157. $pkey = $schema['primary key'][0];
  158. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  159. $fkey = $fkeys[0];
  160. // Get the field defaults.
  161. $record_id = '';
  162. $fkey_value = $element['#entity']->chado_record_id;
  163. $subject_id = '';
  164. $type_id = '';
  165. $object_id = '';
  166. $value = '';
  167. $rank = '';
  168. // If the field already has a value then it will come through the $items
  169. // array. This happens when editing an existing record.
  170. if (array_key_exists($delta, $items)) {
  171. $record_id = $items[$delta][$table_name . '__' . $pkey];
  172. $subject_id = $items[$delta][$table_name . '__subject_id'];
  173. $type_id = $items[$delta][$table_name . '__type_id'];
  174. $object_id = $items[$delta][$table_name . '__object_id'];
  175. $value = $items[$delta][$table_name . '__value'];
  176. $rank = $items[$delta][$table_name . '__rank'];
  177. }
  178. // Check $form_state['values'] to see if an AJAX call set the values.
  179. if (array_key_exists('values', $form_state) and array_key_exists($delta, $form_state['values'])) {
  180. $record_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__' . $pkey);
  181. $subject_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__subject_id');
  182. $type_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__type_id');
  183. $object_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__object_id');
  184. $value = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__value');
  185. $rank = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__rank');
  186. }
  187. $widget['#table_name'] = $table_name;
  188. $widget['#fkey_field'] = $fkey;
  189. // $widget['#element_validate'] = array('chado_linker__relationship_validate');
  190. // $widget['#theme'] = 'chado_linker__relationship_widget';
  191. $widget['#prefix'] = "<span id='$table_name-$delta'>";
  192. $widget['#suffix'] = "</span>";
  193. $widget['value'] = array(
  194. '#type' => 'value',
  195. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  196. );
  197. $widget[$table_name . '__' . $pkey] = array(
  198. '#type' => 'value',
  199. '#default_value' => $record_id,
  200. );
  201. // TODO: for now just add all fields as values, eventually we need
  202. // a form for specifying relationships.
  203. $widget[$table_name . '__subject_id'] = array(
  204. '#type' => 'value',
  205. '#default_value' => $subject_id,
  206. );
  207. $widget[$table_name . '__type_id'] = array(
  208. '#type' => 'value',
  209. '#default_value' => $type_id,
  210. );
  211. $widget[$table_name . '__object_id'] = array(
  212. '#type' => 'value',
  213. '#default_value' => $object_id,
  214. );
  215. if (array_key_exists('value', $schema['fields'])) {
  216. $widget[$table_name . '__value'] = array(
  217. '#type' => 'value',
  218. '#default_value' => $value,
  219. );
  220. }
  221. if (array_key_exists('rank', $schema['fields'])) {
  222. $widget[$table_name . '__rank'] = array(
  223. '#type' => 'value',
  224. '#default_value' => $rank,
  225. );
  226. }
  227. }
  228. /**
  229. * @see TripalField::load()
  230. */
  231. function load($field, $entity, $details) {
  232. $settings = $field['settings'];
  233. $record = $details['record'];
  234. $field_name = $field['field_name'];
  235. $field_type = $field['type'];
  236. $field_table = $field['settings']['chado_table'];
  237. $field_column = $field['settings']['chado_column'];
  238. $base_table = $field['settings']['base_table'];
  239. // Get the PKey for this table
  240. $schema = chado_get_schema($field_table);
  241. $pkey = $schema['primary key'][0];
  242. // Get the Pkeys for the subject and object tables
  243. $subject_fkey_table = '';
  244. $object_fkey_table = '';
  245. $fkeys = $schema['foreign keys'];
  246. foreach ($fkeys as $fktable => $details) {
  247. foreach ($details['columns'] as $fkey_lcolumn => $fkey_rcolumn) {
  248. if ($fkey_lcolumn == 'subject_id') {
  249. $subject_fkey_table = $fktable;
  250. }
  251. if ($fkey_lcolumn == 'object_id') {
  252. $object_fkey_table = $fktable;
  253. }
  254. }
  255. }
  256. $subject_schema = chado_get_schema($subject_fkey_table);
  257. $object_schema = chado_get_schema($object_fkey_table);
  258. $subject_pkey = $subject_schema['primary key'][0];
  259. $object_pkey = $object_schema['primary key'][0];
  260. // Get the FK that links to the base record.
  261. $schema = chado_get_schema($field_table);
  262. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  263. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  264. // Set some defaults for the empty record.
  265. $entity->{$field_name}['und'][0] = array(
  266. 'value' => array(),
  267. $field_table . '__' . $pkey => '',
  268. $field_table . '__subject_id' => '',
  269. $field_table . '__object_id' => '',
  270. $field_table . '__type_id' => TRUE,
  271. );
  272. if (array_key_exists('value', $schema['fields'])) {
  273. $entity->{$field_name}['und'][0][$field_table . '__value'] = '';
  274. }
  275. if (array_key_exists('rank', $schema['fields'])) {
  276. $entity->{$field_name}['und'][0][$field_table . '__rank'] = '';
  277. }
  278. if (!$record) {
  279. return;
  280. }
  281. // Expand the object to include the relationships.
  282. $options = array(
  283. 'return_array' => 1,
  284. // we don't want to fully recurse we only need information about the
  285. // relationship type and the object and subject
  286. 'include_fk' => array(
  287. 'type_id' => 1,
  288. 'object_id' => array(
  289. 'type_id' => 1,
  290. ),
  291. 'subject_id' => array(
  292. 'type_id' => 1,
  293. ),
  294. ),
  295. );
  296. $rel_table = $base_table . '_relationship';
  297. $schema = chado_get_schema($rel_table);
  298. if (array_key_exists('rank', $schema['fields'])) {
  299. $options['order_by'] = array('rank' => 'ASC');
  300. }
  301. $record = chado_expand_var($record, 'table', $rel_table, $options);
  302. if (!$record->$rel_table) {
  303. return;
  304. }
  305. $srelationships = $record->$rel_table->subject_id;
  306. $orelationships = $record->$rel_table->object_id;
  307. $i = 0;
  308. if ($orelationships) {
  309. foreach ($orelationships as $relationship) {
  310. $rel_acc = $relationship->type_id->dbxref_id->db_id->name . ':' . $relationship->type_id->dbxref_id->accession;
  311. $rel_type = $relationship->type_id->name;
  312. $verb = $this->get_rel_verb($rel_type);
  313. $subject_name = $relationship->subject_id->name;
  314. $subject_type = $relationship->subject_id->type_id->name;
  315. $object_name = $relationship->object_id->name;
  316. $object_type = $relationship->object_id->type_id->name;
  317. $entity->{$field_name}['und'][$i]['value'] = array(
  318. '@type' => $rel_acc,
  319. 'subject' => array(
  320. '@type' => $relationship->subject_id->type_id->dbxref_id->db_id->name .
  321. ':' . $relationship->subject_id->type_id->dbxref_id->accession,
  322. 'type' => $subject_type,
  323. 'name' => $subject_name,
  324. ),
  325. 'type' => $relationship->type_id->name,
  326. 'object' => array(
  327. '@type' => $relationship->object_id->type_id->dbxref_id->db_id->name .
  328. ':' . $relationship->object_id->type_id->dbxref_id->accession,
  329. // TODO the entity_id should not be here.... wherre to put it.
  330. 'entity_id' => $entity->id,
  331. 'entity_type' => 'TripalEntity',
  332. 'type' => $object_type,
  333. 'name' => $object_name,
  334. )
  335. );
  336. if (property_exists($relationship->subject_id, 'uniquename')) {
  337. $subject_name = $relationship->subject_id->uniquename;
  338. $entity->{$field_name}['und'][$i]['value']['subject']['name'] = $subject_name;
  339. }
  340. if (property_exists($relationship->object_id, 'uniquename')) {
  341. $object_name = $relationship->object_id->uniquename;
  342. $entity->{$field_name}['und'][$i]['value']['object']['name'] = $object_name;
  343. }
  344. if (property_exists($relationship->subject_id, 'entity_id')) {
  345. $entity_id = $relationship->subject_id->entity_id;
  346. $entity->{$field_name}['und'][$i]['value']['subject']['entity_id'] = $entity_id;
  347. $entity->{$field_name}['und'][$i]['value']['subject']['entity_type'] = 'TripalEntity';
  348. }
  349. $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
  350. $entity->{$field_name}['und'][$i]['value']['phrase'] = 'The ' . $subject_type . ', ' .
  351. $subject_name . ', ' . $verb . ' ' . $rel_type_clean . ' this ' .
  352. $object_type . '.';
  353. $entity->{$field_name}['und'][$i][$field_table . '__' . $pkey] = $relationship->$pkey;
  354. $entity->{$field_name}['und'][$i][$field_table . '__subject_id'] = $relationship->subject_id->$subject_pkey;
  355. $entity->{$field_name}['und'][$i][$field_table . '__type_id'] = $relationship->type_id->cvterm_id;
  356. $entity->{$field_name}['und'][$i][$field_table . '__object_id'] = $relationship->object_id->$object_pkey;
  357. if (array_key_exists('value', $schema['fields'])) {
  358. $entity->{$field_name}['und'][$i][$field_table . '__value'] = $relationship->value;
  359. }
  360. if (array_key_exists('rank', $schema['fields'])) {
  361. $entity->{$field_name}['und'][$i][$field_table . '__rank'] = $relationship->rank;
  362. }
  363. $i++;
  364. }
  365. }
  366. if ($srelationships) {
  367. foreach ($srelationships as $relationship) {
  368. $rel_acc = $relationship->type_id->dbxref_id->db_id->name . ':' . $relationship->type_id->dbxref_id->accession;
  369. $rel_type = $relationship->type_id->name;
  370. $verb = $this->get_rel_verb($rel_type);
  371. $subject_name = $relationship->subject_id->name;
  372. $subject_type = $relationship->subject_id->type_id->name;
  373. $object_name = $relationship->object_id->name;
  374. $object_type = $relationship->object_id->type_id->name;
  375. $entity->{$field_name}['und'][$i]['value'] = array(
  376. '@type' => $rel_acc,
  377. 'subject' => array(
  378. '@type' => $relationship->subject_id->type_id->dbxref_id->db_id->name .
  379. ':' . $relationship->subject_id->type_id->dbxref_id->accession,
  380. 'type' => $subject_type,
  381. 'name' => $subject_name,
  382. 'entity_id' => $entity->id,
  383. 'entity_type' => 'TripalEntity',
  384. ),
  385. 'type' => $relationship->type_id->name,
  386. 'object' => array(
  387. '@type' => $relationship->object_id->type_id->dbxref_id->db_id->name .
  388. ':' . $relationship->object_id->type_id->dbxref_id->accession,
  389. 'type' => $object_type,
  390. 'name' => $object_name,
  391. )
  392. );
  393. if (property_exists($relationship->subject_id, 'uniquename')) {
  394. $subject_name = $relationship->subject_id->uniquename;
  395. $entity->{$field_name}['und'][$i]['value']['subject']['name'] = $subject_name;
  396. }
  397. if (property_exists($relationship->object_id, 'uniquename')) {
  398. $object_name = $relationship->object_id->uniquename;
  399. $entity->{$field_name}['und'][$i]['value']['object']['name'] = $object_name;
  400. }
  401. if (property_exists($relationship->object_id, 'entity_id')) {
  402. $entity_id = $relationship->object_id->entity_id;
  403. $entity->{$field_name}['und'][$i]['value']['object']['entity_id'] = $entity_id;
  404. $entity->{$field_name}['und'][$i]['value']['object']['entity_type'] = 'TripalEntity';
  405. }
  406. $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
  407. $entity->{$field_name}['und'][$i]['value']['phrase'] = 'This ' .
  408. $subject_type . ' ' . $verb . ' ' . $rel_type_clean . ' the ' .
  409. $object_type . ', ' . $object_name . '.';
  410. $entity->{$field_name}['und'][$i][$field_table . '__' . $pkey] = $relationship->$pkey;
  411. $entity->{$field_name}['und'][$i][$field_table . '__subject_id'] = $relationship->subject_id->$subject_pkey;
  412. $entity->{$field_name}['und'][$i][$field_table . '__type_id'] = $relationship->type_id->cvterm_id;
  413. $entity->{$field_name}['und'][$i][$field_table . '__object_id'] = $relationship->object_id->$object_pkey;
  414. if (array_key_exists('value', $schema['fields'])) {
  415. $entity->{$field_name}['und'][$i][$field_table . '__value'] = $relationship->value;
  416. }
  417. if (array_key_exists('rank', $schema['fields'])) {
  418. $entity->{$field_name}['und'][$i][$field_table . '__rank'] = $relationship->rank;
  419. }
  420. $i++;
  421. }
  422. }
  423. }
  424. /**
  425. * A helper function to define English verbs for relationship types.
  426. *
  427. * @param $rel_type
  428. * The vocabulary term name for the relationship.
  429. *
  430. * @return
  431. * The verb to use when creating a sentence of the relationship.
  432. */
  433. private function get_rel_verb($rel_type) {
  434. $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
  435. $verb = $rel_type_clean;
  436. switch ($rel_type_clean) {
  437. case 'integral part of':
  438. case 'instance of':
  439. $verb = 'is an';
  440. break;
  441. case 'proper part of':
  442. case 'transformation of':
  443. case 'genome of':
  444. case 'part of':
  445. $verb = 'is a';
  446. case 'position of':
  447. case 'sequence of':
  448. case 'variant of':
  449. $verb = 'is a';
  450. break;
  451. case 'derives from':
  452. case 'connects on':
  453. case 'contains':
  454. case 'finishes':
  455. case 'guides':
  456. case 'has origin':
  457. case 'has part':
  458. case 'has quality':
  459. case 'is consecutive sequence of':
  460. case 'maximally overlaps':
  461. case 'overlaps':
  462. case 'starts':
  463. break;
  464. default:
  465. $verb = 'is';
  466. }
  467. return $verb;
  468. }
  469. }