sbo__relationship.inc 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. <?php
  2. class sbo__relationship extends ChadoField {
  3. // --------------------------------------------------------------------------
  4. // EDITABLE STATIC CONSTANTS
  5. //
  6. // The following constants SHOULD be set for each descendent class. They are
  7. // used by the static functions to provide information to Drupal about
  8. // the field and it's default widget and formatter.
  9. // --------------------------------------------------------------------------
  10. // The default lable for this field.
  11. public static $default_label = 'Relationship';
  12. // The default description for this field.
  13. public static $description = 'Relationships between records.';
  14. // Provide a list of instance specific settings. These can be access within
  15. // the instanceSettingsForm. When the instanceSettingsForm is submitted
  16. // then Drupal with automatically change these settings for the instnace.
  17. // It is recommended to put settings at the instance level whenever possible.
  18. // If you override this variable in a child class be sure to replicate the
  19. // term_name, term_vocab, term_accession and term_fixed keys as these are
  20. // required for all TripalFields.
  21. public static $default_instance_settings = array(
  22. // The short name for the vocabulary (e.g. shcema, SO, GO, PATO, etc.).
  23. 'term_vocabulary' => 'SBO',
  24. // The name of the term.
  25. 'term_name' => 'Relationship',
  26. // The unique ID (i.e. accession) of the term.
  27. 'term_accession' => '0000374',
  28. // Set to TRUE if the site admin is allowed to change the term
  29. // type. This will create form elements when editing the field instance
  30. // to allow the site admin to change the term settings above.
  31. 'term_fixed' => FALSE,
  32. // Inidates if this field should be automatically attached to display
  33. // or web services or if this field should be loaded separately. This
  34. // is convenient for speed. Fields that are slow should for loading
  35. // should ahve auto_attach set to FALSE so tha their values can be
  36. // attached asyncronously.
  37. 'auto_attach' => FALSE,
  38. // Settings to help the site admin control how relationship types and
  39. // valid subject/objects can be selected by the user.
  40. 'relationships' => array(
  41. 'option1_vocabs' => '',
  42. 'option2_vocab' => '',
  43. 'option2_parent' => '',
  44. 'relationship_types' => '',
  45. ),
  46. // The number of items to show on a page.
  47. 'items_per_page' => 10,
  48. );
  49. // The default widget for this field.
  50. public static $default_widget = 'sbo__relationship_widget';
  51. // The default formatter for this field.
  52. public static $default_formatter = 'sbo__relationship_formatter';
  53. // --------------------------------------------------------------------------
  54. // PROTECTED CLASS MEMBERS -- DO NOT OVERRIDE
  55. // --------------------------------------------------------------------------
  56. // An array containing details about the field. The format of this array
  57. // is the same as that returned by field_info_fields()
  58. protected $field;
  59. // An array containing details about an instance of the field. A field does
  60. // not have to have an instance. But if dealing with an instance (such as
  61. // when using the widgetForm, formatterSettingsForm, etc.) it should be set.
  62. protected $instance;
  63. /**
  64. * @see TripalField::elements()
  65. */
  66. public function elementInfo() {
  67. $field_term = $this->getFieldTermID();
  68. return array(
  69. $field_term => array(
  70. 'operations' => array('eq', 'contains', 'starts'),
  71. 'sortable' => FALSE,
  72. 'searchable' => FALSE,
  73. 'type' => 'string',
  74. 'elements' => array(
  75. 'local:relationship_subject' => array(
  76. 'searchable' => FALSE,
  77. 'name' => 'relationship_subject',
  78. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  79. 'sortable' => FALSE,
  80. 'elements' => array(
  81. 'rdfs:type' => array(
  82. 'searchable' => TRUE,
  83. 'name' => 'subject_type',
  84. 'label' => 'Relationship Subject Type',
  85. 'help' => 'The subject\'s data type in a relationship clause',
  86. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  87. 'sortable' => TRUE,
  88. ),
  89. 'schema:name' => array(
  90. 'searchable' => TRUE,
  91. 'name' => 'subject_name',
  92. 'label' => 'Relationship Subject Name',
  93. 'help' => 'The subject\'s name in a relationship clause',
  94. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  95. 'sortable' => TRUE,
  96. ),
  97. 'entity' => array(
  98. 'searchable' => FALSE,
  99. 'sortable' => FALSE,
  100. )
  101. ),
  102. ),
  103. 'local:relationship_type' => array(
  104. 'searchable' => TRUE,
  105. 'name' => 'relationship_type',
  106. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  107. 'sortable' => TRUE,
  108. ),
  109. 'local:relationship_object' => array(
  110. 'searchable' => FALSE,
  111. 'name' => 'species',
  112. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  113. 'sortable' => FALSE,
  114. 'elements' => array(
  115. 'rdfs:type' => array(
  116. 'searchable' => TRUE,
  117. 'name' => 'object_type',
  118. 'label' => 'Relationship Object Type',
  119. 'help' => 'The objects\'s data type in a relationship clause',
  120. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  121. 'sortable' => TRUE,
  122. ),
  123. 'schema:name' => array(
  124. 'searchable' => TRUE,
  125. 'name' => 'object_name',
  126. 'label' => 'Relationship Object Name',
  127. 'help' => 'The objects\'s name in a relationship clause',
  128. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  129. 'sortable' => TRUE,
  130. ),
  131. 'entity' => array(
  132. 'searchable' => FALSE,
  133. 'sortable' => FALSE,
  134. )
  135. ),
  136. ),
  137. ),
  138. )
  139. );
  140. }
  141. /**
  142. *
  143. * @see TripalField::load()
  144. */
  145. public function load($entity) {
  146. $settings = $this->field['settings'];
  147. $record = $entity->chado_record;
  148. $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
  149. $field_name = $this->field['field_name'];
  150. $field_type = $this->field['type'];
  151. $field_table = $this->instance['settings']['chado_table'];
  152. $field_column = $this->instance['settings']['chado_column'];
  153. $base_table = $this->instance['settings']['base_table'];
  154. // Get the PKey for this table
  155. $schema = chado_get_schema($field_table);
  156. $pkey = $schema['primary key'][0];
  157. // Get the foreign keys for the subject and object tables
  158. $subject_fkey_table = '';
  159. $object_fkey_table = '';
  160. $fkeys = $schema['foreign keys'];
  161. $subject_id_key = 'subject_id';
  162. $object_id_key = 'object_id';
  163. foreach ($fkeys as $fktable => $details) {
  164. foreach ($details['columns'] as $fkey_lcolumn => $fkey_rcolumn) {
  165. if (preg_match('/^subject_.*id/', $fkey_lcolumn)) {
  166. $subject_fkey_table = $fktable;
  167. $subject_id_key = $fkey_lcolumn;
  168. }
  169. if (preg_match('/^object_.*id/', $fkey_lcolumn)) {
  170. $object_fkey_table = $fktable;
  171. $object_id_key = $fkey_lcolumn;
  172. }
  173. }
  174. }
  175. $subject_schema = chado_get_schema($subject_fkey_table);
  176. $object_schema = chado_get_schema($object_fkey_table);
  177. $subject_pkey = $subject_schema['primary key'][0];
  178. $object_pkey = $object_schema['primary key'][0];
  179. // Get the FK that links to the base record.
  180. $schema = chado_get_schema($field_table);
  181. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  182. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  183. // Set some defaults for the empty record.
  184. $entity->{$field_name}['und'][0] = array(
  185. 'value' => array(
  186. /* The following shows what may be present in the value array
  187. // Clause
  188. 'SIO:000493' => '',
  189. 'local:relationship_subject' => array(
  190. // Identifier
  191. 'data:0842' => '',
  192. 'schema:name' => '',
  193. 'rdfs:type' => ''
  194. ),
  195. 'local:relationship_object' => array(
  196. // Identifier
  197. 'data:0842' => '',
  198. 'schema:name' => '',
  199. 'rdfs:type' => '',
  200. ),
  201. 'local:relationship_type' => '',
  202. */
  203. ),
  204. 'chado-' . $field_table . '__' . $pkey => '',
  205. 'chado-' . $field_table . '__' . $subject_id_key => '',
  206. 'chado-' . $field_table . '__' . $object_id_key => '',
  207. 'chado-' . $field_table . '__type_id' => '',
  208. // These elements don't need to follow the naming scheme above
  209. // becasue we don't need the chado_field_storage to try and
  210. // save these values.
  211. 'object_name' => '',
  212. 'subject_name' => '',
  213. 'type_name' => '',
  214. );
  215. // If the table has rank and value fields then add those to the default
  216. // value array.
  217. if (array_key_exists('value', $schema['fields'])) {
  218. $entity->{$field_name}['und'][0]['chado-' . $field_table . '__value'] = '';
  219. }
  220. if (array_key_exists('rank', $schema['fields'])) {
  221. $entity->{$field_name}['und'][0]['chado-' . $field_table . '__rank'] = '';
  222. }
  223. // If we have no record then just return.
  224. if (!$record) {
  225. return;
  226. }
  227. // Expand the object to include the relationships.
  228. $options = array(
  229. 'return_array' => 1,
  230. // we don't want to fully recurse we only need information about the
  231. // relationship type and the object and subject
  232. 'include_fk' => array(
  233. 'type_id' => 1,
  234. $object_id_key => array(
  235. 'type_id' => 1,
  236. ),
  237. $subject_id_key => array(
  238. 'type_id' => 1,
  239. ),
  240. ),
  241. );
  242. $rel_table = $base_table . '_relationship';
  243. $schema = chado_get_schema($rel_table);
  244. if (array_key_exists('rank', $schema['fields'])) {
  245. $options['order_by'] = array('rank' => 'ASC');
  246. }
  247. $record = chado_expand_var($record, 'table', $rel_table, $options);
  248. if (!$record->$rel_table) {
  249. return;
  250. }
  251. $srelationships = null;
  252. $orelationships = null;
  253. if (isset($record->$rel_table->$subject_id_key)) {
  254. $srelationships = $record->$rel_table->$subject_id_key;
  255. }
  256. if (isset($record->$rel_table->$object_id_key)) {
  257. $orelationships = $record->$rel_table->$object_id_key;
  258. }
  259. $i = 0;
  260. if ($orelationships) {
  261. foreach ($orelationships as $relationship) {
  262. $rel_acc = $relationship->type_id->dbxref_id->db_id->name . ':' . $relationship->type_id->dbxref_id->accession;
  263. $rel_type = $relationship->type_id->name;
  264. $verb = $this->get_rel_verb($rel_type);
  265. $subject_id = $relationship->$subject_id_key->$subject_pkey;
  266. // The linked to table of a relationship linker table may not always
  267. // have a type_id or name field. So we have to be a bit more
  268. // specific about how we set some variables.
  269. switch ($relationship->tablename) {
  270. case 'acquisition_relationship':
  271. $subject_type = 'acquisition';
  272. $object_type = 'acquisition';
  273. break;
  274. case 'analysis_relationship':
  275. $subject_type = 'analysis';
  276. $object_type = 'analysis';
  277. break;
  278. case 'biomaterial_relationship':
  279. $subject_type = 'biomaterial';
  280. $object_type = 'biomaterial';
  281. break;
  282. case 'cell_line_relationship':
  283. $subject_type = 'cell_line';
  284. $object_type = 'cell_line';
  285. break;
  286. case 'element_relationship':
  287. $subject_name = $relationship->$subject_id_key->feature_id->name;
  288. $object_name = $relationship->$object_id_key->feature_id->name;
  289. break;
  290. case 'organism_relationship':
  291. $subject_name = $relationship->$subject_id_key->genus . ' ' . $relationship->$subject_id_key->species;
  292. $object_name = $relationship->$object_id_key->genus . ' ' . $relationship->$object_id_key->species;
  293. $subject_type = 'organism';
  294. $object_type = 'organism';
  295. break;
  296. case 'project_relationship':
  297. $subject_type = 'project';
  298. $object_type = 'project';
  299. break;
  300. case 'phylonode_relationship':
  301. $subject_name = $relationship->$subject_id_key->label;
  302. $object_name = $relationship->$object_id_key->label;
  303. break;
  304. case 'pub_relationship':
  305. $subject_name = $relationship->$subject_id_key->uniquename;
  306. $object_name = $relationship->$object_id_key->uniquename;
  307. break;
  308. case 'quantification_relationship':
  309. $subject_type = 'quantification';
  310. $object_type = 'quantification';
  311. break;
  312. default:
  313. $subject_name = isset($relationship->$subject_id_key->name) ? $relationship->$subject_id_key->name : '';
  314. $subject_type = isset($relationship->$subject_id_key->type_id) ? $relationship->$subject_id_key->type_id->name : '';
  315. $object_name = isset($relationship->$object_id_key->name) ? $relationship->$object_id_key->name : '';
  316. $object_type = isset($relationship->$object_id_key->type_id) ? $relationship->$object_id_key->type_id->name : '';
  317. }
  318. $entity->{$field_name}['und'][$i]['value'] = array(
  319. 'local:relationship_subject' => array(
  320. 'rdfs:type' => $subject_type,
  321. 'schema:name' => $subject_name,
  322. ),
  323. 'local:relationship_type' => $relationship->type_id->name,
  324. 'local:relationship_object' => array(
  325. 'rdfs:type' => $object_type,
  326. 'schema:name' => $object_name,
  327. 'entity' => 'TripalEntity:' . $entity->id,
  328. )
  329. );
  330. // See if an entity exists for the subject.
  331. $data_table = preg_replace('/_relationship/', '', $relationship->tablename);
  332. $sentity_id = chado_get_record_entity_by_table($data_table, $subject_id);
  333. if ($sentity_id) {
  334. $entity->{$field_name}['und'][$i]['value']['local:relationship_subject']['entity'] = 'TripalEntity:' . $sentity_id;
  335. }
  336. if (property_exists($relationship->$subject_id_key, 'uniquename')) {
  337. $entity->{$field_name}['und'][$i]['value']['local:relationship_subject']['data:0842'] = $relationship->$subject_id_key->uniquename;;
  338. }
  339. if (property_exists($relationship->$object_id_key, 'uniquename')) {
  340. $entity->{$field_name}['und'][$i]['value']['local:relationship_object']['data:0842'] = $relationship->$object_id_key->uniquename;
  341. }
  342. if (property_exists($relationship->$subject_id_key, 'entity_id')) {
  343. $entity_id = $relationship->$subject_id_key->entity_id;
  344. $entity->{$field_name}['und'][$i]['value']['local:relationship_subject']['entity'] = 'TripalEntity:' . $entity_id;
  345. }
  346. // Add the clause to the values array.
  347. $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
  348. $entity->{$field_name}['und'][$i]['value']['SIO:000493'] = 'The ' . $subject_type . ', ' .
  349. $subject_name . ', ' . $verb . ' ' . $rel_type_clean . ' this ' .
  350. $object_type . '.';
  351. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__' . $pkey] = $relationship->$pkey;
  352. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__' . $subject_id_key] = $relationship->$subject_id_key->$subject_pkey;
  353. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__type_id'] = $relationship->type_id->cvterm_id;
  354. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__' . $object_id_key] = $relationship->$object_id_key->$object_pkey;
  355. $entity->{$field_name}['und'][$i]['type_name'] = $relationship->type_id->name;
  356. $entity->{$field_name}['und'][$i]['subject_name'] = $subject_name . ' [id: ' . $relationship->$subject_id_key->$fkey_rcolumn . ']';
  357. $entity->{$field_name}['und'][$i]['object_name'] = $object_name . ' [id: ' . $relationship->$object_id_key->$fkey_rcolumn . ']';
  358. if (array_key_exists('value', $schema['fields'])) {
  359. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__value'] = $relationship->value;
  360. }
  361. if (array_key_exists('rank', $schema['fields'])) {
  362. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__rank'] = $relationship->rank;
  363. }
  364. $i++;
  365. }
  366. }
  367. if ($srelationships) {
  368. foreach ($srelationships as $relationship) {
  369. $rel_acc = $relationship->type_id->dbxref_id->db_id->name . ':' . $relationship->type_id->dbxref_id->accession;
  370. $rel_type = $relationship->type_id->name;
  371. $verb = $this->get_rel_verb($rel_type);
  372. $object_id = $relationship->$object_id_key->$object_pkey;
  373. // The linked to table of a relationship linker table may not always
  374. // have a type_id or name field. So we have to be a bit more
  375. // specific about how we set some variables.
  376. switch ($relationship->tablename) {
  377. case 'acquisition_relationship':
  378. $subject_type = 'acquisition';
  379. $object_type = 'acquisition';
  380. break;
  381. case 'analysis_relationship':
  382. $subject_type = 'analysis';
  383. $object_type = 'analysis';
  384. break;
  385. case 'biomaterial_relationship':
  386. $subject_type = 'biomaterial';
  387. $object_type = 'biomaterial';
  388. break;
  389. case 'cell_line_relationship':
  390. $subject_type = 'cell_line';
  391. $object_type = 'cell_line';
  392. break;
  393. case 'element_relationship':
  394. $subject_name = $relationship->$subject_id_key->feature_id->name;
  395. $object_name = $relationship->$object_id_key->feature_id->name;
  396. break;
  397. case 'organism_relationship':
  398. $subject_name = $relationship->$subject_id_key->genus . ' ' . $relationship->$subject_id_key->species;
  399. $object_name = $relationship->$object_id_key->genus . ' ' . $relationship->$object_id_key->species;
  400. $subject_type = 'organism';
  401. $object_type = 'organism';
  402. break;
  403. case 'project_relationship':
  404. $subject_type = 'project';
  405. $object_type = 'project';
  406. break;
  407. case 'phylonode_relationship':
  408. $subject_name = $relationship->$subject_id_key->label;
  409. $object_name = $relationship->$object_id_key->label;
  410. break;
  411. case 'pub_relationship':
  412. $subject_name = $relationship->$subject_id_key->uniquename;
  413. $object_name = $relationship->$object_id_key->uniquename;
  414. break;
  415. case 'quantification_relationship':
  416. $subject_type = 'quantification';
  417. $object_type = 'quantification';
  418. break;
  419. default:
  420. $subject_name = isset($relationship->$subject_id_key->name) ? $relationship->$subject_id_key->name : '';
  421. $subject_type = isset($relationship->$subject_id_key->type_id) ? $relationship->$subject_id_key->type_id->name : '';
  422. $object_name = isset($relationship->$object_id_key->name) ? $relationship->$object_id_key->name : '';
  423. $object_type = isset($relationship->$object_id_key->type_id) ? $relationship->$object_id_key->type_id->name : '';
  424. }
  425. $entity->{$field_name}['und'][$i]['value'] = array(
  426. 'local:relationship_subject' => array(
  427. 'rdfs:type' => $subject_type,
  428. 'schema:name' => $subject_name,
  429. 'entity' => 'TripalEntity:' . $entity->id,
  430. ),
  431. 'local:relationship_type' => $relationship->type_id->name,
  432. 'local:relationship_object' => array(
  433. 'rdfs:type' => $object_type,
  434. 'schema:name' => $object_name,
  435. )
  436. );
  437. // See if an entity exists for the object.
  438. $data_table = preg_replace('/_relationship/', '', $relationship->tablename);
  439. $oentity_id = chado_get_record_entity_by_table($data_table, $object_id);
  440. if ($oentity_id) {
  441. $entity->{$field_name}['und'][$i]['value']['local:relationship_object']['entity'] = 'TripalEntity:' . $oentity_id;
  442. }
  443. if (property_exists($relationship->$subject_id_key, 'uniquename')) {
  444. $entity->{$field_name}['und'][$i]['value']['local:relationship_subject']['data:0842'] = $relationship->$subject_id_key->uniquename;
  445. }
  446. if (property_exists($relationship->$object_id_key, 'uniquename')) {
  447. $entity->{$field_name}['und'][$i]['value']['local:relationship_object']['data:0842'] = $relationship->$object_id_key->uniquename;
  448. }
  449. if (property_exists($relationship->$object_id_key, 'entity_id')) {
  450. $entity_id = $relationship->$object_id_key->entity_id;
  451. $entity->{$field_name}['und'][$i]['value']['local:relationship_object']['entity'] = 'TripalEntity:' . $entity_id;
  452. }
  453. // Add the clause to the value array.
  454. $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
  455. $entity->{$field_name}['und'][$i]['value']['SIO:000493'] = 'This ' .
  456. $subject_type . ' ' . $verb . ' ' . $rel_type_clean . ' the ' .
  457. $object_type . ' ' . $object_name . '.';
  458. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__' . $pkey] = $relationship->$pkey;
  459. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__' . $subject_id_key] = $relationship->$subject_id_key->$subject_pkey;
  460. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__type_id'] = $relationship->type_id->cvterm_id;
  461. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__' . $object_id_key] = $relationship->$object_id_key->$object_pkey;
  462. $entity->{$field_name}['und'][$i]['type_name'] = $relationship->type_id->name;
  463. $entity->{$field_name}['und'][$i]['subject_name'] = $subject_name . ' [id: ' . $relationship->$subject_id_key->$fkey_rcolumn . ']';
  464. $entity->{$field_name}['und'][$i]['object_name'] = $object_name . ' [id: ' . $relationship->$object_id_key->$fkey_rcolumn . ']';
  465. if (array_key_exists('value', $schema['fields'])) {
  466. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__value'] = $relationship->value;
  467. }
  468. if (array_key_exists('rank', $schema['fields'])) {
  469. $entity->{$field_name}['und'][$i]['chado-' . $field_table . '__rank'] = $relationship->rank;
  470. }
  471. $i++;
  472. }
  473. }
  474. }
  475. /**
  476. * @see ChadoField::query()
  477. */
  478. public function query($query, $condition) {
  479. $alias = $this->field['field_name'];
  480. $chado_table = $this->instance['settings']['chado_table'];
  481. $base_table = $this->instance['settings']['base_table'];
  482. $bschema = chado_get_schema($base_table);
  483. $bpkey = $bschema['primary key'][0];
  484. $operator = $condition['operator'];
  485. // Bulid the list of expected elements that will be provided.
  486. $field_term_id = $this->getFieldTermID();
  487. $rel_subject = $field_term_id . ',local:relationship_subject';
  488. $rel_subject_type = $rel_subject . ',' . 'rdfs:type';
  489. $rel_subject_name = $rel_subject . ',' . 'schema:name';
  490. $rel_subject_identifier = $rel_subject . ',' . 'data:0842';
  491. $rel_type = $field_term_id . ',local:relationship_type';
  492. $rel_object = $field_term_id . ',local:relationship_object';
  493. $rel_object_type = $rel_object . ',' . 'rdfs:type';
  494. $rel_object_name = $rel_object . ',' . 'schema:name';
  495. $rel_object_identifier = $rel_object . ',' . 'data:0842';
  496. // Filter by the name of the subject or object.
  497. if ($condition['column'] == $rel_subject_name) {
  498. $this->queryJoinOnce($query, $chado_table, $alias, "base.$bpkey = $alias.object_id");
  499. $this->queryJoinOnce($query, $base_table, 'base2', "base2.$bpkey = $alias.subject_id");
  500. $query->condition("base2.name", $condition['value'], $operator);
  501. }
  502. if ($condition['column'] == $rel_object_name) {
  503. $this->queryJoinOnce($query, $chado_table, $alias, "base.$bpkey = $alias.subject_id");
  504. $this->queryJoinOnce($query, $base_table, 'base2', "base2.$bpkey = $alias.object_id");
  505. $query->condition("base2.name", $condition['value'], $operator);
  506. }
  507. // Filter by unique name of the subject or object.
  508. if ($condition['column'] == $rel_subject_identifier) {
  509. $this->queryJoinOnce($query, $chado_table, $alias, "base.$bpkey = $alias.object_id");
  510. $this->queryJoinOnce($query, $base_table, 'base2', "base2.$bpkey = $alias.subject_id");
  511. $query->condition("base2.uniquename", $condition['value'], $operator);
  512. }
  513. if ($condition['column'] == $rel_object_identifier) {
  514. $this->queryJoinOnce($query, $chado_table, $alias, "base.$bpkey = $alias.subject_id");
  515. $this->queryJoinOnce($query, $base_table, 'base2', "base2.$bpkey = $alias.object_id");
  516. $query->condition("base2.uniquename", $condition['value'], $operator);
  517. }
  518. // Filter by the type of the subject or object
  519. if ($condition['column'] == $rel_subject_type) {
  520. $this->queryJoinOnce($query, $chado_table, $alias, "base.$bpkey = $alias.object_id");
  521. $this->queryJoinOnce($query, $base_table, 'base2', "base2.$bpkey = $alias.subject_id");
  522. $this->queryJoinOnce($query, 'cvterm', 'SubjectCVT', "SubjectCVT.cvterm_id = base2.type_id");
  523. $this->queryJoinOnce($query, "SubjectCVT.name", $condition['value'], $operator);
  524. }
  525. if ($condition['column'] == $rel_object_type) {
  526. $this->queryJoinOnce($query, $chado_table, $alias, "base.$bpkey = $alias.subject_id");
  527. $this->queryJoinOnce($query, $base_table, 'base2', "base2.$bpkey = $alias.object_id");
  528. $this->queryJoinOnce($query, 'cvterm', 'ObjectCVT', "ObjectCVT.cvterm_id = base2.type_id");
  529. $query->condition("ObjectCVT.name", $condition['value'], $operator);
  530. }
  531. // Filter by relationship type
  532. if ($condition['column'] == 'relationship.relationship_type') {
  533. // This filter commented out because it's way to slow...
  534. // $this->queryJoinOnce($query, $chado_table, $alias, "base.$bpkey = $alias.subject_id OR base.$bpkey = $alias.object_id");
  535. // $this->queryJoinOnce($query, 'cvterm', 'RelTypeCVT', "RelTypeCVT.cvterm_id = $alias.type_id");
  536. // $query->condition("RelTypeCVT.name", $condition['value'], $operator);
  537. }
  538. }
  539. /**
  540. * A helper function to define English verbs for relationship types.
  541. *
  542. * @param $rel_type
  543. * The vocabulary term name for the relationship.
  544. *
  545. * @return
  546. * The verb to use when creating a sentence of the relationship.
  547. */
  548. private function get_rel_verb($rel_type) {
  549. $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
  550. $verb = '';
  551. switch ($rel_type_clean) {
  552. case 'integral part of':
  553. case 'instance of':
  554. $verb = 'is an';
  555. break;
  556. case 'proper part of':
  557. case 'transformation of':
  558. case 'genome of':
  559. case 'part of':
  560. $verb = 'is a';
  561. case 'position of':
  562. case 'sequence of':
  563. case 'variant of':
  564. $verb = 'is a';
  565. break;
  566. case 'derives from':
  567. case 'connects on':
  568. case 'contains':
  569. case 'finishes':
  570. case 'guides':
  571. case 'has origin':
  572. case 'has part':
  573. case 'has quality':
  574. case 'is a maternal parent of':
  575. case 'is a paternal parent of':
  576. case 'is consecutive sequence of':
  577. case 'maximally overlaps':
  578. case 'overlaps':
  579. case 'starts':
  580. break;
  581. default:
  582. $verb = 'is';
  583. }
  584. return $verb;
  585. }
  586. /**
  587. *
  588. * @see TripalField::settingsForm()
  589. */
  590. public function instanceSettingsForm() {
  591. $element = parent::instanceSettingsForm();
  592. $element['items_per_page'] = array(
  593. '#type' => 'textfield',
  594. '#title' => 'Items per Page',
  595. '#description' => t('The number of items that should appear on each page. A pager is provided if more than this number of items exist.'),
  596. '#default_value' => $this->instance['settings']['items_per_page'],
  597. );
  598. //$element = parent::instanceSettingsForm();
  599. $element['relationships'] = array(
  600. '#type' => 'fieldset',
  601. '#title' => 'Allowed Relationship Types',
  602. '#description' => t('There are three ways that relationship types
  603. can be limited for users who have permission to add new relationships.
  604. Please select the most appropriate for you use case. By default
  605. all vocabularies are provided to the user which allows use of any
  606. term for the relationship type.'),
  607. '#collapsed' => TRUE,
  608. '#collapsible' => TRUE,
  609. );
  610. // $element['instructions'] = array(
  611. // '#type' => 'item',
  612. // '#markup' => 'You may provide a list of terms that will be available in a select box
  613. // as the relationship types. This select box will replace the vocabulary select box if the
  614. // following value is set.'
  615. // );
  616. $vocs = tripal_get_cv_select_options();
  617. $element['relationships']['option1'] = array(
  618. '#type' => 'item',
  619. '#title' => 'Option #1',
  620. '#description' => t('Use this option to limit the vocabularies that a user .
  621. could use to specify relationship types. With this option any term in .
  622. the vocabulary can be used for the relationship type. You may select
  623. more than one vocabulary.'),
  624. );
  625. $element['relationships']['option1_vocabs'] = array(
  626. '#type' => 'select',
  627. '#multiple' => TRUE,
  628. '#options' => $vocs,
  629. '#size' => 6,
  630. '#default_value' => $this->instance['settings']['relationships']['option1_vocabs'],
  631. // TODO add ajax here so that the relationship autocomplete below works
  632. );
  633. $element['relationships']['option2'] = array(
  634. '#type' => 'item',
  635. '#title' => '<b>Option #2</b>',
  636. '#description' => 'Some vocabularies are heirarchichal (an ontology). Within this
  637. heirarchy groups of related terms typically fall under a common parent. If you
  638. wish to limit the list of terms that a user can use for the relationship type,
  639. you can provide the parent term here. Then, only that term\'s children will
  640. be avilable for use as a relationship type.',
  641. );
  642. $element['relationships']['option2_vocab'] = array(
  643. '#type' => 'select',
  644. '#description' => 'Specify Default Vocabulary',
  645. '#multiple' => FALSE,
  646. '#options' => $vocs,
  647. '#default_value' => $this->instance['settings']['relationships']['option2_vocab'],
  648. '#ajax' => array(
  649. 'callback' => "sbo__relationship_instance_settings_form_ajax_callback",
  650. 'wrapper' => 'relationships-option2-parent',
  651. 'effect' => 'fade',
  652. 'method' => 'replace'
  653. ),
  654. );
  655. $element['relationships']['option2_parent'] = array(
  656. '#type' => 'textfield',
  657. '#description' => 'Specify a Heirarchical Parent Term',
  658. '#default_value' => $this->instance['settings']['relationships']['option2_parent'],
  659. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/cvterm/",
  660. '#prefix' => '<div id=relationships-option2-parent>',
  661. '#suffix' => '</div>'
  662. );
  663. $element['relationships']['option3'] = array(
  664. '#type' => 'item',
  665. '#title' => 'Option #3',
  666. '#description' => 'Provide terms separated by a new line. The term provided should be
  667. unique and distinguishable by the name. You can use a bar | to separate a vocabulary
  668. and a term to allow more specific assignment.',
  669. );
  670. $element['relationships']['relationship_types'] = array(
  671. '#type' => 'textarea',
  672. '#default_value' => $this->instance['settings']['relationships']['relationship_types'],
  673. );
  674. return $element;
  675. }
  676. /**
  677. *
  678. * @param unknown $form
  679. * @param unknown $form_state
  680. */
  681. public function instanceSettingsFormValidate($form, &$form_state) {
  682. // Get relationships settings
  683. $settings = $form_state['values']['instance']['settings']['relationships'];
  684. $form_state['values']['instance']['settings']['relationships']['relationship_types']= trim($settings['relationship_types']);
  685. // Make sure only one option is selected
  686. $option1test = $settings['option1_vocabs'];
  687. $option1 = isset($settings['option1_vocabs']) && array_pop($option1test);
  688. $option2 = (isset($settings['option2_vocab']) && $settings['option2_vocab']) || $settings['option2_parent'];
  689. $option3 = isset($settings['relationship_types']) && trim($settings['relationship_types']);
  690. if ($option1 && ($option2 || $option3) == 1 ||
  691. $option2 && ($option1 || $option3) == 1 ||
  692. $option3 && ($option1 || $option2) == 1) {
  693. form_set_error("instance][settings][relationships", t("Only one option is allowed to limit the relationship types."));
  694. return;
  695. }
  696. // For option3, make sure the supplied types are valid cvterms
  697. if ($option3) {
  698. $rel_types = explode(PHP_EOL, $settings['relationship_types']);
  699. foreach($rel_types AS $type) {
  700. $type = trim($type);
  701. // Ignore empty lines
  702. if ($type == '') {
  703. continue;
  704. }
  705. // Find the matching cvterm
  706. $sql = "SELECT cvterm_id FROM {cvterm} WHERE name = :name";
  707. $results = chado_query($sql, array(':name' => $type));
  708. $terms = array();
  709. while ($obj = $results->fetchObject()) {
  710. $terms[] = $obj;
  711. }
  712. // Don't save the form if a term can not be found or it matches more than one cvterm
  713. $cv = '';
  714. if (count($terms) == 0) {
  715. // If a term can not be found, maybe the type contains '|', parse it as 'vocabulary|cvterm'
  716. if (strpos($type, '|')) {
  717. $tmp = explode('|', $type, 2);
  718. $type = trim($tmp[1]);
  719. $cv = tripal_get_cv(array('name' => trim($tmp[0])));
  720. if($cv) {
  721. $sql = "SELECT cvterm_id FROM {cvterm} WHERE name = :name AND cv_id = :cv_id";
  722. $results = chado_query($sql, array(':name' => $type, ':cv_id' => $cv->cv_id));
  723. while ($obj = $results->fetchObject()) {
  724. $terms[] = $obj;
  725. }
  726. }
  727. else {
  728. $cv = $tmp[0];
  729. }
  730. }
  731. if (count($terms) != 1) {
  732. $message = "The term '@type' can not be found.";
  733. $token = array('@type' => $type);
  734. if ($cv) {
  735. $message = "The term '@type' can not be found within the vocabulary '@vocab'.";
  736. $token['@vocab'] = $cv;
  737. }
  738. form_set_error("instance][settings][relationships][relationship_types",
  739. t($message, $token));
  740. }
  741. }
  742. else if (count($terms) > 1) {
  743. // If a type matches more than one term, parse it as 'vocabulary|cvterm' and try again
  744. if (strpos($type, '|')) {
  745. $tmp = explode('|', $type, 2);
  746. $type = trim($tmp[1]);
  747. $cv = tripal_get_cv(array('name' => trim($tmp[0])));
  748. if ($cv) {
  749. $sql = "SELECT cvterm_id FROM {cvterm} WHERE name = :name AND cv_id = :cv_id";
  750. $results = chado_query($sql, array(':name' => $type, ':cv_id' => $cv->cv_id));
  751. while ($obj = $results->fetchObject()) {
  752. $terms[] = $obj;
  753. }
  754. }
  755. }
  756. if(count($terms) != 1) {
  757. form_set_error("instance][settings][relationships][relationship_types",
  758. t("The term '@type' matches more than one term. Please specify its vocabulary in the format of 'vocabulary|@type'.", array('@type' => $type)));
  759. }
  760. }
  761. }
  762. }
  763. // For option2: Make sure the parent term is a valid cvterm
  764. if ($option2) {
  765. $cv_id = $settings['option2_vocab'];
  766. $supertype = $settings['option2_parent'];
  767. $term = tripal_get_cvterm(array(
  768. 'name' => trim($supertype),
  769. 'cv_id' => $cv_id,
  770. ));
  771. // Tripal cv autocomplete also allow cvterm synonyms, if the parent term doesn't match
  772. // a cvterm, try cvtermsynonym
  773. if (!$term) {
  774. $synonym = tripal_get_cvterm(
  775. array(
  776. 'synonym' => array(
  777. 'name' => trim($supertype),
  778. )
  779. )
  780. );
  781. if ($synonym && $synonym->cv_id->cv_id == $cv_id) {
  782. $term = $synonym;
  783. }
  784. }
  785. if (!isset($term->cvterm_id)) {
  786. form_set_error("instance][settings][relationships][option2_parent",
  787. t("The term '@type' is not a valid term for the vocabulary selected.", array('@type' => $supertype)));
  788. }
  789. }
  790. }
  791. /**
  792. * @see TripalField::validate()
  793. */
  794. public function validate($entity_type, $entity, $langcode, $items, &$errors) {
  795. $field_name = $this->field['field_name'];
  796. $field_type = $this->field['type'];
  797. $field_table = $this->instance['settings']['chado_table'];
  798. $field_column = $this->instance['settings']['chado_column'];
  799. $base_table = $this->instance['settings']['base_table'];
  800. $schema = chado_get_schema($field_table);
  801. $fkeys = $schema['foreign keys'];
  802. // 'nd_reagent_relationship' and 'project_relationship' have different column names from
  803. // subject_id/object_id. Do a pattern matching to get the column names.
  804. $subject_id_key = 'subject_id';
  805. $object_id_key = 'object_id';
  806. foreach ($schema['foreign keys'][$base_table]['columns'] AS $lcolum => $rcolum) {
  807. if (preg_match('/^subject_.*id/', $lcolum)) {
  808. $subject_id_key = $lcolum;
  809. }
  810. else if (preg_match('/^object_.*id/', $lcolum)) {
  811. $object_id_key = $lcolum;
  812. }
  813. }
  814. foreach ($items as $delta => $item) {
  815. $subject_id = $item['chado-' . $field_table . '__' . $subject_id_key];
  816. $object_id = $item['chado-' . $field_table . '__' . $object_id_key];
  817. $type_id = $item['chado-' . $field_table . '__type_id'];
  818. $type_id = isset($item['type_id']) ? $item['chado-' . $field_table . '__type_id'] : $type_id;
  819. $type_name = isset($item['type_name']) ? $item['type_name'] : '';
  820. $subject_name = $item['subject_name'];
  821. $object_name = $item['object_name'];
  822. // If the row is empty then just continue, there's nothing to validate.
  823. if (!$type_id and !$type_name and !$subject_name and !$object_name) {
  824. continue;
  825. }
  826. // Make sure we have values for all of the fields.
  827. $form_error = FALSE;
  828. if (!$type_name && !$type_id) {
  829. $errors[$field_name][$delta]['und'][] = array(
  830. 'error' => 'sbo__relationship',
  831. 'message' => t("Please provide the type of relationship."),
  832. );
  833. }
  834. if ($entity and !$subject_name) {
  835. $errors[$field_name][$delta]['und'][] = array(
  836. 'error' => 'sbo__relationship',
  837. 'message' => t("Please provide the subject of the relationship."),
  838. );
  839. }
  840. if ($entity and !$object_name) {
  841. $errors[$field_name][$delta]['und'][] = array(
  842. 'error' => 'sbo__relationship',
  843. 'message' => t("Please provide the object of the relationship."),
  844. );
  845. }
  846. if ($form_error) {
  847. continue;
  848. }
  849. // Before submitting this form we need to make sure that our subject_id and
  850. // object_ids are real records. There are two ways to get the record, either
  851. // just with the text value or with an [id: \d+] string embedded. If the
  852. // later we will pull it out.
  853. $subject_id = '';
  854. $fkey_rcolumn = $fkeys[$base_table]['columns'][$subject_id_key];
  855. $matches = array();
  856. if ($entity) {
  857. if(preg_match('/\[id: (\d+)\]/', $subject_name, $matches)) {
  858. $subject_id = $matches[1];
  859. $values = array($fkey_rcolumn => $subject_id);
  860. $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
  861. if (count($subject) == 0) {
  862. $errors[$field_name][$delta]['und'][] = array(
  863. 'error' => 'sbo__relationship',
  864. 'message' => t("The subject record cannot be found using the specified id (e.g. [id: xx])."),
  865. );
  866. }
  867. }
  868. else {
  869. $values = array('uniquename' => $subject_name);
  870. $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
  871. if (count($subject) == 0) {
  872. $errors[$field_name][$delta]['und'][] = array(
  873. 'error' => 'sbo__relationship',
  874. 'message' => t("The subject record cannot be found. Please check spelling."),
  875. );
  876. }
  877. elseif (count($subject) > 1) {
  878. $errors[$field_name][$delta]['und'][] = array(
  879. 'error' => 'sbo__relationship',
  880. 'message' => t("The subject is not unique and therefore the relationship cannot be made."),
  881. );
  882. }
  883. }
  884. }
  885. // Now check for a matching object.
  886. $object_id = '';
  887. $fkey_rcolumn = $fkeys[$base_table]['columns'][$object_id_key];
  888. $matches = array();
  889. if ($entity) {
  890. if (preg_match('/\[id: (\d+)\]/', $object_name, $matches)) {
  891. $object_id = $matches[1];
  892. $values = array($fkey_rcolumn => $object_id);
  893. $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
  894. if (count($subject) == 0) {
  895. $errors[$field_name][$delta]['und'][] = array(
  896. 'error' => 'sbo__relationship',
  897. 'message' => t("The object record cannot be found using the specified id (e.g. [id: xx])."),
  898. );
  899. }
  900. }
  901. else {
  902. $values = array('uniquename' => $object_name);
  903. $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
  904. if (count($object) == 0) {
  905. $errors[$field_name][$delta]['und'][] = array(
  906. 'error' => 'sbo__relationship',
  907. 'message' => t("The object record cannot be found. Please check spelling."),
  908. );;
  909. }
  910. elseif (count($object) > 1) {
  911. $errors[$field_name][$delta]['und'][] = array(
  912. 'error' => 'sbo__relationship',
  913. 'message' => t("The object is not unique and therefore the relationship cannot be made."),
  914. );
  915. }
  916. }
  917. }
  918. // Make sure that either our object or our subject refers to the base record.
  919. if ($entity) {
  920. $chado_record_id = $entity->chado_record_id;
  921. if ($object_id != $chado_record_id and $subject_id != $chado_record_id) {
  922. $errors[$field_name][$delta]['und'][] = array(
  923. 'error' => 'sbo__relationship',
  924. 'message' => t("Either the subject or the object in the relationship must refer to this record."),
  925. );
  926. }
  927. // Make sure that the object and subject are not both the same thing.
  928. if ($object_id == $subject_id) {
  929. $errors[$field_name][$delta]['und'][] = array(
  930. 'error' => 'sbo__relationship',
  931. 'message' => t("The subject and the object in the relationship cannot both refer to the same record."),
  932. );
  933. }
  934. }
  935. }
  936. }
  937. /**
  938. * @see ChadoField::queryOrder()
  939. */
  940. public function queryOrder($query, $order) {
  941. }
  942. }