def cdfMol_pdb(pdb, output, name):
    initial_time = time.time()
    cdf_mol = Chem.BasicMolecule()
    pdb_mol = Chem.BasicMolecule()

    pdb_str = open(pdb, 'r').read().replace('WAT', 'HOH').replace('HIE', 'HIS')
    pdb_reader = Biomol.PDBMoleculeReader(Base.StringIOStream(pdb_str))

    Biomol.setPDBApplyDictAtomBondingToNonStdResiduesParameter(
        pdb_reader, True)
    if not pdb_reader.read(pdb_mol):
        return None

    Chem.calcImplicitHydrogenCounts(pdb_mol, False)
    Chem.perceiveHybridizationStates(pdb_mol, False)
    Chem.setAtomSymbolsFromTypes(pdb_mol, False)
    Chem.perceiveSSSR(pdb_mol, False)
    Chem.setRingFlags(pdb_mol, False)
    Chem.setAromaticityFlags(pdb_mol, False)

    cdf_mol.assign(pdb_mol)
    for atom in cdf_mol.atoms:
        Chem.set3DCoordinatesArray(atom, Math.Vector3DArray())

    i = 0
    while i < cdf_mol.numAtoms:
        Chem.get3DCoordinatesArray(cdf_mol.getAtom(i)).addElement(
            Chem.get3DCoordinates(pdb_mol.getAtom(i)))
        i += 1

    tmp_output = output + name + ".cdf"
    try:
        Chem.FileCDFMolecularGraphWriter(tmp_output).write(cdf_mol)
    except:
        print('> Cdf_mol writing failure.')
        raise

    residues = Biomol.ResidueList(cdf_mol)
    tmp_output = output + name + "_residue_info.txt"
    with open(tmp_output, 'w') as txt_writer:
        txt_writer.write('residue name_resid_chain\n')
        for res in residues:
            res_id = getResidueID(res)
            txt_writer.write('{}: \n'.format(res_id))

    calc_time = time.time() - initial_time
    print('> Cdf and amino acid residue number list files generated in {}s'.
          format(int(calc_time)))
def process():
    if len(sys.argv) < 3:
        print >> sys.stderr, 'Usage:', sys.argv[
            0], '[input CDF-file] [output CDF-file] [[residue subset]]'
        sys.exit(2)

    print '- Processing CDF-file:', sys.argv[1], '...'

    mol = Util.loadCDFMolecule(sys.argv[1])

    if not mol:
        print '!! Could not read file'
        sys.exit(2)

    residues = Biomol.ResidueList(mol)

    print '- Num. residues:', residues.getSize()

    if len(sys.argv) > 3:
        res_subset_ids = Util.toIntegerList(Util.readLines(sys.argv[3]))

        print '- Residue subset:', res_subset_ids

        Util.filterResidues(residues, res_subset_ids)

    print '- num residues', len(residues)

    num_confs = Chem.getNumConformations(mol)

    print '- Num. frames:', num_confs
    print '- Aligning frames...'

    res_positions = []

    for res in residues:
        atoms = Util.getBackboneAtoms(res)
        positions = []
        i = 0

        while i < num_confs:
            positions.append(Util.calcAtomSetCentroid(atoms, i))
            i += 1

        res_positions.append(positions)

    alignment = Math.DKabschAlgorithm()
    al_ref_positions = Math.DMatrix(3, residues.getSize())
    al_positions = Math.DMatrix(3, residues.getSize())
    i = 0

    while i < residues.getSize():
        pos = res_positions[i][0]

        al_ref_positions.setElement(0, i, pos[0])
        al_ref_positions.setElement(1, i, pos[1])
        al_ref_positions.setElement(2, i, pos[2])
        i += 1

    i = 1
    xform = Math.Matrix4D()

    while i < num_confs:
        j = 0

        while j < residues.getSize():
            pos = res_positions[j][i]

            al_positions.setElement(0, j, pos[0])
            al_positions.setElement(1, j, pos[1])
            al_positions.setElement(2, j, pos[2])
            j += 1

        if not alignment.align(al_positions, al_ref_positions):
            print '!! Could not align frame', i

        else:
            xform.assign(alignment.getTransform())
            Chem.transformConformation(mol, i, xform)

        i += 1

    if not Util.saveCDFMolecule(sys.argv[2], mol):
        print '!! Could not write output file'
        sys.exit(2)