All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.biojava.nbio.structure.align.xml.PdbPairXMLConverter Maven / Gradle / Ivy

There is a newer version: 7.2.2
Show newest version
/*
 *                    BioJava development code
 *
 * 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/
 *
 */
package org.biojava.nbio.structure.align.xml;

import org.biojava.nbio.structure.align.client.PdbPair;
import org.biojava.nbio.structure.align.fatcat.FatCatRigid;
import org.biojava.nbio.core.util.PrettyXMLWriter;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.SortedSet;
import java.util.TreeSet;

public class PdbPairXMLConverter {


	public static final String DEFAULT_METHOD_NAME = FatCatRigid.algorithmName;

	public static PdbPairsMessage convertXMLtoPairs(String xml) {
		SortedSet  pairs = new TreeSet();
		PdbPairsMessage message = new PdbPairsMessage();
		try
		{
			//Convert string to XML document
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = factory.newDocumentBuilder();
			InputSource inStream = new InputSource();
			inStream.setCharacterStream(new StringReader(xml));
			Document doc = db.parse(inStream);

			// normalize text representation
			doc.getDocumentElement().normalize();

			NodeList algorithms =  doc.getElementsByTagName("pairs");
			//System.out.println(algorithms.getLength());
			for(int i=0; i pairs, String method){
		StringWriter sw = new StringWriter();
		PrintWriter writer = new PrintWriter(sw);

		if (method == null){
			method = DEFAULT_METHOD_NAME;
		}


		PrettyXMLWriter xml = new PrettyXMLWriter(writer);
		try {


			xml.openTag("pairs");
			xml.attribute("algorithm", method);
			for ( PdbPair pair : pairs){
				xml.openTag("pair");
				xml.attribute("name1", pair.getName1());
				xml.attribute("name2", pair.getName2());
				xml.closeTag("pair");
			}
			xml.closeTag("pairs");
		} catch(IOException ex){
			ex.printStackTrace();
		}

		return sw.toString();
	}


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy