📄 cifreader.java
字号:
String value = line.substring(14).trim(); a = parseIntoDouble(value); possiblySetCellParams(a,b,c,alpha,beta,gamma); } else if (command.equals("length_b")) { String value = line.substring(14).trim(); b = parseIntoDouble(value); possiblySetCellParams(a,b,c,alpha,beta,gamma); } else if (command.equals("length_c")) { String value = line.substring(14).trim(); c = parseIntoDouble(value); possiblySetCellParams(a,b,c,alpha,beta,gamma); } else if (command.equals("angle_alpha")) { String value = line.substring(17).trim(); alpha = parseIntoDouble(value); possiblySetCellParams(a,b,c,alpha,beta,gamma); } else if (command.equals("angle_beta")) { String value = line.substring(16).trim(); beta = parseIntoDouble(value); possiblySetCellParams(a,b,c,alpha,beta,gamma); } else if (command.equals("angle_gamma")) { String value = line.substring(17).trim(); gamma = parseIntoDouble(value); possiblySetCellParams(a,b,c,alpha,beta,gamma); } } private void possiblySetCellParams(double a,double b,double c,double alpha,double beta,double gamma) { if (a != 0.0 && b != 0.0 && c != 0.0 && alpha != 0.0 && beta != 0.0 && gamma != 0.0) { logger.info("Found and set crystal cell parameters"); Vector3d[] axes = CrystalGeometryTools.notionalToCartesian(a,b,c, alpha, beta, gamma); crystal.setA(axes[0]); crystal.setB(axes[1]); crystal.setC(axes[2]); } } private void processLoopBlock() throws IOException { String line = input.readLine().trim(); if (line.startsWith("_atom")) { logger.info("Found atom loop block"); processAtomLoopBlock(line); } else { logger.warn("Skipping loop block"); skipUntilEmptyOrCommentLine(line); } } private void skipUntilEmptyOrCommentLine(String line) throws IOException { // skip everything until empty line, or comment line while (line != null && line.length() > 0 && line.charAt(0) != '#') { line = input.readLine().trim(); } } private void processAtomLoopBlock(String firstLine) throws IOException { int atomLabel = -1; // -1 means not found in this block int atomSymbol = -1; int atomFractX = -1; int atomFractY = -1; int atomFractZ = -1; int atomRealX = -1; int atomRealY = -1; int atomRealZ = -1; String line = firstLine.trim(); int headerCount = 0; boolean hasParsableInformation = false; while (line != null && line.charAt(0) == '_') { headerCount++; if (line.equals("_atom_site_label") || line.equals("_atom_site_label_atom_id")) { atomLabel = headerCount; hasParsableInformation = true; logger.info("label found in col: " + atomLabel); } else if (line.startsWith("_atom_site_fract_x")) { atomFractX = headerCount; hasParsableInformation = true; logger.info("frac x found in col: " + atomFractX); } else if (line.startsWith("_atom_site_fract_y")) { atomFractY = headerCount; hasParsableInformation = true; logger.info("frac y found in col: " + atomFractY); } else if (line.startsWith("_atom_site_fract_z")) { atomFractZ = headerCount; hasParsableInformation = true; logger.info("frac z found in col: " + atomFractZ); } else if (line.equals("_atom_site.Cartn_x")) { atomRealX = headerCount; hasParsableInformation = true; logger.info("cart x found in col: " + atomRealX); } else if (line.equals("_atom_site.Cartn_y")) { atomRealY = headerCount; hasParsableInformation = true; logger.info("cart y found in col: " + atomRealY); } else if (line.equals("_atom_site.Cartn_z")) { atomRealZ = headerCount; hasParsableInformation = true; logger.info("cart z found in col: " + atomRealZ); } else if (line.equals("_atom_site.type_symbol")) { atomSymbol = headerCount; hasParsableInformation = true; logger.info("type_symbol found in col: " + atomSymbol); } else { logger.warn("Ignoring atom loop block field: " + line); } line = input.readLine().trim(); } if (hasParsableInformation == false ) { logger.info("No parsable info found"); skipUntilEmptyOrCommentLine(line); } else { // now that headers are parsed, read the data while(line != null && line.length() > 0 && line.charAt(0) != '#') { logger.debug("new row"); StringTokenizer tokenizer = new StringTokenizer(line); if (tokenizer.countTokens() < headerCount) { logger.warn("Column count mismatch; assuming continued on next line"); logger.debug("Found #expected, #found: " + headerCount + ", " + tokenizer.countTokens()); tokenizer = new StringTokenizer(line + input.readLine()); } int colIndex = 0; // process one row IAtom atom = crystal.getBuilder().newAtom("C"); Point3d frac = new Point3d(); Point3d real = new Point3d(); boolean hasFractional = false; boolean hasCartesian = false; while (tokenizer.hasMoreTokens()) { colIndex++; String field = tokenizer.nextToken(); logger.debug("Parsing col,token: " + colIndex + "=" + field); if (colIndex == atomLabel) { if (atomSymbol == -1) { // no atom symbol found, use label String element = extractFirstLetters(field); atom.setSymbol(element); } atom.setID(field); } else if (colIndex == atomFractX) { hasFractional = true; frac.x = parseIntoDouble(field); } else if (colIndex == atomFractY) { hasFractional = true; frac.y = parseIntoDouble(field); } else if (colIndex == atomFractZ) { hasFractional = true; frac.z = parseIntoDouble(field); } else if (colIndex == atomSymbol) { atom.setSymbol(field); } else if (colIndex == atomRealX) { hasCartesian = true; logger.debug("Adding x3: " + parseIntoDouble(field)); real.x = parseIntoDouble(field); } else if (colIndex == atomRealY) { hasCartesian = true; logger.debug("Adding y3: " + parseIntoDouble(field)); real.y = parseIntoDouble(field); } else if (colIndex == atomRealZ) { hasCartesian = true; logger.debug("Adding x3: " + parseIntoDouble(field)); real.z = parseIntoDouble(field); } } if (hasCartesian) { Vector3d a = crystal.getA(); Vector3d b = crystal.getB(); Vector3d c = crystal.getC(); frac = CrystalGeometryTools.cartesianToFractional(a, b, c, real); atom.setFractionalPoint3d(frac); } if (hasFractional) { atom.setFractionalPoint3d(frac); } logger.debug("Adding atom: " + atom); crystal.addAtom(atom); // look up next row line = input.readLine().trim(); } } } /** * Process double in the format: '.071(1)'. */ private double parseIntoDouble(String value) { double returnVal = 0.0; if (value.charAt(0) == '.') value = "0" + value; int bracketIndex = value.indexOf("("); if (bracketIndex != -1) { value = value.substring(0, bracketIndex); } try { returnVal = Double.parseDouble(value); } catch (Exception exception) { logger.error("Could not parse double string: " + value); } return returnVal; } private String extractFirstLetters(String value) { StringBuffer result = new StringBuffer(); for (int i=0; i<value.length(); i++) { if (Character.isDigit(value.charAt(i))) { break; } else { result.append(value.charAt(i)); } } return result.toString(); } public void close() throws IOException { input.close(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -