[Biopython] SeqRecord subclassing or composition

Uri Laserson laserson at mit.edu
Wed Mar 9 15:28:22 UTC 2011


>
> Unless you modify the methods/atttributes too much, a
> ImmuneChain subclass of SeqRecord should be usable
> as is with SeqIO.write etc. You don't need to 'cast'.
>

I'm more worried about parsing than writing.  As you mentioned, I will have
to upgrade my SeqRecord object to an ImmuneChain object.

So maybe the best approach is a combination of the two code snippets I
included.  It would subclass SeqRecord, and then manually check whether I am
initializing with a pre-existing SeqRecord or just data:

class ImmuneChain(SeqRecord):
    def __init__(self, *args, **kw):
        if isinstance(args[0],SeqRecord):
            # if initializing with SeqRecord, then manually transfer the
data
            # based on the initializer for SeqRecord (http://goo.gl/X95Zf)
            record = args[0]
            SeqRecord.__init__(self, seq, id=record.id, name=record.name,
                     description=record.description, dbxrefs=record.dbxrefs,
                     features=record.features,
annotations=record.annotations,
                     letter_annotations=record.letter_annotations)
        else:
            # assume I'm initializing just like a regular SeqRecord:
            SeqRecord.__init__(*args,**kw)

        # Finally, I perform any problem-specific additional initializations
        # here.
        pass

Does this seem like a good solution?

Also, do you think that it would make sense to make a deep copy of the
SeqRecord object before I use it to initialize the ImmuneChain?

Uri



More information about the Biopython mailing list