tripal_jbrowse_mgmt.api.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <?php
  2. /**
  3. * Get saved settings.
  4. *
  5. * @return null
  6. */
  7. function tripal_jbrowse_mgmt_get_settings() {
  8. $default = [
  9. 'bin_path' => '',
  10. 'link' => '',
  11. 'data_dir' => '',
  12. 'menu_template' => [],
  13. ];
  14. $variable = variable_get('tripal_jbrowse_mgmt_settings', json_encode($default));
  15. $settings = json_decode($variable, TRUE) + $default;
  16. return $settings;
  17. }
  18. /**
  19. * Save settings.
  20. *
  21. * @param array $settings
  22. *
  23. * @return array The final merged settings
  24. */
  25. function tripal_jbrowse_mgmt_save_settings($settings) {
  26. $default = [
  27. 'bin_path' => '',
  28. 'link' => '',
  29. 'data_dir' => '',
  30. 'menu_template' => [],
  31. ];
  32. $final = $settings + $default;
  33. variable_set('tripal_jbrowse_mgmt_settings', json_encode($final));
  34. return $final;
  35. }
  36. /**
  37. * Get an array to instances.
  38. *
  39. * @return mixed
  40. */
  41. function tripal_jbrowse_mgmt_get_instances($conditions = NULL) {
  42. static $users = [];
  43. static $organisms = [];
  44. $instances = db_select('tripal_jbrowse_mgmt_instances', 'H')->fields('H');
  45. if ($conditions) {
  46. foreach ($conditions as $field => $value) {
  47. $instances->condition($field, $value);
  48. }
  49. }
  50. $instances = $instances->execute()->fetchAll();
  51. foreach ($instances as $key => &$instance) {
  52. if (!isset($users[$instance->uid])) {
  53. $users[$instance->uid] = user_load($instance->uid);
  54. }
  55. $instance->user = $users[$instance->uid];
  56. if (!isset($organisms[$instance->organism_id])) {
  57. $organisms[$instance->organism_id] = chado_query('SELECT * FROM {organism} WHERE organism_id=:id',
  58. [':id' => $instance->organism_id])->fetchObject();
  59. }
  60. $instance->organism = $organisms[$instance->organism_id];
  61. }
  62. return $instances;
  63. }
  64. /**
  65. * Create a new instance.
  66. *
  67. * @param $data
  68. *
  69. * @return \DatabaseStatementInterface|int
  70. * @throws \Exception
  71. */
  72. function tripal_jbrowse_mgmt_create_instance($data) {
  73. global $user;
  74. $instance_id = db_insert('tripal_jbrowse_mgmt_instances')->fields([
  75. 'uid' => $user->uid,
  76. 'organism_id' => $data['organism_id'],
  77. 'title' => $data['title'],
  78. 'description' => isset($data['description']) ? $data['description'] : '',
  79. 'created_at' => $data['created_at'],
  80. 'file' => $data['file'],
  81. ])->execute();
  82. if (!$instance_id) {
  83. return FALSE;
  84. }
  85. return $instance_id;
  86. }
  87. /**
  88. * Get an instance by id.
  89. *
  90. * @param $instance_id
  91. *
  92. * @return mixed
  93. */
  94. function tripal_jbrowse_mgmt_get_instance($instance_id) {
  95. $instance = tripal_jbrowse_mgmt_get_instances(['id' => $instance_id]);
  96. return reset($instance);
  97. }
  98. /**
  99. * Update an instance.
  100. *
  101. * @param int $id
  102. * @param array $data
  103. *
  104. * @return \DatabaseStatementInterface
  105. */
  106. function tripal_jbrowse_mgmt_update_instance($id, $data) {
  107. return db_update('tripal_jbrowse_mgmt_instances')
  108. ->fields($data)
  109. ->condition('id', $id)
  110. ->execute();
  111. }
  112. /**
  113. * @param int|object $instance
  114. *
  115. * @return int
  116. * @throws \Exception
  117. */
  118. function tripal_jbrowse_mgmt_delete_instance($instance) {
  119. if (is_object($instance) && property_exists($instance, 'id')) {
  120. $id = $instance->id;
  121. }
  122. elseif (is_numeric($instance)) {
  123. $id = $instance;
  124. }
  125. else {
  126. throw new Exception('Unable to extract instance ID. Please provide a valid ID to delete the instance.');
  127. }
  128. return db_delete('tripal_jbrowse_mgmt_instances')
  129. ->condition('id', $id)
  130. ->execute();
  131. }
  132. /**
  133. * Create a new JBrowse track for a given instance.
  134. *
  135. * @param $data
  136. *
  137. * @return bool|int Track ID or FALSE if an error occurs.
  138. * @throws \Exception
  139. */
  140. function tripal_jbrowse_mgmt_create_track($instance, $data) {
  141. global $user;
  142. $track_id = db_insert('tripal_jbrowse_mgmt_tracks')->fields([
  143. 'uid' => $user->uid,
  144. 'instance_id' => $instance->id,
  145. 'organism_id' => $instance->organism_id,
  146. 'label' => $data['label'],
  147. 'track_type' => $data['track_type'],
  148. 'file_type' => $data['file_type'],
  149. 'created_at' => $data['created_at'],
  150. 'file' => $data['file'],
  151. ])->execute();
  152. if (!$track_id) {
  153. return FALSE;
  154. }
  155. return $track_id;
  156. }
  157. /**
  158. * Delete a track by ID.
  159. *
  160. * @param $track_id
  161. *
  162. * @return int
  163. */
  164. function tripal_jbrowse_mgmt_delete_track($track_id) {
  165. return db_delete('tripal_jbrowse_mgmt_tracks')
  166. ->condition('id', $track_id)
  167. ->execute();
  168. }
  169. /**
  170. * Get attached tracks with users pre-loaded.
  171. *
  172. * @param $instance
  173. *
  174. * @return mixed
  175. */
  176. function tripal_jbrowse_mgmt_get_tracks($instance, array $conditions = []) {
  177. static $users = [];
  178. $tracks = db_select('tripal_jbrowse_mgmt_tracks', 'HJT')->fields('HJT');
  179. foreach ($conditions as $field => $value) {
  180. $tracks->condition($field, $value);
  181. }
  182. $tracks = $tracks->condition('instance_id', $instance->id)
  183. ->execute()
  184. ->fetchAll();
  185. foreach ($tracks as &$track) {
  186. if (!isset($users[$track->uid])) {
  187. $users[$track->uid] = user_load($track->uid);
  188. }
  189. $track->user = $users[$track->uid];
  190. }
  191. return $tracks;
  192. }
  193. /**
  194. * Get a track with instance and user data attached.
  195. *
  196. * @param $track_id
  197. *
  198. * @return mixed
  199. */
  200. function tripal_jbrowse_mgmt_get_track($track_id) {
  201. $track = db_select('tripal_jbrowse_mgmt_tracks', 'HJT')
  202. ->fields('HJT')
  203. ->condition('id', $track_id)
  204. ->execute()
  205. ->fetchObject();
  206. $track->user = user_load($track->uid);
  207. $track->instance = tripal_jbrowse_mgmt_get_instance($track->instance_id);
  208. return $track;
  209. }
  210. /**
  211. * @param $track
  212. * @param array $fields
  213. *
  214. * @return \DatabaseStatementInterface
  215. */
  216. function tripal_jbrowse_mgmt_update_track($track, array $fields) {
  217. return db_update('tripal_jbrowse_mgmt_tracks')
  218. ->fields($fields)
  219. ->condition('id', $track->id)
  220. ->execute();
  221. }
  222. /**
  223. * Get a list of organisms.
  224. *
  225. * @return mixed
  226. */
  227. function tripal_jbrowse_mgmt_get_organisms_list() {
  228. return db_select('chado.organism', 'CO')
  229. ->fields('CO', ['organism_id', 'genus', 'species', 'common_name'])
  230. ->execute()
  231. ->fetchAll();
  232. }
  233. function tripal_jbrowse_mgmt_construct_organism_name($organism) {
  234. $name = $organism->genus;
  235. $name .= " $organism->species";
  236. if (!empty($organism->common_name)) {
  237. $name .= " ($organism->common_name)";
  238. }
  239. return $name;
  240. }
  241. function tripal_jbrowse_mgmt_make_slug($string) {
  242. $slug = str_replace(' ', '_', $string);
  243. $slug = str_replace('(', '_', $slug);
  244. $slug = str_replace(')', '_', $slug);
  245. $slug = str_replace('[', '_', $slug);
  246. $slug = str_replace(']', '_', $slug);
  247. $slug = str_replace('!', '_', $slug);
  248. $slug = str_replace('?', '_', $slug);
  249. $slug = str_replace('"', '_', $slug);
  250. $slug = str_replace('\'', '_', $slug);
  251. $slug = str_replace('\\', '_', $slug);
  252. $slug = str_replace(':', '_', $slug);
  253. return strtolower(trim($slug, '_'));
  254. }
  255. function tripal_jbrowse_mgmt_upload_file($field) {
  256. $file = file_save_upload($field, [
  257. 'file_validate_extensions' => ['fasta faa fna fastq txt gff vcf wig gz tbi bw'],
  258. 'file_validate_size' => [1024 * 1024 * 1024 * 20] // Make it 20 GB max
  259. ]);
  260. return !$file ? FALSE : $file; // drupal_realpath($file->uri);
  261. }
  262. /**
  263. * Moves a file to an intermediate directory, then to the destination, if given.
  264. *
  265. * @param $file
  266. * The file object.
  267. *
  268. * @param $path
  269. * The path to the directory of the new object.
  270. * If the directory provided does not exist, it will be created.
  271. *
  272. * @return
  273. * The path to the moved file, or NULL on fail.
  274. */
  275. function tripal_jbrowse_mgmt_move_file($file, $path = NULL) {
  276. $directory = 'public://tripal/tripal_jbrowse_mgmt';
  277. file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
  278. $file = file_move($file, $directory, FILE_EXISTS_REPLACE);
  279. if (isset($path)) {
  280. file_prepare_directory($path, FILE_CREATE_DIRECTORY);
  281. $oldname = drupal_realpath($file->uri);
  282. $newname = $path . '/' . $file->filename;
  283. if (!rename($oldname, $newname)) {
  284. return NULL;
  285. }
  286. }
  287. return isset($path) ? $newname : drupal_realpath($file->uri);
  288. }
  289. /**
  290. * Copy a file into a new directory.
  291. * If the directory provided does not exist, it will be created.
  292. *
  293. * @param $source
  294. * File path of the source file.
  295. * @param $destination
  296. * File path to the destination directory.
  297. *
  298. * @return bool
  299. */
  300. function tripal_jbrowse_mgmt_copy_file($source, $destination) {
  301. file_prepare_directory($destination, FILE_CREATE_DIRECTORY);
  302. return file_unmanaged_copy($source, $destination, FILE_EXISTS_ERROR);
  303. }
  304. /**
  305. * Build the http query for a given instance to link to JBrowse.
  306. *
  307. * @param $instance
  308. *
  309. * @return array
  310. */
  311. function tripal_jbrowse_mgmt_build_http_query($instance) {
  312. $path = tripal_jbrowse_mgmt_make_slug($instance->title);
  313. $tracks = tripal_jbrowse_mgmt_get_tracks($instance);
  314. $tracks_path = '';
  315. if (!empty($tracks)) {
  316. $tracks_path = implode(',', array_map(function ($track) {
  317. return tripal_jbrowse_mgmt_make_slug($track->label);
  318. }, $tracks));
  319. }
  320. return [
  321. 'data' => "data/$path/data",
  322. 'tracks' => $tracks_path,
  323. ];
  324. }
  325. /**
  326. * Get trackList.json for an instance.
  327. *
  328. * @param object $instance
  329. *
  330. * @return array Decoded json array.
  331. *
  332. * @throws \Exception
  333. */
  334. function tripal_jbrowse_mgmt_get_json($instance) {
  335. $path = tripal_jbrowse_mgmt_get_track_list_file_path($instance);
  336. $contents = file_get_contents($path);
  337. if (!$contents) {
  338. throw new Exception('Unable to find ' . $path . ' file');
  339. }
  340. return json_decode($contents, TRUE);
  341. }
  342. function tripal_jbrowse_mgmt_get_track_json($track) {
  343. if (!$track->instance) {
  344. $track->instance = tripal_jbrowse_mgmt_get_instance($track->instance_id);
  345. }
  346. $json = tripal_jbrowse_mgmt_get_json($track->instance);
  347. $key = tripal_jbrowse_mgmt_make_slug($track->label);
  348. $track_json = NULL;
  349. foreach ($json['tracks'] as $index => $jtrack) {
  350. if ($jtrack['label'] === $key) {
  351. $track_json = $jtrack;
  352. break;
  353. }
  354. }
  355. return $track_json;
  356. }
  357. /**
  358. * @param object $track Track object
  359. * @param array $track_json Edited track array.
  360. *
  361. * @throws \Exception
  362. */
  363. function tripal_jbrowse_mgmt_save_track_json($track, $track_json) {
  364. if (!$track->instance) {
  365. $track->instance = tripal_jbrowse_mgmt_get_instance($track->instance_id);
  366. }
  367. $json = tripal_jbrowse_mgmt_get_json($track->instance);
  368. $key = tripal_jbrowse_mgmt_make_slug($track->label);
  369. foreach ($json['tracks'] as $index => $jtrack) {
  370. if ($jtrack['label'] === $key) {
  371. $json['tracks'][$index] = $track_json;
  372. break;
  373. }
  374. }
  375. return tripal_jbrowse_mgmt_save_json($track->instance, $json);
  376. }
  377. /**
  378. * @param object $instance
  379. * @param array $data
  380. *
  381. * @throws \Exception
  382. * @return bool|int
  383. */
  384. function tripal_jbrowse_mgmt_save_json($instance, $data) {
  385. $path = tripal_jbrowse_mgmt_get_track_list_file_path($instance);
  386. $default = tripal_jbrowse_mgmt_get_json($instance);
  387. $json = $data + $default;
  388. $encoded = json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
  389. return file_put_contents($path, $encoded);
  390. }
  391. /**
  392. * @param $instance
  393. *
  394. * @return string
  395. */
  396. function tripal_jbrowse_mgmt_get_track_list_file_path($instance) {
  397. $settings = tripal_jbrowse_mgmt_get_settings();
  398. $path = $settings['data_dir'];
  399. $path .= '/' . tripal_jbrowse_mgmt_make_slug($instance->title);
  400. $path .= '/data/trackList.json';
  401. return $path;
  402. }
  403. /**
  404. * Gets a list of supported track types.
  405. *
  406. * @return array
  407. */
  408. function hardwoods_get_track_types() {
  409. return [
  410. 'FeatureTrack',
  411. 'CanvasFeatures',
  412. 'HTMLFeatures',
  413. 'HTMLVariants',
  414. 'XYPlot',
  415. ];
  416. }