sbo__relationship.inc 39 KB

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