"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@oclif/core");
const command_exists_1 = require("command-exists");
const child_process_1 = require("child_process");
const base_1 = __importDefault(require("../base"));
class SortGff extends base_1.default {
    async run() {
        const { args: { file }, } = await this.parse(SortGff);
        if ((0, command_exists_1.sync)('sh') &&
            (0, command_exists_1.sync)('sort') &&
            (0, command_exists_1.sync)('grep')) {
            // this command comes from the tabix docs http://www.htslib.org/doc/tabix.html
            (0, child_process_1.spawn)('sh', [
                '-c',
                `(grep "^#" "${file}"; grep -v "^#" "${file}" | sort -t"\`printf '\t'\`" -k1,1 -k4,4n)`,
            ], {
                env: { ...process.env, LC_ALL: 'C' },
                stdio: 'inherit',
            });
        }
        else {
            throw new Error('Unable to sort, requires unix type environment with sort, grep');
        }
    }
}
SortGff.description = 'Helper utility to sort GFF files for tabix. Moves all lines starting with # to the top of the file, and sort by refname and start position using unix utilities sort and grep';
SortGff.examples = [
    '# sort gff and pipe to bgzip',
    '$ jbrowse sort-gff input.gff | bgzip > sorted.gff.gz',
    '$ tabix sorted.gff.gz',
];
SortGff.args = {
    file: core_1.Args.string({
        required: true,
        description: `GFF file`,
    }),
};
SortGff.flags = {
    help: core_1.Flags.help({ char: 'h' }),
};
exports.default = SortGff;
