|  | 
|  | 1 | +# LIBTBX_SET_DISPATCHER_NAME dev.dials.split_scaled_outliers | 
|  | 2 | + | 
|  | 3 | +""" | 
|  | 4 | +Separate scaled reflections from intensity outliers.  Save them in separate files. | 
|  | 5 | +
 | 
|  | 6 | +The scaling component of the DIALS pipeline, dials.scale, rejects reflections that | 
|  | 7 | +have intensities that are deemed to be outliers according to a Wilson intensity | 
|  | 8 | +distribution.  They are not used to calculate scaling statistics, but are labelled | 
|  | 9 | +and retained in the output table of reflections.  This utility separates the outlier | 
|  | 10 | +reflections and the scaled reflections in the reflection table output from | 
|  | 11 | +dials.scale and saves them in separate reflection table files. | 
|  | 12 | +""" | 
|  | 13 | + | 
|  | 14 | +import argparse | 
|  | 15 | +import pathlib | 
|  | 16 | +import sys | 
|  | 17 | +from typing import Union, List, Optional | 
|  | 18 | + | 
|  | 19 | +from dials.array_family import flex | 
|  | 20 | + | 
|  | 21 | +File = Union[str, pathlib.Path] | 
|  | 22 | + | 
|  | 23 | + | 
|  | 24 | +class Formatter( | 
|  | 25 | +    argparse.RawDescriptionHelpFormatter, argparse.ArgumentDefaultsHelpFormatter | 
|  | 26 | +): | 
|  | 27 | +    """Print argument default values and preserve help text linebreaks.""" | 
|  | 28 | + | 
|  | 29 | +    pass | 
|  | 30 | + | 
|  | 31 | + | 
|  | 32 | +parser = argparse.ArgumentParser(description=__doc__, formatter_class=Formatter) | 
|  | 33 | +parser.add_argument( | 
|  | 34 | +    "reflection-table", | 
|  | 35 | +    help=( | 
|  | 36 | +        "A DIALS reflection table (.refl) file containing scaled reflections, " | 
|  | 37 | +        "as produced by dials.scale." | 
|  | 38 | +    ), | 
|  | 39 | +    type=pathlib.Path, | 
|  | 40 | +) | 
|  | 41 | +parser.add_argument( | 
|  | 42 | +    "--scaled-output", | 
|  | 43 | +    "-s", | 
|  | 44 | +    default="scaled.refl", | 
|  | 45 | +    help="File name for output table of scaled reflections.", | 
|  | 46 | +    type=pathlib.Path, | 
|  | 47 | +) | 
|  | 48 | +parser.add_argument( | 
|  | 49 | +    "--outliers-output", | 
|  | 50 | +    "-o", | 
|  | 51 | +    default="outliers.refl", | 
|  | 52 | +    help="File name for output table of reflections deemed to be intensity outliers.", | 
|  | 53 | +    type=pathlib.Path, | 
|  | 54 | +) | 
|  | 55 | +parser.add_argument( | 
|  | 56 | +    "--force-overwrite", | 
|  | 57 | +    "-f", | 
|  | 58 | +    help="Overwrite pre-existing output files.", | 
|  | 59 | +    action="store_true", | 
|  | 60 | +) | 
|  | 61 | + | 
|  | 62 | + | 
|  | 63 | +def split_scaled_and_outliers( | 
|  | 64 | +    reflection_table: File, scaled_output: File, outliers_output: File | 
|  | 65 | +) -> None: | 
|  | 66 | +    """ | 
|  | 67 | +    Save scaled reflections and intensity outliers to separate reflection tables. | 
|  | 68 | +
 | 
|  | 69 | +    Args: | 
|  | 70 | +        reflection_table:  A DIALS reflection table containing reflections after | 
|  | 71 | +                           scaling. | 
|  | 72 | +        scaled_output:  The file path for the scaled reflections. | 
|  | 73 | +        outliers_output:  The file path for the intensity outliers. | 
|  | 74 | +    """ | 
|  | 75 | +    reflections = flex.reflection_table.from_file(reflection_table) | 
|  | 76 | + | 
|  | 77 | +    scaled = reflections.get_flags(reflections.flags.scaled) | 
|  | 78 | +    outliers = reflections.get_flags(reflections.flags.outlier_in_scaling) | 
|  | 79 | + | 
|  | 80 | +    reflections.select(scaled).as_file(scaled_output) | 
|  | 81 | +    reflections.select(outliers).as_file(outliers_output) | 
|  | 82 | + | 
|  | 83 | + | 
|  | 84 | +def command_line_interface(args: Optional[List[str]] = None): | 
|  | 85 | +    """ | 
|  | 86 | +    Call the split_scaled_and_outliers routine as if from the command line. | 
|  | 87 | +
 | 
|  | 88 | +    Args: | 
|  | 89 | +        args:  List of argument words passed as if from the command line input | 
|  | 90 | +               (defaults to sys.argv[1:]). | 
|  | 91 | +    """ | 
|  | 92 | +    args = parser.parse_args(args) | 
|  | 93 | + | 
|  | 94 | +    if not args.reflection_table.is_file(): | 
|  | 95 | +        sys.exit(f"Could not find specified input file {args.reflection_table}.") | 
|  | 96 | + | 
|  | 97 | +    split_scaled_and_outliers( | 
|  | 98 | +        args.reflection_table, args.scaled_output, args.outliers_output | 
|  | 99 | +    ) | 
|  | 100 | + | 
|  | 101 | + | 
|  | 102 | +if __name__ == "__main__": | 
|  | 103 | +    command_line_interface() | 
0 commit comments