123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469 |
- <?php
- /**
- * @file tripal_jbrowse_mgmt_tracks.form.inc
- */
- /**
- * Add a track to an instance form.
- *
- * @param array $form
- * The drupal form.
- * @param array $form_state
- * The form state array.
- * @param int $instance_id
- * The parent instance ID to add the new track to.
- *
- * @return array
- * The modified form array.
- */
- function tripal_jbrowse_mgmt_add_track_form($form, &$form_state, $instance_id) {
- if (empty(tripal_jbrowse_mgmt_get_instance($instance_id))) {
- drupal_not_found();
- return [];
- }
- $form['label'] = [
- '#type' => 'textfield',
- '#title' => t('Track Label'),
- '#description' => t('This will appear on the sidebar.'),
- '#required' => TRUE,
- ];
- $form['instance_id'] = [
- '#type' => 'hidden',
- '#value' => $instance_id,
- ];
- $form['data'] = [
- '#type' => 'fieldset',
- '#title' => t('Track Files'),
- ];
- $form['data']['track_type'] = [
- '#type' => 'select',
- '#description' => t('See http://gmod.org/wiki/JBrowse_Configuration_Guide#flatfile-to-json.pl for more info.'),
- '#required' => TRUE,
- '#title' => t('Track Type'),
- '#options' => drupal_map_assoc(tripal_jbrowse_mgmt_get_track_types()),
- ];
- $form['data']['file_type'] = [
- '#type' => 'select',
- '#title' => t('File Type'),
- '#options' => drupal_map_assoc(['gff', 'bed', 'gbk', 'bam', 'cram', 'vcf', 'bw']),
- '#description' => t('See http://gmod.org/wiki/JBrowse_Configuration_Guide#flatfile-to-json.pl for more info.'),
- '#required' => TRUE,
- ];
- $form['data']['file'] = [
- '#type' => 'file',
- '#title' => t('File'),
- ];
- $form['data']['file2'] = [
- '#type' => 'file',
- '#title' => t('Index File'),
- '#states' => [
- 'visible' => [
- [':input[name="file_type"]' => ['value' => 'vcf'],],
- [':input[name="file_type"]' => ['value' => 'bam'],],
- [':input[name="file_type"]' => ['value' => 'cram'],],
- ],
- ],
- ];
- $form['data']['file_path'] = [
- '#type' => 'textfield',
- '#title' => t('- OR Path to File on Server -'),
- '#description' => t('This path will be ignored if a file is provided above. Ex: sites/default/files/file.fasta or /data/file.fasta'),
- '#maxlength' => 255,
- '#states' => [
- 'invisible' => [
- [':input[name="file_type"]' => ['value' => 'vcf'],],
- [':input[name="file_type"]' => ['value' => 'bam'],],
- [':input[name="file_type"]' => ['value' => 'cram'],],
- ],
- ],
- ];
- $form['data']['dir_path'] = [
- '#type' => 'textfield',
- '#title' => t('- OR Path to Directory on Server -'),
- '#description' => t('This path will be ignored if a file is provided above. ' . 'The directory must contain both the .tbi and .gz files.'),
- '#maxlength' => 255,
- '#states' => [
- 'visible' => [
- [':input[name="file_type"]' => ['value' => 'vcf'],],
- [':input[name="file_type"]' => ['value' => 'bam'],],
- [':input[name="file_type"]' => ['value' => 'cram'],],
- ],
- ],
- ];
- $form['data']['symbolic_link'] = [
- '#type' => 'checkbox',
- '#title' => t('Symbolic Link'),
- '#default_value' => true,
- '#description' => t('Create a symbolic link rather than make a copy of the file. This only applies when a path on the server is supplied.<br>Please have Symbolic Link selected if the same file is used for new track.'),
- ];
- $form['submit'] = [
- '#type' => 'submit',
- '#value' => 'Add New Track',
- ];
- return $form;
- }
- /**
- * Validate the add track form.
- *
- * This function also takes care of the uploading of files.
- * TODO: If the file is invalid, delete it!
- *
- * @param array $form
- * The Drupal form.
- * @param array $form_state
- * The form state.
- */
- function tripal_jbrowse_mgmt_add_track_form_validate($form, &$form_state) {
- $values = $form_state['values'];
- $file = $_FILES['files']['tmp_name']['file'];
- $settings = tripal_jbrowse_mgmt_get_settings();
- $instance = tripal_jbrowse_mgmt_get_instance($values['instance_id']);
- $data = $settings['data_dir'];
- $file_type = $values['file_type'];
- $symbolic_link = $values['symbolic_link'];
- $path = NULL;
- $base_path = $data . '/' . tripal_jbrowse_mgmt_make_slug($instance->title);
- if(isset($instance->analysis_id)){
- $base_path = $base_path . '_' . $instance->analysis_id;
- }
- $base_path = $base_path . '/data';
- if ($file_type === 'bw') {
- $path = $base_path . '/wig';
- }
- else {
- $path = $base_path;
- }
- switch ($file_type) {
- case 'vcf':
- $path = $base_path . '/vcf';
- $index = $_FILES['files']['tmp_name']['file2'];
- $form_state = tripal_jbrowse_mgmt_validate_folder_upload($file, $index, $path, $form_state);
- break;
- case 'bam':
- $path = $base_path . '/bam';
- $index = $_FILES['files']['tmp_name']['file2'];
- $form_state = tripal_jbrowse_mgmt_validate_folder_upload($file, $index, $path, $form_state);
- break;
- case 'cram':
- $path = $base_path . '/bam';
- $index = $_FILES['files']['tmp_name']['file2'];
- $form_state = tripal_jbrowse_mgmt_validate_folder_upload($file, $index, $path, $form_state);
- break;
- default:
- $local_file = isset($values['file_path']) ? $values['file_path'] : NULL;
- if (empty($file) && empty($local_file)) {
- form_set_error('file',
- 'Please provide a local file path or upload a new file.');
- }
- elseif (empty($file) && !empty($local_file)) {
- if (!file_exists($local_file)) {
- form_set_error('file_path', 'The file path provided does not exist.');
- }
- else {
- try {
- if (!tripal_jbrowse_mgmt_copy_file($local_file, $path, $symbolic_link)) {
- form_set_error('file_path', 'Failed to copy file ' . $local_file . ' to ' . $path. '. If this track is expected to create by existed file, please have Symbolic Link selected.');
- }
- } catch (Exception $exception) {
- form_set_error('file_path', 'Failed to copy file ' . $local_file . ' to ' . $path.'. If this track is expected to create by existed file, please have Symbolic Link selected.');
- }
- }
- }
- else {
- $uploaded = tripal_jbrowse_mgmt_upload_file('file');
- if (!$uploaded) {
- form_set_error('file', 'Unable to upload file');
- }
- else {
- $uploaded = tripal_jbrowse_mgmt_move_file($uploaded, $path);
- if (!isset($uploaded)) {
- form_set_error('file', 'Failed to move file to ' . $path);
- }
- else {
- $form_state['values']['uploaded_file'] = $uploaded;
- }
- }
- }
- break;
- }
- }
- /**
- * Handle form submission for adding a track.
- *
- * @param array $form
- * @param array $form_state
- *
- * @throws \Exception
- */
- function tripal_jbrowse_mgmt_add_track_form_submit($form, &$form_state) {
- global $user;
- $values = $form_state['values'];
- $file = isset($values['file_path']) ? $values['file_path'] : NULL;
- if (!empty($values['dir_path'])) {
- $file = $values['dir_path'];
- }
- if (!empty($values['uploaded_file_index'])) {
- $file = $values['uploaded_file_index'];
- }
- if (!empty($values['uploaded_file_data'])) {
- $file = $values['uploaded_file_data'];
- }
- $instance = tripal_jbrowse_mgmt_get_instance($values['instance_id']);
- $track_id = tripal_jbrowse_mgmt_create_track($instance, [
- 'label' => $values['label'],
- 'track_type' => $values['track_type'],
- 'file_type' => $values['file_type'],
- 'file' => $file,
- 'created_at' => time(),
- ]);
- tripal_add_job('Add JBrowse track to ' . $instance->title, 'tripal_jbrowse_mgmt',
- 'tripal_jbrowse_mgmt_add_track_to_instance', [$track_id], $user->uid);
- drupal_goto('admin/tripal/extension/tripal_jbrowse/management/instances/' . $instance->id);
- }
- /**
- * Delete a track form.
- *
- * @param array $form
- * @param array $form_state
- * @param int $track_id
- *
- * @return array
- */
- function tripal_jbrowse_mgmt_delete_track_form($form, &$form_state, $track_id) {
- $track = tripal_jbrowse_mgmt_get_track($track_id);
- if (!$track->id) {
- $form['error'] = [
- '#type' => 'item',
- '#markup' => '<p style="color: red">Unable to find track.</p>',
- ];
- return $form;
- }
- $form['description'] = [
- '#type' => 'item',
- '#markup' => 'Are you sure you want to delete the ' . $track->label . ' track?',
- ];
- $form['track_id'] = [
- '#type' => 'hidden',
- '#value' => $track_id,
- ];
- $form['submit'] = [
- '#type' => 'submit',
- '#value' => 'Delete Track',
- ];
- $form['cancel'] = [
- '#type' => 'markup',
- '#markup' => l('Cancel',
- 'admin/tripal/extension/tripal_jbrowse/management/instances/' . $track->instance_id),
- ];
- return $form;
- }
- /**
- * @param $form
- * @param $form_state
- *
- * @throws \Exception
- */
- function tripal_jbrowse_mgmt_delete_track_form_submit($form, &$form_state) {
- global $user;
- $values = $form_state['values'];
- $track = tripal_jbrowse_mgmt_get_track($values['track_id']);
- tripal_add_job('Delete JBrowse track', 'tripal_jbrowse_mgmt',
- 'tripal_jbrowse_mgmt_delete_track_from_instance', [$values['track_id']],
- $user->uid);
- tripal_jbrowse_mgmt_update_track($track, ['is_deleted' => 1]);
- drupal_goto('admin/tripal/extension/tripal_jbrowse/management/instances/' . $track->instance_id);
- }
- /**
- * Track json editor advance form.
- * allow user to make all configurations of a track
- *
- * @param array $form
- * @param array $form_state
- * @param int $track_id
- *
- * @return array
- * @throws \Exception
- */
- function tripal_jbrowse_mgmt_json_editor_advance_form($form, &$form_state, $track_id) {
- $track = tripal_jbrowse_mgmt_get_track($track_id);
- if (empty($track)) {
- drupal_not_found();
- return $form;
- }
- drupal_set_title('Edit Track Json: ' . $track->label);
- $breadcrumb = array();
- $breadcrumb[] = l('Home', '');
- $breadcrumb[] = l('Administration', 'admin');
- $breadcrumb[] = l('Tripal', 'admin/tripal');
- $breadcrumb[] = l('Extensions', 'admin/tripal/extension');
- $breadcrumb[] = l('Tripal JBrowse', 'admin/tripal/extension/tripal_jbrowse/management');
- $breadcrumb[] = l('Instance', 'admin/tripal/extension/tripal_jbrowse/management/instances/'.$track->instance_id);
- drupal_set_breadcrumb($breadcrumb);
- if (!$track->id) {
- $form['error'] = [
- '#type' => 'item',
- '#markup' => '<p style="color: red">Unable to find track.</p>',
- ];
- return $form;
- }
- $instance = tripal_jbrowse_mgmt_get_instance($track->instance_id);
- $json = tripal_jbrowse_mgmt_get_json($instance);
- $form_state['track_json'] = $json;
- $key = tripal_jbrowse_mgmt_make_slug($track->label);
- $track_json = NULL;
- $track_index = NULL;
- foreach ($json['tracks'] as $index => $jtrack) {
- if ($jtrack['label'] === $key) {
- $track_json = $jtrack;
- $track_index = $index;
- break;
- }
- }
- if (!$track_json) {
- $form['error'] = [
- '#type' => 'item',
- '#markup' => '<p style="color: red">Unable to find track in json!</p>',
- ];
- return $form;
- }
- $form['track_index'] = [
- '#type' => 'hidden',
- '#value' => $track_index,
- ];
- $form['track_id'] = [
- '#type' => 'hidden',
- '#value' => $track->id,
- ];
- $instr_detail = '<p><strong>Only use this functionality if the configuration option you want is not included in the "Track Manage" form.</strong>. For more information on the JBrowse track configuration see the '.l('JBrowse Documentation', 'http://jbrowse.org/docs/canvas_features.html').'.</p>
- <div class="messages warning">Please be extra cautious while editing track configuration, since it will make changes in file trackList.json directly. Also, do not change the "label" since it will disconnect this track from the JBrowse Management form.</div>';
- $form['Instruction']=[
- '#type' => 'markup',
- '#markup' => $instr_detail,
- ];
- $form['track_all_config'] = [
- '#type' => 'textarea',
- '#title' => t('Track configuration in json'),
- '#description' => t('The track info from trackList.json. Only '),
- '#default_value' => json_encode($track_json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES),
- '#rows' => '10',
- '#required' => TRUE,
- ];
- $form['submit'] = [
- '#type' => 'submit',
- '#value' => 'Save Track Configuration',
- ];
- return $form;
- }
- /**
- * Validate the form.
- *
- * @param $form
- * @param $form_state
- */
- function tripal_jbrowse_mgmt_json_editor_advance_form_validate($form, &$form_state) {
- $values = $form_state['values'];
- $track_all_config = $values['track_all_config'] ?? NULL;
- $track_key = $values['track_index'];
- if ($track_all_config && !empty($track_all_config)) {
- if (!json_decode($track_all_config)) {
- form_set_error(
- 'track_all_config',
- 'Invalid JSON. Please verify that the menu template contains only valid JSON.'
- );
- }
- }
- $json_before_edit = $form_state['track_json']['tracks'][$track_key];
- $json_after_edit = json_decode($form_state['values']['track_all_config'], TRUE);
- if (($json_before_edit['key'] != $json_after_edit['key']) OR ($json_before_edit['label'] != $json_after_edit['label'])){
- form_set_error('track_all_config', 'Key or Label changed. Please don\'t change Key or Label here. The functionality is provided in "Track Manage" form.');
- }
- }
- /**
- * @param array $form
- * @param array $form_state
- *
- * @throws \Exception
- */
- function tripal_jbrowse_mgmt_json_editor_advance_form_submit($form, &$form_state) {
- $values = $form_state['values'];
- $track_index = $values['track_index'];
- $track_all_config = $values['track_all_config'] ?? NULL;
- $track = tripal_jbrowse_mgmt_get_track($values['track_id']);
- $json = $form_state['track_json'];
- $json['tracks'][$track_index] = json_decode($track_all_config, TRUE);
- tripal_jbrowse_mgmt_update_track($track, ['label' => $json['tracks'][$track_index]['key']]);
- if (tripal_jbrowse_mgmt_save_json($track->instance, $json) === FALSE) {
- drupal_set_message(
- 'Unable to save JSON file. Please make sure it\'s editable.'
- );
- return;
- }
- drupal_set_message('Track updated successfully');
- }
|