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

org.biojava.nbio.structure.xtal.SpaceGroup Maven / Gradle / Ivy

There is a newer version: 7.1.3
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.xtal;

import org.biojava.nbio.structure.jama.EigenvalueDecomposition;
import org.biojava.nbio.structure.jama.Matrix;
import org.biojava.nbio.structure.xtal.io.TransfAlgebraicAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.vecmath.AxisAngle4d;
import javax.vecmath.Matrix3d;
import javax.vecmath.Matrix4d;
import javax.vecmath.Vector3d;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Marshaller;
import jakarta.xml.bind.annotation.XmlAccessType;
import jakarta.xml.bind.annotation.XmlAccessorType;
import jakarta.xml.bind.annotation.XmlRootElement;
import jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * A crystallographic space group. We store the standard numeric identifier,
 * the international short symbol and the transformations corresponding to
 * each space group (as Matrix4ds and in algebraic notation).
 * The information for all (protein crystallography) space groups can be
 * parsed from the XML file in the resource directory.
 *
 * See: http://en.wikipedia.org/wiki/Space_group
 *
 * @author duarte_j
 * @see SymoplibParser
 */
@XmlRootElement(name = "SpaceGroup", namespace ="http://www.biojava.org")
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
public class SpaceGroup implements Serializable {

	private static final long serialVersionUID = 1L;
	private static final Logger logger = LoggerFactory.getLogger(SpaceGroup.class);


	private static final Pattern splitPat1 = Pattern.compile("((?:[+-]?[XYZ])+)([+-][0-9/.]+)");
	private static final Pattern splitPat2 = Pattern.compile("([+-]?[0-9/.]+)((?:[+-][XYZ])+)");
	private static final Pattern coordPat = Pattern.compile("(?:([+-])?([XYZ]))+?"); // the last +? is for ungreedy matching
	private static final Pattern transCoefPat = Pattern.compile("([-+]?[0-9.]+)(?:/([0-9.]+))?");

	private static final Pattern nonEnantPat = Pattern.compile("[-abcmnd]");

	protected static final double DELTA=0.0000001;

	private  int id;
	private  int multiplicity;
	private  int primitiveMultiplicity;
	private  String shortSymbol;
	private  String altShortSymbol;
	private  List transformations;
	private  List transfAlgebraic;
	private  Vector3d[] cellTranslations; // in space groups I, C, F or H there are pure cell translations corresponding to recenterings

	private AxisAngle4d[] axisAngles;

	private int[] axisTypes; // indices of array are transformIds

	private BravaisLattice bravLattice;

	@SuppressWarnings("unused")
	private SpaceGroup(){
		// required by JAXB

	}

	public SpaceGroup(int id, int multiplicity, int primitiveMultiplicity, String shortSymbol, String altShortSymbol, BravaisLattice bravLattice) {
		this.id = id;
		this.multiplicity = multiplicity;
		this.primitiveMultiplicity = primitiveMultiplicity;
		this.shortSymbol = shortSymbol;
		this.altShortSymbol = altShortSymbol;
		transformations = new ArrayList(multiplicity);
		transfAlgebraic = new ArrayList<>(multiplicity);
		cellTranslations = new Vector3d[multiplicity/primitiveMultiplicity];
		this.bravLattice = bravLattice;
	}

	/**
	 * Get the space group for the given international short name, using
	 * the PDB format, e.g. 'P 21 21 21' or 'C 1 c 1'
	 * @param shortName
	 * @return the SpaceGroup or null if the shortName is not valid
	 * @see SymoplibParser#getSpaceGroup(String)
	 */
	public static SpaceGroup parseSpaceGroup(String shortName) {
		return SymoplibParser.getSpaceGroup(shortName);
	}

	public void addTransformation(String transfAlgebraic) {
		this.transfAlgebraic.add(transfAlgebraic);
		this.transformations.add(getMatrixFromAlgebraic(transfAlgebraic));
	}

	protected void initializeCellTranslations() {
		if ( cellTranslations != null && cellTranslations.length >0) {
			// we already initialized this
			return;
		}
		cellTranslations = new Vector3d[multiplicity/primitiveMultiplicity];
		cellTranslations[0] = new Vector3d(0,0,0);

		if ( transformations == null){
			logger.warn("transformations == null" + this.toXML());
		}

		if (multiplicity==primitiveMultiplicity) {
			return;
		}
		int fold = multiplicity/primitiveMultiplicity;



		for (int n=1;n getTransformations() {
		List transfs = new ArrayList();
		for (int i=1;i0?"":"-"):String.format(Locale.US, "%4.2f",c));
		} else {
			return (deltaComp(Math.abs(c),1,DELTA)?(c>0?"+":"-"):String.format(Locale.US, "%+4.2f",c));
		}
	}

	private static String formatTransCoef(double c) {
		if (Math.abs((Math.rint(c)-c))0) {
			switch (trace) {
			case 3:
				axisType=1;
				break;
			case -1:
				axisType=2;
				break;
			case 0:
				axisType=3;
				break;
			case 1:
				axisType=4;
				break;
			case 2:
				axisType=6;
				break;
			default:
				throw new RuntimeException("Trace of transform does not correspond to one of the expected types. This is most likely a bug");
			}
		} else {
			switch (trace) {
			case -3:
				axisType=-1;
				break;
			case 1:
				axisType=-2;
				break;
			case 0:
				axisType=-3;
				break;
			case -1:
				axisType=-4;
				break;
			case -2:
				axisType=-6;
				break;
			default:
				throw new RuntimeException("Trace of transform does not correspond to one of the expected types. This is most likely a bug");
			}
		}
		return axisType;
	}

	@Override
	public String toString() {
		return getShortSymbol();
	}

	public String toXML(){


		JAXBContext jaxbContextStringSortedSet = null;

		try {
			jaxbContextStringSortedSet= JAXBContext.newInstance(SpaceGroup.class);
		} catch (JAXBException e){
			logger.error("Error converting to XML",e);
			return null;
		}

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		PrintStream ps = new PrintStream(baos);


		try {

			Marshaller m = jaxbContextStringSortedSet.createMarshaller();

			m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

			m.marshal( this, ps);


		} catch (JAXBException e){
			logger.error("Error converting to XML",e);
		}

		return baos.toString();
	}

	//@XmlElementWrapper(name="transfAlgebraicList", namespace="http://www.biojava.org")
	//@XmlElement
	@XmlJavaTypeAdapter(TransfAlgebraicAdapter.class)
	public List getTransfAlgebraic() {
		return transfAlgebraic;
	}

	public void setTransfAlgebraic(List transfAlgebraic) {
		//System.out.println("setting transfAlgebraic " + transfAlgebraic);
		if ( transformations == null || transformations.size() == 0)
			transformations = new ArrayList(transfAlgebraic.size());

		if ( this.transfAlgebraic == null || this.transfAlgebraic.size() == 0)
			this.transfAlgebraic = new ArrayList<>(transfAlgebraic.size());

		for ( String transf : transfAlgebraic){
			addTransformation(transf);
		}
	}


	public int[] getAxisTypes() {
		return axisTypes;
	}

	public void setAxisTypes(int[] axisTypes) {
		this.axisTypes = axisTypes;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public static Pattern getSplitpat1() {
		return splitPat1;
	}

	public static Pattern getSplitpat2() {
		return splitPat2;
	}

	public static Pattern getCoordpat() {
		return coordPat;
	}

	public static Pattern getTranscoefpat() {
		return transCoefPat;
	}

	public static Pattern getNonenantpat() {
		return nonEnantPat;
	}

	public static double getDelta() {
		return DELTA;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setMultiplicity(int multiplicity) {

		this.multiplicity = multiplicity;
	}

	public void setPrimitiveMultiplicity(int primitiveMultiplicity) {
		this.primitiveMultiplicity = primitiveMultiplicity;
	}

	public void setShortSymbol(String shortSymbol) {
		this.shortSymbol = shortSymbol;
	}

	public void setAltShortSymbol(String altShortSymbol) {
		this.altShortSymbol = altShortSymbol;
	}


	public void setBravLattice(BravaisLattice bravLattice) {
		this.bravLattice = bravLattice;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy