/* * This code may be freely distributed and modified under the * terms of the GNU Lesser General Public Licence. This should * be distributed with the code. If you do not have a copy, * see: * * http://www.gnu.org/copyleft/lesser.html * * Copyright for this code is held jointly by the individual * authors. These should be listed in @author doc comments. * * For more information on the BioJava project and its aims, * or to join the biojava-l mailing list, visit the home page * at: * * http://www.biojava.org/ * * Created on 26.04.2004 * @author Andreas Prlic * */ package org.biojava.bio.structure.io; import java.io.IOException; import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.biojava.bio.structure.Atom; import org.biojava.bio.structure.Chain; import org.biojava.bio.structure.DBRef; import org.biojava.bio.structure.Group; import org.biojava.bio.structure.PDBHeader; import org.biojava.bio.structure.SSBond; import org.biojava.bio.structure.Structure; import org.biojava.bio.structure.StructureException; import org.biojava.utils.xml.XMLWriter; /** Methods to convert a structure object into different file formats. * @author Andreas Prlic * @since 1.4 */ public class FileConvert { Structure structure ; boolean printConnections; /** * Constructs a FileConvert object. * * @param struc a Structure object */ public FileConvert(Structure struc) { structure = struc ; printConnections = true; } /** align a string to the right * length is the total length the new string should take, inlcuding spaces on the left * incredible that this tool is missing in java !!! */ private String alignRight(String input, int length){ int n = input.length(); if ( n >= length) return input; String spaces = " " ; int diff = length - n ; StringBuffer s = new StringBuffer(); s.append(spaces.substring(0,diff)); s.append(input); return s.toString(); } private String alignLeft(String input, int length){ if (input.length() >= length) { return input; } String spaces = " " ; input += spaces.substring(0, length - input.length() ); return input; } /** returns if the Connections should be added * default is true; * @return if the printConnections flag is set */ public boolean doPrintConnections() { return printConnections; } /** enable/disable printing of connections * connections are sometimes buggy in PDB files * so there are some cases where one might turn this off. * @param printConnections */ public void setPrintConnections(boolean printConnections) { this.printConnections = printConnections; } /** prints the connections in PDB style * * Thanks to Tamas Horvath for this one */ private String printPDBConnections(){ String newline = System.getProperty("line.separator"); StringBuffer str = new StringBuffer(); List cons = (ArrayList) structure.getConnections(); for (int cnr = 0; cnr PDB files have "." ! DecimalFormat d3 = (DecimalFormat)NumberFormat.getInstance(java.util.Locale.UK); d3.setMaximumIntegerDigits(3); d3.setMinimumFractionDigits(3); d3.setMaximumFractionDigits(3); DecimalFormat d2 = (DecimalFormat)NumberFormat.getInstance(java.util.Locale.UK); d2.setMaximumIntegerDigits(2); d2.setMinimumFractionDigits(2); d2.setMaximumFractionDigits(2); // TODO: print all the PDB header informaton in PDB style // some objects (PDBHeader, Compound) are still missing // PDBHeader header = structure.getPDBHeader(); header.toPDB(str); for (SSBond ssbond : structure.getSSBonds()){ ssbond.toPDB(str); str.append(newline); } for (DBRef dbref : structure.getDBRefs()){ dbref.toPDB(str); str.append(newline); } // // print the atom records // // do for all models int nrModels = structure.nrModels() ; if ( structure.isNmr()) { str.append("EXPDTA NMR, "+ nrModels+" STRUCTURES"+newline) ; } for (int m = 0 ; m < nrModels ; m++) { List model = structure.getModel(m); // todo support NMR structures ... if ( structure.isNmr()) { str.append("MODEL " + (m+1)+ newline); } // do for all chains int nrChains = model.size(); for ( int c =0; c> cons = structure.getConnections(); for (int cnr = 0; cnr
  • atomserial (mandatory) : Atom serial number
  • bond1 .. bond4 (optional): Serial number of bonded atom
  • hydrogen1 .. hydrogen4 (optional):Serial number of hydrogen bonded atom
  • salt1 .. salt2 (optional): Serial number of salt bridged atom */ Map con = (Map)cons.get(cnr); Integer as = (Integer)con.get("atomserial"); int atomserial = as.intValue(); List atomids = new ArrayList() ; // test salt and hydrogen first // if (con.containsKey("salt1")) atomids.add(con.get("salt1")); if (con.containsKey("salt2")) atomids.add(con.get("salt2")); if (atomids.size()!=0){ addConnection(xw,"salt",atomserial,atomids); atomids = new ArrayList() ; } if (con.containsKey("hydrogen1")) atomids.add(con.get("hydrogen1")); if (con.containsKey("hydrogen2")) atomids.add(con.get("hydrogen2")); if (con.containsKey("hydrogen3")) atomids.add(con.get("hydrogen3")); if (con.containsKey("hydrogen4")) atomids.add(con.get("hydrogen4")); if (atomids.size()!=0){ addConnection(xw,"hydrogen",atomserial,atomids); atomids = new ArrayList() ; } if (con.containsKey("bond1")) atomids.add(con.get("bond1")); if (con.containsKey("bond2")) atomids.add(con.get("bond2")); if (con.containsKey("bond3")) atomids.add(con.get("bond3")); if (con.containsKey("bond4")) atomids.add(con.get("bond4")); if (atomids.size()!=0){ addConnection(xw,"bond",atomserial,atomids); } } } } private void addConnection(XMLWriter xw,String connType, int atomserial, List atomids){ try{ xw.openTag("connect"); xw.attribute("atomSerial",Integer.toString(atomserial)); xw.attribute("type",connType); for (int i=0;i