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-2024.
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.parsers;
39  
40  import static org.apache.commons.io.FilenameUtils.isExtension;
41  import static org.apache.commons.io.FilenameUtils.removeExtension;
42  
43  import ffx.potential.MolecularAssembly;
44  import java.io.File;
45  import java.io.IOException;
46  import java.nio.file.Path;
47  import java.nio.file.Paths;
48  import java.util.logging.Logger;
49  
50  /**
51   * DiffractionFile class.
52   *
53   * @author Timothy D. Fenn
54   * @since 1.0
55   */
56  public class DiffractionFile {
57  
58    private static final Logger logger = Logger.getLogger(DiffractionFile.class.getName());
59  
60    private final String fileName;
61    private final double weight;
62    private final boolean neutron;
63    private final DiffractionFileFilter diffractionFilter;
64  
65    /**
66     * read in a diffraction file, weight set to 1.0 and neutron value of false
67     *
68     * @param filename file name to read in
69     */
70    public DiffractionFile(String filename) {
71      this(filename, 1.0, false);
72    }
73  
74    /**
75     * read in a diffraction file, neutron value set to false
76     *
77     * @param filename file name to read in
78     * @param weight the weight of the data
79     */
80    public DiffractionFile(String filename, double weight) {
81      this(filename, weight, false);
82    }
83  
84    /**
85     * read in a diffraction file
86     *
87     * @param filename file name to read in
88     * @param weight the weight of the data
89     * @param neutron if true, this is a neutron data set
90     */
91    public DiffractionFile(String filename, double weight, boolean neutron) {
92      File tmp = new File(filename);
93      if (!tmp.exists()) {
94        logger.severe(" Data file: " + filename + " not found!");
95      }
96  
97      if (isExtension(filename, "mtz")) {
98        diffractionFilter = new MTZFilter();
99      } else if (isExtension(filename, new String[] {"cif", "ent", "sf"})) {
100       diffractionFilter = new CIFFilter();
101     } else if (isExtension(filename, new String[] {"cns", "hkl"})) {
102       diffractionFilter = new CNSFilter();
103     } else {
104       diffractionFilter = null;
105     }
106 
107     if (diffractionFilter == null) {
108       logger.severe(
109           "input data file format not recognized!\n Please use an appropriate file extension: [MTZ: mtz] [CIF: cif, ent, sf] [CNS: cns, hkl] to identify your data file type!");
110     }
111 
112     this.fileName = filename;
113     this.weight = weight;
114     this.neutron = neutron;
115   }
116 
117   /**
118    * read in a diffraction file based on the molecular assembly fileName, using a weight of 1.0 and
119    * neutron value of false
120    *
121    * @param assembly {@link ffx.potential.MolecularAssembly} from which a fileName will be
122    *     determined
123    */
124   public DiffractionFile(MolecularAssembly[] assembly) {
125     this(assembly[0], 1.0, false);
126   }
127 
128   /**
129    * Constructor for DiffractionFile.
130    *
131    * @param assembly an array of {@link ffx.potential.MolecularAssembly} objects.
132    * @param weight a double.
133    */
134   public DiffractionFile(MolecularAssembly[] assembly, double weight) {
135     this(assembly[0], weight, false);
136   }
137 
138   /**
139    * Constructor for DiffractionFile.
140    *
141    * @param assembly an array of {@link ffx.potential.MolecularAssembly} objects.
142    * @param weight a double.
143    * @param neutron a boolean.
144    */
145   public DiffractionFile(MolecularAssembly[] assembly, double weight, boolean neutron) {
146     this(assembly[0], weight, neutron);
147   }
148 
149   /**
150    * read in a diffraction file based on the molecular assembly fileName, using a weight of 1.0 and
151    * neutron value of false
152    *
153    * @param assembly {@link ffx.potential.MolecularAssembly} from which a fileName will be
154    *     determined
155    */
156   public DiffractionFile(MolecularAssembly assembly) {
157     this(assembly, 1.0, false);
158   }
159 
160   /**
161    * read in a diffraction file based on the molecular assembly fileName, using a neutron value of
162    * false
163    *
164    * @param assembly {@link ffx.potential.MolecularAssembly} from which a fileName will be
165    *     determined
166    * @param weight the weight of the data
167    */
168   public DiffractionFile(MolecularAssembly assembly, double weight) {
169     this(assembly, weight, false);
170   }
171 
172   /**
173    * read in a diffraction file based on the molecular assembly fileName, using a weight of 1.0 and
174    * neutron value of false
175    *
176    * @param assembly {@link ffx.potential.MolecularAssembly} from which a fileName will be
177    *     determined
178    * @param weight the weight of the data
179    * @param neutron if true, this is a neutron data set
180    */
181   public DiffractionFile(MolecularAssembly assembly, double weight, boolean neutron) {
182     String name = removeExtension(assembly.getFile().getPath());
183 
184     File tmp = new File(name + ".mtz");
185     if (tmp.exists()) {
186       // logger.info("\n Data file: " + tmp.getName());
187       diffractionFilter = new MTZFilter();
188     } else {
189       tmp = new File(name + ".cif");
190       if (tmp.exists()) {
191         // logger.info("\n Data file: " + tmp.getName());
192         diffractionFilter = new CIFFilter();
193       } else {
194         tmp = new File(name + ".ent");
195         if (tmp.exists()) {
196           // logger.info("\n Data file: " + tmp.getName());
197           diffractionFilter = new CIFFilter();
198         } else {
199           tmp = new File(name + ".sf");
200           if (tmp.exists()) {
201             // logger.info("\n Data file: " + tmp.getName());
202             diffractionFilter = new CIFFilter();
203           } else {
204             tmp = new File(name + ".cns");
205             if (tmp.exists()) {
206               // logger.info("\n Data file: " + tmp.getName());
207               diffractionFilter = new CNSFilter();
208             } else {
209               tmp = new File(name + ".hkl");
210               if (tmp.exists()) {
211                 // logger.info("\n Data file: " + tmp.getName());
212                 diffractionFilter = new CNSFilter();
213               } else {
214                 logger.severe("no input data found!");
215                 diffractionFilter = null;
216               }
217             }
218           }
219         }
220       }
221     }
222     String filenameHolder; // Compiler complains if I set this.fileName directly.
223     try {
224       Path filepath = Paths.get(tmp.getCanonicalPath());
225       Path pwdPath = Paths.get(new File("").getCanonicalPath());
226       filenameHolder = pwdPath.relativize(filepath).toString();
227     } catch (IOException ex) {
228       logger.warning(
229           " Relative path to provided data file could not be resolved: using data file name instead.");
230       filenameHolder = tmp.getName();
231     }
232     this.fileName = filenameHolder;
233     this.weight = weight;
234     this.neutron = neutron;
235   }
236 
237   /**
238    * getDiffractionfilter.
239    *
240    * @return the diffractionFilter
241    */
242   public DiffractionFileFilter getDiffractionfilter() {
243     return diffractionFilter;
244   }
245 
246   /**
247    * getFilename.
248    *
249    * @return the fileName
250    */
251   public String getFilename() {
252     return fileName;
253   }
254 
255   /**
256    * return the weight of this dataset
257    *
258    * @return weight wA
259    */
260   public double getWeight() {
261     return weight;
262   }
263 
264   /**
265    * is this a neutron dataset?
266    *
267    * @return true if neutron
268    */
269   public boolean isNeutron() {
270     return neutron;
271   }
272 }