Create a BLAST database of the primer file
546352
def make_blastdb(self):
        
        # remove the path and the file extension for easier future globbing
        db = os.path.splitext(self.formattedprimers)[0]
        nhr = '{db}.nhr'.format(db=db)  # add nhr for searching
        if not os.path.isfile(str(nhr)):
            # Create the databases
            command = 'makeblastdb -in {primerfile} -parse_seqids -max_file_sz 2GB -dbtype nucl -out {outfile}'\
                .format(primerfile=self.formattedprimers,
                        outfile=db)
            run_subprocess(command)
403497
def create_db():
    
    from flask_appbuilder.models.sqla import Model

    engine = current_app.appbuilder.get_session.get_bind(mapper=None, clause=None)
    Model.metadata.create_all(engine)
    click.echo(click.style("DB objects created", fg="green"))
1190268
def blast(args):
    
    from jcvi.apps.align import run_megablast

    p = OptionParser(blast.__doc__)
    p.add_option("-n", type="int", default=2,
            help="Take best N hits [default: %default]")
    opts, args = p.parse_args(args)

    if len(args) != 2:
        sys.exit(not p.print_help())

    allfasta, clonename = args
    fastadir = "fasta"
    infile = op.join(fastadir, clonename + ".fasta")
    if not op.exists(infile):
        entrez([clonename, "--skipcheck", "--outdir=" + fastadir])

    outfile = "{0}.{1}.blast".format(clonename, allfasta.split(".")[0])
    run_megablast(infile=infile, outfile=outfile, db=allfasta, \
            pctid=GoodPct, hitlen=GoodOverlap)

    blasts = [BlastLine(x) for x in open(outfile)]
    besthits = []
    for b in blasts:
        if b.query.count("|") >= 3:
            b.query = b.query.split("|")[3]

        if b.subject.count("|") >= 3:
            b.subject = b.subject.split("|")[3]

        b.query = b.query.rsplit(".", 1)[0]
        b.subject = b.subject.rsplit(".", 1)[0]

        if b.query == b.subject:
            continue

        if b.subject not in besthits:
            besthits.append(b.subject)
        if len(besthits) == opts.n:
            break

    for b in besthits:
        overlap([clonename, b, "--dir=" + fastadir])
760093
def tb_h_file_creation(target, source, env):
    """"""

    files = [str(x) for x in source]

    try:
        desc = TBDescriptor(files)
    except pyparsing.ParseException as e:
        raise BuildError("Could not parse tilebus file", parsing_exception=e)

    block = desc.get_block(config_only=True)
    block.render_template(block.CommandHeaderTemplate, out_path=str(target[0]))
    block.render_template(block.ConfigHeaderTemplate, out_path=str(target[1]))
964637
def create_db(self, models):
        """"""
        # first create the table definitions
        self.tables = dict(
            [
                (model_name, self.create_model_table(model))
                for model_name, model in iteritems(models)
            ]
        )
        # now create the tables in memory
        logger.debug("Creating %d database table(s)...", len(self.tables))
        try:
            self.Base.metadata.create_all(self.engine)
        except Exception as exc:
            raise StatikError(
                message="Failed to create in-memory data model.",
                orig_exc=exc
            )
        self.load_all_model_data(models)
1134860
private static <S extends Sequence<S>> BlastDB build(List<S> sequences, boolean buildIdIndex,
                                                         Path path, boolean tmp) {
        if (sequences.isEmpty())
            throw new IllegalArgumentException("No records.");

        if (path == null)
            path = getTmpDBPath();

        Alphabet<S> alphabet = sequences.get(0).getAlphabet();

        try {
            String name = RandomUtil.getThreadLocalRandomData().nextHexString(40);
            String fullName = path.resolve(name).toString();
            List<String> cmd = new ArrayList<>();
            cmd.addAll(Arrays.asList(Blast.CMD_MAKEBLASTDB, "-dbtype", Blast.toBlastAlphabet(alphabet),
                    "-out", fullName, "-title", name));
            cmd.addAll(Arrays.asList("-parse_seqids", "-hash_index"));
            Process proc = Blast.getProcessBuilder(cmd).start();
            FastaWriter<S> writer = new FastaWriter<>(proc.getOutputStream(), FastaWriter.DEFAULT_MAX_LENGTH);
            for (int i = 0; i < sequences.size(); i++)
                writer.write(getIdFasta(i), sequences.get(i));
            writer.close();
            String err = IOUtils.toString(proc.getErrorStream(), Charset.forName("US-ASCII"));
            if (proc.waitFor() != 0)
                throw new RuntimeException("Something goes wrong: " + err);
            return BlastDB.get(fullName, tmp);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
82981
def makeblastdb(self):
        """"""
        while True:  # while daemon
            fastapath = self.dqueue.get()  # grabs fastapath from dqueue
            # remove the path and the file extension for easier future globbing
            db = fastapath.split('.')[0]
            nhr = '{}.nhr'.format(db)  # add nhr for searching
            if not os.path.isfile(str(nhr)):  # if check for already existing dbs
                # Create the databases
                # TODO use MakeBLASTdb class
                subprocess.call(shlex.split('makeblastdb -in {} -parse_seqids -max_file_sz 2GB -dbtype nucl -out {}'
                                            .format(fastapath, db)), stdout=self.fnull, stderr=self.fnull)
            dotter()
            self.dqueue.task_done()
1261351
def main():
    
    parser = create_parser()
    options = vars(parser.parse_args())
    HASH_STORE.IGNORE_CACHE_FILE = options[constants.LABEL_FORCE]
    moban_file = options[constants.LABEL_MOBANFILE]
    load_engine_factory_and_engines()  # Error: jinja2 if removed
    if moban_file is None:
        moban_file = mobanfile.find_default_moban_file()
    if moban_file:
        try:
            count = handle_moban_file(moban_file, options)
            moban_exit(options[constants.LABEL_EXIT_CODE], count)
        except (
            exceptions.DirectoryNotFound,
            exceptions.NoThirdPartyEngine,
            exceptions.MobanfileGrammarException,
        ) as e:
            reporter.report_error_message(str(e))
            moban_exit(options[constants.LABEL_EXIT_CODE], constants.ERROR)
    else:
        try:
            count = handle_command_line(options)
            moban_exit(options[constants.LABEL_EXIT_CODE], count)
        except exceptions.NoTemplate as e:
            reporter.report_error_message(str(e))
            moban_exit(options[constants.LABEL_EXIT_CODE], constants.ERROR)
980468
def construct_blastn_cmdline(
    fname1, fname2, outdir, blastn_exe=pyani_config.BLASTN_DEFAULT
):
    
    fstem1 = os.path.splitext(os.path.split(fname1)[-1])[0]
    fstem2 = os.path.splitext(os.path.split(fname2)[-1])[0]
    fstem1 = fstem1.replace("-fragments", "")
    prefix = os.path.join(outdir, "%s_vs_%s" % (fstem1, fstem2))
    cmd = (
        "{0} -out {1}.blast_tab -query {2} -db {3} "
        + "-xdrop_gap_final 150 -dust no -evalue 1e-15 "
        + "-max_target_seqs 1 -outfmt '6 qseqid sseqid length mismatch "
        + "pident nident qlen slen qstart qend sstart send positive "
        + "ppos gaps' -task blastn"
    )
    return cmd.format(blastn_exe, prefix, fname1, fname2)
752546
def main():
    """"""
    desc = 'Generate files to benchmark'
    parser = argparse.ArgumentParser(description=desc)
    parser.add_argument(
        '--src',
        dest='src_dir',
        default='src',
        help='The directory containing the templates'
    )
    parser.add_argument(
        '--out',
        dest='out_dir',
        default='generated',
        help='The output directory'
    )
    parser.add_argument(
        '--seed',
        dest='seed',
        default='13',
        help='The random seed (to ensure consistent regeneration)'
    )

    args = parser.parse_args()

    random.seed(int(args.seed))

    mkdir_p(args.out_dir)

    for template in templates_in(args.src_dir):
        modes = template.modes()

        n_range = template.range()
        for n_value in n_range:
            base = template.instantiate(n_value)
            for mode in modes:
                write_file(
                    os.path.join(
                        args.out_dir,
                        out_filename(template, n_value, mode)
                    ),
                    mode.convert_from(base)
                )
        write_file(
            os.path.join(args.out_dir, '{0}.json'.format(template.name)),
            json.dumps({
                'files': {
                    n: {
                        m.identifier: out_filename(template, n, m)
                        for m in modes
                    } for n in n_range
                },
                'name': template.name,
                'x_axis_label': template.property('x_axis_label'),
                'desc': template.property('desc'),
                'modes': {m.identifier: m.description() for m in modes}
            })
        )
1274107
def create_archive(
    source: Path,
    target: Path,
    interpreter: str,
    main: str,
    compressed: bool = True
) -> None:
    
    # Check that main has the right format.
    mod, sep, fn = main.partition(":")
    mod_ok = all(part.isidentifier() for part in mod.split("."))
    fn_ok = all(part.isidentifier() for part in fn.split("."))
    if not (sep == ":" and mod_ok and fn_ok):
        raise zipapp.ZipAppError("Invalid entry point: " + main)

    main_py = MAIN_TEMPLATE.format(module=mod, fn=fn)

    with maybe_open(target, "wb") as fd:
        # write shebang
        write_file_prefix(fd, interpreter)

        # determine compression
        compression = zipfile.ZIP_DEFLATED if compressed else zipfile.ZIP_STORED

        # create zipapp
        with zipfile.ZipFile(fd, "w", compression=compression) as z:
            for child in source.rglob("*"):

                # skip compiled files
                if child.suffix == '.pyc':
                    continue

                arcname = child.relative_to(source)
                z.write(str(child), str(arcname))

            # write main
            z.writestr("__main__.py", main_py.encode("utf-8"))

    # make executable
    # NOTE on windows this is no-op
    target.chmod(target.stat().st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
490806
def _blast(bvname2vals, name_map):
    """"""
    if len(name_map) == 0:
        return dict()
    return fn.merge(*(dict(zip(names, bvname2vals[bvname]))
                      for bvname, names in name_map))
1192881
def make_tree(file):
    """"""
    max_files_count = current_app.config.get('PREVIEWER_ZIP_MAX_FILES', 1000)
    tree = {'type': 'folder', 'id': -1, 'children': {}}

    try:
        with file.open() as fp:
            zf = zipfile.ZipFile(fp)
            # Detect filenames encoding.
            sample = ' '.join(zf.namelist()[:max_files_count])
            if not isinstance(sample, binary_type):
                sample = sample.encode('utf-16be')
            encoding = chardet.detect(sample).get('encoding', 'utf-8')
            for i, info in enumerate(zf.infolist()):
                if i > max_files_count:
                    raise BufferError('Too many files inside the ZIP file.')
                comps = info.filename.split(os.sep)
                node = tree
                for c in comps:
                    if not isinstance(c, text_type):
                        c = c.decode(encoding)
                    if c not in node['children']:
                        if c == '':
                            node['type'] = 'folder'
                            continue
                        node['children'][c] = {
                            'name': c,
                            'type': 'item',
                            'id': 'item{0}'.format(i),
                            'children': {}
                        }
                    node = node['children'][c]
                node['size'] = info.file_size
    except BufferError:
        return tree, True, None
    except (zipfile.LargeZipFile):
        return tree, False, 'Zipfile is too large to be previewed.'
    except Exception as e:
        current_app.logger.warning(str(e), exc_info=True)
        return tree, False, 'Zipfile is not previewable.'

    return tree, False, None
1053610
def epcr_primers(self, primerfile):
        
        logging.info('Populating primer dictionaries')
        for record in SeqIO.parse(primerfile, 'fasta'):
            # from https://stackoverflow.com/a/27552377 - find any degenerate bases in the primer sequence, and
            # create all possibilities as a list
            degenerates = Seq.IUPAC.IUPACData.ambiguous_dna_values
            primerlist = list(map(''.join, product(*map(degenerates.get, str(record.seq).upper()))))
            # As the record.id is being updated in the loop below, set the name of the primer here so that will
            # be able to be recalled when setting the new record.ids
            primername = record.id
            # Iterate through all the possible primers created from any degenerate bases
            for primer in primerlist:
                # Split the base name of the target from the direction
                # e.g. vtx1a-F1 is split in vtx1a and F1
                basename, direction = primername.split('-')
                # Populate the dictionaries of forward and reverse primers based on the direction determined above
                if direction.startswith('F'):
                    # Attempt to add the current primer sequence to the dictionary
                    try:
                        self.forward_dict[basename].append(primer)
                    # On a key error, initialise the list of primers
                    except KeyError:
                        self.forward_dict[basename] = list()
                        self.forward_dict[basename].append(primer)
                else:
                    try:
                        self.reverse_dict[basename].append(primer)
                    except KeyError:
                        self.reverse_dict[basename] = list()
                        self.reverse_dict[basename].append(primer)
528458
def makeblastdb(self):
        
        # Iterate through the samples to set the bait file.
        for sample in self.runmetadata.samples:
            if sample.general.bestassemblyfile != 'NA':
                # Remove the file extension
                db = os.path.splitext(sample[self.analysistype].baitfile)[0]
                # Add '.nhr' for searching below
                nhr = '{}.nhr'.format(db)
                # Check for already existing database files
                if not os.path.isfile(str(nhr)):
                    # Create the databases
                    command = 'makeblastdb -in {} -parse_seqids -max_file_sz 2GB -dbtype nucl -out {}'\
                        .format(sample[self.analysistype].baitfile, db)
                    out, err = run_subprocess(command)
                    write_to_logfile(command,
                                     command,
                                     self.logfile, sample.general.logout, sample.general.logerr,
                                     sample[self.analysistype].logout, sample[self.analysistype].logerr)
                    write_to_logfile(out,
                                     err,
                                     self.logfile, sample.general.logout, sample.general.logerr,
                                     sample[self.analysistype].logout, sample[self.analysistype].logerr)
1018944
def makeblastdb(self, fastapath):
        
        # remove the path and the file extension for easier future globbing
        db = fastapath.split('.')[0]
        nhr = '{}.nhr'.format(db)  # add nhr for searching
        if not os.path.isfile(str(nhr)):  # if check for already existing dbs
            # Create the databases
            threadlock = threading.Lock()
            command = 'makeblastdb -in {} -parse_seqids -max_file_sz 2GB -dbtype nucl -out {}'.format(fastapath, db)
            out, err = run_subprocess(command)
            threadlock.acquire()
            write_to_logfile(out, err, self.logfile)
            threadlock.release()
        dotter()
1023486
def _prepare_sample(data, run_folder):
    
    want = set(["description", "files", "genome_build", "name", "analysis", "upload", "algorithm"])
    out = {}
    for k, v in data.items():
        if k in want:
            out[k] = _relative_paths(v, run_folder)
    if "algorithm" not in out:
        analysis, algorithm = _select_default_algorithm(out.get("analysis"))
        out["algorithm"] = algorithm
        out["analysis"] = analysis
    description = "%s-%s" % (out["name"], clean_name(out["description"]))
    out["name"] = [out["name"], description]
    out["description"] = description
    return out