use of ffx.crystal.HKL in project ffx by mjschnie.
the class SplineEnergy method target.
/**
* <p>
* target</p>
*
* @param x an array of double.
* @param g an array of double.
* @param gradient a boolean.
* @param print a boolean.
* @return a double.
*/
public double target(double[] x, double[] g, boolean gradient, boolean print) {
double r = 0.0;
double rf = 0.0;
double rfree = 0.0;
double rfreef = 0.0;
double sum = 0.0;
double sumfo = 0.0;
// Zero out the gradient.
if (gradient) {
fill(g, 0.0);
}
for (HKL ih : reflectionlist.hkllist) {
int i = ih.index();
if (Double.isNaN(fc[i][0]) || Double.isNaN(fo[i][0]) || fo[i][1] <= 0.0) {
continue;
}
if (type == Type.FOTOESQ && fo[i][0] <= 0.0) {
continue;
}
double eps = ih.epsilon();
double s = Crystal.invressq(crystal, ih);
// spline setup
double fh = spline.f(s, x);
refinementdata.getFcTotIP(i, fct);
double d2 = 0.0;
double dr = 0.0;
double w = 0.0;
switch(type) {
case Type.FOFC:
w = 1.0;
double f1 = refinementdata.getF(i);
double f2 = fct.abs();
double d = f1 - fh * f2;
d2 = d * d;
dr = -2.0 * f2 * d;
sumfo += f1 * f1;
break;
case Type.F1F2:
w = 2.0 / ih.epsilonc();
double ieps = 1.0 / eps;
f1 = pow(fct.abs(), 2.0) * ieps;
f2 = pow(refinementdata.getF(i), 2) * ieps;
d = fh * f1 - f2;
d2 = d * d / f1;
dr = 2.0 * d;
sumfo = 1.0;
break;
case Type.FCTOESQ:
w = 2.0 / ih.epsilonc();
f1 = pow(fct.abs() / sqrt(eps), 2);
d = f1 * fh - 1.0;
d2 = d * d / f1;
dr = 2.0 * d;
sumfo = 1.0;
break;
case Type.FOTOESQ:
w = 2.0 / ih.epsilonc();
f1 = pow(refinementdata.getF(i) / sqrt(eps), 2);
d = f1 * fh - 1.0;
d2 = d * d / f1;
dr = 2.0 * d;
sumfo = 1.0;
break;
}
sum += w * d2;
double afo = abs(fo[i][0]);
double afh = abs(fh * fct.abs());
if (refinementdata.isFreeR(i)) {
rfree += abs(afo - afh);
rfreef += afo;
} else {
r += abs(afo - afh);
rf += afo;
}
if (gradient) {
int i0 = spline.i0();
int i1 = spline.i1();
int i2 = spline.i2();
double g0 = spline.dfi0();
double g1 = spline.dfi1();
double g2 = spline.dfi2();
g[i0] += w * dr * g0;
g[i1] += w * dr * g1;
g[i2] += w * dr * g2;
}
}
/**
* Tim - should this only be done for Type.FOFC??
*/
if (gradient) {
double isumfo = 1.0 / sumfo;
for (int i = 0; i < g.length; i++) {
g[i] *= isumfo;
}
}
if (print) {
StringBuilder sb = new StringBuilder("\n");
sb.append(" Computed Potential Energy\n");
sb.append(String.format(" residual: %8.3f\n", sum / sumfo));
if (type == Type.FOFC || type == Type.F1F2) {
sb.append(String.format(" R: %8.3f Rfree: %8.3f\n", (r / rf) * 100.0, (rfree / rfreef) * 100.0));
}
sb.append("x: ");
for (int i = 0; i < x.length; i++) {
sb.append(String.format("%8g ", x[i]));
}
sb.append("\ng: ");
for (int i = 0; i < g.length; i++) {
sb.append(String.format("%8g ", g[i]));
}
sb.append("\n");
logger.info(sb.toString());
}
totalEnergy = sum / sumfo;
return sum / sumfo;
}
use of ffx.crystal.HKL in project ffx by mjschnie.
the class CrystalReciprocalSpace method computeAtomicGradients.
/**
* compute inverse FFT to determine atomic gradients
*
* @param hklData structure factors to apply inverse FFT
* @param freer array of free r flags corresponding to hkldata
* @param flag Rfree flag value
* @param refinementMode
* {@link RefinementMinimize.RefinementMode refinement mode}
* @param print if true, print information on timings during the calculation
* @see RefinementMinimize.RefinementMode
* @see DiffractionRefinementData
*/
public void computeAtomicGradients(double[][] hklData, int[] freer, int flag, RefinementMode refinementMode, boolean print) {
if (solvent && solventModel == SolventModel.NONE) {
return;
}
// zero out the density
for (int i = 0; i < complexFFT3DSpace; i++) {
densityGrid[i] = 0.0;
}
int nfree = 0;
StringBuilder sb = new StringBuilder();
long symtime = -System.nanoTime();
int nsym = crystal.spaceGroup.symOps.size();
// int nsym = 1;
List<SymOp> symops = crystal.spaceGroup.symOps;
ComplexNumber c = new ComplexNumber();
ComplexNumber cj = new ComplexNumber();
HKL ij = new HKL();
for (HKL ih : reflectionList.hkllist) {
double[] fc = hklData[ih.index()];
if (Double.isNaN(fc[0])) {
continue;
}
// cross validation check!!!
if (freer != null) {
if (freer[ih.index()] == flag) {
nfree++;
continue;
}
}
c.re(fc[0]);
c.im(fc[1]);
// scale
c.timesIP(2.0 / fftScale);
// apply symmetry
for (int j = 0; j < nsym; j++) {
cj.copy(c);
crystal.applyTransSymRot(ih, ij, symops.get(j));
double shift = Crystal.sym_phase_shift(ih, symops.get(j));
int h = Crystal.mod(ij.h(), fftX);
int k = Crystal.mod(ij.k(), fftY);
int l = Crystal.mod(ij.l(), fftZ);
if (h < halfFFTX + 1) {
final int ii = iComplex3D(h, k, l, fftX, fftY);
cj.phaseShiftIP(shift);
densityGrid[ii] += cj.re();
densityGrid[ii + 1] += -cj.im();
} else {
h = (fftX - h) % fftX;
k = (fftY - k) % fftY;
l = (fftZ - l) % fftZ;
final int ii = iComplex3D(h, k, l, fftX, fftY);
cj.phaseShiftIP(shift);
densityGrid[ii] += cj.re();
densityGrid[ii + 1] += cj.im();
}
}
}
symtime += System.nanoTime();
long startTime = System.nanoTime();
complexFFT3D.ifft(densityGrid);
long fftTime = System.nanoTime() - startTime;
/*
CCP4MapWriter mapout = new CCP4MapWriter(fftX, fftY, fftZ, crystal, "/tmp/foo.map");
mapout.write(densityGrid);
*/
startTime = System.nanoTime();
long permanentDensityTime = 0;
try {
if (solvent) {
solventGradientRegion.setRefinementMode(refinementMode);
parallelTeam.execute(solventGradientRegion);
} else {
for (int i = 0; i < nAtoms; i++) {
optAtomicGradientWeight.set(i, 0);
}
atomicGradientSchedule.updateWeights(previousOptAtomicGradientWeight);
atomicGradientRegion.setRefinementMode(refinementMode);
parallelTeam.execute(atomicGradientRegion);
for (int i = 0; i < nAtoms; i++) {
previousOptAtomicGradientWeight[i] = optAtomicGradientWeight.get(i);
}
}
permanentDensityTime = System.nanoTime() - startTime;
} catch (Exception e) {
String message = "Exception computing atomic gradients.";
logger.log(Level.SEVERE, message, e);
}
if (solvent) {
sb.append(String.format(" Solvent symmetry: %8.3f\n", symtime * toSeconds));
sb.append(String.format(" Solvent inverse FFT: %8.3f\n", fftTime * toSeconds));
sb.append(String.format(" Grid solvent gradients: %8.3f\n", permanentDensityTime * toSeconds));
sb.append(String.format(" %d reflections ignored (cross validation set)\n", nfree));
} else {
sb.append(String.format(" Atomic symmetry: %8.3f\n", symtime * toSeconds));
sb.append(String.format(" Atomic inverse FFT: %8.3f\n", fftTime * toSeconds));
sb.append(String.format(" Grid atomic gradients: %8.3f\n", permanentDensityTime * toSeconds));
sb.append(String.format(" %d reflections ignored (cross validation set)\n", nfree));
}
if (logger.isLoggable(Level.INFO) && print) {
logger.info(sb.toString());
}
}
use of ffx.crystal.HKL in project ffx by mjschnie.
the class CIFFilter method getResolution.
/**
* {@inheritDoc}
*/
@Override
public double getResolution(File cifFile, Crystal crystal) {
double resolution = Double.POSITIVE_INFINITY;
try {
BufferedReader br = new BufferedReader(new FileReader(cifFile));
String string;
int nCol = 0;
boolean inHKL = false;
while ((string = br.readLine()) != null) {
String[] strArray = string.split("\\s+");
if (strArray[0].startsWith("_refln.")) {
inHKL = true;
br.mark(0);
String[] cifArray = strArray[0].split("\\.+");
switch(Header.toHeader(cifArray[1])) {
case index_h:
h = nCol;
break;
case index_k:
k = nCol;
break;
case index_l:
l = nCol;
break;
}
nCol++;
} else if (inHKL) {
if (h < 0 || k < 0 || l < 0) {
String message = " Fatal error in CIF file - no H K L indexes?\n";
logger.log(Level.SEVERE, message);
return -1.0;
}
break;
}
}
// Go back to where the reflections start.
br.reset();
HKL hkl = new HKL();
while ((string = br.readLine()) != null) {
// Reached end, break.
if (string.trim().startsWith("#END")) {
break;
} else if (string.trim().startsWith("data")) {
break;
} else if (string.trim().startsWith("#")) {
continue;
}
// Some files split data on to multiple lines.
String[] strArray = string.trim().split("\\s+");
while (strArray.length < nCol) {
string = string + " " + br.readLine();
strArray = string.trim().split("\\s+");
}
int ih = Integer.parseInt(strArray[h]);
int ik = Integer.parseInt(strArray[k]);
int il = Integer.parseInt(strArray[l]);
hkl.h(ih);
hkl.k(ik);
hkl.l(il);
resolution = min(resolution, Crystal.res(crystal, hkl));
}
} catch (IOException e) {
String message = " CIF IO Exception.";
logger.log(Level.WARNING, message, e);
return -1.0;
}
return resolution;
}
use of ffx.crystal.HKL in project ffx by mjschnie.
the class CIFFilter method readFile.
/**
* {@inheritDoc}
*/
@Override
public boolean readFile(File cifFile, ReflectionList reflectionList, DiffractionRefinementData refinementData, CompositeConfiguration properties) {
int nRead, nNAN, nRes;
int nIgnore, nCIFIgnore;
int nFriedel, nCut;
boolean transpose = false;
boolean intensitiesToAmplitudes = false;
StringBuilder sb = new StringBuilder();
sb.append(String.format(" Opening %s\n", cifFile.getName()));
if (refinementData.rfreeflag < 0) {
refinementData.setFreeRFlag(1);
sb.append(format(" Setting R free flag to CIF default: %d\n", refinementData.rfreeflag));
}
try {
BufferedReader br = new BufferedReader(new FileReader(cifFile));
String string;
int nCol = 0;
boolean inHKL = false;
while ((string = br.readLine()) != null) {
String[] stringArray = string.split("\\s+");
if (stringArray[0].startsWith("_refln.")) {
inHKL = true;
br.mark(0);
String[] cifArray = stringArray[0].split("\\.+");
switch(Header.toHeader(cifArray[1])) {
case index_h:
h = nCol;
break;
case index_k:
k = nCol;
break;
case index_l:
l = nCol;
break;
case F_meas:
case F_meas_au:
fo = nCol;
break;
case F_meas_sigma:
case F_meas_sigma_au:
sigFo = nCol;
break;
case intensity_meas:
io = nCol;
break;
case intensity_sigma:
sigIo = nCol;
break;
case status:
rFree = nCol;
break;
}
nCol++;
} else if (inHKL) {
if (h < 0 || k < 0 || l < 0) {
String message = " Fatal error in CIF file - no H K L indexes?\n";
logger.log(Level.SEVERE, message);
return false;
}
break;
}
}
if (fo < 0 && sigFo < 0 && io > 0 && sigIo > 0) {
intensitiesToAmplitudes = true;
}
if (fo < 0 && io < 0) {
logger.severe(" Reflection data (I/F) not found in CIF file!");
}
// Go back to where the reflections start.
br.reset();
// Check if HKLs need to be transposed or not.
HKL mate = new HKL();
int nPosIgnore = 0;
int nTransIgnore = 0;
while ((string = br.readLine()) != null) {
if (string.trim().startsWith("#END")) {
// Reached end, break.
break;
} else if (string.trim().startsWith("data")) {
break;
} else if (string.trim().startsWith("#")) {
continue;
}
// Some files split data on to multiple lines.
String[] strArray = string.trim().split("\\s+");
while (strArray.length < nCol) {
string = string + " " + br.readLine();
strArray = string.trim().split("\\s+");
}
if (rFree > 0) {
// Ignored cases.
if (strArray[rFree].charAt(0) == 'x' || strArray[rFree].charAt(0) == '<' || strArray[rFree].charAt(0) == '-' || strArray[rFree].charAt(0) == 'h' || strArray[rFree].charAt(0) == 'l') {
continue;
}
}
int ih = Integer.parseInt(strArray[h]);
int ik = Integer.parseInt(strArray[k]);
int il = Integer.parseInt(strArray[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 (nPosIgnore > nTransIgnore) {
transpose = true;
}
// Re-open to start at beginning.
br = new BufferedReader(new FileReader(cifFile));
inHKL = false;
while ((string = br.readLine()) != null) {
String[] strArray = string.split("\\s+");
if (strArray[0].startsWith("_refln.")) {
br.mark(0);
inHKL = true;
} else if (inHKL) {
break;
}
}
// Go back to where the reflections start.
br.reset();
// 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 = nNAN = nRes = nIgnore = nCIFIgnore = nFriedel = nCut = 0;
while ((string = br.readLine()) != null) {
// Reached end, break.
if (string.trim().startsWith("#END")) {
break;
} else if (string.trim().startsWith("data")) {
break;
} else if (string.trim().startsWith("#")) {
continue;
}
// Some files split data on to multiple lines.
String[] strArray = string.trim().split("\\s+");
while (strArray.length < nCol) {
string = string + " " + br.readLine();
strArray = string.trim().split("\\s+");
}
int ih = Integer.parseInt(strArray[h]);
int ik = Integer.parseInt(strArray[k]);
int il = Integer.parseInt(strArray[l]);
boolean friedel = reflectionList.findSymHKL(ih, ik, il, mate, transpose);
HKL hkl = reflectionList.getHKL(mate);
if (hkl != null) {
boolean isnull = false;
if (rFree > 0) {
if (strArray[rFree].charAt(0) == 'o') {
refinementData.setFreeR(hkl.index(), 0);
} else if (strArray[rFree].charAt(0) == 'f') {
refinementData.setFreeR(hkl.index(), 1);
} else if (strArray[rFree].charAt(0) == 'x') {
isnull = true;
nNAN++;
} else if (strArray[rFree].charAt(0) == '<' || strArray[rFree].charAt(0) == '-' || strArray[rFree].charAt(0) == 'h' || strArray[rFree].charAt(0) == 'l') {
isnull = true;
nCIFIgnore++;
} else {
refinementData.setFreeR(hkl.index(), Integer.parseInt(strArray[rFree]));
}
}
if (!intensitiesToAmplitudes && !isnull) {
if (strArray[fo].charAt(0) == '?' || strArray[sigFo].charAt(0) == '?') {
isnull = true;
nNAN++;
continue;
}
if (refinementData.fsigfcutoff > 0.0) {
double f1 = Double.parseDouble(strArray[fo]);
double sigf1 = Double.parseDouble(strArray[sigFo]);
if ((f1 / sigf1) < refinementData.fsigfcutoff) {
nCut++;
continue;
}
}
if (friedel) {
anofSigF[hkl.index()][2] = Double.parseDouble(strArray[fo]);
anofSigF[hkl.index()][3] = Double.parseDouble(strArray[sigFo]);
nFriedel++;
} else {
anofSigF[hkl.index()][0] = Double.parseDouble(strArray[fo]);
anofSigF[hkl.index()][1] = Double.parseDouble(strArray[sigFo]);
}
}
if (intensitiesToAmplitudes && !isnull) {
if (strArray[io].charAt(0) == '?' || strArray[sigIo].charAt(0) == '?') {
isnull = true;
nNAN++;
continue;
}
if (friedel) {
anofSigF[hkl.index()][2] = Double.parseDouble(strArray[io]);
anofSigF[hkl.index()][3] = Double.parseDouble(strArray[sigIo]);
nFriedel++;
} else {
anofSigF[hkl.index()][0] = Double.parseDouble(strArray[io]);
anofSigF[hkl.index()][1] = Double.parseDouble(strArray[sigIo]);
}
}
nRead++;
} else {
HKL tmp = new HKL(ih, ik, il);
if (!reflectionList.resolution.inInverseResSqRange(Crystal.invressq(reflectionList.crystal, tmp))) {
nRes++;
} else {
nIgnore++;
}
}
}
br.close();
// Set up fsigf from F+ and F-.
refinementData.generate_fsigf_from_anofsigf(anofSigF);
if (intensitiesToAmplitudes) {
refinementData.intensities_to_amplitudes();
}
} catch (IOException ioe) {
System.out.println("IO Exception: " + ioe.getMessage());
return false;
}
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 with NaN (ignored): %d\n", nNAN));
sb.append(String.format(" HKL NOT read in (status <, -, h or l): %d\n", nCIFIgnore));
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());
}
String doRFree = System.getProperty("generate-rfree", "false");
if (doRFree.equalsIgnoreCase("true")) {
refinementData.generateRFree();
}
return true;
}
use of ffx.crystal.HKL in project ffx by mjschnie.
the class CrystalStats method printRStats.
/**
* print R factors and associated statistics in a binned fashion
*/
public void printRStats() {
double[][] res = new double[n][2];
double[] nhkl = new double[n + 1];
double[][] rb = new double[n + 1][2];
double[][] sumfo = new double[n + 1][2];
double[][] s = new double[n + 1][4];
double numer = 0.0;
double denom = 0.0;
double sumall = 0.0;
double sumfoall = 0.0;
ReflectionSpline sigmaaspline = new ReflectionSpline(reflectionlist, refinementdata.sigmaa.length);
for (int i = 0; i < n; i++) {
res[i][0] = Double.NEGATIVE_INFINITY;
res[i][1] = Double.POSITIVE_INFINITY;
}
for (HKL ih : reflectionlist.hkllist) {
int i = ih.index();
int b = ih.bin();
// ignored cases
if (Double.isNaN(fc[i][0]) || Double.isNaN(fo[i][0]) || fo[i][1] <= 0.0) {
continue;
}
// spline setup
double ss = Crystal.invressq(crystal, ih);
double fh = spline.f(ss, refinementdata.spline);
double sa = sigmaaspline.f(ss, refinementdata.sigmaa);
double wa = sigmaaspline.f(ss, refinementdata.sigmaw);
double eoscale = sigmaaspline.f(ss, refinementdata.foesq);
// determine res limits of each bin
double rs = Crystal.res(crystal, ih);
if (rs > res[b][0]) {
res[b][0] = rs;
}
if (rs < res[b][1]) {
res[b][1] = rs;
}
ComplexNumber c = new ComplexNumber(fc[i][0], fc[i][1]);
numer = abs(abs(fo[i][0]) - fh * abs(c.abs()));
denom = abs(fo[i][0]);
if (refinementdata.isFreeR(i)) {
rb[b][1] += numer;
sumfo[b][1] += denom;
rb[n][1] += numer;
sumfo[n][1] += denom;
sumall += numer;
sumfoall += denom;
} else {
rb[b][0] += numer;
sumfo[b][0] += denom;
rb[n][0] += numer;
sumfo[n][0] += denom;
sumall += numer;
sumfoall += denom;
}
nhkl[b]++;
nhkl[n]++;
s[b][0] += (sa - s[b][0]) / nhkl[b];
s[b][1] += (wa - s[b][1]) / nhkl[b];
s[b][2] += ((wa / Math.sqrt(eoscale)) - s[b][2]) / nhkl[b];
s[b][3] += (fomphi[i][0] - s[b][3]) / nhkl[b];
s[n][0] += (sa - s[n][0]) / nhkl[n];
s[n][1] += (wa - s[n][1]) / nhkl[n];
s[n][2] += ((wa / Math.sqrt(eoscale)) - s[n][2]) / nhkl[n];
s[n][3] += (fomphi[i][0] - s[n][3]) / nhkl[n];
}
StringBuilder sb = new StringBuilder(String.format("\n %15s | %7s | %7s | %7s | %7s | %7s | %7s\n", "Res. Range", " R", "Rfree", "s", "w(E)", "w(F)", "FOM"));
for (int i = 0; i < n; i++) {
sb.append(String.format(" %7.3f %7.3f | ", res[i][0], res[i][1]));
sb.append(String.format("%7.2f | %7.2f | %7.4f | %7.4f | %7.2f | %7.4f\n", (rb[i][0] / sumfo[i][0]) * 100.0, (rb[i][1] / sumfo[i][1]) * 100.0, s[i][0], s[i][1], s[i][2], s[i][3]));
}
sb.append(String.format(" %7.3f %7.3f | ", res[0][0], res[n - 1][1]));
sb.append(String.format("%7.2f | %7.2f | %7.4f | %7.4f | %7.2f | %7.4f\n", (rb[n][0] / sumfo[n][0]) * 100.0, (rb[n][1] / sumfo[n][1]) * 100.0, s[n][0], s[n][1], s[n][2], s[n][3]));
sb.append(" s and w are analagous to D and sum_wc");
reslow = res[0][0];
reshigh = res[n - 1][1];
highreslow = res[n - 1][0];
highreshigh = res[n - 1][1];
r = (rb[n][0] / sumfo[n][0]) * 100.0;
rfree = (rb[n][1] / sumfo[n][1]) * 100.0;
rall = (sumall / sumfoall) * 100.0;
highr = (rb[n - 1][0] / sumfo[n - 1][0]) * 100.0;
highrfree = (rb[n - 1][1] / sumfo[n - 1][1]) * 100.0;
if (print) {
logger.info(sb.toString());
}
}
Aggregations