123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485 |
- <?php
- /**
- * Get saved settings.
- *
- * @return null
- */
- function tripal_jbrowse_mgmt_get_settings() {
- $default = [
- 'bin_path' => '',
- 'link' => '',
- 'data_dir' => '',
- 'menu_template' => [],
- ];
- $variable = variable_get('tripal_jbrowse_mgmt_settings', json_encode($default));
- $settings = json_decode($variable, TRUE) + $default;
- return $settings;
- }
- /**
- * Save settings.
- *
- * @param array $settings
- *
- * @return array The final merged settings
- */
- function tripal_jbrowse_mgmt_save_settings($settings) {
- $default = [
- 'bin_path' => '',
- 'link' => '',
- 'data_dir' => '',
- 'menu_template' => [],
- ];
- $final = $settings + $default;
- variable_set('tripal_jbrowse_mgmt_settings', json_encode($final));
- return $final;
- }
- /**
- * Get an array to instances.
- *
- * @return mixed
- */
- function tripal_jbrowse_mgmt_get_instances($conditions = NULL) {
- static $users = [];
- static $organisms = [];
- $instances = db_select('tripal_jbrowse_mgmt_instances', 'H')->fields('H');
- if ($conditions) {
- foreach ($conditions as $field => $value) {
- $instances->condition($field, $value);
- }
- }
- $instances = $instances->execute()->fetchAll();
- foreach ($instances as $key => &$instance) {
- if (!isset($users[$instance->uid])) {
- $users[$instance->uid] = user_load($instance->uid);
- }
- $instance->user = $users[$instance->uid];
- if (!isset($organisms[$instance->organism_id])) {
- $organisms[$instance->organism_id] = chado_query('SELECT * FROM {organism} WHERE organism_id=:id',
- [':id' => $instance->organism_id])->fetchObject();
- }
- $instance->organism = $organisms[$instance->organism_id];
- }
- return $instances;
- }
- /**
- * Create a new instance.
- *
- * @param $data
- *
- * @return \DatabaseStatementInterface|int
- * @throws \Exception
- */
- function tripal_jbrowse_mgmt_create_instance($data) {
- global $user;
- $instance_id = db_insert('tripal_jbrowse_mgmt_instances')->fields([
- 'uid' => $user->uid,
- 'organism_id' => $data['organism_id'],
- 'title' => $data['title'],
- 'description' => isset($data['description']) ? $data['description'] : '',
- 'created_at' => $data['created_at'],
- 'file' => $data['file'],
- ])->execute();
- if (!$instance_id) {
- return FALSE;
- }
- return $instance_id;
- }
- /**
- * Get an instance by id.
- *
- * @param $instance_id
- *
- * @return mixed
- */
- function tripal_jbrowse_mgmt_get_instance($instance_id) {
- $instance = tripal_jbrowse_mgmt_get_instances(['id' => $instance_id]);
- return reset($instance);
- }
- /**
- * Update an instance.
- *
- * @param int $id
- * @param array $data
- *
- * @return \DatabaseStatementInterface
- */
- function tripal_jbrowse_mgmt_update_instance($id, $data) {
- return db_update('tripal_jbrowse_mgmt_instances')
- ->fields($data)
- ->condition('id', $id)
- ->execute();
- }
- /**
- * @param int|object $instance
- *
- * @return int
- * @throws \Exception
- */
- function tripal_jbrowse_mgmt_delete_instance($instance) {
- if (is_object($instance) && property_exists($instance, 'id')) {
- $id = $instance->id;
- }
- elseif (is_numeric($instance)) {
- $id = $instance;
- }
- else {
- throw new Exception('Unable to extract instance ID. Please provide a valid ID to delete the instance.');
- }
- return db_delete('tripal_jbrowse_mgmt_instances')
- ->condition('id', $id)
- ->execute();
- }
- /**
- * Create a new JBrowse track for a given instance.
- *
- * @param $data
- *
- * @return bool|int Track ID or FALSE if an error occurs.
- * @throws \Exception
- */
- function tripal_jbrowse_mgmt_create_track($instance, $data) {
- global $user;
- $track_id = db_insert('tripal_jbrowse_mgmt_tracks')->fields([
- 'uid' => $user->uid,
- 'instance_id' => $instance->id,
- 'organism_id' => $instance->organism_id,
- 'label' => $data['label'],
- 'track_type' => $data['track_type'],
- 'file_type' => $data['file_type'],
- 'created_at' => $data['created_at'],
- 'file' => $data['file'],
- ])->execute();
- if (!$track_id) {
- return FALSE;
- }
- return $track_id;
- }
- /**
- * Delete a track by ID.
- *
- * @param $track_id
- *
- * @return int
- */
- function tripal_jbrowse_mgmt_delete_track($track_id) {
- return db_delete('tripal_jbrowse_mgmt_tracks')
- ->condition('id', $track_id)
- ->execute();
- }
- /**
- * Get attached tracks with users pre-loaded.
- *
- * @param $instance
- *
- * @return mixed
- */
- function tripal_jbrowse_mgmt_get_tracks($instance, array $conditions = []) {
- static $users = [];
- $tracks = db_select('tripal_jbrowse_mgmt_tracks', 'HJT')->fields('HJT');
- foreach ($conditions as $field => $value) {
- $tracks->condition($field, $value);
- }
- $tracks = $tracks->condition('instance_id', $instance->id)
- ->execute()
- ->fetchAll();
- foreach ($tracks as &$track) {
- if (!isset($users[$track->uid])) {
- $users[$track->uid] = user_load($track->uid);
- }
- $track->user = $users[$track->uid];
- }
- return $tracks;
- }
- /**
- * Get a track with instance and user data attached.
- *
- * @param $track_id
- *
- * @return mixed
- */
- function tripal_jbrowse_mgmt_get_track($track_id) {
- $track = db_select('tripal_jbrowse_mgmt_tracks', 'HJT')
- ->fields('HJT')
- ->condition('id', $track_id)
- ->execute()
- ->fetchObject();
- $track->user = user_load($track->uid);
- $track->instance = tripal_jbrowse_mgmt_get_instance($track->instance_id);
- return $track;
- }
- /**
- * @param $track
- * @param array $fields
- *
- * @return \DatabaseStatementInterface
- */
- function tripal_jbrowse_mgmt_update_track($track, array $fields) {
- return db_update('tripal_jbrowse_mgmt_tracks')
- ->fields($fields)
- ->condition('id', $track->id)
- ->execute();
- }
- /**
- * Get a list of organisms.
- *
- * @return mixed
- */
- function tripal_jbrowse_mgmt_get_organisms_list() {
- return db_select('chado.organism', 'CO')
- ->fields('CO', ['organism_id', 'genus', 'species', 'common_name'])
- ->execute()
- ->fetchAll();
- }
- function tripal_jbrowse_mgmt_construct_organism_name($organism) {
- $name = $organism->genus;
- $name .= " $organism->species";
- if (!empty($organism->common_name)) {
- $name .= " ($organism->common_name)";
- }
- return $name;
- }
- function tripal_jbrowse_mgmt_make_slug($string) {
- $slug = str_replace(' ', '_', $string);
- $slug = str_replace('(', '_', $slug);
- $slug = str_replace(')', '_', $slug);
- $slug = str_replace('[', '_', $slug);
- $slug = str_replace(']', '_', $slug);
- $slug = str_replace('!', '_', $slug);
- $slug = str_replace('?', '_', $slug);
- $slug = str_replace('"', '_', $slug);
- $slug = str_replace('\'', '_', $slug);
- $slug = str_replace('\\', '_', $slug);
- $slug = str_replace(':', '_', $slug);
- return strtolower(trim($slug, '_'));
- }
- function tripal_jbrowse_mgmt_upload_file($field) {
- $file = file_save_upload($field, [
- 'file_validate_extensions' => ['fasta faa fna fastq txt gff vcf wig gz tbi bw'],
- 'file_validate_size' => [1024 * 1024 * 1024 * 20] // Make it 20 GB max
- ]);
- return !$file ? FALSE : $file; // drupal_realpath($file->uri);
- }
- /**
- * Moves a file to an intermediate directory, then to the destination, if given.
- *
- * @param $file
- * The file object.
- *
- * @param $path
- * The path to the directory of the new object.
- * If the directory provided does not exist, it will be created.
- *
- * @return
- * The path to the moved file, or NULL on fail.
- */
- function tripal_jbrowse_mgmt_move_file($file, $path = NULL) {
- $directory = 'public://tripal/tripal_jbrowse_mgmt';
- file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
- $file = file_move($file, $directory, FILE_EXISTS_REPLACE);
- if (isset($path)) {
- file_prepare_directory($path, FILE_CREATE_DIRECTORY);
- $oldname = drupal_realpath($file->uri);
- $newname = $path . '/' . $file->filename;
- if (!rename($oldname, $newname)) {
- return NULL;
- }
- }
- return isset($path) ? $newname : drupal_realpath($file->uri);
- }
- /**
- * Copy a file into a new directory.
- * If the directory provided does not exist, it will be created.
- *
- * @param $source
- * File path of the source file.
- * @param $destination
- * File path to the destination directory.
- *
- * @return bool
- */
- function tripal_jbrowse_mgmt_copy_file($source, $destination) {
- file_prepare_directory($destination, FILE_CREATE_DIRECTORY);
- return file_unmanaged_copy($source, $destination, FILE_EXISTS_ERROR);
- }
- /**
- * Build the http query for a given instance to link to JBrowse.
- *
- * @param $instance
- *
- * @return array
- */
- function tripal_jbrowse_mgmt_build_http_query($instance) {
- $path = tripal_jbrowse_mgmt_make_slug($instance->title);
- $tracks = tripal_jbrowse_mgmt_get_tracks($instance);
- $tracks_path = '';
- if (!empty($tracks)) {
- $tracks_path = implode(',', array_map(function ($track) {
- return tripal_jbrowse_mgmt_make_slug($track->label);
- }, $tracks));
- }
- return [
- 'data' => "data/$path/data",
- 'tracks' => $tracks_path,
- ];
- }
- /**
- * Get trackList.json for an instance.
- *
- * @param object $instance
- *
- * @return array Decoded json array.
- *
- * @throws \Exception
- */
- function tripal_jbrowse_mgmt_get_json($instance) {
- $path = tripal_jbrowse_mgmt_get_track_list_file_path($instance);
- $contents = file_get_contents($path);
- if (!$contents) {
- throw new Exception('Unable to find ' . $path . ' file');
- }
- return json_decode($contents, TRUE);
- }
- function tripal_jbrowse_mgmt_get_track_json($track) {
- if (!$track->instance) {
- $track->instance = tripal_jbrowse_mgmt_get_instance($track->instance_id);
- }
- $json = tripal_jbrowse_mgmt_get_json($track->instance);
- $key = tripal_jbrowse_mgmt_make_slug($track->label);
- $track_json = NULL;
- foreach ($json['tracks'] as $index => $jtrack) {
- if ($jtrack['label'] === $key) {
- $track_json = $jtrack;
- break;
- }
- }
- return $track_json;
- }
- /**
- * @param object $track Track object
- * @param array $track_json Edited track array.
- *
- * @throws \Exception
- */
- function tripal_jbrowse_mgmt_save_track_json($track, $track_json) {
- if (!$track->instance) {
- $track->instance = tripal_jbrowse_mgmt_get_instance($track->instance_id);
- }
- $json = tripal_jbrowse_mgmt_get_json($track->instance);
- $key = tripal_jbrowse_mgmt_make_slug($track->label);
- foreach ($json['tracks'] as $index => $jtrack) {
- if ($jtrack['label'] === $key) {
- $json['tracks'][$index] = $track_json;
- break;
- }
- }
- return tripal_jbrowse_mgmt_save_json($track->instance, $json);
- }
- /**
- * @param object $instance
- * @param array $data
- *
- * @throws \Exception
- * @return bool|int
- */
- function tripal_jbrowse_mgmt_save_json($instance, $data) {
- $path = tripal_jbrowse_mgmt_get_track_list_file_path($instance);
- $default = tripal_jbrowse_mgmt_get_json($instance);
- $json = $data + $default;
- $encoded = json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
- return file_put_contents($path, $encoded);
- }
- /**
- * @param $instance
- *
- * @return string
- */
- function tripal_jbrowse_mgmt_get_track_list_file_path($instance) {
- $settings = tripal_jbrowse_mgmt_get_settings();
- $path = $settings['data_dir'];
- $path .= '/' . tripal_jbrowse_mgmt_make_slug($instance->title);
- $path .= '/data/trackList.json';
- return $path;
- }
- /**
- * Gets a list of supported track types.
- *
- * @return array
- */
- function hardwoods_get_track_types() {
- return [
- 'FeatureTrack',
- 'CanvasFeatures',
- 'HTMLFeatures',
- 'HTMLVariants',
- 'XYPlot',
- ];
- }
|