Ejemplo n.º 1
0
def abinitio_cmd(stack_file, output):
    """\b
        ############################
                 Abinitio
        ############################

        Abinitio accepts a stack file, calculates Abinitio algorithm on it and saves
        the results into output file (default adds '_abinitio' to stack name)
    """

    if output is None:
        output = set_output_name(stack_file, 'abinitio')

    if os.path.exists(output):
        logger.error(f"file {yellow(output)} already exsits! remove first "
                     "or use another name with '-o NAME'")
        return

    stack = load_stack_from_file(stack_file)

    logger.info(f'running abinitio on stack file {stack_file}..')
    output_stack = Abinitio.cryo_abinitio_c1_worker(stack)

    with mrcfile.new(output) as mrc_fh:
        mrc_fh.set_data(output_stack.astype('float32'))

    logger.info(f"saved to {yellow(output)}.")
Ejemplo n.º 2
0
def normalize_cmd(stack_file, output=None):
    """ \b
        ############################
            Normalize Stack
        ############################

        Normalize stack of projections.

        \b
        Example:
        $ python aspire.py normalize projections.mrc
        will produce file projections_normalized.mrc

    """

    if output is None:
        output = set_output_name(stack_file, 'normalized')

    if os.path.exists(output):
        logger.error(f"output file {yellow(output)} already exsits! "
                     f"remove first or use another name with '-o NAME' flag")
        return

    logger.info("normalizing projections..")
    stack = load_stack_from_file(stack_file, c_contiguous=True)
    normalized_stack = PreProcessor.normalize_background(stack.astype('float64'))
    with mrcfile.new(output) as fh:
        fh.set_data(normalized_stack.astype('float32'))

    logger.info(f"saved to {yellow(output)}.")
Ejemplo n.º 3
0
def star_phaseflip_cmd(stack_file, output=None):
    """ \b
        ############################
            Prewhitten Stack
        ############################

        Prewhitten projections in stack file.

        \b
        Example:
        $ python aspire.py prewhitten projections.mrc
        will produce file projections_prewhitten.mrc

    """

    if output is None:
        output = set_output_name(stack_file, 'prewhitten')

    if os.path.exists(output):
        logger.error(f"output file {yellow(output)} already exsits! "
                     f"remove first or use another name with '-o NAME' flag")
        return

    logger.info("prewhittening projections..")
    PreProcessor.prewhiten_stack_file(stack_file, output=output)
    logger.info(f"saved to {yellow(output)}.")
Ejemplo n.º 4
0
    def downsample_stack_file(cls,
                              stack_file,
                              side,
                              output_stack_file=None,
                              mask_file=None):

        if output_stack_file is None:
            output_stack_file = set_output_name(stack_file, 'downsampled')

        if os.path.exists(output_stack_file):
            raise FileExistsError(
                f"output file '{yellow(output_stack_file)}' already exists!")

        if mask_file:
            if not os.path.exists(mask_file):
                logger.error(f"mask file {yellow(mask_file)} doesn't exist!")
            mask = load_stack_from_file(mask_file)
        else:
            mask = None

        stack = load_stack_from_file(stack_file)
        downsampled_stack = cls.downsample(stack,
                                           side,
                                           compute_fx=False,
                                           stack=True,
                                           mask=mask)
        logger.info(
            f"downsampled stack from size {stack.shape} to {downsampled_stack.shape}."
            f" saving to {yellow(output_stack_file)}..")

        with mrcfile.new(output_stack_file) as mrc_fh:
            mrc_fh.set_data(downsampled_stack)
        logger.debug(f"saved to {output_stack_file}")
Ejemplo n.º 5
0
def chained_save_stack(ctx_obj, o):
    """ Save MRC stack to output file """
    if os.path.exists(o):  # TODO move this check before anything starts running
        logger.error("output file {} already exists! "
                     "please rename/delete or use flag -o with different output name")
        sys.exit(1)

    logger.info("saving stack {}..".format(o))
    mrcfile.new(o, ctx_obj.stack)
Ejemplo n.º 6
0
    def decorator(func):

        missing_binaries = set()
        for fn in filenames:
            if not is_resource(aspire.data, f'{fn}'):
                missing_binaries.add(fn)

        if missing_binaries:
            for fn in missing_binaries:
                logger.error(f"Binary file {yellow(fn)} is required!")

        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            return func(*args, **kwargs)

        return wrapper
Ejemplo n.º 7
0
    def decorator(func):

        missing_binaries = set()
        for fn in filenames:
            if not os.path.exists(f'./binaries/{fn}'):
                missing_binaries.add(fn)

        if missing_binaries:
            for fn in missing_binaries:
                logger.error(f"Binary file {yellow(fn)} is required!")

            logger.error(f"Please run \'{yellow('make data')}\' and try again.")
            sys.exit(1)

        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            return func(*args, **kwargs)

        return wrapper
Ejemplo n.º 8
0
def classify_cmd(stack_file, output, avg_nn, classification_nn):
    """ \b
        ############################
          Classification-Averaging
        ############################

        This command accepts a stack file and calculates the
        classification averaging algorithm.

        \b
        When it's done, it saves 2 files:
            1) The full classified stack
            2) A subset of the classified stack (for faster calculations)

        \b
        Example:
            input - stack.mrc
            output1 - stack_classified.mrc (or use flag -o to override)
            output2 - stack_classified_subset.mrc
    """
    if output is None:
        output = set_output_name(stack_file, 'classified')

    if os.path.exists(output):
        logger.error(f"output file {yellow(output)} already exsits! "
                     f"remove first or use another name with '-o NAME' flag")
        return

    subset_output_name = set_output_name(output, 'subset')
    if os.path.exists(subset_output_name):
        logger.error(
            f"subset file {yellow(subset_output_name)} already exsits! "
            f"remove first or use another name with '-o NAME' flag")
        return

    logger.info(f'class-averaging {stack_file}..')
    ClassAverages.run(stack_file,
                      output,
                      n_nbor=classification_nn,
                      nn_avg=avg_nn)
    logger.info(f"saved to {yellow(output)}.")
    logger.info(f"saved to {yellow(subset_output_name)}.")
Ejemplo n.º 9
0
def star_phaseflip_cmd(star_file, output=None):
    """ \b
        ############################
            Phaseflip (STAR file)
        ############################

        \b
        Apply phase-flip to projections in multiple mrc files having a
        STAR file pointing at them.
        After phaseflipping them, they will all be saved in 1 MRC file.
        Default output will add '_phaseflipped.mrc' to star file basename

        \b
        Example:
        ./aspire.py phaseflip ../my_projections/set.star
        will produce file set_phaseflipped.mrc

    """

    if not star_file.endswith('.star'):
        logger.error("input file name doesn't end with '.star'!")
        return

    if output is None:
        # convert 'path/to/foo.star' -> 'foo_phaseflipped.mrc'
        output = '_phaseflipped.mrc'.join(star_file.rsplit('.star', 1))
        output = os.path.basename(output)

    if os.path.exists(output):
        logger.error(f"output file {yellow(output)} already exists! "
                     "Use flag '-o OUTPUT' or remove file.")
        return

    logger.info("phaseflipping projections..")
    stack = PreProcessor.phaseflip_star_file(star_file)
    with mrcfile.new(output) as fh:
        fh.set_data(stack.astype('float32'))

    logger.info(f"saved {yellow(output)}.")