#!/usr/bin/env python

import IMP.em


def main():
    IMP.set_log_level(IMP.SILENT)
    desc = """
Calculates the protein principal components and writes them in cmm format.
The 3D points participating in the PCA calculation are the centers of voxels
with density above the input threshold."""
    p = IMP.ArgumentParser(description=desc)
    p.add_argument("pdb", help="input protein PDB file name")
    p.add_argument("pca", help="output PCA file name")
    args = p.parse_args()
    in_prot_fn = args.pdb
    out_pca_fn = args.pca
    mdl = IMP.Model()
    mol = IMP.atom.read_pdb(in_prot_fn, mdl)
    vecs = []
    for xyz in IMP.core.XYZs(IMP.core.get_leaves(mol)):
        vecs.append(xyz.get_coordinates())
    pca = IMP.algebra.get_principal_components(vecs)
    f = open(out_pca_fn, "w")
    IMP.em.write_pca_cmm(pca, f)
    f.close()

if __name__ == "__main__":
    main()
