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.realspace.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.Level; 49 import java.util.logging.Logger; 50 51 /** 52 * RealSpaceFile class. 53 * 54 * @author Timothy D. Fenn 55 * @since 1.0 56 */ 57 public class RealSpaceFile { 58 59 private static final Logger logger = Logger.getLogger(RealSpaceFile.class.getName()); 60 61 private final String filename; 62 private final double weight; 63 private final RealSpaceFileFilter realSpaceFileFilter; 64 65 /** 66 * Read in a Real Space density file and set weight set to 1.0. 67 * 68 * @param filename file name to read in 69 */ 70 public RealSpaceFile(String filename) { 71 this(filename, 1.0); 72 } 73 74 /** 75 * Read in a Real Space density file. 76 * 77 * @param fileName file name to read in 78 * @param weight the weight of the data 79 */ 80 public RealSpaceFile(String fileName, double weight) { 81 File tmp = new File(fileName); 82 if (!tmp.exists()) { 83 logger.severe(" Data file: " + fileName + " not found!"); 84 } 85 86 if (isExtension(fileName, "map")) { 87 realSpaceFileFilter = new CCP4MapFilter(); 88 } else { 89 realSpaceFileFilter = null; 90 } 91 92 this.filename = fileName; 93 this.weight = weight; 94 } 95 96 /** 97 * Read in a Real Space density file based on the molecular assembly filename, using a weight of 98 * 1.0 and neutron value of false. 99 * 100 * @param assembly {@link ffx.potential.MolecularAssembly} from which a filename will be 101 * determined. 102 */ 103 public RealSpaceFile(MolecularAssembly[] assembly) { 104 this(assembly[0], 1.0); 105 } 106 107 /** 108 * Read in a Real Space density file based on the molecular assembly filename, using a weight of 109 * 1.0. 110 * 111 * @param assembly {@link ffx.potential.MolecularAssembly} from which a filename will be 112 * determined. 113 */ 114 public RealSpaceFile(MolecularAssembly assembly) { 115 this(assembly, 1.0); 116 } 117 118 /** 119 * Read in a Real Space density file based on the molecular assembly filename, using a weight of 120 * 1.0. 121 * 122 * @param assembly {@link ffx.potential.MolecularAssembly} from which a filename will be 123 * determined 124 * @param weight the weight of the data 125 */ 126 public RealSpaceFile(MolecularAssembly assembly, double weight) { 127 String name = removeExtension(assembly.getFile().getPath()); 128 129 File tmp = new File(name + ".map"); 130 if (tmp.exists()) { 131 logger.info(" Data file: " + tmp.getName()); 132 realSpaceFileFilter = new CCP4MapFilter(); 133 } else { 134 logger.severe(" No input data was found."); 135 realSpaceFileFilter = null; 136 } 137 138 String filenameHolder; 139 try { 140 Path filePath = Paths.get(tmp.getCanonicalPath()); 141 Path pwdPath = Paths.get(new File("").getCanonicalPath()); 142 filenameHolder = pwdPath.relativize(filePath).toString(); 143 } catch (IOException e) { 144 String message = 145 " Relative path to provided data file could not be resolved: using map file name instead."; 146 logger.log(Level.WARNING, message, e); 147 filenameHolder = tmp.getName(); 148 } 149 this.filename = filenameHolder; 150 this.weight = weight; 151 } 152 153 /** 154 * Getter for the field <code>filename</code>. 155 * 156 * @return the filename 157 */ 158 public String getFilename() { 159 return filename; 160 } 161 162 /** 163 * Getter for the field <code>realSpaceFileFilter</code>. 164 * 165 * @return the realSpaceFileFilter 166 */ 167 public RealSpaceFileFilter getRealSpaceFileFilter() { 168 return realSpaceFileFilter; 169 } 170 171 /** 172 * Return the weight of this dataset. 173 * 174 * @return weight The weight (wA). 175 */ 176 public double getWeight() { 177 return weight; 178 } 179 }