sbo__relationship.inc 38 KB

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