001/* 002 * BioJava development code 003 * 004 * This code may be freely distributed and modified under the 005 * terms of the GNU Lesser General Public Licence. This should 006 * be distributed with the code. If you do not have a copy, 007 * see: 008 * 009 * http://www.gnu.org/copyleft/lesser.html 010 * 011 * Copyright for this code is held jointly by the individual 012 * authors. These should be listed in @author doc comments. 013 * 014 * For more information on the BioJava project and its aims, 015 * or to join the biojava-l mailing list, visit the home page 016 * at: 017 * 018 * http://www.biojava.org/ 019 * 020 */ 021package org.biojava.nbio.structure.align.xml; 022 023import java.io.IOException; 024import java.util.List; 025 026import javax.vecmath.Matrix4d; 027 028import org.biojava.nbio.core.util.PrettyXMLWriter; 029import org.biojava.nbio.structure.StructureIdentifier; 030import org.biojava.nbio.structure.align.multiple.Block; 031import org.biojava.nbio.structure.align.multiple.BlockSet; 032import org.biojava.nbio.structure.align.multiple.MultipleAlignment; 033import org.biojava.nbio.structure.align.multiple.MultipleAlignmentEnsemble; 034import org.biojava.nbio.structure.align.multiple.ScoresCache; 035import org.biojava.nbio.structure.align.multiple.util.MultipleAlignmentWriter; 036 037/** 038 * Helper methods to convert all the hierarchy levels of a MultipleAlignment 039 * into an XML format. 040 * <p> 041 * To convert a MultipleAlignment to an XML String use the 042 * {@link MultipleAlignmentWriter#toXML(MultipleAlignmentEnsemble)} method. 043 * 044 * @author Aleix Lafita 045 * @since 4.1.1 046 * 047 */ 048public class MultipleAlignmentXMLConverter { 049 050 public synchronized static void printXMLensemble(PrettyXMLWriter xml, 051 MultipleAlignmentEnsemble ensemble) throws IOException { 052 053 xml.openTag("MultipleAlignmentEnsemble"); 054 055 printXMLheader(xml,ensemble); 056 057 for (MultipleAlignment msa:ensemble.getMultipleAlignments()){ 058 printXMLalignment(xml,msa); 059 } 060 printXMLscoresCache(xml,ensemble); 061 062 xml.closeTag("MultipleAlignmentEnsemble"); 063 } 064 065 public synchronized static void printXMLalignment(PrettyXMLWriter xml, 066 MultipleAlignment msa) throws IOException { 067 068 xml.openTag("MultipleAlignment"); 069 070 for(BlockSet bs:msa.getBlockSets()) { 071 printXMLblockSet(xml, bs); 072 } 073 printXMLscoresCache(xml,msa); 074 075 xml.closeTag("MultipleAlignment"); 076 } 077 078 public synchronized static void printXMLblockSet(PrettyXMLWriter xml, 079 BlockSet bs) throws IOException { 080 081 xml.openTag("BlockSet"); 082 083 for(Block b:bs.getBlocks()) { 084 printXMLblock(xml, b); 085 } 086 087 if (bs.getTransformations() != null){ 088 for(Matrix4d t:bs.getTransformations()){ 089 printXMLmatrix4d(xml, t); 090 } 091 } 092 printXMLscoresCache(xml,bs); 093 094 xml.closeTag("BlockSet"); 095 } 096 097 public synchronized static void printXMLblock(PrettyXMLWriter xml, 098 Block b) throws IOException { 099 100 xml.openTag("Block"); 101 List<List<Integer>> alignment = b.getAlignRes(); 102 103 for (int pos=0;pos<alignment.get(0).size(); pos++){ 104 105 xml.openTag("eqr"+pos); 106 for (int str=0; str<alignment.size(); str++){ 107 xml.attribute("str"+(str+1),alignment.get(str).get(pos)+""); 108 } 109 xml.closeTag("eqr"+pos); 110 } 111 printXMLscoresCache(xml,b); 112 113 xml.closeTag("Block"); 114 } 115 116 public synchronized static void printXMLmatrix4d(PrettyXMLWriter xml, 117 Matrix4d transform) throws IOException { 118 119 if (transform == null) return; 120 xml.openTag("Matrix4d"); 121 122 for (int x=0;x<4;x++){ 123 for (int y=0;y<4;y++){ 124 String key = "mat"+(x+1)+(y+1); 125 String value = transform.getElement(x,y)+""; 126 xml.attribute(key,value); 127 } 128 } 129 xml.closeTag("Matrix4d"); 130 } 131 132 public synchronized static void printXMLscoresCache(PrettyXMLWriter xml, 133 ScoresCache cache) throws IOException { 134 135 if (cache == null) return; 136 xml.openTag("ScoresCache"); 137 138 //We need a new tag for every score, we don't know their names 139 for (String score:cache.getScores()){ 140 xml.openTag(score); 141 String value = cache.getScore(score)+""; 142 xml.attribute("value", value); 143 xml.closeTag(score); 144 } 145 xml.closeTag("ScoresCache"); 146 } 147 148 public synchronized static void printXMLheader(PrettyXMLWriter xml, 149 MultipleAlignmentEnsemble ensemble) throws IOException{ 150 151 //Creation properties 152 xml.attribute("Algorithm", ensemble.getAlgorithmName()); 153 xml.attribute("Version", ensemble.getVersion()); 154 xml.attribute("IOTime", ensemble.getIoTime()+""); 155 xml.attribute("CalculationTime", ensemble.getCalculationTime()+""); 156 157 //Structure Identifiers 158 xml.openTag("Structures"); 159 for (int i=0; i<ensemble.size(); i++){ 160 StructureIdentifier name = ensemble.getStructureIdentifiers().get(i); 161 xml.attribute("name"+(i+1), name.getIdentifier()); 162 } 163 xml.closeTag("Structures"); 164 } 165}