r"""Deformation energy of the consecutive overlapped DNA segments

        It can be used to calculate deformation energy of several base-step/s from input DNA object with
        reference to the same base-step/s DNA present in the current DNA object.

        For local deformation energy calculation see :meth:`dnaEY.getLocalDeformationEnergy`.

        Parameters
        ----------
        bp : list
            List of two base-steps forming the DNA segment.
            For example: with ``bp=[5, 50]``, 5-50 base-step segment will be considered.

        span : int
            Length of overlapping (local) DNA segments. It should be less than four.

        complexDna : :class:`dnaMD.DNA`
            Input :class:`dnaMD.DNA` instance for which deformation energy will be calculated.

        freeDnaFrames : list
            To select a trajectory segment of current (free) DNA data.
            List of two trajectory frames between which parameters will be extracted. It can be used to select portions
            of the trajectory. For example, with ``frames=[100, 1000]``, 100th to 1000th frame of the trajectory will be
            considered.

        boundDnaFrames : list
            To select a trajectory segment of input (bound) DNA data.
            List of two trajectory frames between which parameters will be extracted. It can be used to select portions
            of the trajectory. For example, with ``frames=[100, 1000]``, 100th to 1000th frame of the trajectory will be
            considered.

        helical : bool
            If ``helical=True``, elastic matrix for **helical base-step** parameters are calculated. Otherwise,
            by default, elastic matrix for **base-step** parameters are calculated.

        unit : str
            Unit of energy. Allowed units are: ``'kT', 'kJ/mol' and 'kcal/mol'``.

        which : str or list
            For which motions (degrees of freedom), energy should be calculated. It should be either a list containing
            terms listed below or "all" for all energy terms.

            Following keywords are available:
                * ``'full'`` : Use entire elastic matrix -- all parameters with their coupling
                * ``'diag'`` : Use diagonal of elastic matrix -- all motions but no coupling
                * ``'shift'`` or ``'x-disp'``
                * ``'slide'`` or ``'y-idsp'``
                * ``'rise'`` or ``'h-rise'``
                * ``'tilt'`` or ``'inclination'``
                * ``'roll'`` or ``'tip'``
                * ``'twist'`` or ``'h-twist'``

        err_type : str
            Error estimation by autocorrelation method ``err_type='acf'`` or
            block averaging method ``err_type='block'``

        tool : str
            GROMACS tool to calculate error. In older versions it is `g_analyze` while in
            newer versions (above 2016) it is `gmx analyze`.

        outFile : str
            Output file in csv format.

        Returns
        -------
        segments : list
            list of DNA segments for which local deformation energy was calculated.

        energies : dict of numpy.ndarray
            Dictionary of 1D array of shape (segments) containing energy terms requested for DNA.

        error : OrderedDict
            A ordered dictionary of 1D arrays of shape (segments). The keys in dictionary are energy-terms in
            the same order as listed above..

def getLocalDeformationEnergySegments(self, bp, complexDna, span=2,  freeDnaFrames=None, boundDnaFrames=None,
                                          helical=False, unit='kT', which='all', err_type='block', tool='gmx analyze',
                                          outFile=None):
        r"""Deformation energy of the consecutive overlapped DNA segments

        It can be used to calculate deformation energy of several base-step/s from input DNA object with
        reference to the same base-step/s DNA present in the current DNA object.

        For local deformation energy calculation see :meth:`dnaEY.getLocalDeformationEnergy`.

        Parameters
        ----------
        bp : list
            List of two base-steps forming the DNA segment.
            For example: with ``bp=[5, 50]``, 5-50 base-step segment will be considered.

        span : int
            Length of overlapping (local) DNA segments. It should be less than four.

        complexDna : :class:`dnaMD.DNA`
            Input :class:`dnaMD.DNA` instance for which deformation energy will be calculated.

        freeDnaFrames : list
            To select a trajectory segment of current (free) DNA data.
            List of two trajectory frames between which parameters will be extracted. It can be used to select portions
            of the trajectory. For example, with ``frames=[100, 1000]``, 100th to 1000th frame of the trajectory will be
            considered.

        boundDnaFrames : list
            To select a trajectory segment of input (bound) DNA data.
            List of two trajectory frames between which parameters will be extracted. It can be used to select portions
            of the trajectory. For example, with ``frames=[100, 1000]``, 100th to 1000th frame of the trajectory will be
            considered.

        helical : bool
            If ``helical=True``, elastic matrix for **helical base-step** parameters are calculated. Otherwise,
            by default, elastic matrix for **base-step** parameters are calculated.

        unit : str
            Unit of energy. Allowed units are: ``'kT', 'kJ/mol' and 'kcal/mol'``.

        which : str or list
            For which motions (degrees of freedom), energy should be calculated. It should be either a list containing
            terms listed below or "all" for all energy terms.

            Following keywords are available:
                * ``'full'`` : Use entire elastic matrix -- all parameters with their coupling
                * ``'diag'`` : Use diagonal of elastic matrix -- all motions but no coupling
                * ``'shift'`` or ``'x-disp'``
                * ``'slide'`` or ``'y-idsp'``
                * ``'rise'`` or ``'h-rise'``
                * ``'tilt'`` or ``'inclination'``
                * ``'roll'`` or ``'tip'``
                * ``'twist'`` or ``'h-twist'``

        err_type : str
            Error estimation by autocorrelation method ``err_type='acf'`` or
            block averaging method ``err_type='block'``

        tool : str
            GROMACS tool to calculate error. In older versions it is `g_analyze` while in
            newer versions (above 2016) it is `gmx analyze`.

        outFile : str
            Output file in csv format.

        Returns
        -------
        segments : list
            list of DNA segments for which local deformation energy was calculated.

        energies : dict of numpy.ndarray
            Dictionary of 1D array of shape (segments) containing energy terms requested for DNA.

        error : OrderedDict
            A ordered dictionary of 1D arrays of shape (segments). The keys in dictionary are energy-terms in
            the same order as listed above..

        """

        if helical:
            energyTerms =  ['full', 'diag', 'x-disp', 'y-disp', 'h-rise', 'inclination', 'tip', 'h-twist']
        else:
            energyTerms = ['full', 'diag', 'shift', 'slide', 'rise', 'tilt', 'roll', 'twist']

        if isinstance(which, str):
            if which != 'all':
                raise ValueError('Either use "all" or use list of terms from this {0} list \n.'.format(energyTerms))
            else:
                which = energyTerms
        elif isinstance(which, list):
            for key in which:
                if key not in energyTerms:
                    raise ValueError('{0} is not a supported keyword.\n Use from the following list: \n{1}'.format(
                        which, energyTerms))
        else:
            raise ValueError('Either use "all" or use list of terms from this {0} list \n.'.format(energyTerms))

        segments, errors, energies = [], OrderedDict(), OrderedDict()
        for name in which:
            energies[name] = []
            errors[name] = []

        for s in range(bp[0], bp[1]):
            if s+span-1 > bp[1]:
                break
            time, energy_t = self.getLocalDeformationEnergy([s, s+span-1], complexDna, freeDnaFrames=freeDnaFrames,
                                                            boundDnaFrames=boundDnaFrames, helical=helical,
                                                            unit=unit, which=which)
            error_t = dnaMD.get_error(time, list(energy_t.values()), len(which), err_type=err_type, tool=tool)
            for i in range(len(which)):
                en_t = np.mean(energy_t[which[i]])           # Calculate average
                energies[which[i]].append(en_t)
                errors[which[i]].append(error_t[i])
            segments.append('{0}-{1}'.format(s, s+span-1))

        # Write output file
        if outFile is not None:
            with open(outFile, 'w') as fout:
                fout.write('#bps')
                for name in which:
                    fout.write(', {0}, {0}-error'.format(name))
                fout.write('\n')
                for s in range(len(segments)):
                    fout.write('{0}'.format(segments[s]))
                    for name in which:
                        fout.write(', {0:.5f}, {1:.5f}'.format(energies[name][s], errors[name][s]))
                    fout.write('\n')

        return segments, energies, errors