tripal_jbrowse_mgmt_commands.inc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * Add a reference sequence to the instance.
  4. *
  5. * @param $instance
  6. *
  7. * @return string
  8. * @throws \Exception
  9. */
  10. function tripal_jbrowse_mgmt_cmd_prepare_refseq($instance) {
  11. $settings = tripal_jbrowse_mgmt_get_settings();
  12. $data = $settings['data_dir'];
  13. $bin = $settings['bin_path'];
  14. $path = $data . '/' . tripal_jbrowse_mgmt_make_slug($instance->title);
  15. if (!file_exists($path)) {
  16. if (!mkdir($path)) {
  17. throw new Exception(
  18. 'Unable to make data directory! Please make sure the directory
  19. at ' . $data . ' exists and is writable by the current user.'
  20. );
  21. }
  22. }
  23. $out = $path . '/data';
  24. tripal_jbrowse_mgmt_safe_exec(
  25. 'perl',
  26. [
  27. $bin . '/prepare-refseqs.pl',
  28. '--fasta',
  29. $instance->file,
  30. '--out',
  31. $out,
  32. ],
  33. $ignore,
  34. $ret
  35. );
  36. return $ret;
  37. }
  38. /**
  39. * Add a track to an instance.
  40. *
  41. * @param $track
  42. *
  43. * @return string
  44. * @throws \Exception
  45. */
  46. function tripal_jbrowse_mgmt_cmd_add_track($track) {
  47. $settings = tripal_jbrowse_mgmt_get_settings();
  48. $data = $settings['data_dir'];
  49. $bin = $settings['bin_path'];
  50. $instance = $track->instance;
  51. $menu_template = $settings['menu_template'];
  52. $path = $data . '/' . tripal_jbrowse_mgmt_make_slug($instance->title);
  53. $out = $path . '/data';
  54. if (!file_exists($out)) {
  55. throw new Exception('Data directory does not exist: ' . $out);
  56. }
  57. switch ($track->track_type)
  58. {
  59. case 'HTMLVariants':
  60. $instance = tripal_jbrowse_mgmt_get_instance($track->instance_id);
  61. $json = tripal_jbrowse_mgmt_get_json($instance);
  62. $directory = 'vcf';
  63. $file_name = $track->file;
  64. if (is_dir($track->file)) {
  65. $file_name = glob($track->file . '/' . '*.vcf.gz')[0];
  66. }
  67. $file_name = pathinfo($file_name)['basename'];
  68. $json['tracks'][] = [
  69. 'label' => $track->label,
  70. 'key' => $track->label,
  71. 'storeClass' => 'JBrowse/Store/SeqFeature/VCFTabix',
  72. 'urlTemplate' => $directory . '/' . $file_name,
  73. 'type' => $track->track_type,
  74. ];
  75. tripal_jbrowse_mgmt_save_json($instance, $json);
  76. break;
  77. case 'XYPlot':
  78. $instance = tripal_jbrowse_mgmt_get_instance($track->instance_id);
  79. $json = tripal_jbrowse_mgmt_get_json($instance);
  80. $basename = pathinfo($track->file)['basename'];
  81. $json['tracks'][] = [
  82. 'label' => $track->label,
  83. 'key' => $track->label,
  84. 'storeClass' => 'JBrowse/Store/SeqFeature/BigWig',
  85. 'urlTemplate' => 'wig/' . $basename,
  86. 'type' => 'JBrowse/View/Track/Wiggle/XYPlot',
  87. ];
  88. tripal_jbrowse_mgmt_save_json($instance, $json);
  89. break;
  90. default:
  91. tripal_jbrowse_mgmt_safe_exec(
  92. 'perl',
  93. [
  94. $bin . '/flatfile-to-json.pl',
  95. '--' . $track->file_type,
  96. $instance->file,
  97. '--trackLabel',
  98. tripal_jbrowse_mgmt_make_slug($track->label),
  99. '--key',
  100. $track->label,
  101. '--out',
  102. $out,
  103. '--trackType',
  104. $track->track_type,
  105. ],
  106. $ignore,
  107. $ret
  108. );
  109. return $ret;
  110. }
  111. }
  112. /**
  113. * Generate names for a specific instance.
  114. *
  115. * @param $instance
  116. *
  117. * @return string
  118. * @throws \Exception
  119. */
  120. function tripal_jbrowse_mgmt_cmd_generate_names($instance) {
  121. $settings = tripal_jbrowse_mgmt_get_settings();
  122. $data = $settings['data_dir'];
  123. $bin = $settings['bin_path'];
  124. $path = $data . '/' . tripal_jbrowse_mgmt_make_slug($instance->title);
  125. if (!file_exists($path)) {
  126. if (!mkdir($path)) {
  127. throw new Exception(
  128. 'Unable to make data directory! Please make sure the directory
  129. at ' . $data . ' exists and is writable by the current user.'
  130. );
  131. }
  132. }
  133. $out = $path . '/data';
  134. tripal_jbrowse_mgmt_safe_exec(
  135. 'perl',
  136. [
  137. $bin . '/generate-names.pl',
  138. '--out',
  139. $out,
  140. ],
  141. $ignore,
  142. $ret
  143. );
  144. return $ret;
  145. }
  146. /**
  147. * Delete a track to an instance.
  148. *
  149. * @param $track
  150. *
  151. * @return string
  152. * @throws \Exception
  153. */
  154. function tripal_jbrowse_mgmt_cmd_delete_track($track) {
  155. $settings = tripal_jbrowse_mgmt_get_settings();
  156. $data = $settings['data_dir'];
  157. $bin = $settings['bin_path'];
  158. $instance = $track->instance;
  159. $path = $data . '/' . tripal_jbrowse_mgmt_make_slug($instance->title);
  160. $out = $path . '/data';
  161. if (!file_exists($out)) {
  162. throw new Exception('Data directory does not exist: ' . $out);
  163. }
  164. return tripal_jbrowse_mgmt_safe_exec(
  165. 'perl',
  166. [
  167. $bin . '/remove-track.pl',
  168. '--trackLabel',
  169. tripal_jbrowse_mgmt_make_slug($track->label),
  170. '--dir',
  171. $out,
  172. '--delete',
  173. ]
  174. );
  175. }
  176. /**
  177. * Safely execute a command.
  178. *
  179. * @param string $command The path to the command to execute.
  180. * @param array $args Arguments passed as flag => $argument or a list of
  181. * arguments as [arg1, arg2, arg3]
  182. * @param array $output If the output argument is present, then the
  183. * specified array will be filled with every line of output from the
  184. * command. Trailing whitespace, such as \n, is not
  185. * included in this array. Note that if the array already contains some
  186. * elements, exec will append to the end of the array.
  187. * If you do not want the function to append elements, call
  188. * unset on the array before passing it to
  189. * exec.
  190. * @param int $return_var If the return_var argument is present
  191. * along with the output argument, then the
  192. * return status of the executed command will be written to this
  193. *
  194. * @return string
  195. */
  196. function tripal_jbrowse_mgmt_safe_exec(
  197. $command,
  198. array $args = [],
  199. &$output = NULL,
  200. &$return = NULL
  201. ) {
  202. $cmd = escapeshellcmd($command) . ' ';
  203. foreach ($args as $flag => $arg) {
  204. if (is_string($flag)) {
  205. $cmd .= escapeshellarg($flag) . ' ';
  206. }
  207. $cmd .= escapeshellarg($arg) . ' ';
  208. }
  209. print "Running the following command:\n";
  210. print $cmd . "\n";
  211. return exec($cmd, $output, $return);
  212. }