def strip_pdb(pdb_path, chains = [], strip_hetatms = False):
    '''Takes a PDB file and strips all lines except ATOM and HETATM records. If chains is specified, only those chains are kept. If strip_hetatms is True then HETATM lines are also stripped.
       Returns (True, a path to the stripped PDB file) on success and (False, a list of errors) on failure.'''
    chains = set(chains)
    contents = open(pdb_path).read().split("\n") # file handle should get garbage collected
    if strip_hetatms:
        if chains:
            atom_lines = [l for l in contents if l.startswith("ATOM  ") and l[21] in chains]
        else:
            atom_lines = [l for l in contents if l.startswith("ATOM  ")]
    else:
        if chains:
            atom_lines = [l for l in contents if (l.startswith("ATOM  ") or l.startswith("HETATM")) and l[21] in chains]
        else:
            atom_lines = [l for l in contents if (l.startswith("ATOM  ") or l.startswith("HETATM"))]
    existing_chains = set([l[21] for l in atom_lines])
    if chains.difference(existing_chains):
        return False, ["Error: The following chains do not exist in the PDB file - %s" % ", ".join(list(chains.difference(existing_chains)))]

    F, temp_pdb_path = tempfile.mkstemp(dir=".")
    temp_pdb_handle = os.fdopen(F, "w")
    temp_pdb_handle.write("\n".join(atom_lines))
    temp_pdb_handle.close()
    return True, temp_pdb_path