List of usage examples for java.nio ByteOrder equals
public boolean equals(Object obj)
From source file:ffx.xray.parsers.MTZFilter.java
/** * Read the structure factors.//w w w .j a va 2 s . c om * * @param mtzFile * @param reflectionList * @param fcData * @param properties * @return */ public boolean readFcs(File mtzFile, ReflectionList reflectionList, DiffractionRefinementData fcData, CompositeConfiguration properties) { int nRead, nIgnore, nRes, nFriedel, nCut; ByteOrder byteOrder = ByteOrder.nativeOrder(); FileInputStream fileInputStream; DataInputStream dataInputStream; StringBuilder sb = new StringBuilder(); try { fileInputStream = new FileInputStream(mtzFile); dataInputStream = new DataInputStream(fileInputStream); byte headerOffset[] = new byte[4]; byte bytes[] = new byte[80]; int offset = 0; // Eat "MTZ" title. dataInputStream.read(bytes, offset, 4); String mtzString = null; // Header offset. dataInputStream.read(headerOffset, offset, 4); // Machine stamp. dataInputStream.read(bytes, offset, 4); ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); int stamp = byteBuffer.order(ByteOrder.BIG_ENDIAN).getInt(); String stampString = Integer.toHexString(stamp); switch (stampString.charAt(0)) { case '1': case '3': if (byteOrder.equals(ByteOrder.LITTLE_ENDIAN)) { byteOrder = ByteOrder.BIG_ENDIAN; } break; case '4': if (byteOrder.equals(ByteOrder.BIG_ENDIAN)) { byteOrder = ByteOrder.LITTLE_ENDIAN; } break; } byteBuffer = ByteBuffer.wrap(headerOffset); int headerOffsetI = byteBuffer.order(byteOrder).getInt(); // Skip to header and parse. dataInputStream.skipBytes((headerOffsetI - 4) * 4); for (Boolean parsing = true; parsing; dataInputStream.read(bytes, offset, 80)) { mtzString = new String(bytes); parsing = parseHeader(mtzString); } // Column identifiers. fc = phiC = fs = phiS = -1; boolean print = true; parseFcColumns(print); if (h < 0 || k < 0 || l < 0) { String message = " Fatal error in MTZ file - no H K L indexes?\n"; logger.log(Level.SEVERE, message); return false; } // Reopen to start at beginning. fileInputStream = new FileInputStream(mtzFile); dataInputStream = new DataInputStream(fileInputStream); // Skip initial header. dataInputStream.skipBytes(80); float data[] = new float[nColumns]; HKL mate = new HKL(); // Read in data. ComplexNumber complexNumber = new ComplexNumber(); nRead = nIgnore = nRes = nFriedel = nCut = 0; for (int i = 0; i < nReflections; i++) { for (int j = 0; j < nColumns; j++) { dataInputStream.read(bytes, offset, 4); byteBuffer = ByteBuffer.wrap(bytes); data[j] = byteBuffer.order(byteOrder).getFloat(); } int ih = (int) data[h]; int ik = (int) data[k]; int il = (int) data[l]; boolean friedel = reflectionList.findSymHKL(ih, ik, il, mate, false); HKL hkl = reflectionList.getHKL(mate); if (hkl != null) { if (fc > 0 && phiC > 0) { complexNumber.re(data[fc] * cos(toRadians(data[phiC]))); complexNumber.im(data[fc] * sin(toRadians(data[phiC]))); fcData.setFc(hkl.index(), complexNumber); } if (fs > 0 && phiS > 0) { complexNumber.re(data[fs] * cos(toRadians(data[phiS]))); complexNumber.im(data[fs] * sin(toRadians(data[phiS]))); fcData.setFs(hkl.index(), complexNumber); } nRead++; } else { HKL tmp = new HKL(ih, ik, il); if (!reflectionList.resolution .inInverseResSqRange(Crystal.invressq(reflectionList.crystal, tmp))) { nRes++; } else { nIgnore++; } } } if (logger.isLoggable(Level.INFO)) { sb.append(format(" MTZ file type (machine stamp): %s\n", stampString)); sb.append(format(" Fc HKL read in: %d\n", nRead)); sb.append(format(" Fc HKL read as friedel mates: %d\n", nFriedel)); sb.append(format(" Fc HKL NOT read in (too high resolution): %d\n", nRes)); sb.append(format(" Fc HKL NOT read in (not in internal list?): %d\n", nIgnore)); sb.append(format(" Fc HKL NOT read in (F/sigF cutoff): %d\n", nCut)); sb.append( format(" HKL in internal list: %d\n", reflectionList.hkllist.size())); logger.info(sb.toString()); } } catch (EOFException e) { String message = " MTZ end of file reached."; logger.log(Level.WARNING, message, e); return false; } catch (IOException e) { String message = " MTZ IO Exception."; logger.log(Level.WARNING, message, e); return false; } return true; }
From source file:ffx.xray.MTZFilter.java
/** * {@inheritDoc}/* w w w . j a v a 2 s. co m*/ */ @Override public boolean readFile(File mtzFile, ReflectionList reflectionlist, DiffractionRefinementData refinementdata, CompositeConfiguration properties) { int nread, nignore, nres, nfriedel, ncut; ByteOrder b = ByteOrder.nativeOrder(); FileInputStream fis; DataInputStream dis; boolean transpose = false; StringBuilder sb = new StringBuilder(); //sb.append(String.format("\n Opening %s\n", mtzFile.getName())); try { fis = new FileInputStream(mtzFile); dis = new DataInputStream(fis); byte headeroffset[] = new byte[4]; byte bytes[] = new byte[80]; int offset = 0; // eat "MTZ" title dis.read(bytes, offset, 4); String mtzstr = new String(bytes); // header offset dis.read(headeroffset, offset, 4); // machine stamp dis.read(bytes, offset, 4); ByteBuffer bb = ByteBuffer.wrap(bytes); int stamp = bb.order(ByteOrder.BIG_ENDIAN).getInt(); String stampstr = Integer.toHexString(stamp); switch (stampstr.charAt(0)) { case '1': case '3': if (b.equals(ByteOrder.LITTLE_ENDIAN)) { b = ByteOrder.BIG_ENDIAN; } break; case '4': if (b.equals(ByteOrder.BIG_ENDIAN)) { b = ByteOrder.LITTLE_ENDIAN; } break; } bb = ByteBuffer.wrap(headeroffset); int headeroffseti = bb.order(b).getInt(); // skip to header and parse dis.skipBytes((headeroffseti - 4) * 4); for (Boolean parsing = true; parsing; dis.read(bytes, offset, 80)) { mtzstr = new String(bytes); parsing = parseHeader(mtzstr); } // column identifiers foString = sigfoString = rfreeString = null; if (properties != null) { foString = properties.getString("fostring", null); sigfoString = properties.getString("sigfostring", null); rfreeString = properties.getString("rfreestring", null); } h = k = l = fo = sigfo = rfree = -1; fplus = sigfplus = fminus = sigfminus = rfreeplus = rfreeminus = -1; boolean print = true; parseColumns(print); if (h < 0 || k < 0 || l < 0) { String message = "Fatal error in MTZ file - no H K L indexes?\n"; logger.log(Level.SEVERE, message); return false; } // reopen to start at beginning fis = new FileInputStream(mtzFile); dis = new DataInputStream(fis); // skip initial header dis.skipBytes(80); // check if HKLs need to be transposed or not float data[] = new float[nColumns]; HKL mate = new HKL(); int nposignore = 0; int ntransignore = 0; int nzero = 0; int none = 0; for (int i = 0; i < nReflections; i++) { for (int j = 0; j < nColumns; j++) { dis.read(bytes, offset, 4); bb = ByteBuffer.wrap(bytes); data[j] = bb.order(b).getFloat(); } int ih = (int) data[h]; int ik = (int) data[k]; int il = (int) data[l]; boolean friedel = reflectionlist.findSymHKL(ih, ik, il, mate, false); HKL hklpos = reflectionlist.getHKL(mate); if (hklpos == null) { nposignore++; } friedel = reflectionlist.findSymHKL(ih, ik, il, mate, true); HKL hkltrans = reflectionlist.getHKL(mate); if (hkltrans == null) { ntransignore++; } if (rfree > 0) { if (((int) data[rfree]) == 0) { nzero++; } else if (((int) data[rfree]) == 1) { none++; } } if (rfreeplus > 0) { if (((int) data[rfreeplus]) == 0) { nzero++; } else if (((int) data[rfreeplus]) == 1) { none++; } } if (rfreeminus > 0) { if (((int) data[rfreeminus]) == 0) { nzero++; } else if (((int) data[rfreeminus]) == 1) { none++; } } } if (nposignore > ntransignore) { transpose = true; } if (none > (nzero * 2) && refinementdata.rfreeflag < 0) { refinementdata.setFreeRFlag(0); sb.append(String.format(" Setting R free flag to %d based on MTZ file data.\n", refinementdata.rfreeflag)); } else if (nzero > (none * 2) && refinementdata.rfreeflag < 0) { refinementdata.setFreeRFlag(1); sb.append(String.format(" Setting R free flag to %d based on MTZ file data.\n", refinementdata.rfreeflag)); } else if (refinementdata.rfreeflag < 0) { refinementdata.setFreeRFlag(0); sb.append(String.format(" Setting R free flag to MTZ default: %d\n", refinementdata.rfreeflag)); } // reopen to start at beginning fis = new FileInputStream(mtzFile); dis = new DataInputStream(fis); // skip initial header dis.skipBytes(80); // read in data double anofsigf[][] = new double[refinementdata.n][4]; for (int i = 0; i < refinementdata.n; i++) { anofsigf[i][0] = anofsigf[i][1] = anofsigf[i][2] = anofsigf[i][3] = Double.NaN; } nread = nignore = nres = nfriedel = ncut = 0; for (int i = 0; i < nReflections; i++) { for (int j = 0; j < nColumns; j++) { dis.read(bytes, offset, 4); bb = ByteBuffer.wrap(bytes); data[j] = bb.order(b).getFloat(); } int ih = (int) data[h]; int ik = (int) data[k]; int il = (int) data[l]; boolean friedel = reflectionlist.findSymHKL(ih, ik, il, mate, transpose); HKL hkl = reflectionlist.getHKL(mate); if (hkl != null) { if (fo > 0 && sigfo > 0) { if (refinementdata.fsigfcutoff > 0.0) { if ((data[fo] / data[sigfo]) < refinementdata.fsigfcutoff) { ncut++; continue; } } if (friedel) { anofsigf[hkl.index()][2] = data[fo]; anofsigf[hkl.index()][3] = data[sigfo]; nfriedel++; } else { anofsigf[hkl.index()][0] = data[fo]; anofsigf[hkl.index()][1] = data[sigfo]; } } else { if (fplus > 0 && sigfplus > 0) { if (refinementdata.fsigfcutoff > 0.0) { if ((data[fplus] / data[sigfplus]) < refinementdata.fsigfcutoff) { ncut++; continue; } } anofsigf[hkl.index()][0] = data[fplus]; anofsigf[hkl.index()][1] = data[sigfplus]; } if (fminus > 0 && sigfminus > 0) { if (refinementdata.fsigfcutoff > 0.0) { if ((data[fminus] / data[sigfminus]) < refinementdata.fsigfcutoff) { ncut++; continue; } } anofsigf[hkl.index()][2] = data[fminus]; anofsigf[hkl.index()][3] = data[sigfminus]; } } if (rfree > 0) { refinementdata.setFreeR(hkl.index(), (int) data[rfree]); } else { if (rfreeplus > 0 && rfreeminus > 0) { // not sure what the correct thing to do here is? refinementdata.setFreeR(hkl.index(), (int) data[rfreeplus]); } else if (rfreeplus > 0) { refinementdata.setFreeR(hkl.index(), (int) data[rfreeplus]); } else if (rfreeminus > 0) { refinementdata.setFreeR(hkl.index(), (int) data[rfreeminus]); } } nread++; } else { HKL tmp = new HKL(ih, ik, il); if (!reflectionlist.resolution .inInverseResSqRange(Crystal.invressq(reflectionlist.crystal, tmp))) { nres++; } else { nignore++; } } } // set up fsigf from F+ and F- refinementdata.generate_fsigf_from_anofsigf(anofsigf); sb.append(String.format(" MTZ file type (machine stamp): %s\n", stampstr)); sb.append(String.format(" HKL data is %s\n", transpose ? "transposed" : "not transposed")); sb.append(String.format(" HKL read in: %d\n", nread)); sb.append(String.format(" HKL read as friedel mates: %d\n", nfriedel)); sb.append(String.format(" HKL NOT read in (too high resolution): %d\n", nres)); sb.append(String.format(" HKL NOT read in (not in internal list?): %d\n", nignore)); sb.append(String.format(" HKL NOT read in (F/sigF cutoff): %d\n", ncut)); sb.append( String.format(" HKL in internal list: %d\n", reflectionlist.hkllist.size())); if (logger.isLoggable(Level.INFO)) { logger.info(sb.toString()); } if (rfree < 0 && rfreeplus < 0 && rfreeminus < 0) { refinementdata.generateRFree(); } } catch (EOFException eof) { System.out.println("EOF reached "); return false; } catch (IOException ioe) { System.out.println("IO Exception: " + ioe.getMessage()); return false; } return true; }
From source file:ffx.xray.parsers.MTZFilter.java
/** * {@inheritDoc}/*from w ww . j a va 2 s . c om*/ */ @Override public boolean readFile(File mtzFile, ReflectionList reflectionList, DiffractionRefinementData refinementData, CompositeConfiguration properties) { int nRead, nIgnore, nRes, nFriedel, nCut; ByteOrder byteOrder = ByteOrder.nativeOrder(); FileInputStream fileInputStream; DataInputStream dataInputStream; boolean transpose = false; StringBuilder sb = new StringBuilder(); try { fileInputStream = new FileInputStream(mtzFile); dataInputStream = new DataInputStream(fileInputStream); byte headerOffset[] = new byte[4]; byte bytes[] = new byte[80]; int offset = 0; // Eat "MTZ" title. dataInputStream.read(bytes, offset, 4); String mtzstr = null; // Header offset. dataInputStream.read(headerOffset, offset, 4); // Machine stamp. dataInputStream.read(bytes, offset, 4); ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); int stamp = byteBuffer.order(ByteOrder.BIG_ENDIAN).getInt(); String stampString = Integer.toHexString(stamp); switch (stampString.charAt(0)) { case '1': case '3': if (byteOrder.equals(ByteOrder.LITTLE_ENDIAN)) { byteOrder = ByteOrder.BIG_ENDIAN; } break; case '4': if (byteOrder.equals(ByteOrder.BIG_ENDIAN)) { byteOrder = ByteOrder.LITTLE_ENDIAN; } break; } byteBuffer = ByteBuffer.wrap(headerOffset); int headerOffsetI = byteBuffer.order(byteOrder).getInt(); // skip to header and parse dataInputStream.skipBytes((headerOffsetI - 4) * 4); for (Boolean parsing = true; parsing; dataInputStream.read(bytes, offset, 80)) { mtzstr = new String(bytes); parsing = parseHeader(mtzstr); } // column identifiers foString = sigFoString = rFreeString = null; if (properties != null) { foString = properties.getString("fostring", null); sigFoString = properties.getString("sigfostring", null); rFreeString = properties.getString("rfreestring", null); } h = k = l = fo = sigFo = rFree = -1; fPlus = sigFPlus = fMinus = sigFMinus = rFreePlus = rFreeMinus = -1; boolean print = true; parseColumns(print); if (h < 0 || k < 0 || l < 0) { String message = "Fatal error in MTZ file - no H K L indexes?\n"; logger.log(Level.SEVERE, message); return false; } // Reopen to start at beginning. fileInputStream = new FileInputStream(mtzFile); dataInputStream = new DataInputStream(fileInputStream); // Skip initial header. dataInputStream.skipBytes(80); // Check if HKLs need to be transposed or not. float data[] = new float[nColumns]; HKL mate = new HKL(); int nPosIgnore = 0; int nTransIgnore = 0; int nZero = 0; int none = 0; for (int i = 0; i < nReflections; i++) { for (int j = 0; j < nColumns; j++) { dataInputStream.read(bytes, offset, 4); byteBuffer = ByteBuffer.wrap(bytes); data[j] = byteBuffer.order(byteOrder).getFloat(); } int ih = (int) data[h]; int ik = (int) data[k]; int il = (int) data[l]; boolean friedel = reflectionList.findSymHKL(ih, ik, il, mate, false); HKL hklpos = reflectionList.getHKL(mate); if (hklpos == null) { nPosIgnore++; } friedel = reflectionList.findSymHKL(ih, ik, il, mate, true); HKL hkltrans = reflectionList.getHKL(mate); if (hkltrans == null) { nTransIgnore++; } if (rFree > 0) { if (((int) data[rFree]) == 0) { nZero++; } else if (((int) data[rFree]) == 1) { none++; } } if (rFreePlus > 0) { if (((int) data[rFreePlus]) == 0) { nZero++; } else if (((int) data[rFreePlus]) == 1) { none++; } } if (rFreeMinus > 0) { if (((int) data[rFreeMinus]) == 0) { nZero++; } else if (((int) data[rFreeMinus]) == 1) { none++; } } } if (nPosIgnore > nTransIgnore) { transpose = true; } if (none > (nZero * 2) && refinementData.rfreeflag < 0) { refinementData.setFreeRFlag(0); sb.append(format(" Setting R free flag to %d based on MTZ file data.\n", refinementData.rfreeflag)); } else if (nZero > (none * 2) && refinementData.rfreeflag < 0) { refinementData.setFreeRFlag(1); sb.append(format(" Setting R free flag to %d based on MTZ file data.\n", refinementData.rfreeflag)); } else if (refinementData.rfreeflag < 0) { refinementData.setFreeRFlag(0); sb.append(format(" Setting R free flag to MTZ default: %d\n", refinementData.rfreeflag)); } // reopen to start at beginning fileInputStream = new FileInputStream(mtzFile); dataInputStream = new DataInputStream(fileInputStream); // skip initial header dataInputStream.skipBytes(80); // read in data double anofSigF[][] = new double[refinementData.n][4]; for (int i = 0; i < refinementData.n; i++) { anofSigF[i][0] = anofSigF[i][1] = anofSigF[i][2] = anofSigF[i][3] = Double.NaN; } nRead = nIgnore = nRes = nFriedel = nCut = 0; for (int i = 0; i < nReflections; i++) { for (int j = 0; j < nColumns; j++) { dataInputStream.read(bytes, offset, 4); byteBuffer = ByteBuffer.wrap(bytes); data[j] = byteBuffer.order(byteOrder).getFloat(); } int ih = (int) data[h]; int ik = (int) data[k]; int il = (int) data[l]; boolean friedel = reflectionList.findSymHKL(ih, ik, il, mate, transpose); HKL hkl = reflectionList.getHKL(mate); if (hkl != null) { if (fo > 0 && sigFo > 0) { if (refinementData.fsigfcutoff > 0.0) { if ((data[fo] / data[sigFo]) < refinementData.fsigfcutoff) { nCut++; continue; } } if (friedel) { anofSigF[hkl.index()][2] = data[fo]; anofSigF[hkl.index()][3] = data[sigFo]; nFriedel++; } else { anofSigF[hkl.index()][0] = data[fo]; anofSigF[hkl.index()][1] = data[sigFo]; } } else { if (fPlus > 0 && sigFPlus > 0) { if (refinementData.fsigfcutoff > 0.0) { if ((data[fPlus] / data[sigFPlus]) < refinementData.fsigfcutoff) { nCut++; continue; } } anofSigF[hkl.index()][0] = data[fPlus]; anofSigF[hkl.index()][1] = data[sigFPlus]; } if (fMinus > 0 && sigFMinus > 0) { if (refinementData.fsigfcutoff > 0.0) { if ((data[fMinus] / data[sigFMinus]) < refinementData.fsigfcutoff) { nCut++; continue; } } anofSigF[hkl.index()][2] = data[fMinus]; anofSigF[hkl.index()][3] = data[sigFMinus]; } } if (rFree > 0) { refinementData.setFreeR(hkl.index(), (int) data[rFree]); } else { if (rFreePlus > 0 && rFreeMinus > 0) { // not sure what the correct thing to do here is? refinementData.setFreeR(hkl.index(), (int) data[rFreePlus]); } else if (rFreePlus > 0) { refinementData.setFreeR(hkl.index(), (int) data[rFreePlus]); } else if (rFreeMinus > 0) { refinementData.setFreeR(hkl.index(), (int) data[rFreeMinus]); } } nRead++; } else { HKL tmp = new HKL(ih, ik, il); if (!reflectionList.resolution .inInverseResSqRange(Crystal.invressq(reflectionList.crystal, tmp))) { nRes++; } else { nIgnore++; } } } // Set up fsigf from F+ and F-. refinementData.generate_fsigf_from_anofsigf(anofSigF); // Log results. if (logger.isLoggable(Level.INFO)) { sb.append(format(" MTZ file type (machine stamp): %s\n", stampString)); sb.append(format(" HKL data is %s\n", transpose ? "transposed" : "not transposed")); sb.append(format(" HKL read in: %d\n", nRead)); sb.append(format(" HKL read as friedel mates: %d\n", nFriedel)); sb.append(format(" HKL NOT read in (too high resolution): %d\n", nRes)); sb.append(format(" HKL NOT read in (not in internal list?): %d\n", nIgnore)); sb.append(format(" HKL NOT read in (F/sigF cutoff): %d\n", nCut)); sb.append(format(" HKL in internal list: %d\n", reflectionList.hkllist.size())); logger.info(sb.toString()); } if (rFree < 0 && rFreePlus < 0 && rFreeMinus < 0) { refinementData.generateRFree(); } } catch (EOFException e) { String message = " MTZ end of file reached."; logger.log(Level.WARNING, message, e); return false; } catch (IOException e) { String message = " MTZ IO Exception."; logger.log(Level.WARNING, message, e); return false; } return true; }