Returns a dict pdb_id -> chain(s) -> chain and SCOPe details.

def get_pfam_details(self, pfam_accession):
        '''Returns a dict pdb_id -> chain(s) -> chain and SCOPe details.'''

        results = self.execute_select('''
            SELECT DISTINCT scop_node.*, scop_node.release_id AS scop_node_release_id,
            pfam.release_id AS pfam_release_id, pfam.name AS pfam_name, pfam.accession, pfam.description AS pfam_description, pfam.length AS pfam_length,
            pfam_type.description AS pfam_type_description
            FROM `link_pfam`
            INNER JOIN scop_node on node_id=scop_node.id
            INNER JOIN pfam ON link_pfam.pfam_accession = pfam.accession
            INNER JOIN pfam_type ON pfam.pfam_type_id = pfam_type.id
            WHERE pfam.accession=%s ORDER BY scop_node.release_id DESC''', parameters = (pfam_accession,))

        if not results:
            return None

        # Only consider the most recent Pfam releases and most recent SCOPe records, giving priority to SCOPe revisions over Pfam revisions
        most_recent_record = None
        for r in results:
            accession = r['accession']
            if (not most_recent_record) or (r['scop_node_release_id'] > most_recent_record['scop_node_release_id']):
                most_recent_record = r
            elif r['pfam_release_id'] > most_recent_record['pfam_release_id']:
                most_recent_record = r

        d = dict(
            pfam_accession = most_recent_record['accession'],
            pfam_name = most_recent_record['pfam_name'],
            pfam_description = most_recent_record['pfam_description'],
            pfam_type_description = most_recent_record['pfam_type_description'],
            pfam_length = most_recent_record['pfam_length'],
            pfam_release_id = most_recent_record['pfam_release_id'],
            sunid = most_recent_record['sunid'],
            sccs = most_recent_record['sccs'],
            sid = most_recent_record['sid'],
            scop_release_id = most_recent_record['scop_node_release_id'],
            SCOPe_sources = 'SCOPe',
            SCOPe_search_fields = 'link_pfam.pfam_accession',
            SCOPe_trust_level = 1
        )

        for k, v in sorted(self.levels.iteritems()):
            d[v] = None

        level, parent_node_id = most_recent_record['level_id'], most_recent_record['parent_node_id']

        # Store the top-level description
        d[self.levels[level]] = most_recent_record['description']

        # Wind up the level hierarchy and retrieve the descriptions
        c = 0
        while level > 2 :
            parent_details = self.execute_select('SELECT * FROM scop_node WHERE id=%s', parameters = (parent_node_id,))
            assert(len(parent_details) <= 1)
            if parent_details:
                parent_details = parent_details[0]
                level, parent_node_id = parent_details['level_id'], parent_details['parent_node_id']
                d[self.levels[level]] = parent_details['description']
            else:
                break
            # This should never trigger but just in case...
            c += 1
            if c > 20:
                raise Exception('There is a logical error in the script or database which may result in an infinite lookup loop.')

        assert(d['Protein'] == d['Species'] == d['PDB Entry Domain'] == None)
        return d