View Javadoc
1   //******************************************************************************
2   //
3   // Title:       Force Field X.
4   // Description: Force Field X - Software for Molecular Biophysics.
5   // Copyright:   Copyright (c) Michael J. Schnieders 2001-2025.
6   //
7   // This file is part of Force Field X.
8   //
9   // Force Field X is free software; you can redistribute it and/or modify it
10  // under the terms of the GNU General Public License version 3 as published by
11  // the Free Software Foundation.
12  //
13  // Force Field X is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16  // details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // Force Field X; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA 02111-1307 USA
21  //
22  // Linking this library statically or dynamically with other modules is making a
23  // combined work based on this library. Thus, the terms and conditions of the
24  // GNU General Public License cover the whole combination.
25  //
26  // As a special exception, the copyright holders of this library give you
27  // permission to link this library with independent modules to produce an
28  // executable, regardless of the license terms of these independent modules, and
29  // to copy and distribute the resulting executable under terms of your choice,
30  // provided that you also meet, for each linked independent module, the terms
31  // and conditions of the license of that module. An independent module is a
32  // module which is not derived from or based on this library. If you modify this
33  // library, you may extend this exception to your version of the library, but
34  // you are not obligated to do so. If you do not wish to do so, delete this
35  // exception statement from your version.
36  //
37  //******************************************************************************
38  package ffx.xray.commands;
39  
40  import ffx.algorithms.cli.AlgorithmsCommand;
41  import ffx.crystal.Crystal;
42  import ffx.crystal.ReflectionList;
43  import ffx.crystal.Resolution;
44  import ffx.numerics.Potential;
45  import ffx.potential.MolecularAssembly;
46  import ffx.utilities.FFXBinding;
47  import ffx.xray.DiffractionRefinementData;
48  import ffx.xray.parsers.CIFFilter;
49  import ffx.xray.parsers.MTZWriter;
50  import ffx.xray.parsers.MTZWriter.MTZType;
51  import picocli.CommandLine.Command;
52  import picocli.CommandLine.Parameters;
53  
54  import java.io.File;
55  import java.util.List;
56  
57  import static org.apache.commons.io.FilenameUtils.removeExtension;
58  
59  /**
60   * The CIF2MTZ script saves a CIF file to MTZ format.
61   * <br>
62   * Usage:
63   * <br>
64   * ffxc xray.CIF2MTZ &lt;filename&gt;
65   */
66  @Command(description = " Convert a CIF file to MTZ format.", name = "xray.CIFtoMTZ")
67  public class CIFtoMTZ extends AlgorithmsCommand {
68  
69    /**
70     * A CIF filename.
71     */
72    @Parameters(arity = "2", paramLabel = "file", description = "A PDB file and a CIF diffraction file.")
73    private List<String> filenames = null;
74  
75    private MolecularAssembly[] molecularAssemblies;
76    private DiffractionRefinementData refinementData;
77  
78    /**
79     * CIF2MTZ constructor.
80     */
81    public CIFtoMTZ() {
82      super();
83    }
84  
85    /**
86     * CIF2MTZ constructor that sets the command line arguments.
87     *
88     * @param args Command line arguments.
89     */
90    public CIFtoMTZ(String[] args) {
91      super(args);
92    }
93  
94    /**
95     * CIF2MTZ constructor.
96     *
97     * @param binding The Binding to use.
98     */
99    public CIFtoMTZ(FFXBinding binding) {
100     super(binding);
101   }
102 
103   /**
104    * Execute the script.
105    */
106   @Override
107   public CIFtoMTZ run() {
108 
109     if (!init()) {
110       return this;
111     }
112 
113     String pdb = filenames.get(0);
114     String cif = filenames.get(1);
115 
116     logger.info("\n Running CIF2MTZ on " + cif);
117     molecularAssemblies = algorithmFunctions.openAll(pdb);
118 
119     CIFFilter cifFilter = new CIFFilter();
120     ReflectionList reflectionlist =
121         cifFilter.getReflectionList(new File(cif), molecularAssemblies[0].getProperties());
122 
123     if (reflectionlist == null) {
124       logger.info(" Using crystal information from the PDB file to generate MTZ file.");
125 
126       Crystal crystal = molecularAssemblies[0].getCrystal().getUnitCell();
127       double res = cifFilter.getResolution(new File(cif), crystal);
128       if (res < 0.0) {
129         logger.info(" The resolution could not be determined from the PDB and CIF files.");
130         return this;
131       }
132 
133       Resolution resolution = new Resolution(res);
134       reflectionlist = new ReflectionList(crystal, resolution,
135           molecularAssemblies[0].getProperties());
136     }
137 
138     refinementData = new DiffractionRefinementData(molecularAssemblies[0].getProperties(), reflectionlist);
139     cifFilter.readFile(new File(cif), reflectionlist, refinementData, molecularAssemblies[0].getProperties());
140 
141     MTZWriter mtzwriter = new MTZWriter(reflectionlist, refinementData,
142         removeExtension(cif) + ".mtz", MTZType.DATAONLY);
143     mtzwriter.write();
144 
145     return this;
146   }
147 
148   @Override
149   public List<Potential> getPotentials() {
150     return getPotentialsFromAssemblies(molecularAssemblies);
151   }
152 }