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.ArrayList;
56  import java.util.Arrays;
57  import java.util.List;
58  import java.util.stream.Collectors;
59  
60  import static org.apache.commons.io.FilenameUtils.removeExtension;
61  
62  /**
63   * The CIF2MTZ script saves a CIF file to MTZ format.
64   * <br>
65   * Usage:
66   * <br>
67   * ffxc xray.CIF2MTZ &lt;filename&gt;
68   */
69  @Command(description = " Convert a CIF file to MTZ format.", name = "xray.CIFtoMTZ")
70  public class CIFtoMTZ extends AlgorithmsCommand {
71  
72    /**
73     * A CIF filename.
74     */
75    @Parameters(arity = "2", paramLabel = "file", description = "A PDB file and a CIF diffraction file.")
76    private List<String> filenames = null;
77  
78    private MolecularAssembly[] molecularAssemblies;
79    private DiffractionRefinementData refinementData;
80  
81    /**
82     * CIF2MTZ constructor.
83     */
84    public CIFtoMTZ() {
85      super();
86    }
87  
88    /**
89     * CIF2MTZ constructor that sets the command line arguments.
90     * @param args Command line arguments.
91     */
92    public CIFtoMTZ(String[] args) {
93      super(args);
94    }
95  
96    /**
97     * CIF2MTZ constructor.
98     * @param binding The Binding to use.
99     */
100   public CIFtoMTZ(FFXBinding binding) {
101     super(binding);
102   }
103 
104   /**
105    * Execute the script.
106    */
107   @Override
108   public CIFtoMTZ run() {
109 
110     if (!init()) {
111       return this;
112     }
113 
114     String pdb = filenames.get(0);
115     String cif = filenames.get(1);
116 
117     logger.info("\n Running CIF2MTZ on " + cif);
118 
119     // Use PotentialsFunctions methods instead of Groovy method closures to do work.
120     molecularAssemblies = algorithmFunctions.openAll(pdb);
121 
122     CIFFilter cifFilter = new CIFFilter();
123     ReflectionList reflectionlist =
124         cifFilter.getReflectionList(new File(cif), molecularAssemblies[0].getProperties());
125 
126     if (reflectionlist == null) {
127       System.out.println(" Using crystal information from the PDB file to generate MTZ file.");
128 
129       Crystal crystal = molecularAssemblies[0].getCrystal().getUnitCell();
130       double res = cifFilter.getResolution(new File(cif), crystal);
131       if (res < 0.0) {
132         System.out.println(" Resolution could not be determined from the PDB and CIF files.");
133         return this;
134       }
135 
136       Resolution resolution = new Resolution(res);
137       reflectionlist = new ReflectionList(crystal, resolution,
138           molecularAssemblies[0].getProperties());
139     }
140 
141     refinementData = new DiffractionRefinementData(molecularAssemblies[0].getProperties(),
142         reflectionlist);
143     cifFilter.readFile(new File(cif), reflectionlist, refinementData,
144         molecularAssemblies[0].getProperties());
145 
146     MTZWriter mtzwriter = new MTZWriter(reflectionlist, refinementData,
147         removeExtension(cif) + ".mtz", MTZType.DATAONLY);
148     mtzwriter.write();
149 
150     return this;
151   }
152 
153   @Override
154   public List<Potential> getPotentials() {
155     if (molecularAssemblies == null) {
156       return new ArrayList<>();
157     } else {
158       return Arrays.stream(molecularAssemblies)
159           .filter(a -> a != null)
160           .map(a -> a.getPotentialEnergy())
161           .filter(e -> e != null)
162           .collect(Collectors.toList());
163     }
164   }
165 }