tripal_chado.cv.inc 26 KB

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