tripal_jbrowse_mgmt_commands.inc 5.6 KB

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