# 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
        exon_db = seqdb.AnnotationDB(exon_slices, hg18,
                                     sliceAttrDict=dict(id=0, exon_id=1,
                                                        orientation=2,
                                                        gene_id=3, start=4,
                                                        stop=5))
        msa = cnestedlist.NLMSA(os.path.join(self.path,
                                             'refGene_exonAnnot_hg18'), 'w',
                                pairwiseMode=True, bidirectional=False)
        for lines in open(os.path.join(testInputDir,
                                       'refGene_exonAnnot%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
            exon_slices[row[1]] = row
            exon = exon_db[row[1]] # GET THE ANNOTATION OBJECT FOR THIS EXON
            msa.addAnnotation(exon) # SAVE IT TO GENOME MAPPING
        exon_db.clear_cache() # not really necessary; cache should autoGC
        # SHELVE SHOULD BE EXPLICITLY CLOSED IN ORDER TO SAVE CURRENT CONTENTS
        exon_slices.close()
        msa.build() # FINALIZE GENOME ALIGNMENT INDEXES
        exon_db.__doc__ = 'Exon Annotation Database for hg18'
        pygr.Data.addResource('TEST.Annotation.hg18.exons', exon_db)
        msa.__doc__ = 'NLMSA Exon for hg18'
        pygr.Data.addResource('TEST.Annotation.NLMSA.hg18.exons', msa)
        exon_schema = pygr.Data.ManyToManyRelation(hg18, exon_db,
                                                   bindAttrs=('exon1', ))
        exon_schema.__doc__ = 'Exon Schema for hg18'
        pygr.Data.addSchema('TEST.Annotation.NLMSA.hg18.exons', exon_schema)
        # BUILD ANNOTATION DATABASE FOR REFSEQ SPLICES

# 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

        exon_db = seqdb.AnnotationDB(exon_slices, hg18,
                                     sliceAttrDict=dict(id=0, exon_id=1,
                                                        orientation=2,
                                                        gene_id=3, start=4,
                                                        stop=5))
        msa = cnestedlist.NLMSA(os.path.join(self.path,
                                             'refGene_exonAnnot_hg18'), 'w',
                                pairwiseMode=True, bidirectional=False)
        for lines in open(os.path.join(testInputDir,
                                       'refGene_exonAnnot%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
            exon_slices[row[1]] = row
            exon = exon_db[row[1]] # GET THE ANNOTATION OBJECT FOR THIS EXON
            msa.addAnnotation(exon) # SAVE IT TO GENOME MAPPING
        exon_db.clear_cache() # not really necessary; cache should autoGC
        # SHELVE SHOULD BE EXPLICITLY CLOSED IN ORDER TO SAVE CURRENT CONTENTS
        exon_slices.close()
        msa.build() # FINALIZE GENOME ALIGNMENT INDEXES
        exon_db.__doc__ = 'Exon Annotation Database for hg18'
        pygr.Data.addResource('TEST.Annotation.hg18.exons', exon_db)
        msa.__doc__ = 'NLMSA Exon for hg18'
        pygr.Data.addResource('TEST.Annotation.NLMSA.hg18.exons', msa)
        exon_schema = pygr.Data.ManyToManyRelation(hg18, exon_db,
                                                   bindAttrs=('exon1', ))
        exon_schema.__doc__ = 'Exon Schema for hg18'
        pygr.Data.addSchema('TEST.Annotation.NLMSA.hg18.exons', exon_schema)
        # BUILD ANNOTATION DATABASE FOR REFSEQ SPLICES

        splice_slices = Collection(