ihmwg / python-ihm

Python package for handling IHM mmCIF and BinaryCIF files
MIT License
14 stars 7 forks source link

Change PolyResidueFeature type to 'residue' when a unique residue is provided #22

Closed mtrellet closed 6 years ago

mtrellet commented 6 years ago

When instantiating a PolyResidueFeature object the type is automatically set to 'residue range' even when a unique residue is provided. This is a minor syntax issue but the IHM dictionary allows for 'residue' type that would make sense here.

Since the PolyResidueFeature takes a list of AsymUnit or AsymUnitRange as input but exposes a unique type for each instance, it might be necessary to assign the type only at the dumper level. Or slightly re-design the class to have a type for each list element.

A temporary but dirty workaround I have is to check the residue IDs upon initialization, this will only work when the list has only one element though...

class PolyResidueFeature(Feature):
    """Selection of one or more residues from the system.

       :param sequence ranges: A list of :class:`AsymUnitRange` and/or
              :class:`AsymUnit` objects.
    """
    type = 'residue range'

    def __init__(self, ranges):
        self.ranges = ranges
        # Convert type to residue when the only range provided is constituted by
        # a unique residue
        if len(self.ranges) == 1 and \
                self.ranges[0].seq_id_range[0] == self.ranges[0].seq_id_range[1]:
            self.type = 'residue'

    # todo: handle case where ranges span multiple entities?
    entity = property(lambda self: self.ranges[0].entity
                                   if self.ranges else None)