chado_linker__relationship.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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' => 'chado-linker--relationship-table',
  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::widget_form()
  153. */
  154. function widget_form(&$widget, $form, $form_state, $field,
  155. $instance, $langcode, $items, $delta, $element) {
  156. $entity = $form['#entity'];
  157. $field_name = $field['field_name'];
  158. // Get the FK column that links to the base table.
  159. $table_name = $field['settings']['chado_table'];
  160. $base_table = $field['settings']['base_table'];
  161. $schema = chado_get_schema($table_name);
  162. $pkey = $schema['primary key'][0];
  163. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  164. $fkey = $fkeys[0];
  165. // Get the field defaults.
  166. $record_id = '';
  167. $fkey_value = $element['#entity']->chado_record_id;
  168. $subject_id = '';
  169. $type_id = '';
  170. $object_id = '';
  171. $value = '';
  172. $rank = '';
  173. // If the field already has a value then it will come through the $items
  174. // array. This happens when editing an existing record.
  175. if (array_key_exists($delta, $items)) {
  176. $record_id = $items[$delta][$table_name . '__' . $pkey];
  177. $subject_id = $items[$delta][$table_name . '__subject_id'];
  178. $type_id = $items[$delta][$table_name . '__type_id'];
  179. $object_id = $items[$delta][$table_name . '__object_id'];
  180. $value = $items[$delta][$table_name . '__value'];
  181. $rank = $items[$delta][$table_name . '__rank'];
  182. }
  183. // Check $form_state['values'] to see if an AJAX call set the values.
  184. if (array_key_exists('values', $form_state) and array_key_exists($delta, $form_state['values'])) {
  185. $record_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__' . $pkey);
  186. $subject_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__subject_id');
  187. $type_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__type_id');
  188. $object_id = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__object_id');
  189. $value = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__value');
  190. $rank = tripal_chado_get_field_form_values($table_name, $form_state, $delta, $table_name . '__rank');
  191. }
  192. $widget['#table_name'] = $table_name;
  193. $widget['#fkey_field'] = $fkey;
  194. // $widget['#element_validate'] = array('chado_linker__relationship_validate');
  195. // $widget['#theme'] = 'chado_linker__relationship_widget';
  196. $widget['#prefix'] = "<span id='$table_name-$delta'>";
  197. $widget['#suffix'] = "</span>";
  198. $widget['value'] = array(
  199. '#type' => 'value',
  200. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  201. );
  202. $widget[$table_name . '__' . $pkey] = array(
  203. '#type' => 'value',
  204. '#default_value' => $record_id,
  205. );
  206. // TODO: for now just add all fields as values, eventually we need
  207. // a form for specifying relationships.
  208. $widget[$table_name . '__subject_id'] = array(
  209. '#type' => 'value',
  210. '#default_value' => $subject_id,
  211. );
  212. $widget[$table_name . '__type_id'] = array(
  213. '#type' => 'value',
  214. '#default_value' => $type_id,
  215. );
  216. $widget[$table_name . '__object_id'] = array(
  217. '#type' => 'value',
  218. '#default_value' => $object_id,
  219. );
  220. if (array_key_exists('value', $schema['fields'])) {
  221. $widget[$table_name . '__value'] = array(
  222. '#type' => 'value',
  223. '#default_value' => $value,
  224. );
  225. }
  226. if (array_key_exists('rank', $schema['fields'])) {
  227. $widget[$table_name . '__rank'] = array(
  228. '#type' => 'value',
  229. '#default_value' => $rank,
  230. );
  231. }
  232. }
  233. /**
  234. * @see TripalField::load()
  235. */
  236. function load($field, $entity, $details) {
  237. $settings = $field['settings'];
  238. $record = $details['record'];
  239. $field_name = $field['field_name'];
  240. $field_type = $field['type'];
  241. $field_table = $field['settings']['chado_table'];
  242. $field_column = $field['settings']['chado_column'];
  243. $base_table = $field['settings']['base_table'];
  244. // Get the PKey for this table
  245. $schema = chado_get_schema($field_table);
  246. $pkey = $schema['primary key'][0];
  247. // Get the Pkeys for the subject and object tables
  248. $subject_fkey_table = '';
  249. $object_fkey_table = '';
  250. $fkeys = $schema['foreign keys'];
  251. foreach ($fkeys as $fktable => $details) {
  252. foreach ($details['columns'] as $fkey_lcolumn => $fkey_rcolumn) {
  253. if ($fkey_lcolumn == 'subject_id') {
  254. $subject_fkey_table = $fktable;
  255. }
  256. if ($fkey_lcolumn == 'object_id') {
  257. $object_fkey_table = $fktable;
  258. }
  259. }
  260. }
  261. $subject_schema = chado_get_schema($subject_fkey_table);
  262. $object_schema = chado_get_schema($object_fkey_table);
  263. $subject_pkey = $subject_schema['primary key'][0];
  264. $object_pkey = $object_schema['primary key'][0];
  265. // Get the FK that links to the base record.
  266. $schema = chado_get_schema($field_table);
  267. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  268. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  269. // Set some defaults for the empty record.
  270. $entity->{$field_name}['und'][0] = array(
  271. 'value' => array(),
  272. $field_table . '__' . $pkey => '',
  273. $field_table . '__subject_id' => '',
  274. $field_table . '__object_id' => '',
  275. $field_table . '__type_id' => TRUE,
  276. );
  277. if (array_key_exists('value', $schema['fields'])) {
  278. $entity->{$field_name}['und'][0][$field_table . '__value'] = '';
  279. }
  280. if (array_key_exists('rank', $schema['fields'])) {
  281. $entity->{$field_name}['und'][0][$field_table . '__rank'] = '';
  282. }
  283. if (!$record) {
  284. return;
  285. }
  286. // Expand the object to include the relationships.
  287. $options = array(
  288. 'return_array' => 1,
  289. // we don't want to fully recurse we only need information about the
  290. // relationship type and the object and subject
  291. 'include_fk' => array(
  292. 'type_id' => 1,
  293. 'object_id' => array(
  294. 'type_id' => 1,
  295. ),
  296. 'subject_id' => array(
  297. 'type_id' => 1,
  298. ),
  299. ),
  300. );
  301. $rel_table = $base_table . '_relationship';
  302. $schema = chado_get_schema($rel_table);
  303. if (array_key_exists('rank', $schema['fields'])) {
  304. $options['order_by'] = array('rank' => 'ASC');
  305. }
  306. $record = chado_expand_var($record, 'table', $rel_table, $options);
  307. if (!$record->$rel_table) {
  308. return;
  309. }
  310. $srelationships = $record->$rel_table->subject_id;
  311. $orelationships = $record->$rel_table->object_id;
  312. $i = 0;
  313. if ($orelationships) {
  314. foreach ($orelationships as $relationship) {
  315. $rel_acc = $relationship->type_id->dbxref_id->db_id->name . ':' . $relationship->type_id->dbxref_id->accession;
  316. $rel_type = $relationship->type_id->name;
  317. $verb = $this->get_rel_verb($rel_type);
  318. $subject_name = $relationship->subject_id->name;
  319. $subject_type = $relationship->subject_id->type_id->name;
  320. $object_name = $relationship->object_id->name;
  321. $object_type = $relationship->object_id->type_id->name;
  322. $entity->{$field_name}['und'][$i]['value'] = array(
  323. '@type' => $rel_acc,
  324. 'subject' => array(
  325. '@type' => $relationship->subject_id->type_id->dbxref_id->db_id->name .
  326. ':' . $relationship->subject_id->type_id->dbxref_id->accession,
  327. 'type' => $subject_type,
  328. 'name' => $subject_name,
  329. ),
  330. 'type' => $relationship->type_id->name,
  331. 'object' => array(
  332. '@type' => $relationship->object_id->type_id->dbxref_id->db_id->name .
  333. ':' . $relationship->object_id->type_id->dbxref_id->accession,
  334. // TODO the entity_id should not be here.... wherre to put it.
  335. 'entity_id' => $entity->id,
  336. 'entity_type' => 'TripalEntity',
  337. 'type' => $object_type,
  338. 'name' => $object_name,
  339. )
  340. );
  341. if (property_exists($relationship->subject_id, 'uniquename')) {
  342. $subject_name = $relationship->subject_id->uniquename;
  343. $entity->{$field_name}['und'][$i]['value']['subject']['name'] = $subject_name;
  344. }
  345. if (property_exists($relationship->object_id, 'uniquename')) {
  346. $object_name = $relationship->object_id->uniquename;
  347. $entity->{$field_name}['und'][$i]['value']['object']['name'] = $object_name;
  348. }
  349. if (property_exists($relationship->subject_id, 'entity_id')) {
  350. $entity_id = $relationship->subject_id->entity_id;
  351. $entity->{$field_name}['und'][$i]['value']['subject']['entity_id'] = $entity_id;
  352. $entity->{$field_name}['und'][$i]['value']['subject']['entity_type'] = 'TripalEntity';
  353. }
  354. $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
  355. $entity->{$field_name}['und'][$i]['value']['phrase'] = 'The ' . $subject_type . ', ' .
  356. $subject_name . ', ' . $verb . ' ' . $rel_type_clean . ' this ' .
  357. $object_type . '.';
  358. $entity->{$field_name}['und'][$i][$field_table . '__' . $pkey] = $relationship->$pkey;
  359. $entity->{$field_name}['und'][$i][$field_table . '__subject_id'] = $relationship->subject_id->$subject_pkey;
  360. $entity->{$field_name}['und'][$i][$field_table . '__type_id'] = $relationship->type_id->cvterm_id;
  361. $entity->{$field_name}['und'][$i][$field_table . '__object_id'] = $relationship->object_id->$object_pkey;
  362. if (array_key_exists('value', $schema['fields'])) {
  363. $entity->{$field_name}['und'][$i][$field_table . '__value'] = $relationship->value;
  364. }
  365. if (array_key_exists('rank', $schema['fields'])) {
  366. $entity->{$field_name}['und'][$i][$field_table . '__rank'] = $relationship->rank;
  367. }
  368. $i++;
  369. }
  370. }
  371. if ($srelationships) {
  372. foreach ($srelationships as $relationship) {
  373. $rel_acc = $relationship->type_id->dbxref_id->db_id->name . ':' . $relationship->type_id->dbxref_id->accession;
  374. $rel_type = $relationship->type_id->name;
  375. $verb = $this->get_rel_verb($rel_type);
  376. $subject_name = $relationship->subject_id->name;
  377. $subject_type = $relationship->subject_id->type_id->name;
  378. $object_name = $relationship->object_id->name;
  379. $object_type = $relationship->object_id->type_id->name;
  380. $entity->{$field_name}['und'][$i]['value'] = array(
  381. '@type' => $rel_acc,
  382. 'subject' => array(
  383. '@type' => $relationship->subject_id->type_id->dbxref_id->db_id->name .
  384. ':' . $relationship->subject_id->type_id->dbxref_id->accession,
  385. 'type' => $subject_type,
  386. 'name' => $subject_name,
  387. 'entity_id' => $entity->id,
  388. 'entity_type' => 'TripalEntity',
  389. ),
  390. 'type' => $relationship->type_id->name,
  391. 'object' => array(
  392. '@type' => $relationship->object_id->type_id->dbxref_id->db_id->name .
  393. ':' . $relationship->object_id->type_id->dbxref_id->accession,
  394. 'type' => $object_type,
  395. 'name' => $object_name,
  396. )
  397. );
  398. if (property_exists($relationship->subject_id, 'uniquename')) {
  399. $subject_name = $relationship->subject_id->uniquename;
  400. $entity->{$field_name}['und'][$i]['value']['subject']['name'] = $subject_name;
  401. }
  402. if (property_exists($relationship->object_id, 'uniquename')) {
  403. $object_name = $relationship->object_id->uniquename;
  404. $entity->{$field_name}['und'][$i]['value']['object']['name'] = $object_name;
  405. }
  406. if (property_exists($relationship->object_id, 'entity_id')) {
  407. $entity_id = $relationship->object_id->entity_id;
  408. $entity->{$field_name}['und'][$i]['value']['object']['entity_id'] = $entity_id;
  409. $entity->{$field_name}['und'][$i]['value']['object']['entity_type'] = 'TripalEntity';
  410. }
  411. $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
  412. $entity->{$field_name}['und'][$i]['value']['phrase'] = 'This ' .
  413. $subject_type . ' ' . $verb . ' ' . $rel_type_clean . ' the ' .
  414. $object_type . ', ' . $object_name . '.';
  415. $entity->{$field_name}['und'][$i][$field_table . '__' . $pkey] = $relationship->$pkey;
  416. $entity->{$field_name}['und'][$i][$field_table . '__subject_id'] = $relationship->subject_id->$subject_pkey;
  417. $entity->{$field_name}['und'][$i][$field_table . '__type_id'] = $relationship->type_id->cvterm_id;
  418. $entity->{$field_name}['und'][$i][$field_table . '__object_id'] = $relationship->object_id->$object_pkey;
  419. if (array_key_exists('value', $schema['fields'])) {
  420. $entity->{$field_name}['und'][$i][$field_table . '__value'] = $relationship->value;
  421. }
  422. if (array_key_exists('rank', $schema['fields'])) {
  423. $entity->{$field_name}['und'][$i][$field_table . '__rank'] = $relationship->rank;
  424. }
  425. $i++;
  426. }
  427. }
  428. }
  429. /**
  430. * A helper function to define English verbs for relationship types.
  431. *
  432. * @param $rel_type
  433. * The vocabulary term name for the relationship.
  434. *
  435. * @return
  436. * The verb to use when creating a sentence of the relationship.
  437. */
  438. private function get_rel_verb($rel_type) {
  439. $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
  440. $verb = $rel_type_clean;
  441. switch ($rel_type_clean) {
  442. case 'integral part of':
  443. case 'instance of':
  444. $verb = 'is an';
  445. break;
  446. case 'proper part of':
  447. case 'transformation of':
  448. case 'genome of':
  449. case 'part of':
  450. $verb = 'is a';
  451. case 'position of':
  452. case 'sequence of':
  453. case 'variant of':
  454. $verb = 'is a';
  455. break;
  456. case 'derives from':
  457. case 'connects on':
  458. case 'contains':
  459. case 'finishes':
  460. case 'guides':
  461. case 'has origin':
  462. case 'has part':
  463. case 'has quality':
  464. case 'is consecutive sequence of':
  465. case 'maximally overlaps':
  466. case 'overlaps':
  467. case 'starts':
  468. break;
  469. default:
  470. $verb = 'is';
  471. }
  472. return $verb;
  473. }
  474. }