<commit_before>#!/usr/bin/env python
# XDSIdxref.py
#   Copyright (C) 2006 CCLRC, <NAME>
#
#   This code is distributed under the BSD license, a copy of which is 
#   included in the root directory of this package.
#
# A wrapper to handle the JOB=IDXREF module in XDS.
#

import os
import sys
import math
import shutil

if not os.environ.has_key('XIA2CORE_ROOT'):
    raise RuntimeError, 'XIA2CORE_ROOT not defined'

if not os.environ.has_key('XIA2_ROOT'):
    raise RuntimeError, 'XIA2_ROOT not defined'

if not os.path.join(os.environ['XIA2CORE_ROOT'],
                    'Python') in sys.path:
    sys.path.append(os.path.join(os.environ['XIA2CORE_ROOT'],
                                 'Python'))
    
if not os.environ['XIA2_ROOT'] in sys.path:
    sys.path.append(os.environ['XIA2_ROOT'])

from Driver.DriverFactory import DriverFactory

# interfaces that this inherits from ...
from Schema.Interfaces.FrameProcessor import FrameProcessor

# generic helper stuff
from XDS import header_to_xds, xds_check_version_supported, xds_check_error
from Handlers.Streams import Debug

# specific helper stuff
from XDSIdxrefHelpers import _parse_idxref_lp, _parse_idxref_lp_distance_etc
from Experts.LatticeExpert import SortLattices

# global flags 
from Handlers.Flags import Flags

def XDSIdxref(DriverType = None):

    DriverInstance = DriverFactory.Driver(DriverType)

    class XDSIdxrefWrapper(DriverInstance.__class__,
                           FrameProcessor):
        '''A wrapper for wrapping XDS in idxref mode.'''

        def __init__(self):

            # set up the object ancestors...

            DriverInstance.__class__.__init__(self)
            FrameProcessor.__init__(self)

            # now set myself up...
            
            
            self._parallel = Flags.get_parallel()

            if self._parallel <= 1:
                self.set_executable('xds')
            else:
                self.set_executable('xds_par')

            # generic bits

            self._data_range = (0, 0)
            self._spot_range = []
            self._background_range = (0, 0)
            self._resolution_range = (0, 0)

            self._org = [0.0, 0.0]

            self._cell = None
            self._symm = 0

            # results

            self._refined_beam = (0, 0)
            self._refined_distance = 0

            self._indexing_solutions = { }

            self._indxr_input_lattice = None
            self._indxr_input_cell = None
            
            self._indxr_lattice = None
            self._indxr_cell = None
            self._indxr_mosaic = None

            self._input_data_files = { }
            self._output_data_files = { }

            self._input_data_files_list = ['SPOT.XDS']

            self._output_data_files_list = ['SPOT.XDS',
                                            'XPARM.XDS']

            return

        # getter and setter for input / output data

        def set_indexer_input_lattice(self, lattice):
            self._indxr_input_lattice = lattice
            return

        def set_indexer_input_cell(self, cell):
            if not type(cell) == type(()):
                raise RuntimeError, 'cell must be a 6-tuple de floats'

            if len(cell) != 6:
                raise RuntimeError, 'cell must be a 6-tuple de floats'

            self._indxr_input_cell = tuple(map(float, cell))
            return

        def set_input_data_file(self, name, data):
            self._input_data_files[name] = data
            return

        def get_output_data_file(self, name):
            return self._output_data_files[name]

        def get_refined_beam(self):
            return self._refined_beam

        def get_refined_distance(self):
            return self._refined_distance

        def get_indexing_solutions(self):
            return self._indexing_solutions

        def get_indexing_solution(self):
            return self._indxr_lattice, self._indxr_cell, self._indxr_mosaic

        # this needs setting up from setup_from_image in FrameProcessor

        def set_beam_centre(self, x, y):
            self._org = float(x), float(y)

        def set_data_range(self, start, end):
            self._data_range = (start, end)

        def add_spot_range(self, start, end):
            self._spot_range.append((start, end))

        def set_background_range(self, start, end):
            self._background_range = (start, end)

        def run(self, ignore_errors = False):
            '''Run idxref.'''

            header = header_to_xds(self.get_header())

            xds_inp = open(os.path.join(self.get_working_directory(),
                                        'XDS.INP'), 'w')

            # what are we doing?
            xds_inp.write('JOB=IDXREF\n')
            xds_inp.write('MAXIMUM_NUMBER_OF_PROCESSORS=%d\n' % \
                          self._parallel) 
            
            # FIXME this needs to be calculated from the beam centre...
            
            xds_inp.write('ORGX=%f ORGY=%f\n' % \
                          tuple(self._org))

            lattice_to_spacegroup = {'aP':1,
                                     'mP':3,
                                     'mC':5,
                                     'oP':16,
                                     'oC':20,
                                     'oF':22,
                                     'oI':23,
                                     'tP':75,
                                     'tI':79,
                                     'hP':143,
                                     'hR':146,
                                     'cP':195,
                                     'cF':196,
                                     'cI':197}

            # FIXME this looks like a potential bug - what will
            # happen if the input lattice has not been set??
            if self._indxr_input_cell:
                self._cell = self._indxr_input_cell
            if self._indxr_input_lattice:
                self._symm = lattice_to_spacegroup[self._indxr_input_lattice]

            xds_inp.write('SPACE_GROUP_NUMBER=%d\n' % self._symm)
            if self._cell:
                cell_format = '%6.2f %6.2f %6.2f %6.2f %6.2f %6.2f'
                xds_inp.write('UNIT_CELL_CONSTANTS=%s\n' % \
                              cell_format % self._cell)

            for record in header:
                xds_inp.write('%s\n' % record)

            name_template = os.path.join(self.get_directory(),
                                         self.get_template().replace('#', '?'))

            record = 'NAME_TEMPLATE_OF_DATA_FRAMES=%s\n' % \
                     name_template

            if len(record) < 80:
                xds_inp.write(record)
                
            else:
                # else we need to make a softlink, then run, then remove 
                # softlink....

                try:
                    Debug.write('Linking %s to %s' % (
                        self.get_directory(),<commit_msg>Only write the space group number IF it was given as input along with the target unit cell - this is a backup to bug # 2434<commit_after>#!/usr/bin/env python
# XDSIdxref.py
#   Copyright (C) 2006 CCLRC, <NAME>
#
#   This code is distributed under the BSD license, a copy of which is 
#   included in the root directory of this package.
#
# A wrapper to handle the JOB=IDXREF module in XDS.
#

import os
import sys
import math
import shutil

if not os.environ.has_key('XIA2CORE_ROOT'):
    raise RuntimeError, 'XIA2CORE_ROOT not defined'

if not os.environ.has_key('XIA2_ROOT'):
    raise RuntimeError, 'XIA2_ROOT not defined'

if not os.path.join(os.environ['XIA2CORE_ROOT'],
                    'Python') in sys.path:
    sys.path.append(os.path.join(os.environ['XIA2CORE_ROOT'],
                                 'Python'))
    
if not os.environ['XIA2_ROOT'] in sys.path:
    sys.path.append(os.environ['XIA2_ROOT'])

from Driver.DriverFactory import DriverFactory

# interfaces that this inherits from ...
from Schema.Interfaces.FrameProcessor import FrameProcessor

# generic helper stuff
from XDS import header_to_xds, xds_check_version_supported, xds_check_error
from Handlers.Streams import Debug

# specific helper stuff
from XDSIdxrefHelpers import _parse_idxref_lp, _parse_idxref_lp_distance_etc
from Experts.LatticeExpert import SortLattices

# global flags 
from Handlers.Flags import Flags

def XDSIdxref(DriverType = None):

    DriverInstance = DriverFactory.Driver(DriverType)

    class XDSIdxrefWrapper(DriverInstance.__class__,
                           FrameProcessor):
        '''A wrapper for wrapping XDS in idxref mode.'''

        def __init__(self):

            # set up the object ancestors...

            DriverInstance.__class__.__init__(self)
            FrameProcessor.__init__(self)

            # now set myself up...
            
            
            self._parallel = Flags.get_parallel()

            if self._parallel <= 1:
                self.set_executable('xds')
            else:
                self.set_executable('xds_par')

            # generic bits

            self._data_range = (0, 0)
            self._spot_range = []
            self._background_range = (0, 0)
            self._resolution_range = (0, 0)

            self._org = [0.0, 0.0]

            self._cell = None
            self._symm = 0

            # results

            self._refined_beam = (0, 0)
            self._refined_distance = 0

            self._indexing_solutions = { }

            self._indxr_input_lattice = None
            self._indxr_input_cell = None
            
            self._indxr_lattice = None
            self._indxr_cell = None
            self._indxr_mosaic = None

            self._input_data_files = { }
            self._output_data_files = { }

            self._input_data_files_list = ['SPOT.XDS']

            self._output_data_files_list = ['SPOT.XDS',
                                            'XPARM.XDS']

            return

        # getter and setter for input / output data

        def set_indexer_input_lattice(self, lattice):
            self._indxr_input_lattice = lattice
            return

        def set_indexer_input_cell(self, cell):
            if not type(cell) == type(()):
                raise RuntimeError, 'cell must be a 6-tuple de floats'

            if len(cell) != 6:
                raise RuntimeError, 'cell must be a 6-tuple de floats'

            self._indxr_input_cell = tuple(map(float, cell))
            return

        def set_input_data_file(self, name, data):
            self._input_data_files[name] = data
            return

        def get_output_data_file(self, name):
            return self._output_data_files[name]

        def get_refined_beam(self):
            return self._refined_beam

        def get_refined_distance(self):
            return self._refined_distance

        def get_indexing_solutions(self):
            return self._indexing_solutions

        def get_indexing_solution(self):
            return self._indxr_lattice, self._indxr_cell, self._indxr_mosaic

        # this needs setting up from setup_from_image in FrameProcessor

        def set_beam_centre(self, x, y):
            self._org = float(x), float(y)

        def set_data_range(self, start, end):
            self._data_range = (start, end)

        def add_spot_range(self, start, end):
            self._spot_range.append((start, end))

        def set_background_range(self, start, end):
            self._background_range = (start, end)

        def run(self, ignore_errors = False):
            '''Run idxref.'''

            header = header_to_xds(self.get_header())

            xds_inp = open(os.path.join(self.get_working_directory(),
                                        'XDS.INP'), 'w')

            # what are we doing?
            xds_inp.write('JOB=IDXREF\n')
            xds_inp.write('MAXIMUM_NUMBER_OF_PROCESSORS=%d\n' % \
                          self._parallel) 
            
            # FIXME this needs to be calculated from the beam centre...
            
            xds_inp.write('ORGX=%f ORGY=%f\n' % \
                          tuple(self._org))

            lattice_to_spacegroup = {'aP':1,
                                     'mP':3,
                                     'mC':5,
                                     'oP':16,
                                     'oC':20,
                                     'oF':22,
                                     'oI':23,
                                     'tP':75,
                                     'tI':79,
                                     'hP':143,
                                     'hR':146,
                                     'cP':195,
                                     'cF':196,
                                     'cI':197}

            # FIXME this looks like a potential bug - what will
            # happen if the input lattice has not been set??
            if self._indxr_input_cell:
                self._cell = self._indxr_input_cell
            if self._indxr_input_lattice:
                self._symm = lattice_to_spacegroup[self._indxr_input_lattice]

            if self._cell:
                xds_inp.write('SPACE_GROUP_NUMBER=%d\n' % self._symm)
                cell_format = '%6.2f %6.2f %6.2f %6.2f %6.2f %6.2f'
                xds_inp.write('UNIT_CELL_CONSTANTS=%s\n' % \
                              cell_format % self._cell)

            for record in header:
                xds_inp.write('%s\n' % record)

            name_template = os.path.join(self.get_directory(),
                                         self.get_template().replace('#', '?'))

            record = 'NAME_TEMPLATE_OF_DATA_FRAMES=%s\n' % \
                     name_template

            if len(record) < 80:
                xds_inp.write(record)
                
            else:
                # else we need to make a softlink, then run, then remove 
                # softlink....

                try:
                    Debug.write('Linking %s to %s' % (
                        self.get_directory(),
{'fraction_non_alphanumeric': 0.08853575482406356, 'fraction_numerical': 0.010783200908059024, 'mean_word_length': 1.4888771186440677, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 38, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4978306', 'n_tokens_mistral': 4200, 'n_tokens_neox': 3949, 'n_words': 889}