tripal_chado.cv.inc 28 KB

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