tripal_chado.cv.inc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. <?php
  2. /**
  3. * Loads an OBO File using the new TripalImporter. Expected to be run by a
  4. * Tripal Job.
  5. */
  6. function tripal_cv_load_obo($obo_id, $job = NULL) {
  7. module_load_include('inc', 'tripal_chado', 'includes/TripalImporter/OBOImporter');
  8. $obo_importer = new OBOImporter();
  9. $obo_importer->create(['obo_id' => $obo_id]);
  10. if ($job) {
  11. $obo_importer->setJob($job);
  12. }
  13. $obo_importer->run();
  14. }
  15. /**
  16. * Provide landing page to the new admin pages
  17. *
  18. * @ingroup tripal_cv
  19. */
  20. function tripal_cv_admin_cv_listing() {
  21. $output = '';
  22. $breadcrumb = [];
  23. $breadcrumb[] = l('Home', '<front>');
  24. $breadcrumb[] = l('Administration', 'admin');
  25. $breadcrumb[] = l('Tripal', 'admin/tripal');
  26. $breadcrumb[] = l('Data Loaders', 'admin/tripal/loaders');
  27. $breadcrumb[] = l('Chado Vocabularies', 'admin/tripal/loaders/chado_vocabs');
  28. drupal_set_breadcrumb($breadcrumb);
  29. // Add the view
  30. $cvs_view = views_embed_view('tripal_cv_admin_cvs', 'default');
  31. $cvterms_view = views_embed_view('tripal_cv_admin_cvterms', 'default');
  32. if (isset($cvs_view) && isset($cvterms_view)) {
  33. $output .= $cvs_view;
  34. }
  35. else {
  36. $output .= '<p>The Tripal Controlled Vocabulary module uses primarily views to provide an '
  37. . 'administrative interface. Currently one or more views needed for this '
  38. . 'administrative interface are disabled. <strong>Click each of the following links to '
  39. . 'enable the pertinent views</strong>:</p>';
  40. $output .= '<ul>';
  41. if (!isset($cvs_view)) {
  42. $output .= '<li>' . l('Tripal Vocabularies', 'admin/tripal/vocab/views/cvs/enable') . '</li>';
  43. }
  44. if (!isset($cvterm_view)) {
  45. $output .= '<li>' . l('Tripal Vocabulary Terms', 'admin/tripal/vocab/views/cvterms/enable') . '</li>';
  46. }
  47. $output .= '</ul>';
  48. }
  49. return $output;
  50. }
  51. /**
  52. * Provides the actual "Select CV" form on the Update/Delete Controlled
  53. * Vocabulary page. This form also triggers the edit javascript
  54. *
  55. * @todo Modify this form to use Drupal AJAX
  56. *
  57. * @ingroup tripal_cv
  58. */
  59. function tripal_cv_cv_edit_form($form, &$form_state, $cv_id = NULL) {
  60. // set the breadcrumb
  61. $breadcrumb = [];
  62. $breadcrumb[] = l('Home', '<front>');
  63. $breadcrumb[] = l('Administration', 'admin');
  64. $breadcrumb[] = l('Tripal', 'admin/tripal');
  65. $breadcrumb[] = l('Data Loaders', 'admin/tripal/loaders');
  66. $breadcrumb[] = l('Chado Vocabularies', 'admin/tripal/loaders/chado_vocabs');
  67. $breadcrumb[] = l('Manage Chado CVs', 'admin/tripal/loaders/chado_vocabs/chado_cvs');
  68. drupal_set_breadcrumb($breadcrumb);
  69. $default_name = '';
  70. $default_desc = '';
  71. if ($cv_id) {
  72. $values = ['cv_id' => $cv_id];
  73. $result = chado_select_record('cv', ['*'], $values);
  74. $cv = $result[0];
  75. $default_name = $cv->name;
  76. $default_desc = $cv->definition;
  77. }
  78. $form['cv_id'] = [
  79. '#type' => 'value',
  80. '#value' => $cv_id,
  81. ];
  82. $form['name'] = [
  83. '#type' => 'textfield',
  84. '#title' => t("Controlled Vocabulary name"),
  85. '#description' => t('Please enter the name for this vocabulary.'),
  86. '#required' => TRUE,
  87. '#default_value' => $default_name,
  88. '#maxlength' => 255,
  89. ];
  90. $form['definition'] = [
  91. '#type' => 'textarea',
  92. '#title' => t('Description'),
  93. '#description' => t('Please enter a definition for this vocabulary'),
  94. '#default_value' => $default_desc,
  95. ];
  96. $form['update'] = [
  97. '#type' => 'submit',
  98. '#value' => t('Update'),
  99. ];
  100. $form['delete'] = [
  101. '#type' => 'markup',
  102. '#markup' => l('delete', 'admin/tripal/loaders/chado_vocabs/chado_cv/delete/' . $cv_id),
  103. ];
  104. return $form;
  105. }
  106. /**
  107. * Validation function for tripal_cv_cv_edit_form
  108. *
  109. * @ingroup tripal_cv
  110. */
  111. function tripal_cv_cv_edit_form_validate($form, &$form_state) {
  112. $name = array_key_exists('name', $form_state['values']) ? trim($form_state['values']['name']) : '';
  113. $desc = array_key_exists('definition', $form_state['values']) ? trim($form_state['values']['definition']) : '';
  114. $cv_id = array_key_exists('cv_id', $form_state['values']) ? trim($form_state['values']['cv_id']) : '';
  115. // make sure the cv name is unique
  116. $values = ['name' => $name];
  117. $results = chado_select_record('cv', ['cv_id'], $values);
  118. if (count($results) > 0 and $results[0]->cv_id != $cv_id) {
  119. form_set_error('name', 'The vocabulary name must be unique');
  120. }
  121. }
  122. /**
  123. * Submit cv edit form
  124. *
  125. * @ingroup tripal_cv
  126. */
  127. function tripal_cv_cv_edit_form_submit($form, &$form_state) {
  128. $name = array_key_exists('name', $form_state['values']) ? trim($form_state['values']['name']) : '';
  129. $desc = array_key_exists('definition', $form_state['values']) ? trim($form_state['values']['definition']) : '';
  130. $cv_id = array_key_exists('cv_id', $form_state['values']) ? trim($form_state['values']['cv_id']) : '';
  131. $op = array_key_exists('op', $form_state['values']) ? trim($form_state['values']['op']) : '';
  132. $values = [
  133. 'name' => $name,
  134. 'definition' => $desc,
  135. ];
  136. if (strcmp($op, 'Update') == 0) {
  137. $match = ['cv_id' => $cv_id];
  138. $success = chado_update_record('cv', $match, $values);
  139. // Clear the cached terms
  140. cache_clear_all('tripal_chado:term:*', 'cache', TRUE);
  141. if ($success) {
  142. drupal_set_message(t("Controlled vocabulary updated"));
  143. drupal_goto('admin/tripal/loaders/chado_vocabs/chado_cvs');
  144. }
  145. else {
  146. drupal_set_message(t("Failed to update controlled vocabulary."));
  147. }
  148. }
  149. }
  150. /**
  151. * Form to add controlled vocabularies
  152. *
  153. * @ingroup tripal_cv
  154. */
  155. function tripal_cv_cv_add_form($form, &$form_state) {
  156. // set the breadcrumb
  157. $breadcrumb = [];
  158. $breadcrumb[] = l('Home', '<front>');
  159. $breadcrumb[] = l('Administration', 'admin');
  160. $breadcrumb[] = l('Tripal', 'admin/tripal');
  161. $breadcrumb[] = l('Data Loaders', 'admin/tripal/loaders');
  162. $breadcrumb[] = l('Chado Vocabularies', 'admin/tripal/loaders/chado_vocabs');
  163. $breadcrumb[] = l('Manage Chado CVs', 'admin/tripal/loaders/chado_vocabs/chado_cvs');
  164. drupal_set_breadcrumb($breadcrumb);
  165. $default_name = '';
  166. $default_desc = '';
  167. // add a fieldset for the Drupal Schema API
  168. $form = [
  169. '#type' => 'fieldset',
  170. '#title' => 'Controlled Vocabulary Details',
  171. '#collapsible' => 0,
  172. ];
  173. $form['name'] = [
  174. '#type' => 'textfield',
  175. '#title' => t("Controlled Vocabulary name"),
  176. '#description' => t('Please enter the name for this vocabulary.'),
  177. '#required' => TRUE,
  178. '#default_value' => $default_name,
  179. '#maxlength' => 255,
  180. ];
  181. $form['definition'] = [
  182. '#type' => 'textarea',
  183. '#title' => t('Description'),
  184. '#description' => t('Please enter a definition for this vocabulary'),
  185. '#default_value' => $default_desc,
  186. ];
  187. $form['add'] = [
  188. '#type' => 'submit',
  189. '#value' => t('Add'),
  190. ];
  191. return $form;
  192. }
  193. /**
  194. * Validation fucntion for tripal_cv_cv_add_form
  195. *
  196. * @ingroup tripal_cv
  197. */
  198. function tripal_cv_cv_add_form_validate($form, &$form_state) {
  199. $name = array_key_exists('name', $form_state['values']) ? trim($form_state['values']['name']) : '';
  200. $desc = array_key_exists('definition', $form_state['values']) ? trim($form_state['values']['definition']) : '';
  201. // make sure the cv name is unique
  202. $values = ['name' => $name];
  203. $results = chado_select_record('cv', ['cv_id'], $values);
  204. if (count($results) > 0) {
  205. form_set_error('name', 'The vocabulary name must be unique');
  206. }
  207. }
  208. /**
  209. * Submit cv add form
  210. *
  211. * @ingroup tripal_cv
  212. */
  213. function tripal_cv_cv_add_form_submit($form, &$form_state) {
  214. $name = array_key_exists('name', $form_state['values']) ? trim($form_state['values']['name']) : '';
  215. $desc = array_key_exists('definition', $form_state['values']) ? trim($form_state['values']['definition']) : '';
  216. $values = [
  217. 'name' => $name,
  218. 'definition' => $desc,
  219. ];
  220. $success = chado_insert_record('cv', $values);
  221. if ($success) {
  222. drupal_set_message(t("Controlled vocabulary added"));
  223. drupal_goto('admin/tripal/loaders/chado_vocabs/chado_cvs');
  224. }
  225. else {
  226. drupal_set_message(t("Failed to add controlled vocabulary."));
  227. }
  228. }
  229. /**
  230. * Ajax callback for the tripal_cv_form
  231. *
  232. * @ingroup tripal_cv
  233. */
  234. function tripal_cv_edit_form_ajax($form, $form_state) {
  235. $elements = [];
  236. // add in the form fields and the buttons
  237. if (array_key_exists('cv_id', $form_state['values'])) {
  238. $elements['fields'] = $form;
  239. $elements['update'] = $form['update'];
  240. $elements['delete'] = $form['delete'];
  241. }
  242. // add back in the cv-edit-div that is used for the next round of AJAX
  243. $elements['fields']['#prefix'] = '<div id="cv-edit-div">';
  244. $elements['fields']['#suffix'] = '</div">';
  245. // reset the values for the fields to the defaults
  246. $elements['fields']['name']['#value'] = $elements['fields']['name']['#default_value'];
  247. $elements['fields']['definition']['#value'] = $elements['fields']['definition']['#default_value'];
  248. //drupal_set_message('<pre>' . print_r($elements, TRUE) . '</pre>', "status");
  249. return $elements;
  250. }
  251. /**
  252. * Form for editing cvterms
  253. *
  254. * @ingroup tripal_cv
  255. */
  256. function tripal_cv_cvterm_edit_form($form, &$form_state) {
  257. // set the breadcrumb
  258. $breadcrumb = [];
  259. $breadcrumb[] = l('Home', '<front>');
  260. $breadcrumb[] = l('Administration', 'admin');
  261. $breadcrumb[] = l('Tripal', 'admin/tripal');
  262. $breadcrumb[] = l('Data Loaders', 'admin/tripal/loaders');
  263. $breadcrumb[] = l('Chado Vocabularies', 'admin/tripal/loaders/chado_vocabs');
  264. $breadcrumb[] = l('Controlled Vocabulary Terms', 'admin/tripal/loaders/chado_vocabs/chado_cvterms');
  265. drupal_set_breadcrumb($breadcrumb);
  266. $step = 0;
  267. if (empty($form_state['storage']['step'])) {
  268. $form_state['storage']['step'] = 0;
  269. }
  270. else {
  271. $step = $form_state['storage']['step'];
  272. }
  273. $cv_id = 0;
  274. if ($step == 1) {
  275. $cv_id = $form_state['storage']['cv_id'];
  276. $cvterm_name = $form_state['storage']['name'];
  277. $cvterm_id = $form_state['storage']['cvterm_id'];
  278. }
  279. // get the cv if form was submitted via AJAX
  280. $cvterm = '';
  281. if (array_key_exists('values', $form_state)) {
  282. $cv_id = $form_state['values']['cv_id'];
  283. if (array_key_exists('cvterm', $form_state['values'])) {
  284. $cvterm = $form_state['values']['cvterm'];
  285. }
  286. }
  287. elseif (isset($form_state['build_info']['args'][0])) {
  288. $cv_id = $form_state['build_info']['args'][0];
  289. $cvterm_id = $form_state['build_info']['args'][1];
  290. if ($form_state['build_info']['args'][1]) {
  291. $cvterm_name = chado_query('SELECT name FROM {cvterm} WHERE cvterm_id = :id',
  292. [':id' => $cvterm_id])->fetchField();
  293. $step = 1;
  294. }
  295. }
  296. // get a list of CVs
  297. $cvs = [];
  298. $sql = "SELECT * FROM {cv} WHERE NOT name = 'tripal' ORDER BY name ";
  299. $results = chado_query($sql);
  300. $cvs[] = 'Select a vocabulary';
  301. foreach ($results as $cv) {
  302. $cvs[$cv->cv_id] = $cv->name;
  303. }
  304. $form['cv_id'] = [
  305. '#title' => t('Controlled Vocabulary Name'),
  306. '#type' => 'select',
  307. '#options' => $cvs,
  308. '#required' => TRUE,
  309. '#default_value' => $cv_id,
  310. '#ajax' => [
  311. 'callback' => 'tripal_cv_cvterm_edit_form_ajax',
  312. 'wrapper' => 'cvterm-edit-div',
  313. 'event' => 'change',
  314. 'method' => 'replace',
  315. 'event' => 'change',
  316. ],
  317. ];
  318. if ($cv_id and $step == 0) {
  319. $form['name'] = [
  320. '#type' => 'textfield',
  321. '#title' => t("Term Name"),
  322. '#default_value' => $cvterm,
  323. '#required' => TRUE,
  324. '#autocomplete_path' => "admin/tripal/tripal_cv/cvterm/auto_name/$cv_id",
  325. '#description' => t('Enter the term to edit.'),
  326. ];
  327. $form['continue'] = [
  328. '#type' => 'submit',
  329. '#value' => 'continue',
  330. ];
  331. }
  332. elseif ($step == 1) {
  333. tripal_cv_add_cvterm_form_fields($form, $form_state, $cv_id, $cvterm_name);
  334. // when editing there are certain fields the user should not change for a term
  335. // let's mark those as disabled
  336. $form['cv_id']['#disabled'] = TRUE;
  337. $form['db_id']['#disabled'] = TRUE;
  338. $form['accession']['#disabled'] = TRUE;
  339. // add in the div for replacing the fields if needed
  340. $form['#prefix'] = '<div id="cvterm-edit-div">';
  341. $form['#suffix'] = '</div>';
  342. // add in the cvterm id
  343. $form['cvterm_id'] = [
  344. '#type' => 'hidden',
  345. '#value' => $cvterm_id,
  346. ];
  347. $form['update'] = [
  348. '#type' => 'submit',
  349. '#value' => t('Update'),
  350. ];
  351. $form['delete'] = [
  352. '#type' => 'submit',
  353. '#value' => t('Delete'),
  354. '#attributes' => ['onclick' => 'if(!confirm("Really Delete?")){return false;}'],
  355. ];
  356. }
  357. if ($step == 0) {
  358. // if we don't have a cv_id then this is the first time the form has
  359. // benn loaded and we need to create the div where ajax replacement elements get stored
  360. $form['div_replace'] = [
  361. '#type' => 'item',
  362. '#prefix' => '<div id="cvterm-edit-div">',
  363. '#suffix' => '</div>',
  364. ];
  365. }
  366. return $form;
  367. }
  368. /**
  369. * Form for adding cvterms
  370. *
  371. * @ingroup tripal_cv
  372. */
  373. function tripal_cv_cvterm_add_form($form, &$form_state) {
  374. // set the breadcrumb
  375. $breadcrumb = [];
  376. $breadcrumb[] = l('Home', '<front>');
  377. $breadcrumb[] = l('Administration', 'admin');
  378. $breadcrumb[] = l('Tripal', 'admin/tripal');
  379. $breadcrumb[] = l('Data Loaders', 'admin/tripal/loaders');
  380. $breadcrumb[] = l('Chado Vocabularies', 'admin/tripal/loaders/chado_vocabs');
  381. $breadcrumb[] = l('Controlled Vocabulary Terms', 'admin/tripal/loaders/chado_vocabs/chado_cvterms');
  382. drupal_set_breadcrumb($breadcrumb);
  383. $cv_id = 0;
  384. if (array_key_exists('values', $form_state)) {
  385. $cv_id = $form_state['values']['cv_id'];
  386. }
  387. elseif (isset($form_state['build_info']['args'][0])) {
  388. $cv_id = $form_state['build_info']['args'][0];
  389. }
  390. // get a list of CVs
  391. $cvs = [];
  392. $sql = "SELECT * FROM {cv} WHERE NOT name = 'tripal' ORDER BY name ";
  393. $results = chado_query($sql);
  394. $cvs[] = 'Select a vocabulary';
  395. foreach ($results as $cv) {
  396. $cvs[$cv->cv_id] = $cv->name;
  397. }
  398. $form['cv_id'] = [
  399. '#title' => t('Controlled Vocabulary (Ontology) Name'),
  400. '#type' => 'select',
  401. '#options' => $cvs,
  402. '#required' => TRUE,
  403. '#default_value' => $cv_id,
  404. ];
  405. tripal_cv_add_cvterm_form_fields($form, $form_state);
  406. $form['add'] = [
  407. '#type' => 'submit',
  408. '#value' => t('Add Term'),
  409. ];
  410. return $form;
  411. }
  412. /**
  413. * Form fields in common between add/edit forms
  414. *
  415. * @ingroup tripal_cv
  416. */
  417. function tripal_cv_add_cvterm_form_fields(&$form, $form_state, $cv_id = 0, $cvterm_name = '') {
  418. $name = '';
  419. $definition = '';
  420. $is_relationship = '';
  421. $is_obsolete = '';
  422. $db_id = '';
  423. $accession = '';
  424. $cvterm = NULL;
  425. // get default values
  426. if ($cvterm_name) {
  427. $values = ['cv_id' => $cv_id, 'name' => $cvterm_name];
  428. $cvterm = chado_generate_var('cvterm', $values);
  429. $name = $cvterm->name;
  430. $definition = $cvterm->definition;
  431. $is_relationship = $cvterm->is_relationshiptype;
  432. $is_obsolete = $cvterm->is_obsolete;
  433. $db_id = $cvterm->dbxref_id->db_id->db_id;
  434. $accession = $cvterm->dbxref_id->accession;
  435. }
  436. $form['name'] = [
  437. '#type' => 'textfield',
  438. '#title' => t("Term Name"),
  439. '#default_value' => $name,
  440. '#description' => t('The term must be unique within the database selected below.'),
  441. '#required' => TRUE,
  442. ];
  443. $form['internal_id'] = [
  444. '#type' => 'item',
  445. '#title' => t("Internal ID"),
  446. '#markup' => $cvterm ? $cvterm->cvterm_id : '',
  447. ];
  448. $form['definition'] = [
  449. '#type' => 'textarea',
  450. '#title' => t('Description'),
  451. '#description' => t('Please enter a description for this term'),
  452. '#default_value' => $definition,
  453. ];
  454. $form['is_relationship'] = [
  455. '#type' => 'checkbox',
  456. '#title' => t('This term describes a relationship?'),
  457. '#default_value' => $is_relationship,
  458. ];
  459. $form['is_obsolete'] = [
  460. '#type' => 'checkbox',
  461. '#title' => t('This term is obsolete?'),
  462. '#default_value' => $is_obsolete,
  463. ];
  464. $values = [];
  465. $columns = ['db_id', 'name'];
  466. $options = ['order_by' => ['name' => 'ASC']];
  467. $results = chado_select_record('db', $columns, $values, $options);
  468. $dbs = [];
  469. $dbs[] = '';
  470. foreach ($results as $db) {
  471. $dbs[$db->db_id] = $db->name;
  472. }
  473. $form['db_id'] = [
  474. '#type' => 'select',
  475. '#title' => t('Database'),
  476. '#description' => t('All terms must be assocated with a database. If there is no database for this term (e.g. it is a custom term specific to this site) then select the database \'null\' or consider creating a database specific for your site and use that anytime you would like to add terms.'),
  477. '#options' => $dbs,
  478. '#default_value' => $db_id,
  479. '#required' => TRUE,
  480. ];
  481. $form['accession'] = [
  482. '#type' => 'textfield',
  483. '#title' => t("Accession"),
  484. '#description' => t('If this term has an existing accession (unique identifier) in the database
  485. please enter that here. If the accession is numeric with a database prefix (e.g. GO:003023), please
  486. enter just the numeric value. The database prefix will be appended whenever the term is displayed.
  487. If you do not have a numeric value consider entering the term name as the accession.'),
  488. '#required' => TRUE,
  489. '#default_value' => $accession,
  490. ];
  491. }
  492. /**
  493. * Validate cvterm edit form
  494. *
  495. * @ingroup tripal_cv
  496. */
  497. function tripal_cv_cvterm_edit_form_validate($form, &$form_state) {
  498. $cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : '';
  499. $db_id = array_key_exists('db_id', $form_state['values']) ? $form_state['values']['db_id'] : '';
  500. $name = array_key_exists('name', $form_state['values']) ? $form_state['values']['name'] : '';
  501. $cvterm_id = array_key_exists('cvterm_id', $form_state['values']) ? $form_state['values']['cvterm_id'] : '';
  502. $accession = array_key_exists('accession', $form_state['values']) ? $form_state['values']['accession'] : '';
  503. $step = $form_state['storage']['step'];
  504. // make sure the cv term name is unique for this vocabulary
  505. if ($step == 1) {
  506. $values = ['name' => $name, 'cv_id' => $cv_id];
  507. $results = chado_select_record('cvterm', ['cvterm_id'], $values);
  508. foreach ($results as $r) {
  509. if ($r->cvterm_id != $cvterm_id) {
  510. form_set_error('name', 'The term name must be unique for this vocabulary. Another term with this name already exists.');
  511. }
  512. }
  513. }
  514. }
  515. /**
  516. * Validate cv add form
  517. *
  518. * @ingroup tripal_cv
  519. */
  520. function tripal_cv_cvterm_add_form_validate($form, &$form_state) {
  521. $cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : '';
  522. $db_id = array_key_exists('db_id', $form_state['values']) ? $form_state['values']['db_id'] : '';
  523. $name = array_key_exists('name', $form_state['values']) ? $form_state['values']['name'] : '';
  524. $accession = array_key_exists('accession', $form_state['values']) ? $form_state['values']['accession'] : '';
  525. $values = ['cv_id' => $cv_id];
  526. $results = chado_select_record('cv', ['name'], $values);
  527. if (!$results or count($results) == 0) {
  528. form_set_error('cv_id', 'The controlled vocabulary does not exist');
  529. }
  530. // make sure the DB exists
  531. $values = ['db_id' => $db_id];
  532. $results = chado_select_record('db', ['name'], $values);
  533. if (!$results or count($results) == 0) {
  534. form_set_error('db_id', 'The database name does not exist');
  535. }
  536. // make sure the cv term name is unique for this vocabulary
  537. $values = ['name' => $name, 'cv_id' => $cv_id];
  538. $results = chado_select_record('cvterm', ['cvterm_id'], $values);
  539. if (count($results) > 0) {
  540. form_set_error('name', 'The term name must be unique for this vocabulary. Another term with this name already exists.');
  541. }
  542. // make sure this accession is unique for the database
  543. $values = ['accession' => $accession, 'db_id' => $db_id];
  544. $results = chado_select_record('dbxref', ['dbxref_id'], $values);
  545. if (count($results) > 0) {
  546. form_set_error('accession', 'The accession is not uniuqe for this vocabulary\'s database.');
  547. }
  548. }
  549. /**
  550. * Edits existing controlled vocabulary terms
  551. *
  552. * @ingroup tripal_cv
  553. */
  554. function tripal_cv_cvterm_edit_form_submit($form, &$form_state) {
  555. $cv_id = array_key_exists('cv_id', $form_state['values']) ? trim($form_state['values']['cv_id']) : '';
  556. $name = array_key_exists('name', $form_state['values']) ? trim($form_state['values']['name']) : '';
  557. $definition = array_key_exists('definition', $form_state['values']) ? trim($form_state['values']['definition']) : '';
  558. $is_relationship = array_key_exists('is_relationship', $form_state['values']) ? trim($form_state['values']['is_relationship']) : '';
  559. $is_obsolete = array_key_exists('is_obsolete', $form_state['values']) ? trim($form_state['values']['is_obsolete']) : '';
  560. $cvterm_id = array_key_exists('cvterm_id', $form_state['values']) ? trim($form_state['values']['cvterm_id']) : '';
  561. $db_id = array_key_exists('db_id', $form_state['values']) ? trim($form_state['values']['db_id']) : '';
  562. $accession = array_key_exists('accession', $form_state['values']) ? trim($form_state['values']['accession']) : '';
  563. $op = array_key_exists('op', $form_state['values']) ? trim($form_state['values']['op']) : '';
  564. if ($op == 'Update') {
  565. // get the original cvterm_id
  566. $values = ['name' => $name, 'cv_id' => $cv_id];
  567. $results = chado_select_record('cvterm', ['cvterm_id'], $values);
  568. $cvterm = $results[0];
  569. // get the cv
  570. $values = ['cv_id' => $cv_id];
  571. $results = chado_select_record('cv', ['name'], $values);
  572. $cv = $results[0];
  573. // get the db
  574. $values = ['db_id' => $db_id];
  575. $results = chado_select_record('db', ['name'], $values);
  576. $db = $results[0];
  577. // now add the term
  578. $term = [
  579. 'name' => $name,
  580. 'namespace' => $cv->name,
  581. 'id' => $accession,
  582. 'definition' => $definition,
  583. 'is_obsolete' => $is_obsolete,
  584. 'cv_name' => $cv->name,
  585. 'is_relationship' => $is_relationship,
  586. 'db_name' => $db->name,
  587. ];
  588. $cvterm = chado_insert_cvterm($term, ['update_existing' => TRUE]);
  589. if ($cvterm) {
  590. drupal_set_message('Term updated successfully.');
  591. drupal_goto('admin/tripal/loaders/chado_vocabs/chado_cvterms');
  592. }
  593. else {
  594. drupal_set_message('Could not add term. Check Drupal recent logs for error messages.', 'error');
  595. }
  596. }
  597. if ($op == 'Delete') {
  598. $values = ['cvterm_id' => $cvterm_id];
  599. $success = chado_delete_record('cvterm', $values);
  600. if ($success) {
  601. drupal_set_message('Term deleted successfully.');
  602. }
  603. else {
  604. drupal_set_message('Could not delete term term. Check Drupal recent logs for error messages.', 'error');
  605. }
  606. }
  607. }
  608. /**
  609. * Adds new terms to an existing cv
  610. *
  611. * @ingroup tripal_cv
  612. */
  613. function tripal_cv_cvterm_add_form_submit($form, &$form_state) {
  614. $cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : '';
  615. $name = array_key_exists('name', $form_state['values']) ? $form_state['values']['name'] : '';
  616. $definition = array_key_exists('definition', $form_state['values']) ? $form_state['values']['definition'] : '';
  617. $is_relationship = array_key_exists('is_relationship', $form_state['values']) ? $form_state['values']['is_relationship'] : '';
  618. $is_obsolete = array_key_exists('is_obsolete', $form_state['values']) ? $form_state['values']['is_obsolete'] : '';
  619. $db_id = array_key_exists('db_id', $form_state['values']) ? $form_state['values']['db_id'] : '';
  620. $accession = array_key_exists('accession', $form_state['values']) ? $form_state['values']['accession'] : '';
  621. // get the database
  622. $values = ['db_id' => $db_id];
  623. $results = chado_select_record('db', ['name'], $values);
  624. $db = $results[0];
  625. // get the cv
  626. $values = ['cv_id' => $cv_id];
  627. $results = chado_select_record('cv', ['name'], $values);
  628. $cv = $results[0];
  629. // now add the term
  630. $term = [
  631. 'name' => $name,
  632. 'namespace' => $cv->name,
  633. 'id' => $accession,
  634. 'definition' => $definition,
  635. 'is_obsolete' => $is_obsolete,
  636. 'cv_name' => $cv->name,
  637. 'is_relationship' => $is_relationship,
  638. 'db_name' => $db->name,
  639. ];
  640. $cvterm = chado_insert_cvterm($term, ['update_existing' => TRUE]);
  641. if ($cvterm) {
  642. drupal_set_message('Term added successfully.');
  643. }
  644. else {
  645. drupal_set_message('Could not add term. Check Drupal recent logs for error messages.', 'error');
  646. }
  647. }
  648. /**
  649. * Ajax callback for the tripal_cv_form
  650. *
  651. * @ingroup tripal_cv
  652. */
  653. function tripal_cv_cvterm_edit_form_ajax($form, $form_state) {
  654. $elements = [];
  655. $elements['name'] = $form['name'];
  656. $elements['continue'] = $form['continue'];
  657. // add back in the cv-edit-div that is used for the next round of AJAX
  658. $elements['name']['#prefix'] = '<div id="cvterm-edit-div">';
  659. $elements['name']['#suffix'] = '</div">';
  660. return $elements;
  661. }
  662. /**
  663. * Form for re-doing the cvterm path
  664. *
  665. * @ingroup tripal_cv
  666. */
  667. function tripal_cv_cvtermpath_form() {
  668. // get a list of db from chado for user to choose
  669. $sql = "SELECT * FROM {cv} WHERE NOT name = 'tripal' ORDER BY name ";
  670. $results = chado_query($sql);
  671. $cvs = [];
  672. $cvs[] = '';
  673. foreach ($results as $cv) {
  674. $cvs[$cv->cv_id] = $cv->name;
  675. }
  676. $form['cvid'] = [
  677. '#title' => t('Controlled Vocabulary/Ontology Name'),
  678. '#type' => 'select',
  679. '#options' => $cvs,
  680. '#description' => t('The Chado cvtermpath is a database table that provides lineage for ontology terms
  681. and is useful for quickly finding any ancestor parent of a term. This table must be populated for each
  682. ontology. Select a controlled vocabulary for which you would like to upate the cvtermpath.'),
  683. ];
  684. $form['description'] = [
  685. '#type' => 'item',
  686. '#value' => t("Submit a job to update chado cvtermpath table."),
  687. '#weight' => 1,
  688. ];
  689. $form['button'] = [
  690. '#type' => 'submit',
  691. '#value' => t('Update cvtermpath'),
  692. '#weight' => 2,
  693. ];
  694. return $form;
  695. }
  696. /**
  697. * Cvterm path form submit
  698. *
  699. * @ingroup tripal_cv
  700. */
  701. function tripal_cv_cvtermpath_form_submit($form, &$form_state) {
  702. global $user;
  703. $cvid = $form_state['values']['cvid'];
  704. // first get the controlled vocabulary name:
  705. $sql = "SELECT * FROM {cv} WHERE cv_id = :cv_id";
  706. $cv = chado_query($sql, [':cv_id' => $cvid])->fetchObject();
  707. // Submit a job to update cvtermpath
  708. $job_args = [$cvid];
  709. if ($form_state['values']['op'] == t('Update cvtermpath')) {
  710. tripal_add_job("Update cvtermpath: $cv->name", 'tripal_cv',
  711. 'chado_update_cvtermpath', $job_args, $user->uid, 10);
  712. }
  713. }
  714. /**
  715. * A confirmation form for deleting a controlled vocabulary.
  716. */
  717. function tripal_cv_cv_delete_form($form, &$form_state, $cv_id) {
  718. $cv = chado_get_cv(['cv_id' => $cv_id]);
  719. $form['cv_id'] = [
  720. '#type' => 'value',
  721. '#value' => $cv_id,
  722. ];
  723. return confirm_form($form,
  724. t('Confirm removal of the vocabulary: "' . $cv->name . '"? '),
  725. 'admin/tripal/loaders/chado_vocabs/chado_cv/edit/' . $cv_id,
  726. t('WARNING: removal of a vocabulary will result in removal of all terms, and any associations used for other records in the site that use those terms.')
  727. );
  728. }
  729. /**
  730. * Implements the submit hook for tripal_cv_cv_delete_form.
  731. */
  732. function tripal_cv_cv_delete_form_submit($form, &$form_state) {
  733. $cv_id = $form_state['values']['cv_id'];
  734. try {
  735. $match = ['cv_id' => $cv_id];
  736. $success = chado_delete_record('cv', $match);
  737. drupal_set_message(t("Controlled vocabulary deleted"));
  738. drupal_goto('admin/tripal/loaders/chado_vocabs/chado_cv');
  739. } catch (Exception $e) {
  740. drupal_set_message(t("Failed to delete controlled vocabulary."), 'error');
  741. }
  742. }