#!/usr/bin/env python

import IMP.em


def main():
    IMP.set_log_level(IMP.SILENT)
    desc = """
Calculates the map 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("-p", "--apix", type=float, dest="apix", help="voxel size")
    p.add_argument("density", help="EM density file name")
    p.add_argument("threshold", type=float, help="EM density threshold")
    p.add_argument("pca", help="Output PCA file name")
    args = p.parse_args()
    in_map_fn = args.density
    threshold = args.threshold
    out_pca_fn = args.pca
    dmap = IMP.em.read_map(in_map_fn)
    if args.apix:
        dmap.update_voxel_size(args.apix)
    dens_vecs = IMP.em.density2vectors(dmap, threshold)
    dens_pca = IMP.algebra.get_principal_components(dens_vecs)
    f = open(out_pca_fn, "w")
    IMP.em.write_pca_cmm(dens_pca, f)
    f.close()

if __name__ == "__main__":
    main()
