Example #1
0
    def merge_assemblies(self):
        """ 
        merge all given assemblies into a new assembly. Copies the params
        from the first passed in extant assembly. this function is called 
        with the ipyrad -m flag. You must pass it at least 3 values, the first
        is a new assembly name (a new `param-newname.txt` will be created).
        The second and third args must be params files for currently existing
        assemblies. Any args beyond the third must also be params file for
        extant assemblies.
        """
        print(HEADER)
        print("\n  Merging assemblies: {}".format(self.args.merge[1:]))

        # Make sure there are the right number of args
        if len(self.args.merge) < 3:
            sys.exit(_WRONG_NUM_CLI_MERGE)

        # Make sure the first arg isn't a params file, someone could do it...
        newname = self.args.merge[0]
        if os.path.exists(newname) and ("params-" in newname):
            sys.exit(_WRONG_ORDER_CLI_MERGE)

        # Make sure first arg will create a param file that doesn't exist
        if os.path.exists("params-" + newname +
                          ".txt") and (not self.args.force):
            sys.exit(_NAME_EXISTS_MERGE.format("params-" + newname + ".txt"))

        # Make sure the rest of the args are params files that already exist
        assemblies_to_merge = self.args.merge[1:]
        for assembly in assemblies_to_merge:
            if not os.path.exists(assembly):
                sys.exit(_DOES_NOT_EXIST_MERGE.format(assembly))

        # Get assemblies for each of the passed in params files.
        # We're recycling some of the machinery for loading assemblies here
        assemblies = []
        for params_file in self.args.merge[1:]:
            self.args.params = params_file
            self.parse_params()
            self.get_assembly()
            assemblies.append(self.data)

        # Do the merge
        merged_assembly = ip.merge(newname, assemblies)

        ## Write out the merged assembly params file and report success
        merged_assembly.write_params("params-{}.txt".format(newname),
                                     force=self.args.force)

        print("\n  Merging succeeded. New params file for merged assembly:")
        print("\n    params-{}.txt\n".format(newname))
        sys.exit(1)
Example #2
0
def merge_assemblies(args):
    """ 
    merge all given assemblies into a new assembly. Copies the params
    from the first passed in extant assembly. this function is called 
    with the ipyrad -m flag. You must pass it at least 3 values, the first
    is a new assembly name (a new `param-newname.txt` will be created).
    The second and third args must be params files for currently existing
    assemblies. Any args beyond the third must also be params file for
    extant assemblies.
    """
    print("\n  Merging assemblies: {}".format(args.merge[1:]))

    ## Make sure there are the right number of args
    if len(args.merge) < 3:
        msg = "\n  Attempting to merge assemblies but wrong number of args. "\
            + "The format for the merging command is:"\
            + "\n\n    ipyrad -m new_assembly_name params-1.txt params-2.txt"
        sys.exit(msg)
    ## Make sure the first arg isn't a params file, i could see someone
    ## someone trying this
    newname = args.merge[0]
    if os.path.exists(newname) and "params-" in newname:
        msg = "\n  Looks like you're trying to pass in a params file that "\
            + "already exists. The first arg\n  for -m should be the name of "\
            + "the new assembly (this will create a new params file for you)."
        sys.exit(msg) 
    ## Make sure first arg will create a param file that doesn't already exist
    if os.path.exists("params-" + newname + ".txt") and not args.force:
        msg = "\n  First argument for ipyrad -m should be the name of the new"\
            + " assembly. This will create\n  a new params file called params-"\
            + newname + ".txt, but this file already exists."
        sys.exit(msg)
    ## Make sure the rest of the args are params files that already exist
    assemblies_to_merge = args.merge[1:]
    for assembly in assemblies_to_merge:
        if not os.path.exists(assembly):
            msg = "\n  All arguments after the first for -m need to be params"\
                + " files for assemblies with\n  samples that are all in state 1."\
                + " This assembly param file does not exist: " + assembly
            sys.exit(msg)

    assemblies = []
    ## Get assemblies for each of the passed in params files.
    ## We're recycling some of the machinery for loading assemblies here
    for params_file in args.merge[1:]:
        args.params = params_file
        parsedict = parse_params(args)
        assemblies.append(getassembly(args, parsedict))

    ## Do the merge
    merged_assembly = ip.merge(newname, assemblies)

    ## Set the values for some params that don't make sense inside
    ## merged assemblies
    merged_names = ", ".join([x.name for x in assemblies])
    merged_assembly.paramsdict["raw_fastq_path"] = "Merged: " + merged_names
    merged_assembly.paramsdict["barcodes_path"] = "Merged: " + merged_names
    merged_assembly.paramsdict["sorted_fastq_path"] = "Merged: " + merged_names

    ## Write out the merged assembly params file and report success
    merged_assembly.write_params("params-{}.txt".format(newname), force=args.force)

    print("\n  Merging succeeded. New params file for merged assembly:")
    print("\n    params-{}.txt\n".format(newname))