tripal_chado.cv.inc 28 KB

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