
org.biojava.nbio.structure.align.xml.MultipleAlignmentXMLParser Maven / Gradle / Ivy
Show all versions of biojava-structure Show documentation
/*
* 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 java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import javax.vecmath.Matrix4d;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.biojava.nbio.structure.StructureIdentifier;
import org.biojava.nbio.structure.align.client.StructureName;
import org.biojava.nbio.structure.align.multiple.Block;
import org.biojava.nbio.structure.align.multiple.BlockImpl;
import org.biojava.nbio.structure.align.multiple.BlockSet;
import org.biojava.nbio.structure.align.multiple.BlockSetImpl;
import org.biojava.nbio.structure.align.multiple.MultipleAlignment;
import org.biojava.nbio.structure.align.multiple.MultipleAlignmentEnsemble;
import org.biojava.nbio.structure.align.multiple.MultipleAlignmentEnsembleImpl;
import org.biojava.nbio.structure.align.multiple.MultipleAlignmentImpl;
import org.biojava.nbio.structure.align.multiple.ScoresCache;
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 org.xml.sax.SAXException;
/**
* Parse an XML file representing a {@link MultipleAlignmentEnsemble}, so
* that the original alignment can be recovered.
*
* Atoms need to be downloaded, either manually or using the method
* getAtomArrays() in MultipleAlignmentEnsemble.
*
* @author Aleix Lafita
* @since 4.1.1
*
*/
public class MultipleAlignmentXMLParser {
/**
* Creates a list of MultipleAlignment ensembles from an XML file.
* This recovers only the information that was previously stored.
* If the Atoms are needed, the method getAtomArrays() will automatically
* download the structures from the stored structure identifiers.
*
* @param xml String XML file containing any number of ensembles
* @return List of ensembles in the file
* @throws ParserConfigurationException
* @throws SAXException
* @throws IOException
*/
public static List parseXMLfile(String xml)
throws ParserConfigurationException, SAXException, IOException {
List ensembles =
new ArrayList();
//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);
doc.getDocumentElement().normalize();
//In case there are more than one ensemble in the document (generalize)
NodeList listOfEnsembles =
doc.getElementsByTagName("MultipleAlignmentEnsemble");
//Explore all the ensembles, if multiple ones
for (int e=0; e transforms = new ArrayList();
NodeList children = root.getChildNodes();
for (int i=0; i> alignRes = new ArrayList>();
b.setAlignRes(alignRes);
NodeList children = root.getChildNodes();
for(int i=0; i());
}
String residue = node.getTextContent();
if (residue.equals("null")){
alignRes.get(str-1).add(null);
} else {
alignRes.get(str-1).add(new Integer(residue));
}
str++;
node = atts.getNamedItem("str"+str);
}
}
else if (child.getNodeName().equals("ScoresCache")){
parseScoresCache(child, b);
}
}
return b;
}
public static Matrix4d parseMatrix4d(Node node) {
Matrix4d m = new Matrix4d();
NamedNodeMap atts = node.getAttributes();
for (int x=0; x<4; x++){
for (int y=0; y<4; y++){
String key = "mat"+(x+1)+(y+1);
String value = atts.getNamedItem(key).getTextContent();
m.setElement(x, y, new Double(value));
}
}
return m;
}
public static void parseScoresCache(Node root, ScoresCache cache) {
NodeList children = root.getChildNodes();
for (int i=0; i names = new ArrayList();
ensemble.setStructureIdentifiers(names);
NamedNodeMap atts = root.getAttributes();
int str = 1;
Node node = atts.getNamedItem("name"+str);
while (node!=null){
String name = node.getTextContent();
names.add(new StructureName(name));
str++;
node = atts.getNamedItem("name"+str);
}
}
}