Exemplo n.º 1
0
def test_cli_config_set_default_genome(tmp_dir):
    args = parser.parse_args(
        ["config", "--set-default-genome", "/path/to/genome/root"])
    config_file = os.path.join(tmp_dir, "test_cli_config.motifscanrc")
    run(args=args, config_file=config_file)

    config = Config(config_file)
    assert config.get_genome_dir() == "/path/to/genome/root"
Exemplo n.º 2
0
def install_genome(args, config_file=None):
    config = Config(config_file)
    if config.has_genome_assembly(args.name):
        logger.error(f"Genome assembly {args.name!r} already exists!")
        sys.exit(1)

    genome_dir = os.path.abspath(
        args.output_dir or os.path.join(config.get_genome_dir(), args.name))
    logger.info(f"Installing genome assembly {args.name!r} into {genome_dir}")
    if not os.path.isdir(genome_dir):
        os.makedirs(genome_dir)
    if os.listdir(genome_dir):
        logger.error("Directory not empty! Please specify another directory "
                     "or delete files under it.")
        sys.exit(1)

    fasta_path = fasta_path_fmt.format(genome_dir, args.name)
    bg_freq_path = bg_freq_path_fmt.format(genome_dir, args.name)
    gene_path = gene_path_fmt.format(genome_dir, args.name)

    if args.remote:
        download_dir = os.path.join(genome_dir, 'downloads')
        try:
            db = UcscDatabase()
            dst_fasta = db.download_sequence(args.remote, download_dir)
            logger.debug(f"Extracting the sequence file to {fasta_path}")
            merge_extracted_files(dst_fasta, fasta_path)
            dst_gene = db.download_gene(args.remote, download_dir)
            logger.debug(f"Extracting the gene annotation file to {gene_path}")
            merge_extracted_files(dst_gene, gene_path)
            if args.clean:
                logger.debug(f"Removing the download directory {download_dir}")
                shutil.rmtree(download_dir)
        except RemoteGenomeNotFoundError as e:
            logger.error(e)
            sys.exit(1)
    else:
        logger.info("Copying the sequence file(s)")
        merge_files(args.fasta_files, fasta_path)
        logger.info("Copying the gene annotation file")
        copy_file(args.gene_file, gene_path)

    logger.info("Calculating nucleotide frequencies of the genome background")
    bg_freq = cal_bg_freq(fasta_path)
    logger.info("Writing nucleotide frequencies")
    write_bg_freq(bg_freq_path, bg_freq)

    logger.info("Updating the config file")
    config.set_genome_path(args.name, genome_dir)
    config.write()
    logger.info("Successfully installed!")