List of usage examples for java.util ArrayList remove
public boolean remove(Object o)
From source file:ffx.potential.parsers.BiojavaFilter.java
/** * <p>//from ww w.ja v a2 s. c o m * writeFile</p> * * @param saveFile a {@link java.io.File} object. * @param append a {@link java.lang.StringBuilder} object. * @param printLinear Whether to print atoms linearly or by element * @return Success of writing. */ public boolean writeFile(File saveFile, boolean append, boolean printLinear) { if (saveFile == null) { return false; } if (vdwH) { logger.info(" Printing hydrogens to van der Waals centers instead of nuclear locations."); } /** * Create StringBuilders for ATOM, ANISOU and TER records that can be * reused. */ StringBuilder sb = new StringBuilder("ATOM "); StringBuilder anisouSB = new StringBuilder("ANISOU"); StringBuilder terSB = new StringBuilder("TER "); for (int i = 6; i < 80; i++) { sb.append(' '); anisouSB.append(' '); terSB.append(' '); } FileWriter fw; BufferedWriter bw; try { File newFile = saveFile; if (!append) { newFile = version(saveFile); } activeMolecularAssembly.setFile(newFile); activeMolecularAssembly.setName(newFile.getName()); logger.log(Level.INFO, " Saving {0}", newFile.getName()); fw = new FileWriter(newFile, append); bw = new BufferedWriter(fw); // ============================================================================= // The CRYST1 record presents the unit cell parameters, space group, and Z // value. If the structure was not determined by crystallographic means, CRYST1 // simply provides the unitary values, with an appropriate REMARK. // // 7 - 15 Real(9.3) a a (Angstroms). // 16 - 24 Real(9.3) b b (Angstroms). // 25 - 33 Real(9.3) c c (Angstroms). // 34 - 40 Real(7.2) alpha alpha (degrees). // 41 - 47 Real(7.2) beta beta (degrees). // 48 - 54 Real(7.2) gamma gamma (degrees). // 56 - 66 LString sGroup Space group. // 67 - 70 Integer z Z value. // ============================================================================= Crystal crystal = activeMolecularAssembly.getCrystal(); if (crystal != null && !crystal.aperiodic()) { Crystal c = crystal.getUnitCell(); if (!listMode) { bw.write(format("CRYST1%9.3f%9.3f%9.3f%7.2f%7.2f%7.2f %10s\n", c.a, c.b, c.c, c.alpha, c.beta, c.gamma, padRight(c.spaceGroup.pdbName, 10))); } else { listOutput.add(format("CRYST1%9.3f%9.3f%9.3f%7.2f%7.2f%7.2f %10s", c.a, c.b, c.c, c.alpha, c.beta, c.gamma, padRight(c.spaceGroup.pdbName, 10))); } } // ============================================================================= // The SSBOND record identifies each disulfide bond in protein and polypeptide // structures by identifying the two residues involved in the bond. // The disulfide bond distance is included after the symmetry operations at // the end of the SSBOND record. // // 8 - 10 Integer serNum Serial number. // 12 - 14 LString(3) "CYS" Residue name. // 16 Character chainID1 Chain identifier. // 18 - 21 Integer seqNum1 Residue sequence number. // 22 AChar icode1 Insertion code. // 26 - 28 LString(3) "CYS" Residue name. // 30 Character chainID2 Chain identifier. // 32 - 35 Integer seqNum2 Residue sequence number. // 36 AChar icode2 Insertion code. // 60 - 65 SymOP sym1 Symmetry oper for 1st resid // 67 - 72 SymOP sym2 Symmetry oper for 2nd resid // 74 78 Real(5.2) Length Disulfide bond distance // // If SG of cysteine is disordered then there are possible alternate linkages. // wwPDB practice is to put together all possible SSBOND records. This is // problematic because the alternate location identifier is not specified in // the SSBOND record. // ============================================================================= int serNum = 1; Polymer polymers[] = activeMolecularAssembly.getChains(); if (polymers != null) { for (Polymer polymer : polymers) { ArrayList<Residue> residues = polymer.getResidues(); for (Residue residue : residues) { if (residue.getName().equalsIgnoreCase("CYS")) { List<Atom> cysAtoms = residue.getAtomList(); Atom SG1 = null; for (Atom atom : cysAtoms) { if (atom.getName().equalsIgnoreCase("SG")) { SG1 = atom; break; } } List<Bond> bonds = SG1.getBonds(); for (Bond bond : bonds) { Atom SG2 = bond.get1_2(SG1); if (SG2.getName().equalsIgnoreCase("SG")) { if (SG1.xyzIndex < SG2.xyzIndex) { bond.energy(false); if (!listMode) { bw.write(format("SSBOND %3d CYS %1s %4s CYS %1s %4s %36s %5.2f\n", serNum++, SG1.getChainID().toString(), Hybrid36.encode(4, SG1.getResidueNumber()), SG2.getChainID().toString(), Hybrid36.encode(4, SG2.getResidueNumber()), "", bond.getValue())); } else { listOutput.add( format("SSBOND %3d CYS %1s %4s CYS %1s %4s %36s %5.2f\n", serNum++, SG1.getChainID().toString(), Hybrid36.encode(4, SG1.getResidueNumber()), SG2.getChainID().toString(), Hybrid36.encode(4, SG2.getResidueNumber()), "", bond.getValue())); } } } } } } } } // ============================================================================= // // 7 - 11 Integer serial Atom serial number. // 13 - 16 Atom name Atom name. // 17 Character altLoc Alternate location indicator. // 18 - 20 Residue name resName Residue name. // 22 Character chainID Chain identifier. // 23 - 26 Integer resSeq Residue sequence number. // 27 AChar iCode Code for insertion of residues. // 31 - 38 Real(8.3) x Orthogonal coordinates for X in Angstroms. // 39 - 46 Real(8.3) y Orthogonal coordinates for Y in Angstroms. // 47 - 54 Real(8.3) z Orthogonal coordinates for Z in Angstroms. // 55 - 60 Real(6.2) occupancy Occupancy. // 61 - 66 Real(6.2) tempFactor Temperature factor. // 77 - 78 LString(2) element Element symbol, right-justified. // 79 - 80 LString(2) charge Charge on the atom. // ============================================================================= // 1 2 3 4 5 6 7 //123456789012345678901234567890123456789012345678901234567890123456789012345678 //ATOM 1 N ILE A 16 60.614 71.140 -10.592 1.00 7.38 N //ATOM 2 CA ILE A 16 60.793 72.149 -9.511 1.00 6.91 C MolecularAssembly molecularAssemblies[] = this.getMolecularAssemblys(); int serial = 1; // Loop over biomolecular chains if (polymers != null) { for (Polymer polymer : polymers) { currentSegID = polymer.getName(); currentChainID = polymer.getChainID(); sb.setCharAt(21, currentChainID); // Loop over residues ArrayList<Residue> residues = polymer.getResidues(); for (Residue residue : residues) { String resName = residue.getName(); if (resName.length() > 3) { resName = resName.substring(0, 3); } int resID = residue.getResidueNumber(); sb.replace(17, 20, padLeft(resName.toUpperCase(), 3)); sb.replace(22, 26, String.format("%4s", Hybrid36.encode(4, resID))); // Loop over atoms ArrayList<Atom> residueAtoms = residue.getAtomList(); ArrayList<Atom> backboneAtoms = residue.getBackboneAtoms(); boolean altLocFound = false; for (Atom atom : backboneAtoms) { writeAtom(atom, serial++, sb, anisouSB, bw); Character altLoc = atom.getAltLoc(); if (altLoc != null && !altLoc.equals(' ')) { altLocFound = true; } residueAtoms.remove(atom); } for (Atom atom : residueAtoms) { writeAtom(atom, serial++, sb, anisouSB, bw); Character altLoc = atom.getAltLoc(); if (altLoc != null && !altLoc.equals(' ')) { altLocFound = true; } } // Write out alternate conformers if (altLocFound) { for (int ma = 1; ma < molecularAssemblies.length; ma++) { MolecularAssembly altMolecularAssembly = molecularAssemblies[ma]; Polymer altPolymer = altMolecularAssembly.getPolymer(currentChainID, currentSegID, false); Residue altResidue = altPolymer.getResidue(resName, resID, false); backboneAtoms = altResidue.getBackboneAtoms(); residueAtoms = altResidue.getAtomList(); for (Atom atom : backboneAtoms) { if (atom.getAltLoc() != null && !atom.getAltLoc().equals(' ') && !atom.getAltLoc().equals('A')) { writeAtom(atom, serial++, sb, anisouSB, bw); } residueAtoms.remove(atom); } for (Atom atom : residueAtoms) { if (atom.getAltLoc() != null && !atom.getAltLoc().equals(' ') && !atom.getAltLoc().equals('A')) { writeAtom(atom, serial++, sb, anisouSB, bw); } } } } } terSB.replace(6, 11, String.format("%5s", Hybrid36.encode(5, serial++))); terSB.replace(12, 16, " "); terSB.replace(16, 26, sb.substring(16, 26)); if (!listMode) { bw.write(terSB.toString()); bw.newLine(); } else { listOutput.add(terSB.toString()); } } } sb.replace(0, 6, "HETATM"); sb.setCharAt(21, 'A'); int resID = 1; Polymer polymer = activeMolecularAssembly.getPolymer('A', "A", false); if (polymer != null) { ArrayList<Residue> residues = polymer.getResidues(); for (Residue residue : residues) { int resID2 = residue.getResidueNumber(); if (resID2 >= resID) { resID = resID2 + 1; } } } /** * Loop over molecules, ions and then water. */ ArrayList<Molecule> molecules = activeMolecularAssembly.getMolecules(); for (int i = 0; i < molecules.size(); i++) { Molecule molecule = (Molecule) molecules.get(i); Character chainID = molecule.getChainID(); sb.setCharAt(21, chainID); String resName = molecule.getResidueName(); if (resName.length() > 3) { resName = resName.substring(0, 3); } sb.replace(17, 20, padLeft(resName.toUpperCase(), 3)); sb.replace(22, 26, String.format("%4s", Hybrid36.encode(4, resID))); ArrayList<Atom> moleculeAtoms = molecule.getAtomList(); boolean altLocFound = false; for (Atom atom : moleculeAtoms) { writeAtom(atom, serial++, sb, anisouSB, bw); Character altLoc = atom.getAltLoc(); if (altLoc != null && !altLoc.equals(' ')) { altLocFound = true; } } // Write out alternate conformers if (altLocFound) { for (int ma = 1; ma < molecularAssemblies.length; ma++) { MolecularAssembly altMolecularAssembly = molecularAssemblies[ma]; MSNode altmolecule = altMolecularAssembly.getMolecules().get(i); moleculeAtoms = altmolecule.getAtomList(); for (Atom atom : moleculeAtoms) { if (atom.getAltLoc() != null && !atom.getAltLoc().equals(' ') && !atom.getAltLoc().equals('A')) { writeAtom(atom, serial++, sb, anisouSB, bw); } } } } resID++; } ArrayList<MSNode> ions = activeMolecularAssembly.getIons(); for (int i = 0; i < ions.size(); i++) { Molecule ion = (Molecule) ions.get(i); Character chainID = ion.getChainID(); sb.setCharAt(21, chainID); String resName = ion.getResidueName(); if (resName.length() > 3) { resName = resName.substring(0, 3); } sb.replace(17, 20, padLeft(resName.toUpperCase(), 3)); sb.replace(22, 26, String.format("%4s", Hybrid36.encode(4, resID))); ArrayList<Atom> ionAtoms = ion.getAtomList(); boolean altLocFound = false; for (Atom atom : ionAtoms) { writeAtom(atom, serial++, sb, anisouSB, bw); Character altLoc = atom.getAltLoc(); if (altLoc != null && !altLoc.equals(' ')) { altLocFound = true; } } // Write out alternate conformers if (altLocFound) { for (int ma = 1; ma < molecularAssemblies.length; ma++) { MolecularAssembly altMolecularAssembly = molecularAssemblies[ma]; MSNode altion = altMolecularAssembly.getIons().get(i); ionAtoms = altion.getAtomList(); for (Atom atom : ionAtoms) { if (atom.getAltLoc() != null && !atom.getAltLoc().equals(' ') && !atom.getAltLoc().equals('A')) { writeAtom(atom, serial++, sb, anisouSB, bw); } } } } resID++; } ArrayList<MSNode> waters = activeMolecularAssembly.getWaters(); for (int i = 0; i < waters.size(); i++) { Molecule water = (Molecule) waters.get(i); Character chainID = water.getChainID(); sb.setCharAt(21, chainID); String resName = water.getResidueName(); if (resName.length() > 3) { resName = resName.substring(0, 3); } sb.replace(17, 20, padLeft(resName.toUpperCase(), 3)); sb.replace(22, 26, String.format("%4s", Hybrid36.encode(4, resID))); ArrayList<Atom> waterAtoms = water.getAtomList(); boolean altLocFound = false; for (Atom atom : waterAtoms) { writeAtom(atom, serial++, sb, anisouSB, bw); Character altLoc = atom.getAltLoc(); if (altLoc != null && !altLoc.equals(' ')) { altLocFound = true; } } // Write out alternate conformers if (altLocFound) { for (int ma = 1; ma < molecularAssemblies.length; ma++) { MolecularAssembly altMolecularAssembly = molecularAssemblies[ma]; MSNode altwater = altMolecularAssembly.getWaters().get(i); waterAtoms = altwater.getAtomList(); for (Atom atom : waterAtoms) { if (atom.getAltLoc() != null && !atom.getAltLoc().equals(' ') && !atom.getAltLoc().equals('A')) { writeAtom(atom, serial++, sb, anisouSB, bw); } } } } resID++; } if (!listMode) { bw.write("END"); bw.newLine(); } else { listOutput.add("END"); } bw.close(); } catch (Exception e) { String message = "Exception writing to file: " + saveFile.toString(); logger.log(Level.WARNING, message, e); return false; } return true; }
From source file:ch.epfl.lsir.xin.algorithm.core.SocialReg.java
/** * This function learns a matrix factorization model using Stochastic Gradient Descent * *//*ww w .ja v a2s .com*/ public void buildSGD() { double preError = Double.MAX_VALUE; for (int i = 0; i < this.Iterations; i++) { System.out.println("Iteration: " + i); ArrayList<MatrixEntry2D> entries = this.ratingMatrix.getValidEntries(); double error = 0; //overall error of this iteration System.out.println("Ratings: " + entries.size()); while (entries.size() > 0) { //find a random entry int r = new Random().nextInt(entries.size()); MatrixEntry2D entry = entries.get(r); double prediction = predict(entry.getRowIndex(), entry.getColumnIndex()); // System.out.println(prediction); if (prediction > this.maxRating) prediction = this.maxRating; if (prediction < this.minRating) prediction = this.minRating; double difference = entry.getValue() - prediction; for (int l = 0; l < this.latentFactors; l++) { //user latent update is a modified version of the original paper double tempU = -1; if (this.config.getString("MODE").equals("AVERAGE")) { ArrayList<Integer> friendIDs = this.socialMatrix.getRatedItemIndex(entry.getRowIndex()); double simSum = 0; double friendSum = 0; for (int j = 0; j < friendIDs.size(); j++) { double sim = this.socialMatrix.getRatingMatrix().get(entry.getRowIndex()) .get(friendIDs.get(j)); if (sim == 0) continue; simSum = simSum + sim; friendSum = friendSum + sim * this.userMatrix.get(friendIDs.get(j), l); } if (simSum != 0) friendSum = friendSum / simSum; else friendSum = 0; // System.out.println(friendSum); tempU = this.userMatrix.get(entry.getRowIndex(), l) + this.learningRate * (difference * this.itemMatrix.get(entry.getColumnIndex(), l) - this.regUser * this.userMatrix.get(entry.getRowIndex(), l) - this.socialReg * (this.userMatrix.get(entry.getRowIndex(), l) - friendSum)); } else if (this.config.getString("MODE").equals("INDIVIDUAL")) { ArrayList<Integer> friendIDs = this.socialMatrix.getRatedItemIndex(entry.getRowIndex()); double friendSum = 0; for (int j = 0; j < friendIDs.size(); j++) { friendSum += this.socialMatrix.getRatingMatrix().get(entry.getRowIndex()) .get(friendIDs.get(j)) * (this.userMatrix.get(entry.getRowIndex(), l) - this.userMatrix.get(friendIDs.get(j), l)); } tempU = this.userMatrix.get(entry.getRowIndex(), l) + this.learningRate * (difference * this.itemMatrix.get(entry.getColumnIndex(), l) - this.regUser * this.userMatrix.get(entry.getRowIndex(), l) - this.socialReg * friendSum); } else { logger.println("MODE is not set correctly."); } double tempI = this.itemMatrix.get(entry.getColumnIndex(), l) + this.learningRate * (2 * difference * this.userMatrix.get(entry.getRowIndex(), l) - this.regItem * this.itemMatrix.get(entry.getColumnIndex(), l)); this.userMatrix.set(entry.getRowIndex(), l, tempU); this.itemMatrix.set(entry.getColumnIndex(), l, tempI); } entries.remove(r); } //error entries = this.ratingMatrix.getValidEntries(); for (int k = 0; k < entries.size(); k++) { MatrixEntry2D entry = entries.get(k); double prediction = predict(entry.getRowIndex(), entry.getColumnIndex()); if (prediction > this.maxRating) prediction = this.maxRating; if (prediction < this.minRating) prediction = this.minRating; error = error + Math.abs(entry.getValue() - prediction); // for( int j = 0 ; j < this.latentFactors ; j++ ) // { // error = error + this.regUser/2 * Math.pow(this.userMatrix.get(entry.getRowIndex(), j), 2) + // this.regItem/2 * Math.pow(this.itemMatrix.get(entry.getColumnIndex(), j), 2); // } } this.logger.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " Iteration " + i + " : Error ~ " + error); this.logger.flush(); //check for convergence if (Math.abs(error - preError) <= this.convergence && error <= preError) { logger.println("The algorithm convergences."); this.logger.flush(); break; } // learning rate update strategy updateLearningRate(error, preError); preError = error; logger.flush(); } }
From source file:de.fosd.jdime.strategy.LinebasedStrategy.java
/** * This line-based <code>merge</code> method uses the merging routine of * the external tool <code>git</code>. * <p>/*from w w w. j a v a 2s. com*/ * Basically, the input <code>FileArtifacts</code> are passed as arguments to * `git merge-file -q -p`. * <p> * In a common run, the number of processed lines of code, the number of * conflicting situations, and the number of conflicting lines of code will * be counted. Empty lines and comments are skipped to keep * <code>MergeStrategies</code> comparable, as JDime does (in its current * implementation) not respect comments. * <p> * In case of a performance benchmark, the output is simply ignored for the * sake of speed, and the merge will be run the specified amount of times, * aiming to allow the computation of a reasonable mean runtime. * * @param operation <code>MergeOperation</code> that is executed by this strategy * @param context <code>MergeContext</code> that is used to retrieve environmental parameters * * @throws IOException * @throws InterruptedException */ @Override public final void merge(final MergeOperation<FileArtifact> operation, final MergeContext context) throws IOException, InterruptedException { assert (operation != null); assert (context != null); MergeTriple<FileArtifact> triple = operation.getMergeTriple(); assert (triple != null); assert (triple.isValid()) : "The merge triple is not valid!"; assert (triple.getLeft() instanceof FileArtifact); assert (triple.getBase() instanceof FileArtifact); assert (triple.getRight() instanceof FileArtifact); assert (triple.getLeft().exists() && !triple.getLeft().isDirectory()); assert ((triple.getBase().exists() && !triple.getBase().isDirectory()) || triple.getBase().isEmptyDummy()); assert (triple.getRight().exists() && !triple.getRight().isDirectory()); context.resetStreams(); FileArtifact target = null; if (operation.getTarget() != null) { assert (operation.getTarget() instanceof FileArtifact); target = operation.getTarget(); assert (!target.exists() || target.isEmpty()) : "Would be overwritten: " + target; } List<String> cmd = new ArrayList<>(); cmd.add(BASECMD); cmd.addAll(Arrays.asList(BASEARGS)); for (FileArtifact file : triple.getList()) { cmd.add(file.getPath()); } ProcessBuilder pb = new ProcessBuilder(cmd); ArrayList<Long> runtimes = new ArrayList<>(); int conflicts = 0; int loc = 0; int cloc = 0; // launch the merge process by invoking GNU merge (rcs has to be // installed) LOG.debug("Running external command: " + StringUtils.join(cmd, " ")); for (int i = 0; i < context.getBenchmarkRuns() + 1 && (i == 0 || context.isBenchmark()); i++) { long cmdStart = System.currentTimeMillis(); Process pr = pb.start(); if (i == 0 && (!context.isBenchmark() || context.hasStats())) { // process input stream BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream())); boolean conflict = false; boolean comment = false; int tmp = 0; String line; while ((line = buf.readLine()) != null) { context.appendLine(line); if (context.hasStats()) { if (line.matches("^$") || line.matches("^\\s*$") || line.matches("^\\s*//.*$")) { // skip empty lines and single line comments continue; } else if (line.matches("^\\s*/\\*.*")) { if (line.matches("^\\s*/\\*.*?\\*/")) { // one line comment continue; } else { // starting block comment comment = true; continue; } } else if (line.matches("^.*?\\*/")) { // ending block comment comment = false; continue; } if (line.matches("^\\s*<<<<<<<.*")) { if (LOG.isDebugEnabled()) { LOG.debug("CONFLICT in " + triple); } conflict = true; comment = false; tmp = cloc; conflicts++; } else if (line.matches("^\\s*=======.*")) { comment = false; } else if (line.matches("^\\s*>>>>>>>.*")) { conflict = false; comment = false; if (tmp == cloc) { // only conflicting comments or empty lines conflicts--; } } else { loc++; if (conflict && !comment) { cloc++; } } } } buf.close(); // process error stream buf = new BufferedReader(new InputStreamReader(pr.getErrorStream())); while ((line = buf.readLine()) != null) { if (i == 0 && (!context.isBenchmark() || context.hasStats())) { context.appendErrorLine(line); } } buf.close(); } pr.getInputStream().close(); pr.getErrorStream().close(); pr.getOutputStream().close(); pr.waitFor(); long runtime = System.currentTimeMillis() - cmdStart; runtimes.add(runtime); if (LOG.isInfoEnabled() && context.isBenchmark() && context.hasStats()) { if (i == 0) { LOG.info("Initial run: " + runtime + " ms"); } else { LOG.info("Run " + i + " of " + context.getBenchmarkRuns() + ": " + runtime + " ms"); } } } if (context.isBenchmark() && runtimes.size() > 1) { // remove first run as it took way longer due to all the counting runtimes.remove(0); } Long runtime = MergeContext.median(runtimes); LOG.debug("Linebased merge time was " + runtime + " ms."); if (context.hasErrors()) { LOG.fatal("Errors occured while calling '" + cmd + "')"); System.err.println(context.getStdErr()); } // write output if (target != null) { assert (target.exists()); target.write(context.getStdIn()); } // add statistical data to context if (context.hasStats()) { assert (cloc <= loc); Stats stats = context.getStats(); StatsElement linesElement = stats.getElement("lines"); assert (linesElement != null); StatsElement newElement = new StatsElement(); newElement.setMerged(loc); newElement.setConflicting(cloc); linesElement.addStatsElement(newElement); if (conflicts > 0) { assert (cloc > 0); stats.addConflicts(conflicts); StatsElement filesElement = stats.getElement("files"); assert (filesElement != null); filesElement.incrementConflicting(); } else { assert (cloc == 0); } stats.increaseRuntime(runtime); MergeTripleStats scenariostats = new MergeTripleStats(triple, conflicts, cloc, loc, runtime, null, null, null); stats.addScenarioStats(scenariostats); } }
From source file:ffx.potential.parsers.PDBFilter.java
/** * <p>/*from ww w. j a v a 2 s . c om*/ * writeFile</p> * * @param saveFile a {@link java.io.File} object. * @param append a {@link java.lang.StringBuilder} object. * @param printLinear Whether to print atoms linearly or by element * @return Success of writing. */ public boolean writeFile(File saveFile, boolean append, boolean printLinear) { if (saveFile == null) { return false; } if (vdwH) { logger.info(" Printing hydrogens to van der Waals centers instead of nuclear locations."); } if (nSymOp != 0) { logger.info(String.format(" Printing atoms with symmetry operator %s\n", activeMolecularAssembly.getCrystal().spaceGroup.getSymOp(nSymOp).toString())); } /** * Create StringBuilders for ATOM, ANISOU and TER records that can be * reused. */ StringBuilder sb = new StringBuilder("ATOM "); StringBuilder anisouSB = new StringBuilder("ANISOU"); StringBuilder terSB = new StringBuilder("TER "); StringBuilder model = null; for (int i = 6; i < 80; i++) { sb.append(' '); anisouSB.append(' '); terSB.append(' '); } FileWriter fw; BufferedWriter bw; try { File newFile = saveFile; if (!append) { if (!noVersioning) { newFile = version(saveFile); } } else if (modelsWritten >= 0) { model = new StringBuilder(String.format("MODEL %-4d", ++modelsWritten)); for (int i = 15; i < 80; i++) { model.append(' '); } } activeMolecularAssembly.setFile(newFile); activeMolecularAssembly.setName(newFile.getName()); if (logWrites) { logger.log(Level.INFO, " Saving {0}", newFile.getName()); } fw = new FileWriter(newFile, append); bw = new BufferedWriter(fw); /** * Will come before CRYST1 and ATOM records, but after anything * written by writeFileWithHeader (particularly X-ray refinement * statistics). */ String[] headerLines = activeMolecularAssembly.getHeaderLines(); for (String line : headerLines) { bw.write(String.format("%s\n", line)); } if (model != null) { if (!listMode) { bw.write(model.toString()); bw.newLine(); } else { listOutput.add(model.toString()); } } // ============================================================================= // The CRYST1 record presents the unit cell parameters, space group, and Z // value. If the structure was not determined by crystallographic means, CRYST1 // simply provides the unitary values, with an appropriate REMARK. // // 7 - 15 Real(9.3) a a (Angstroms). // 16 - 24 Real(9.3) b b (Angstroms). // 25 - 33 Real(9.3) c c (Angstroms). // 34 - 40 Real(7.2) alpha alpha (degrees). // 41 - 47 Real(7.2) beta beta (degrees). // 48 - 54 Real(7.2) gamma gamma (degrees). // 56 - 66 LString sGroup Space group. // 67 - 70 Integer z Z value. // ============================================================================= Crystal crystal = activeMolecularAssembly.getCrystal(); if (crystal != null && !crystal.aperiodic()) { Crystal c = crystal.getUnitCell(); if (!listMode) { bw.write(format("CRYST1%9.3f%9.3f%9.3f%7.2f%7.2f%7.2f %10s\n", c.a, c.b, c.c, c.alpha, c.beta, c.gamma, padRight(c.spaceGroup.pdbName, 10))); } else { listOutput.add(format("CRYST1%9.3f%9.3f%9.3f%7.2f%7.2f%7.2f %10s", c.a, c.b, c.c, c.alpha, c.beta, c.gamma, padRight(c.spaceGroup.pdbName, 10))); } } // ============================================================================= // The SSBOND record identifies each disulfide bond in protein and polypeptide // structures by identifying the two residues involved in the bond. // The disulfide bond distance is included after the symmetry operations at // the end of the SSBOND record. // // 8 - 10 Integer serNum Serial number. // 12 - 14 LString(3) "CYS" Residue name. // 16 Character chainID1 Chain identifier. // 18 - 21 Integer seqNum1 Residue sequence number. // 22 AChar icode1 Insertion code. // 26 - 28 LString(3) "CYS" Residue name. // 30 Character chainID2 Chain identifier. // 32 - 35 Integer seqNum2 Residue sequence number. // 36 AChar icode2 Insertion code. // 60 - 65 SymOP sym1 Symmetry oper for 1st resid // 67 - 72 SymOP sym2 Symmetry oper for 2nd resid // 74 78 Real(5.2) Length Disulfide bond distance // // If SG of cysteine is disordered then there are possible alternate linkages. // wwPDB practice is to put together all possible SSBOND records. This is // problematic because the alternate location identifier is not specified in // the SSBOND record. // ============================================================================= int serNum = 1; Polymer polymers[] = activeMolecularAssembly.getChains(); if (polymers != null) { for (Polymer polymer : polymers) { ArrayList<Residue> residues = polymer.getResidues(); for (Residue residue : residues) { if (residue.getName().equalsIgnoreCase("CYS")) { List<Atom> cysAtoms = residue.getAtomList(); Atom SG1 = null; for (Atom atom : cysAtoms) { if (atom.getName().equalsIgnoreCase("SG")) { SG1 = atom; break; } } List<Bond> bonds = SG1.getBonds(); for (Bond bond : bonds) { Atom SG2 = bond.get1_2(SG1); if (SG2.getName().equalsIgnoreCase("SG")) { if (SG1.xyzIndex < SG2.xyzIndex) { bond.energy(false); if (!listMode) { bw.write(format("SSBOND %3d CYS %1s %4s CYS %1s %4s %36s %5.2f\n", serNum++, SG1.getChainID().toString(), Hybrid36.encode(4, SG1.getResidueNumber()), SG2.getChainID().toString(), Hybrid36.encode(4, SG2.getResidueNumber()), "", bond.getValue())); } else { listOutput.add( format("SSBOND %3d CYS %1s %4s CYS %1s %4s %36s %5.2f\n", serNum++, SG1.getChainID().toString(), Hybrid36.encode(4, SG1.getResidueNumber()), SG2.getChainID().toString(), Hybrid36.encode(4, SG2.getResidueNumber()), "", bond.getValue())); } } } } } } } } // ============================================================================= // // 7 - 11 Integer serial Atom serial number. // 13 - 16 Atom name Atom name. // 17 Character altLoc Alternate location indicator. // 18 - 20 Residue name resName Residue name. // 22 Character chainID Chain identifier. // 23 - 26 Integer resSeq Residue sequence number. // 27 AChar iCode Code for insertion of residues. // 31 - 38 Real(8.3) x Orthogonal coordinates for X in Angstroms. // 39 - 46 Real(8.3) y Orthogonal coordinates for Y in Angstroms. // 47 - 54 Real(8.3) z Orthogonal coordinates for Z in Angstroms. // 55 - 60 Real(6.2) occupancy Occupancy. // 61 - 66 Real(6.2) tempFactor Temperature factor. // 77 - 78 LString(2) element Element symbol, right-justified. // 79 - 80 LString(2) charge Charge on the atom. // ============================================================================= // 1 2 3 4 5 6 7 //123456789012345678901234567890123456789012345678901234567890123456789012345678 //ATOM 1 N ILE A 16 60.614 71.140 -10.592 1.00 7.38 N //ATOM 2 CA ILE A 16 60.793 72.149 -9.511 1.00 6.91 C MolecularAssembly molecularAssemblies[] = this.getMolecularAssemblys(); int serial = 1; // Loop over biomolecular chains if (polymers != null) { for (Polymer polymer : polymers) { currentSegID = polymer.getName(); currentChainID = polymer.getChainID(); sb.setCharAt(21, currentChainID); // Loop over residues ArrayList<Residue> residues = polymer.getResidues(); for (Residue residue : residues) { String resName = residue.getName(); if (resName.length() > 3) { resName = resName.substring(0, 3); } int resID = residue.getResidueNumber(); sb.replace(17, 20, padLeft(resName.toUpperCase(), 3)); sb.replace(22, 26, String.format("%4s", Hybrid36.encode(4, resID))); // Loop over atoms ArrayList<Atom> residueAtoms = residue.getAtomList(); ArrayList<Atom> backboneAtoms = residue.getBackboneAtoms(); boolean altLocFound = false; for (Atom atom : backboneAtoms) { writeAtom(atom, serial++, sb, anisouSB, bw); Character altLoc = atom.getAltLoc(); if (altLoc != null && !altLoc.equals(' ')) { altLocFound = true; } residueAtoms.remove(atom); } for (Atom atom : residueAtoms) { writeAtom(atom, serial++, sb, anisouSB, bw); Character altLoc = atom.getAltLoc(); if (altLoc != null && !altLoc.equals(' ')) { altLocFound = true; } } // Write out alternate conformers if (altLocFound) { for (int ma = 1; ma < molecularAssemblies.length; ma++) { MolecularAssembly altMolecularAssembly = molecularAssemblies[ma]; Polymer altPolymer = altMolecularAssembly.getPolymer(currentChainID, currentSegID, false); Residue altResidue = altPolymer.getResidue(resName, resID, false); backboneAtoms = altResidue.getBackboneAtoms(); residueAtoms = altResidue.getAtomList(); for (Atom atom : backboneAtoms) { if (atom.getAltLoc() != null && !atom.getAltLoc().equals(' ') && !atom.getAltLoc().equals('A')) { writeAtom(atom, serial++, sb, anisouSB, bw); } residueAtoms.remove(atom); } for (Atom atom : residueAtoms) { if (atom.getAltLoc() != null && !atom.getAltLoc().equals(' ') && !atom.getAltLoc().equals('A')) { writeAtom(atom, serial++, sb, anisouSB, bw); } } } } } terSB.replace(6, 11, String.format("%5s", Hybrid36.encode(5, serial++))); terSB.replace(12, 16, " "); terSB.replace(16, 26, sb.substring(16, 26)); if (!listMode) { bw.write(terSB.toString()); bw.newLine(); } else { listOutput.add(terSB.toString()); } } } sb.replace(0, 6, "HETATM"); sb.setCharAt(21, 'A'); int resID = 1; Polymer polymer = activeMolecularAssembly.getPolymer('A', "A", false); if (polymer != null) { ArrayList<Residue> residues = polymer.getResidues(); for (Residue residue : residues) { int resID2 = residue.getResidueNumber(); if (resID2 >= resID) { resID = resID2 + 1; } } } /** * Loop over molecules, ions and then water. */ ArrayList<Molecule> molecules = activeMolecularAssembly.getMolecules(); for (int i = 0; i < molecules.size(); i++) { Molecule molecule = (Molecule) molecules.get(i); Character chainID = molecule.getChainID(); sb.setCharAt(21, chainID); String resName = molecule.getResidueName(); if (resName.length() > 3) { resName = resName.substring(0, 3); } sb.replace(17, 20, padLeft(resName.toUpperCase(), 3)); sb.replace(22, 26, String.format("%4s", Hybrid36.encode(4, resID))); ArrayList<Atom> moleculeAtoms = molecule.getAtomList(); boolean altLocFound = false; for (Atom atom : moleculeAtoms) { writeAtom(atom, serial++, sb, anisouSB, bw); Character altLoc = atom.getAltLoc(); if (altLoc != null && !altLoc.equals(' ')) { altLocFound = true; } } // Write out alternate conformers if (altLocFound) { for (int ma = 1; ma < molecularAssemblies.length; ma++) { MolecularAssembly altMolecularAssembly = molecularAssemblies[ma]; MSNode altmolecule = altMolecularAssembly.getMolecules().get(i); moleculeAtoms = altmolecule.getAtomList(); for (Atom atom : moleculeAtoms) { if (atom.getAltLoc() != null && !atom.getAltLoc().equals(' ') && !atom.getAltLoc().equals('A')) { writeAtom(atom, serial++, sb, anisouSB, bw); } } } } resID++; } ArrayList<MSNode> ions = activeMolecularAssembly.getIons(); for (int i = 0; i < ions.size(); i++) { Molecule ion = (Molecule) ions.get(i); Character chainID = ion.getChainID(); sb.setCharAt(21, chainID); String resName = ion.getResidueName(); if (resName.length() > 3) { resName = resName.substring(0, 3); } sb.replace(17, 20, padLeft(resName.toUpperCase(), 3)); sb.replace(22, 26, String.format("%4s", Hybrid36.encode(4, resID))); ArrayList<Atom> ionAtoms = ion.getAtomList(); boolean altLocFound = false; for (Atom atom : ionAtoms) { writeAtom(atom, serial++, sb, anisouSB, bw); Character altLoc = atom.getAltLoc(); if (altLoc != null && !altLoc.equals(' ')) { altLocFound = true; } } // Write out alternate conformers if (altLocFound) { for (int ma = 1; ma < molecularAssemblies.length; ma++) { MolecularAssembly altMolecularAssembly = molecularAssemblies[ma]; MSNode altion = altMolecularAssembly.getIons().get(i); ionAtoms = altion.getAtomList(); for (Atom atom : ionAtoms) { if (atom.getAltLoc() != null && !atom.getAltLoc().equals(' ') && !atom.getAltLoc().equals('A')) { writeAtom(atom, serial++, sb, anisouSB, bw); } } } } resID++; } ArrayList<MSNode> waters = activeMolecularAssembly.getWaters(); for (int i = 0; i < waters.size(); i++) { Molecule water = (Molecule) waters.get(i); Character chainID = water.getChainID(); sb.setCharAt(21, chainID); String resName = water.getResidueName(); if (resName.length() > 3) { resName = resName.substring(0, 3); } sb.replace(17, 20, padLeft(resName.toUpperCase(), 3)); sb.replace(22, 26, String.format("%4s", Hybrid36.encode(4, resID))); ArrayList<Atom> waterAtoms = water.getAtomList(); boolean altLocFound = false; for (Atom atom : waterAtoms) { writeAtom(atom, serial++, sb, anisouSB, bw); Character altLoc = atom.getAltLoc(); if (altLoc != null && !altLoc.equals(' ')) { altLocFound = true; } } // Write out alternate conformers if (altLocFound) { for (int ma = 1; ma < molecularAssemblies.length; ma++) { MolecularAssembly altMolecularAssembly = molecularAssemblies[ma]; MSNode altwater = altMolecularAssembly.getWaters().get(i); waterAtoms = altwater.getAtomList(); for (Atom atom : waterAtoms) { if (atom.getAltLoc() != null && !atom.getAltLoc().equals(' ') && !atom.getAltLoc().equals('A')) { writeAtom(atom, serial++, sb, anisouSB, bw); } } } } resID++; } String end = model != null ? "ENDMDL" : "END"; if (!listMode) { bw.write(end); bw.newLine(); } else { listOutput.add(end); } bw.close(); } catch (Exception e) { String message = "Exception writing to file: " + saveFile.toString(); logger.log(Level.WARNING, message, e); return false; } return true; }
From source file:hybridewah.HybridBitmap.java
/** * get the locations of the true values as one vector and add the offset to return the position including the offset. (may use more memory * than iterator())/*ww w . ja v a2 s . c om*/ * * @return the positions */ public List<Integer> getPositions(int offset) { final ArrayList<Integer> v = new ArrayList<Integer>(); if (this.verbatim) { int ntz = 0; long data = 0; for (int i = 0; i < this.actualsizeinwords; i++) { data = this.buffer[i]; //if (data > 0) { while (data != 0) { ntz = Long.numberOfTrailingZeros(data); data ^= (1l << ntz); v.add(new Integer(1 + ntz + (i * wordinbits)) + offset); } //} } return v; } else { final EWAHIterator i = new EWAHIterator(this.buffer, this.actualsizeinwords); int pos = 1; while (i.hasNext()) { RunningLengthWord localrlw = i.next(); if (localrlw.getRunningBit()) { for (int j = 0; j < localrlw.getRunningLength(); ++j) { for (int c = 0; c < wordinbits; ++c) v.add(new Integer(pos++) + offset); } } else { pos += wordinbits * localrlw.getRunningLength(); } for (int j = 0; j < localrlw.getNumberOfLiteralWords(); ++j) { long data = i.buffer()[i.literalWords() + j]; while (data != 0) { final int ntz = Long.numberOfTrailingZeros(data); data ^= (1l << ntz); v.add(new Integer(ntz + pos) + offset); } pos += wordinbits; } } while ((v.size() > 0) && (v.get(v.size() - 1).intValue() >= this.sizeinbits)) v.remove(v.size() - 1); return v; } }
From source file:imitationNLG.SFX.java
public Double evaluateGeneration(HashMap<String, JAROW> classifierAttrs, HashMap<String, HashMap<String, JAROW>> classifierWords, ArrayList<DatasetInstance> trainingData, ArrayList<DatasetInstance> testingData, HashMap<String, HashSet<String>> availableAttributeActions, HashMap<String, HashMap<String, HashSet<Action>>> availableWordActions, HashMap<Integer, HashSet<String>> nGrams, boolean printResults, int epoch) { System.out.println("Evaluate argument generation "); int totalArgDistance = 0; ArrayList<ScoredFeaturizedTranslation<IString, String>> generations = new ArrayList<>(); ArrayList<ArrayList<Action>> generationActions = new ArrayList<>(); HashMap<ArrayList<Action>, DatasetInstance> generationActionsMap = new HashMap<>(); ArrayList<ArrayList<Sequence<IString>>> finalReferences = new ArrayList<>(); ArrayList<String> predictedStrings = new ArrayList<>(); ArrayList<String> predictedStringMRs = new ArrayList<>(); ArrayList<Double> attrCoverage = new ArrayList<>(); ArrayList<ArrayList<String>> predictedAttrLists = new ArrayList<>(); HashSet<HashMap<String, HashSet<String>>> mentionedAttrs = new HashSet<HashMap<String, HashSet<String>>>(); for (DatasetInstance di : testingData) { String predicate = di.getMeaningRepresentation().getPredicate(); ArrayList<Action> predictedActionList = new ArrayList<>(); ArrayList<Action> predictedWordList = new ArrayList<>(); //PHRASE GENERATION EVALUATION String predictedAttr = ""; ArrayList<String> predictedAttrValues = new ArrayList<>(); ArrayList<String> predictedAttributes = new ArrayList<>(); HashSet<String> attrValuesToBeMentioned = new HashSet<>(); HashSet<String> attrValuesAlreadyMentioned = new HashSet<>(); HashMap<String, ArrayList<String>> valuesToBeMentioned = new HashMap<>(); for (String attribute : di.getMeaningRepresentation().getAttributes().keySet()) { for (String value : di.getMeaningRepresentation().getAttributes().get(attribute)) { attrValuesToBeMentioned.add(attribute.toLowerCase() + "=" + value.toLowerCase()); }/*w ww. j a v a 2s .c om*/ valuesToBeMentioned.put(attribute, new ArrayList<>(di.getMeaningRepresentation().getAttributes().get(attribute))); } if (attrValuesToBeMentioned.isEmpty()) { attrValuesToBeMentioned.add("empty=empty"); } HashSet<String> attrValuesToBeMentionedCopy = new HashSet<>(attrValuesToBeMentioned); while (!predictedAttr.equals(SFX.TOKEN_END) && predictedAttrValues.size() < maxAttrRealizationSize) { if (!predictedAttr.isEmpty()) { attrValuesToBeMentioned.remove(predictedAttr); } Instance attrTrainingVector = SFX.this.createAttrInstance(predicate, "@TOK@", predictedAttrValues, predictedActionList, attrValuesAlreadyMentioned, attrValuesToBeMentioned, di.getMeaningRepresentation(), availableAttributeActions); if (attrTrainingVector != null) { Prediction predictAttr = classifierAttrs.get(predicate).predict(attrTrainingVector); if (predictAttr.getLabel() != null) { predictedAttr = predictAttr.getLabel().trim(); String predictedValue = ""; if (!predictedAttr.equals(SFX.TOKEN_END)) { predictedValue = chooseNextValue(predictedAttr, attrValuesToBeMentioned, trainingData); HashSet<String> rejectedAttrs = new HashSet<String>(); while (predictedValue.isEmpty() && !predictedAttr.equals(SFX.TOKEN_END)) { rejectedAttrs.add(predictedAttr); predictedAttr = SFX.TOKEN_END; double maxScore = -Double.MAX_VALUE; for (String attr : predictAttr.getLabel2Score().keySet()) { if (!rejectedAttrs.contains(attr) && (Double .compare(predictAttr.getLabel2Score().get(attr), maxScore) > 0)) { maxScore = predictAttr.getLabel2Score().get(attr); predictedAttr = attr; } } if (!predictedAttr.equals(SFX.TOKEN_END)) { predictedValue = chooseNextValue(predictedAttr, attrValuesToBeMentioned, trainingData); } } } if (!predictedAttr.equals(SFX.TOKEN_END)) { predictedAttr += "=" + predictedValue; } predictedAttrValues.add(predictedAttr); String attribute = predictedAttrValues.get(predictedAttrValues.size() - 1).split("=")[0]; String attrValue = predictedAttrValues.get(predictedAttrValues.size() - 1); predictedAttributes.add(attrValue); //GENERATE PHRASES if (!attribute.equals(SFX.TOKEN_END)) { if (classifierWords.get(predicate).containsKey(attribute)) { String predictedWord = ""; boolean isValueMentioned = false; String valueTBM = ""; if (attrValue.contains("=")) { valueTBM = attrValue.substring(attrValue.indexOf('=') + 1); } if (valueTBM.isEmpty()) { isValueMentioned = true; } ArrayList<String> subPhrase = new ArrayList<>(); while (!predictedWord.equals(RoboCup.TOKEN_END) && predictedWordList.size() < maxWordRealizationSize) { ArrayList<String> predictedAttributesForInstance = new ArrayList<>(); for (int i = 0; i < predictedAttributes.size() - 1; i++) { predictedAttributesForInstance.add(predictedAttributes.get(i)); } if (!predictedAttributes.get(predictedAttributes.size() - 1) .equals(attrValue)) { predictedAttributesForInstance .add(predictedAttributes.get(predictedAttributes.size() - 1)); } Instance wordTrainingVector = createWordInstance(predicate, new Action("@TOK@", attrValue), predictedAttributesForInstance, predictedActionList, isValueMentioned, attrValuesAlreadyMentioned, attrValuesToBeMentioned, di.getMeaningRepresentation(), availableWordActions.get(predicate), nGrams, false); if (wordTrainingVector != null) { if (classifierWords.get(predicate) != null) { if (classifierWords.get(predicate).get(attribute) != null) { Prediction predictWord = classifierWords.get(predicate) .get(attribute).predict(wordTrainingVector); if (predictWord.getLabel() != null) { predictedWord = predictWord.getLabel().trim(); predictedActionList.add(new Action(predictedWord, attrValue)); if (!predictedWord.equals(SFX.TOKEN_END)) { subPhrase.add(predictedWord); predictedWordList.add(new Action(predictedWord, attrValue)); } } else { predictedWord = SFX.TOKEN_END; predictedActionList.add(new Action(predictedWord, attrValue)); } } else { predictedWord = SFX.TOKEN_END; predictedActionList.add(new Action(predictedWord, attrValue)); } } } if (!isValueMentioned) { if (!predictedWord.equals(SFX.TOKEN_END)) { if (predictedWord.startsWith(SFX.TOKEN_X) && (valueTBM.matches("\"[xX][0-9]+\"") || valueTBM.matches("[xX][0-9]+") || valueTBM.startsWith(SFX.TOKEN_X))) { isValueMentioned = true; } else if (!predictedWord.startsWith(SFX.TOKEN_X) && !(valueTBM.matches("\"[xX][0-9]+\"") || valueTBM.matches("[xX][0-9]+") || valueTBM.startsWith(SFX.TOKEN_X))) { String valueToCheck = valueTBM; if (valueToCheck.equals("no") || valueToCheck.equals("yes") || valueToCheck.equals("yes or no") || valueToCheck.equals("none") || valueToCheck.equals("dont_care") || valueToCheck.equals("empty")) { if (attribute.contains("=")) { valueToCheck = attribute.replace("=", ":"); } else { valueToCheck = attribute + ":" + valueTBM; } } if (!valueToCheck.equals("empty:empty") && valueAlignments.containsKey(valueToCheck)) { for (ArrayList<String> alignedStr : valueAlignments .get(valueToCheck).keySet()) { if (endsWith(subPhrase, alignedStr)) { isValueMentioned = true; break; } } } } } if (isValueMentioned) { attrValuesAlreadyMentioned.add(attrValue); attrValuesToBeMentioned.remove(attrValue); } } String mentionedAttrValue = ""; if (!predictedWord.startsWith(SFX.TOKEN_X)) { for (String attrValueTBM : attrValuesToBeMentioned) { if (attrValueTBM.contains("=")) { String value = attrValueTBM .substring(attrValueTBM.indexOf('=') + 1); if (!(value.matches("\"[xX][0-9]+\"") || value.matches("[xX][0-9]+") || value.startsWith(SFX.TOKEN_X))) { String valueToCheck = value; if (valueToCheck.equals("no") || valueToCheck.equals("yes") || valueToCheck.equals("yes or no") || valueToCheck.equals("none") || valueToCheck.equals("dont_care") || valueToCheck.equals("empty")) { valueToCheck = attrValueTBM.replace("=", ":"); } if (!valueToCheck.equals("empty:empty") && valueAlignments.containsKey(valueToCheck)) { for (ArrayList<String> alignedStr : valueAlignments .get(valueToCheck).keySet()) { if (endsWith(subPhrase, alignedStr)) { mentionedAttrValue = attrValueTBM; break; } } } } } } } if (!mentionedAttrValue.isEmpty()) { attrValuesAlreadyMentioned.add(attrValue); attrValuesToBeMentioned.remove(mentionedAttrValue); } } if (predictedWordList.size() >= maxWordRealizationSize && !predictedActionList .get(predictedActionList.size() - 1).getWord().equals(SFX.TOKEN_END)) { predictedWord = SFX.TOKEN_END; predictedActionList.add(new Action(predictedWord, predictedAttrValues.get(predictedAttrValues.size() - 1))); } } else { String predictedWord = SFX.TOKEN_END; predictedActionList.add(new Action(predictedWord, attrValue)); } } } else { predictedAttr = SFX.TOKEN_END; } } } ArrayList<String> predictedAttrs = new ArrayList<>(); for (String attributeValuePair : predictedAttrValues) { predictedAttrs.add(attributeValuePair.split("=")[0]); } ArrayList<Action> cleanActionList = new ArrayList<Action>(); for (Action action : predictedActionList) { if (!action.getWord().equals(SFX.TOKEN_END) && !action.getWord().equals(SFX.TOKEN_START)) { cleanActionList.add(action); } } for (int i = 0; i < cleanActionList.size(); i++) { for (ArrayList<Action> surrounds : punctPatterns.keySet()) { boolean matches = true; int m = 0; for (int s = 0; s < surrounds.size(); s++) { if (surrounds.get(s) != null) { if (i + s < cleanActionList.size()) { if (!cleanActionList.get(i + s).getWord().equals(surrounds.get(s) .getWord()) /*|| !cleanActionList.get(i).getAttribute().equals(surrounds.get(s).getAttribute())*/) { matches = false; s = surrounds.size(); } else { m++; } } else { matches = false; s = surrounds.size(); } } } if (matches && m > 0) { cleanActionList.add(i + 2, punctPatterns.get(surrounds)); } } } String predictedString = ""; ArrayList<String> predictedAttrList = new ArrayList<String>(); HashSet<String> redundants = new HashSet<String>(); for (Action action : cleanActionList) { if (action.getWord().startsWith(SFX.TOKEN_X)) { predictedString += di.getMeaningRepresentation().getDelexMap().get(action.getWord()) + " "; //predictedString += "x "; if (di.getMeaningRepresentation().getDelexMap().get(action.getWord()) == null || di.getMeaningRepresentation().getDelexMap().get(action.getWord()).equals("null")) { redundants.add(action.getWord()); } } else { predictedString += action.getWord() + " "; } if (predictedAttrList.isEmpty()) { predictedAttrList.add(action.getAttribute()); } else if (!predictedAttrList.get(predictedAttrList.size() - 1).equals(action.getAttribute())) { predictedAttrList.add(action.getAttribute()); } } predictedAttrLists.add(predictedAttrList); if (attrValuesToBeMentionedCopy.size() != 0.0) { double redundAttrs = 0.0; double missingAttrs = 0.0; for (String attr : predictedAttrList) { if (!attrValuesToBeMentionedCopy.contains(attr)) { redundAttrs += 1.0; } } for (String attr : attrValuesToBeMentionedCopy) { if (!predictedAttrList.contains(attr)) { missingAttrs += 1.0; } } double attrSize = (double) attrValuesToBeMentionedCopy.size(); attrCoverage.add((redundAttrs + missingAttrs) / attrSize); } if (predicate.startsWith("?")) { predictedString = predictedString.trim() + "?"; } else { predictedString = predictedString.trim() + "."; } predictedString = predictedString.replaceAll("\\?", " \\? ").replaceAll(":", " : ") .replaceAll("\\.", " \\. ").replaceAll(",", " , ").replaceAll(" ", " ").trim(); if (!mentionedAttrs.contains(di.getMeaningRepresentation().getAttributes())) { predictedStrings.add(predictedString); predictedStringMRs.add(di.getMeaningRepresentation().getMRstr()); mentionedAttrs.add(di.getMeaningRepresentation().getAttributes()); } Sequence<IString> translation = IStrings .tokenize(NISTTokenizer.tokenize(predictedString.toLowerCase())); ScoredFeaturizedTranslation<IString, String> tran = new ScoredFeaturizedTranslation<>(translation, null, 0); generations.add(tran); generationActions.add(predictedActionList); generationActionsMap.put(predictedActionList, di); ArrayList<Sequence<IString>> references = new ArrayList<>(); for (ArrayList<Action> realization : di.getEvalRealizations()) { String cleanedWords = ""; for (Action nlWord : realization) { if (!nlWord.equals(new Action(SFX.TOKEN_START, "")) && !nlWord.equals(new Action(SFX.TOKEN_END, ""))) { if (nlWord.getWord().startsWith(SFX.TOKEN_X)) { cleanedWords += di.getMeaningRepresentation().getDelexMap().get(nlWord.getWord()) + " "; } else { cleanedWords += nlWord.getWord() + " "; } } } cleanedWords = cleanedWords.trim(); if (!cleanedWords.endsWith(".")) { cleanedWords += "."; } cleanedWords = cleanedWords.replaceAll("\\?", " \\? ").replaceAll(":", " : ") .replaceAll("\\.", " \\. ").replaceAll(",", " , ").replaceAll(" ", " ").trim(); references.add(IStrings.tokenize(NISTTokenizer.tokenize(cleanedWords))); } finalReferences.add(references); //EVALUATE ATTRIBUTE SEQUENCE HashSet<ArrayList<String>> goldAttributeSequences = new HashSet<>(); for (DatasetInstance di2 : testingData) { if (di2.getMeaningRepresentation().getAttributes() .equals(di.getMeaningRepresentation().getAttributes())) { goldAttributeSequences.addAll(di2.getEvalMentionedAttributeSequences().values()); } } int minTotArgDistance = Integer.MAX_VALUE; for (ArrayList<String> goldArgs : goldAttributeSequences) { int totArgDistance = 0; HashSet<Integer> matchedPositions = new HashSet<>(); for (int i = 0; i < predictedAttrs.size(); i++) { if (!predictedAttrs.get(i).equals(SFX.TOKEN_START) && !predictedAttrs.get(i).equals(SFX.TOKEN_END)) { int minArgDistance = Integer.MAX_VALUE; int minArgPos = -1; for (int j = 0; j < goldArgs.size(); j++) { if (!matchedPositions.contains(j)) { if (goldArgs.get(j).equals(predictedAttrs.get(i))) { int argDistance = Math.abs(j - i); if (argDistance < minArgDistance) { minArgDistance = argDistance; minArgPos = j; } } } } if (minArgPos == -1) { totArgDistance += 100; } else { matchedPositions.add(minArgPos); totArgDistance += minArgDistance; } } } ArrayList<String> predictedCopy = (ArrayList<String>) predictedAttrs.clone(); for (String goldArg : goldArgs) { if (!goldArg.equals(SFX.TOKEN_END)) { boolean contained = predictedCopy.remove(goldArg); if (!contained) { totArgDistance += 1000; } } } if (totArgDistance < minTotArgDistance) { minTotArgDistance = totArgDistance; } } totalArgDistance += minTotArgDistance; } previousResults = generationActions; crossAvgArgDistances.add(totalArgDistance / (double) testingData.size()); NISTMetric NIST = new NISTMetric(finalReferences); BLEUMetric BLEU = new BLEUMetric(finalReferences, 4, false); BLEUMetric BLEUsmooth = new BLEUMetric(finalReferences, 4, true); Double nistScore = NIST.score(generations); Double bleuScore = BLEU.score(generations); Double bleuSmoothScore = BLEUsmooth.score(generations); double finalCoverage = 0.0; for (double c : attrCoverage) { finalCoverage += c; } finalCoverage /= (double) attrCoverage.size(); crossNIST.add(nistScore); crossBLEU.add(bleuScore); crossBLEUSmooth.add(bleuSmoothScore); System.out.println("Avg arg distance: \t" + totalArgDistance / (double) testingData.size()); System.out.println("NIST: \t" + nistScore); System.out.println("BLEU: \t" + bleuScore); System.out.println("COVERAGE: \t" + finalCoverage); System.out.println("g: " + generations); System.out.println("attr: " + predictedAttrLists); System.out.println("BLEU smooth: \t" + bleuSmoothScore); previousBLEU = bleuScore; if (printResults) { BufferedWriter bw = null; File f = null; try { f = new File("random_SFX" + dataset + "TextsAfter" + (epoch) + "_" + JDAggerForSFX.earlyStopMaxFurtherSteps + "_" + JDAggerForSFX.p + "epochsTESTINGDATA.txt"); } catch (NullPointerException e) { System.err.println("File not found." + e); } try { bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f))); } catch (FileNotFoundException e) { System.err.println("Error opening file for writing! " + e); } try { bw.write("BLEU:" + bleuScore); bw.write("\n"); } catch (IOException e) { System.err.println("Write error!"); } for (int i = 0; i < predictedStrings.size(); i++) { try { //Grafoume to String sto arxeio //SFX HOTEL TEXTS WITH LOLS -> 3 //SFX RESTAURANT TEXTS WITH LOLS -> 5 bw.write("MR;" + predictedStringMRs.get(i).replaceAll(";", ",") + ";"); if (dataset.equals("hotel")) { bw.write("LOLS_SFHOT;"); } else { bw.write("LOLS_SFRES;"); } //bw.write("@@srcdoc@@" + (i + 1)); /*String out = predictedStrings.get(i).replaceAll(" i ", " I ").replaceAll(" -ly ", "ly ").replaceAll(" s ", "s ").replaceAll("\\?", " \\? ").replaceAll(":", " : ").replaceAll("\\.", " \\. ").replaceAll(",", " , ").replaceAll(" ", " "); out = out.substring(0, 1).toUpperCase() + out.substring(1); bw.write(out + ";"); if (dataset.equals("hotel")) { bw.write("WEN_SFHOT;"); } else { bw.write("WEN_SFRES;"); } if (!wenDaToGen.containsKey(predictedStringMRs.get(i).trim().toLowerCase())) { System.out.println(wenDaToGen.keySet()); System.out.println(predictedStringMRs.get(i).trim().toLowerCase()); System.exit(0); } out = wenDaToGen.get(predictedStringMRs.get(i).trim().toLowerCase()).replaceAll(" i ", " I ").replaceAll(" -ly ", "ly ").replaceAll(" s ", "s ").replaceAll("\\?", " \\? ").replaceAll(":", " : ").replaceAll("\\.", " \\. ").replaceAll(",", " , ").replaceAll(" ", " "); out = out.substring(0, 1).toUpperCase() + out.substring(1); bw.write(out + ";");*/ //bw.write("@@judgeFluency@@-1"); //bw.write("@@judgeInform@@-1"); //bw.write("@@judgeQuality@@-1"); bw.write("\n"); } catch (IOException e) { System.err.println("Write error!"); } } try { bw.close(); } catch (IOException e) { System.err.println("Error closing file."); } catch (Exception e) { } } return bleuScore; }
From source file:org.telegram.android.MessagesController.java
public void updateTimerProc() { long currentTime = System.currentTimeMillis(); checkDeletingTask(false);/*from w w w . jav a 2 s .co m*/ if (UserConfig.isClientActivated()) { if (ConnectionsManager.getInstance().getPauseTime() == 0 && ApplicationLoader.isScreenOn && !ApplicationLoader.mainInterfacePaused) { if (statusSettingState != 1 && (lastStatusUpdateTime == 0 || lastStatusUpdateTime <= System.currentTimeMillis() - 55000 || offlineSent)) { statusSettingState = 1; if (statusRequest != 0) { ConnectionsManager.getInstance().cancelRpc(statusRequest, true); } TLRPC.TL_account_updateStatus req = new TLRPC.TL_account_updateStatus(); req.offline = false; statusRequest = ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() { @Override public void run(TLObject response, TLRPC.TL_error error) { if (error == null) { lastStatusUpdateTime = System.currentTimeMillis(); offlineSent = false; statusSettingState = 0; } else { if (lastStatusUpdateTime != 0) { lastStatusUpdateTime += 5000; } } statusRequest = 0; } }); } } else if (statusSettingState != 2 && !offlineSent && ConnectionsManager.getInstance().getPauseTime() <= System.currentTimeMillis() - 2000) { statusSettingState = 2; if (statusRequest != 0) { ConnectionsManager.getInstance().cancelRpc(statusRequest, true); } TLRPC.TL_account_updateStatus req = new TLRPC.TL_account_updateStatus(); req.offline = true; statusRequest = ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() { @Override public void run(TLObject response, TLRPC.TL_error error) { if (error == null) { offlineSent = true; } else { if (lastStatusUpdateTime != 0) { lastStatusUpdateTime += 5000; } } statusRequest = 0; } }); } for (int a = 0; a < 3; a++) { if (getUpdatesStartTime(a) != 0 && getUpdatesStartTime(a) + 1500 < currentTime) { FileLog.e("tmessages", a + " QUEUE UPDATES WAIT TIMEOUT - CHECK QUEUE"); processUpdatesQueue(a, 0); } } } if (!onlinePrivacy.isEmpty()) { ArrayList<Integer> toRemove = null; int currentServerTime = ConnectionsManager.getInstance().getCurrentTime(); for (ConcurrentHashMap.Entry<Integer, Integer> entry : onlinePrivacy.entrySet()) { if (entry.getValue() < currentServerTime - 30) { if (toRemove == null) { toRemove = new ArrayList<>(); } toRemove.add(entry.getKey()); } } if (toRemove != null) { for (Integer uid : toRemove) { onlinePrivacy.remove(uid); } AndroidUtilities.runOnUIThread(new Runnable() { @Override public void run() { NotificationCenter.getInstance().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_STATUS); } }); } } if (!printingUsers.isEmpty() || lastPrintingStringCount != printingUsers.size()) { boolean updated = false; ArrayList<Long> keys = new ArrayList<>(printingUsers.keySet()); for (int b = 0; b < keys.size(); b++) { Long key = keys.get(b); ArrayList<PrintingUser> arr = printingUsers.get(key); for (int a = 0; a < arr.size(); a++) { PrintingUser user = arr.get(a); if (user.lastTime + 5900 < currentTime) { updated = true; arr.remove(user); a--; } } if (arr.isEmpty()) { printingUsers.remove(key); keys.remove(b); b--; } } updatePrintingStrings(); if (updated) { AndroidUtilities.runOnUIThread(new Runnable() { @Override public void run() { NotificationCenter.getInstance().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_USER_PRINT); } }); } } }
From source file:servlets.Samples_servlets.java
private void get_all_samples_handler(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try {/*from ww w . j ava2 s . c o m*/ DAO dao_instance = null; ArrayList<Object> bioconditionsList = null; ArrayList<String> study_samples = null; try { Map<String, Cookie> cookies = this.getCookies(request); String loggedUser, loggedUserID, sessionToken; loggedUser = cookies.get("loggedUser").getValue(); loggedUserID = cookies.get("loggedUserID").getValue(); sessionToken = cookies.get("sessionToken").getValue(); /** * ******************************************************* * STEP 1 CHECK IF THE USER IS LOGGED CORRECTLY IN THE APP. IF * ERROR --> throws exception if not valid session, GO TO STEP * 5b ELSE --> GO TO STEP 2 * ******************************************************* */ if (!checkAccessPermissions(loggedUser, sessionToken)) { throw new AccessControlException("Your session is invalid. User or session token not allowed."); } /** * ******************************************************* * STEP 2 Get ALL THE ANALYSIS Object from DB. IF ERROR --> * throws MySQL exception, GO TO STEP 3b ELSE --> GO TO STEP 3 * ******************************************************* */ boolean loadRecursive = "1".equals(request.getParameter("recursive")); String experiment_id = cookies.get("currentExperimentID").getValue(); Object[] params = { loadRecursive }; dao_instance = DAOProvider.getDAOByName("BioCondition"); bioconditionsList = dao_instance.findAll(params); study_samples = ((BioCondition_JDBCDAO) dao_instance).findSamplesIDByExperimentID(experiment_id); ArrayList<String> bioconditionIds = new ArrayList<String>(); for (String sample_id : study_samples) { if (sample_id.contains(".")) { sample_id = sample_id.split("\\.")[0]; } bioconditionIds.add("BC" + sample_id.substring(2)); } for (int i = bioconditionsList.size() - 1; i >= 0; i--) { if (((BioCondition) bioconditionsList.get(i)).isOwner(loggedUserID) || ((BioCondition) bioconditionsList.get(i)).isPublic() || bioconditionIds .contains(((BioCondition) bioconditionsList.get(i)).getBioConditionID())) { continue; } bioconditionsList.remove(i); } } catch (Exception e) { ServerErrorManager.handleException(e, Samples_servlets.class.getName(), "get_all_samples_handler", e.getMessage()); } finally { /** * ******************************************************* * STEP 3b CATCH ERROR. GO TO STEP 4 * ******************************************************* */ if (ServerErrorManager.errorStatus()) { response.setStatus(400); response.getWriter().print(ServerErrorManager.getErrorResponse()); } else { /** * ******************************************************* * STEP 3A WRITE RESPONSE ERROR. GO TO STEP 4 * ******************************************************* */ String bioconditionsJSON = "{\"samples\" : ["; for (int i = 0; i < bioconditionsList.size(); i++) { bioconditionsJSON += ((BioCondition) bioconditionsList.get(i)).toJSON() + ((i < bioconditionsList.size() - 1) ? "," : ""); } bioconditionsJSON += "], \"samples_current_study\" : ["; for (int i = 0; i < study_samples.size(); i++) { bioconditionsJSON += "\"" + study_samples.get(i) + "\"" + ((i < study_samples.size() - 1) ? "," : ""); } bioconditionsJSON += "]}"; response.getWriter().print(bioconditionsJSON); } /** * ******************************************************* * STEP 4 Close connection. * ******************************************************** */ if (dao_instance != null) { dao_instance.closeConnection(); } } //CATCH IF THE ERROR OCCURRED IN ROLL BACK OR CONNECTION CLOSE } catch (Exception e) { ServerErrorManager.handleException(e, Samples_servlets.class.getName(), "get_all_samples_handler", e.getMessage()); response.setStatus(400); response.getWriter().print(ServerErrorManager.getErrorResponse()); } }
From source file:com.ibm.bi.dml.lops.compile.Dag.java
/** * Exclude rmvar instruction for <varname> from deleteInst, if exists * //from w ww . j ava2 s. c om * @param varName * @param deleteInst */ private void excludeRemoveInstruction(String varName, ArrayList<Instruction> deleteInst) { //for(Instruction inst : deleteInst) { for (int i = 0; i < deleteInst.size(); i++) { Instruction inst = deleteInst.get(i); if ((inst.getType() == INSTRUCTION_TYPE.CONTROL_PROGRAM || inst.getType() == INSTRUCTION_TYPE.SPARK) && ((CPInstruction) inst).getCPInstructionType() == CPINSTRUCTION_TYPE.Variable && ((VariableCPInstruction) inst).isRemoveVariable(varName)) { deleteInst.remove(i); } } }
From source file:org.telegram.android.MessagesController.java
public void resortDialogsWithStickToTop() { dialogs.clear();/*from w w w . j av a 2 s . c o m*/ dialogsServerOnly.clear(); ArrayList<TLRPC.TL_dialog> dialogsOther = new ArrayList<TLRPC.TL_dialog>(); ArrayList<TLRPC.TL_dialog> dialogsAll = new ArrayList<TLRPC.TL_dialog>(); dialogsOther.addAll(dialogs_dict.values()); dialogsAll.addAll(dialogs_dict.values()); for (TLRPC.TL_dialog d : dialogsStickToTop) { int index; for (index = 0; index < dialogs_dict.size(); index++) { if (dialogsAll.get(index).id == d.id) { dialogsOther.remove(d); dialogs.add(d); } } // not find if (index > dialogs_dict.size()) { dialogsStickToTop.remove(d); } } Collections.sort(dialogsOther, new Comparator<TLRPC.TL_dialog>() { @Override public int compare(TLRPC.TL_dialog tl_dialog, TLRPC.TL_dialog tl_dialog2) { if (tl_dialog.last_message_date == tl_dialog2.last_message_date) { return 0; } else if (tl_dialog.last_message_date < tl_dialog2.last_message_date) { return 1; } else { return -1; } } }); dialogs.addAll(dialogsOther); for (TLRPC.TL_dialog d : dialogs) { int high_id = (int) (d.id >> 32); if ((int) d.id != 0 && high_id != 1) { dialogsServerOnly.add(d); } } // dove add start // separate chat and user dialogs separateChatAndUserDialogs(); // dove add end }