# Path: pygr/mapping.py
# class Collection(object):
#     'flexible storage mapping ID --> OBJECT'
# 
#     def __init__(self, saveDict=None, dictClass=dict, **kwargs):
#         '''saveDict, if not None, the internal mapping to use as our storage.
#         filename: if provided, a file path to a shelve (BerkeleyDB) file to
#               store the data in.
#         dictClass: if provided, the class to use for storage of dict data.'''
#         if saveDict is not None:
#             self.d = saveDict
#         elif 'filename' in kwargs: # USE A SHELVE (BERKELEY DB)
#             try:
#                 if kwargs['intKeys']: # ALLOW INT KEYS, HAVE TO USE IntShelve
#                     self.__class__ = IntShelve
#                 else:
#                     raise KeyError
#             except KeyError:
#                 self.__class__ = PicklableShelve
#             return self.__init__(**kwargs)
#         else:
#             self.d = dictClass()
#         classutil.apply_itemclass(self, kwargs)
# 
#     def __getitem__(self, k):
#         return self.d[k]
# 
#     def __setitem__(self, k, v):
#         self.d[k] = v
# 
#     def __delitem__(self, k):
#         del self.d[k]
# 
#     def __len__(self):
#         return len(self.d)
# 
#     def __contains__(self, k):
#         return k in self.d
# 
#     def __iter__(self):
#         return iter(self.d)
# 
#     def __getattr__(self, attr):
#         if attr == '__setstate__' or attr == '__dict__':
#             # This prevents infinite recursion during unpickling.
#             raise AttributeError
#         try: # PROTECT AGAINST INFINITE RECURSE IF NOT FULLY __init__ED...
#             return getattr(self.__dict__['d'], attr)
#         except KeyError:
#             raise AttributeError('Collection has no subdictionary')
#     close = close_if_possible
# 
#     def __del__(self):
#         'must ensure that shelve object is closed to save pending data'
#         try:
#             self.close()
#         except classutil.FileAlreadyClosedError:
#             pass


import unittest
import ConfigParser
import os
import string
import sys
import pygr.Data
    import hashlib
    import md5 as hashlib
        import random
from testlib import testutil, PygrTestProgram
from pygr.mapping import Collection
        from pygr import seqdb
        from pygr import seqdb, cnestedlist, sqlgraph
        from pygr import seqdb, cnestedlist, sqlgraph
        cds_db = seqdb.AnnotationDB(cds_slices, hg18,
                                    sliceAttrDict=dict(id=0, cds_id=1,
                                                       orientation=2,
                                                       gene_id=3, start=4,
                                                       stop=5))
        msa = cnestedlist.NLMSA(os.path.join(self.path,
                                             'refGene_cdsAnnot_hg18'), 'w',
                                pairwiseMode=True, bidirectional=False)
        for lines in open(os.path.join(testInputDir,
                                       'refGene_cdsAnnot%s_hg18.txt'
                                       % smallSamplePostfix),
                          'r').xreadlines():
            row = [x for x in lines.split('\t')] # CONVERT TO LIST SO MUTABLE
            row[1] = int(row[1]) # CONVERT FROM STRING TO INTEGER
            cds_slices[row[1]] = row
            cds = cds_db[row[1]] # GET THE ANNOTATION OBJECT FOR THIS EXON
            msa.addAnnotation(cds) # SAVE IT TO GENOME MAPPING
        cds_db.clear_cache() # not really necessary; cache should autoGC
        # SHELVE SHOULD BE EXPLICITLY CLOSED IN ORDER TO SAVE CURRENT CONTENTS
        cds_slices.close()
        msa.build() # FINALIZE GENOME ALIGNMENT INDEXES
        cds_db.__doc__ = 'CDS Annotation Database for hg18'
        pygr.Data.addResource('TEST.Annotation.hg18.cdss', cds_db)
        msa.__doc__ = 'NLMSA CDS for hg18'
        pygr.Data.addResource('TEST.Annotation.NLMSA.hg18.cdss', msa)
        cds_schema = pygr.Data.ManyToManyRelation(hg18, cds_db,
                                                  bindAttrs=('cds1', ))
        cds_schema.__doc__ = 'CDS Schema for hg18'
        pygr.Data.addSchema('TEST.Annotation.NLMSA.hg18.cdss', cds_schema)
        # BUILD ANNOTATION DATABASE FOR MOST CONSERVED ELEMENTS FROM UCSC

# Path: pygr/mapping.py
# class Collection(object):
#     'flexible storage mapping ID --> OBJECT'
# 
#     def __init__(self, saveDict=None, dictClass=dict, **kwargs):
#         '''saveDict, if not None, the internal mapping to use as our storage.
#         filename: if provided, a file path to a shelve (BerkeleyDB) file to
#               store the data in.
#         dictClass: if provided, the class to use for storage of dict data.'''
#         if saveDict is not None:
#             self.d = saveDict
#         elif 'filename' in kwargs: # USE A SHELVE (BERKELEY DB)
#             try:
#                 if kwargs['intKeys']: # ALLOW INT KEYS, HAVE TO USE IntShelve
#                     self.__class__ = IntShelve
#                 else:
#                     raise KeyError
#             except KeyError:
#                 self.__class__ = PicklableShelve
#             return self.__init__(**kwargs)
#         else:
#             self.d = dictClass()
#         classutil.apply_itemclass(self, kwargs)
# 
#     def __getitem__(self, k):
#         return self.d[k]
# 
#     def __setitem__(self, k, v):
#         self.d[k] = v
# 
#     def __delitem__(self, k):
#         del self.d[k]
# 
#     def __len__(self):
#         return len(self.d)
# 
#     def __contains__(self, k):
#         return k in self.d
# 
#     def __iter__(self):
#         return iter(self.d)
# 
#     def __getattr__(self, attr):
#         if attr == '__setstate__' or attr == '__dict__':
#             # This prevents infinite recursion during unpickling.
#             raise AttributeError
#         try: # PROTECT AGAINST INFINITE RECURSE IF NOT FULLY __init__ED...
#             return getattr(self.__dict__['d'], attr)
#         except KeyError:
#             raise AttributeError('Collection has no subdictionary')
#     close = close_if_possible
# 
#     def __del__(self):
#         'must ensure that shelve object is closed to save pending data'
#         try:
#             self.close()
#         except classutil.FileAlreadyClosedError:
#             pass


# Path: tests/annotation_hg18_megatest.py
import unittest
import ConfigParser
import os
import string
import sys
import pygr.Data
    import hashlib
    import md5 as hashlib
        import random
from testlib import testutil, PygrTestProgram
from pygr.mapping import Collection
        from pygr import seqdb
        from pygr import seqdb, cnestedlist, sqlgraph
        from pygr import seqdb, cnestedlist, sqlgraph

        cds_db = seqdb.AnnotationDB(cds_slices, hg18,
                                    sliceAttrDict=dict(id=0, cds_id=1,
                                                       orientation=2,
                                                       gene_id=3, start=4,
                                                       stop=5))
        msa = cnestedlist.NLMSA(os.path.join(self.path,
                                             'refGene_cdsAnnot_hg18'), 'w',
                                pairwiseMode=True, bidirectional=False)
        for lines in open(os.path.join(testInputDir,
                                       'refGene_cdsAnnot%s_hg18.txt'
                                       % smallSamplePostfix),
                          'r').xreadlines():
            row = [x for x in lines.split('\t')] # CONVERT TO LIST SO MUTABLE
            row[1] = int(row[1]) # CONVERT FROM STRING TO INTEGER
            cds_slices[row[1]] = row
            cds = cds_db[row[1]] # GET THE ANNOTATION OBJECT FOR THIS EXON
            msa.addAnnotation(cds) # SAVE IT TO GENOME MAPPING
        cds_db.clear_cache() # not really necessary; cache should autoGC
        # SHELVE SHOULD BE EXPLICITLY CLOSED IN ORDER TO SAVE CURRENT CONTENTS
        cds_slices.close()
        msa.build() # FINALIZE GENOME ALIGNMENT INDEXES
        cds_db.__doc__ = 'CDS Annotation Database for hg18'
        pygr.Data.addResource('TEST.Annotation.hg18.cdss', cds_db)
        msa.__doc__ = 'NLMSA CDS for hg18'
        pygr.Data.addResource('TEST.Annotation.NLMSA.hg18.cdss', msa)
        cds_schema = pygr.Data.ManyToManyRelation(hg18, cds_db,
                                                  bindAttrs=('cds1', ))
        cds_schema.__doc__ = 'CDS Schema for hg18'
        pygr.Data.addSchema('TEST.Annotation.NLMSA.hg18.cdss', cds_schema)
        # BUILD ANNOTATION DATABASE FOR MOST CONSERVED ELEMENTS FROM UCSC

        ucsc_slices = Collection(