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