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)}.")
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)}.")
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)}.")
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}")
def crop_stack_file(cls, stack_file, size, output_stack_file=None, fill_value=None): if output_stack_file is None: output_stack_file = set_output_name(stack_file, 'cropped') if os.path.exists(output_stack_file): raise FileExistsError( f"output file '{yellow(output_stack_file)}' already exists!") stack = load_stack_from_file(stack_file) fill_value = fill_value or PreProcessorConfig.crop_stack_fill_value cropped_stack = cls.crop_stack(stack, size, fill_value=fill_value) action = 'cropped' if size < stack.shape[1] else 'padded' logger.info( f"{action} stack from size {stack.shape} to size {cropped_stack.shape}." f" saving to {yellow(output_stack_file)}..") with mrcfile.new(output_stack_file) as mrc: mrc.set_data(cropped_stack) logger.debug(f"saved to {output_stack_file}")
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)}.")
def global_phaseflip_stack_file(cls, stack_file, output_stack_file=None): if output_stack_file is None: output_stack_file = set_output_name(stack_file, 'g-pf') if os.path.exists(output_stack_file): raise FileExistsError(f"output file '{yellow(output_stack_file)}' already exists!") in_stack = load_stack_from_file(stack_file) out_stack = cls.global_phaseflip_stack(in_stack) # check if stack was flipped if (out_stack[0] == in_stack[0]).all(): logger.info('not saving new mrc file.') else: with mrcfile.new(output_stack_file) as mrc: mrc.set_data(out_stack) logger.info(f"stack is flipped and saved as {yellow(output_stack_file)}")