tripal_chado.cv.inc 27 KB

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