sbo__relationship.inc 38 KB

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