def read_tigr_roles(filename): """ Reads a truly loony file format that stores the tigrfam mainrole/subrole hierarchy. Returns a tuple containing a list of mainroles whose children are subroles and a dictionary from tigr role id to tigr roles, which can be used with the file TIGRFAMS_ROLE_LINK to attach TIGRFams to roles. """ mainroles = [] mainroles_by_name = {} mainroles_by_id = {} roles_by_id = {} with open(filename, 'r') as f: for line in f: fields = line.rstrip("\n").split("\t") role = OpenStruct() role.tigr_role_id = int(fields[1]) role.type = fields[2].rstrip(':') role.name = fields[3] if role.type=='mainrole': if role.name not in mainroles_by_name: mainroles.append(role) mainroles_by_name[role.name] = role role.children = [] mainroles_by_id[role.tigr_role_id] = mainroles_by_name[role.name] elif role.type=='sub1role': roles_by_id[role.tigr_role_id] = role else: raise "Unknown role type: " + role.type # add subroles to main roles for id in roles_by_id: mainroles_by_id[id].children.append(roles_by_id[id]) return (mainroles, roles_by_id,)