Пример #1
0
def update(args):
    if args.in_place and args.strict:
        raise SystemExit(
            "Incompatible arguments: --strict cannot be used with --in_place "
            "to prevent unexpected loss of config settings.")

    old_config = ruamel.yaml.safe_load(args.config_file)

    # Remove the old version number
    old_config.get('all', {}).pop('version', None)

    new_config = config.new(conda_fp=config._find_conda_fp(),
                            project_fp=old_config.get('all',
                                                      {}).get('version', ''),
                            template=args.template)

    updated_config = config.update(new_config, old_config, args.strict)

    # If strict, preserve the blast databases
    if args.strict:
        blastdbs = old_config.get('blastdbs', None)
        updated_config['blastdbs'] = blastdbs

    if args.in_place:
        cfg_fp = args.config_file.name
        args.config_file.close()
        with open(cfg_fp, 'w') as out:
            config.dump(updated_config, out)
    else:
        config.dump(updated_config, args.out)
Пример #2
0
def write_config(args, project_fp, samplelists):
    multiple_configs = len(samplelists.values()) != 1
    for layout in samplelists.keys(
    ):  # one paired, one unpaired, or one of each
        if multiple_configs:
            config_file = check_existing(
                project_fp /
                Path(layout + "_" + str(Path(project_fp / args.output).name)),
                args.force)
        else:
            config_file = check_existing(project_fp / args.output, args.force)
        cfg = config.new(project_fp=project_fp, template=args.template)
        defaults = {}
        if args.defaults:
            defaults = ruamel.yaml.safe_load(args.defaults)
        # Override loaded config defaults (if any) for a few specific items.
        paired = layout == "paired"
        defaults["all"] = defaults.get("all", {})
        defaults["all"]["paired_end"] = paired
        defaults["all"]["samplelist_fp"] = samplelists[layout].name
        if args.data_acc:
            defaults["all"]["download_reads"] = True
        # Convert cfg from raw text to dict, including any defaults we have
        # set, and write to disk.
        cfg = config.update(cfg, defaults)
        with config_file.open('w') as out:
            config.dump(cfg, out)
        sys.stderr.write("New config file written to {}\n".format(config_file))
    if multiple_configs:
        raise SystemExit(
            "Found both paired and unpaired reads. Wrote two sample lists "
            "and config files, with '_paired' or '_single' appended.")
Пример #3
0
def main(argv=sys.argv):
    """Create a blank config file with the given name."""

    try:
        conda_prefix = os.environ.get("CONDA_PREFIX")
    except (KeyError, IndexError):
        raise SystemExit(
            "Could not determine Conda prefix. Activate your Sunbeam "
            "environment and try this command again.")
     
    parser = argparse.ArgumentParser(
        "init", description="Initialize a new sunbeam project")
    parser.add_argument(
        "-o", "--output", help="Name of config file (written to project_fp)",
        default="sunbeam_config.yml", metavar="FILE")
    parser.add_argument(
        "-f", "--force", help="Overwrite config file if it already exists",
        action="store_true")
    parser.add_argument(
        "-d", "--defaults", type=argparse.FileType("r"), metavar="FILE",
        help="Set of default values to use to populate config file")
    parser.add_argument(
        "project_fp", type=Path,
        help="Project directory (will be created if it does not exist)")
    parser.add_argument(
        "--template", default=None, metavar="FILE",
        help="Custom config file template, in YAML format", 
        type=argparse.FileType("r"))

    args = parser.parse_args(argv)

    # Create project folder if it doesn't exist
    if not args.project_fp.exists():
        sys.stderr.write("Creating project folder at {}...\n".format(args.project_fp))
        args.project_fp.mkdir(parents=True, exist_ok=True)

    cfg_fp = args.project_fp/args.output
    if cfg_fp.exists() and not args.force:
        raise SystemExit(
            "Error: config file already exists at {}. Choose a new name or use --force "
            "to overwrite.".format(cfg_fp))
        
    cfg = config.new(
        conda_fp=conda_prefix,
        project_fp=args.project_fp,
        template=args.template)

    if args.defaults:
        defaults = ruamel.yaml.safe_load(args.defaults)
        cfg = config.update(cfg, defaults)

    with open(cfg_fp, 'w') as out:
        config.dump(cfg, out)

    sys.stderr.write("New config file written to {}\n".format(cfg_fp))
Пример #4
0
def modify(args):
    update_src = args.str if args.str else args.file
    new_values = ruamel.yaml.safe_load(update_src)
    if isinstance(new_values, str) and args.str:
        raise SystemExit(
            "Invalid YAML in --str. Did you make sure to put spaces between "
            "keys and values?")
    modified_config = config.update(args.config_file, new_values, strict=False)
    if args.in_place:
        cfg_fp = args.config_file.name
        args.config_file.close()
        with open(cfg_fp, 'w') as out:
            config.dump(modified_config, out)
    else:
        config.dump(modified_config, args.out)
Пример #5
0
def main(argv=sys.argv):

    try:
        conda_prefix = os.environ.get("CONDA_PREFIX")
    except (KeyError, IndexError):
        raise SystemExit(
            "Could not determine Conda prefix. Activate your Sunbeam "
            "environment and try this command again.")

    description_str = (
        "Initialize a new Sunbeam project in a given directory, creating "
        "a new config file and sample list from SRA accessions.")

    parser = argparse.ArgumentParser("get", description=description_str)
    parser.add_argument(
        "project_fp",
        type=Path,
        help="project directory (will be created if it does not exist)")
    parser.add_argument("-f",
                        "--force",
                        help="overwrite files if they already exist",
                        action="store_true")
    parser.add_argument("--output",
                        help=("name of config file (%(default)s)"),
                        default="sunbeam_config.yml",
                        metavar="FILE")

    configs = parser.add_argument_group("config file options")
    configs.add_argument(
        "--defaults",
        type=argparse.FileType("r"),
        metavar="FILE",
        help="set of default values to use to populate config file")
    configs.add_argument("--template",
                         default=None,
                         metavar="FILE",
                         help="custom config file template, in YAML format",
                         type=argparse.FileType("r"))

    samplelist = parser.add_argument_group("sample list options")
    samplelist.add_argument("--data_acc",
                            metavar="PATH",
                            nargs="+",
                            help="list of SRA accession numbers")

    args = parser.parse_args(argv)

    ### Project Directory

    # Create project folder if it doesn't exist
    project_fp_exists = False
    project_fp = args.project_fp

    try:
        project_fp = args.project_fp.resolve()
        project_fp_exists = project_fp.exists()
    except FileNotFoundError:
        pass

    if not project_fp_exists:
        sys.stderr.write("Creating project folder at {}...\n".format(
            args.project_fp))
        project_fp.mkdir(parents=True, exist_ok=True)

    ### Sample list(s)

    # Create sample list(s).  One or two depending on paired/unpaired
    # situation.
    samplelists = build_sample_list_sra(args.data_acc, args)
    for fp in samplelists.values():
        sys.stderr.write("New sample list written to {}\n".format(fp))

    ### Config(s)

    # TODO from here on, update config-handling to handle possible mixed
    # unpaired/paired case.  (Append suffix to existing args.output in that
    # case, between name and .yml?)

    # Louis thought: gonna append for now since I don't know how to parse Path objects

    multiple_configs = len(samplelists.values()) != 1
    for layout in samplelists.keys(
    ):  # one paired, one unpaired, or one of each
        # Create config file
        if multiple_configs:
            config_file = check_existing(
                project_fp /
                Path(layout + "_" + str(Path(project_fp / args.output).name)),
                args.force)


#            config_file = check_existing(Path(str(project_fp/args.output)+"_"+layout), args.force)
        else:
            config_file = check_existing(project_fp / args.output, args.force)
        cfg = config.new(conda_fp=conda_prefix,
                         project_fp=project_fp,
                         template=args.template)
        if args.defaults:
            defaults = ruamel.yaml.safe_load(args.defaults)
            cfg = config.update(cfg, defaults)
        paired = layout == "paired"
        sample_file = samplelists[layout].name
        cfg = config.update(
            cfg, {"all": {
                "paired_end": paired,
                "download_reads": True
            }})
        with config_file.open('w') as out:
            config.dump(cfg, out)
        sys.stderr.write("New config file written to {}\n".format(config_file))
    if multiple_configs:
        raise Exception(
            "Found both paired and unpaired reads. Wrote two sample lists "
            "and config files, with '_paired' or '_single' appended.")
Пример #6
0
def main(argv=sys.argv):
    """Create a blank config file with the given name."""

    try:
        conda_prefix = os.environ.get("CONDA_PREFIX")
    except (KeyError, IndexError):
        raise SystemExit(
            "Could not determine Conda prefix. Activate your Sunbeam "
            "environment and try this command again.")

    description_str = (
        "Initialize a new Sunbeam project in a given directory, creating "
        "a new config file and (optionally) a sample list.")

    parser = argparse.ArgumentParser("init", description=description_str)
    parser.add_argument(
        "project_fp",
        type=Path,
        help="project directory (will be created if it does not exist)")
    parser.add_argument("-f",
                        "--force",
                        help="overwrite files if they already exist",
                        action="store_true")
    parser.add_argument("--output",
                        help=("name of config file (%(default)s)"),
                        default="sunbeam_config.yml",
                        metavar="FILE")

    configs = parser.add_argument_group("config file options")
    configs.add_argument(
        "--defaults",
        type=argparse.FileType("r"),
        metavar="FILE",
        help="set of default values to use to populate config file")
    configs.add_argument("--template",
                         default=None,
                         metavar="FILE",
                         help="custom config file template, in YAML format",
                         type=argparse.FileType("r"))

    samplelist = parser.add_argument_group("sample list options")
    samplelist.add_argument("--data_fp",
                            type=Path,
                            metavar="PATH",
                            help="path to folder containing fastq.gz files")
    samplelist.add_argument("--format",
                            type=str,
                            metavar="STR",
                            help="filename format (default: guessed)")
    samplelist.add_argument(
        "--single_end",
        action="store_true",
        help="fastq files are in single-end, not paired-end, format")

    args = parser.parse_args(argv)

    # Create project folder if it doesn't exist
    project_fp_exists = False
    project_fp = args.project_fp

    try:
        project_fp = args.project_fp.resolve()
        project_fp_exists = project_fp.exists()
    except FileNotFoundError:
        pass

    if not project_fp_exists:
        sys.stderr.write("Creating project folder at {}...\n".format(
            args.project_fp))
        project_fp.mkdir(parents=True, exist_ok=True)

    # Check if files already exist
    config_file = check_existing(project_fp / args.output, args.force)
    samplelist_file = check_existing(project_fp / "samples.csv", args.force)

    # Create config file
    cfg = config.new(conda_fp=conda_prefix,
                     project_fp=project_fp,
                     template=args.template)

    if args.defaults:
        defaults = ruamel.yaml.safe_load(args.defaults)
        cfg = config.update(cfg, defaults)

    with config_file.open('w') as out:
        config.dump(cfg, out)

    sys.stderr.write("New config file written to {}\n".format(config_file))

    # Create sample list (if given data_fp)
    if args.data_fp:
        try:
            with samplelist_file.open('w') as out:
                build_sample_list(data_fp=args.data_fp,
                                  format_str=args.format,
                                  output_file=out,
                                  is_single_end=args.single_end)
                sys.stderr.write(
                    "New sample list written to {}\n".format(samplelist_file))
        except SampleFormatError as e:
            raise SystemExit(
                "Error: could not create sample list. Specify correct sample filename"
                " format using --format.\n  Reason: {}".format(e))
        except MissingMatePairError as e:
            raise SystemExit(
                "Error: assuming paired-end reads, but could not find mates. Specify "
                "--single-end if not paired-end, or provide sample name format "
                "using --format."
                "\n  Reason: {}".format(e))