Java Code Examples for org.biojava.nbio.structure.align.model.AFPChain#setOptAln()

The following examples show how to use org.biojava.nbio.structure.align.model.AFPChain#setOptAln() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AlignmentTools.java    From biojava with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * @param afpChain Input afpchain. UNMODIFIED
 * @param ca1
 * @param ca2
 * @param optLens
 * @param optAln
 * @return A NEW AfpChain based off the input but with the optAln modified
 * @throws StructureException if an error occured during superposition
 */
public static AFPChain replaceOptAln(AFPChain afpChain, Atom[] ca1, Atom[] ca2,
									 int blockNum, int[] optLens, int[][][] optAln) throws StructureException {
	int optLength = 0;
	for( int blk=0;blk<blockNum;blk++) {
		optLength += optLens[blk];
	}

	//set everything
	AFPChain refinedAFP = (AFPChain) afpChain.clone();
	refinedAFP.setOptLength(optLength);
	refinedAFP.setBlockSize(optLens);
	refinedAFP.setOptLen(optLens);
	refinedAFP.setOptAln(optAln);
	refinedAFP.setBlockNum(blockNum);

	//TODO recalculate properties: superposition, tm-score, etc
	Atom[] ca2clone = StructureTools.cloneAtomArray(ca2); // don't modify ca2 positions
	AlignmentTools.updateSuperposition(refinedAFP, ca1, ca2clone);

	AFPAlignmentDisplay.getAlign(refinedAFP, ca1, ca2clone);
	return refinedAFP;
}
 
Example 2
Source File: OptimalCECPMain.java    From biojava with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Sometimes it's convenient to store an alignment using java collections,
 * where <tt>blocks.get(blockNum).get(0).get(pos)</tt> specifies the aligned
 * residue at position <i>pos</i> of block <i>blockNum</i> of the first
 * protein.
 *
 * This method takes such a collection and stores it into the afpChain's
 * {@link AFPChain#setOptAln(int[][][]) optAln}, setting the associated
 * length variables as well.
 *
 * @param afpChain
 * @param blocks
 */
private static void assignOptAln(AFPChain afpChain, List<List<List<Integer>>> blocks)
{

	int[][][] optAln = new int[blocks.size()][][];
	int[] optLen = new int[blocks.size()];
	int optLength = 0;
	int numBlocks = blocks.size();

	for(int block = 0; block < numBlocks; block++) {
		// block should be 2xN rectangular
		assert(blocks.get(block).size() == 2);
		assert( blocks.get(block).get(0).size() == blocks.get(block).get(1).size());

		optLen[block] = blocks.get(block).get(0).size();
		optLength+=optLen[block];

		optAln[block] = new int[][] {
				new int[optLen[block]],
				new int[optLen[block]]
		};
		for(int pos = 0; pos < optLen[block]; pos++) {
			optAln[block][0][pos] = blocks.get(block).get(0).get(pos);
			optAln[block][1][pos] = blocks.get(block).get(1).get(pos);
		}
	}


	afpChain.setBlockNum(numBlocks);
	afpChain.setOptAln(optAln);
	afpChain.setOptLen(optLen);
	afpChain.setOptLength(optLength);

	// TODO I don't know what these do. Should they be set?
	//afpChain.setBlockSize(blockSize);
	//afpChain.setBlockResList(blockResList);
	//afpChain.setChainLen(chainLen);

}
 
Example 3
Source File: CeCPMainTest.java    From biojava with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Creates a minimal AFPChain from the specified alignment and proteins
 * @param dupAlign
 * @param ca1
 * @param ca2
 * @return
 */
private AFPChain makeDummyAFPChain(int[][][] dupAlign, Atom[] ca1,Atom[] ca2) {
	AFPChain afp = new AFPChain(AFPChain.UNKNOWN_ALGORITHM);
	afp.setOptAln(dupAlign);
	afp.setOptLength(dupAlign[0][1].length);
	afp.setCa1Length(ca1.length);
	afp.setCa2Length(ca2.length);
	afp.setBlockNum(1);
	afp.setOptLen(new int[] {dupAlign[0][1].length});
	return afp;
}
 
Example 4
Source File: CEMirrorSymm.java    From symmetry with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static void reverseOptAln(AFPChain afpChain) {
	int ca2len = afpChain.getCa2Length();

	int[][][] optAln = afpChain.getOptAln();
	int[] optLen = afpChain.getOptLen();

	for (int block = 0; block < afpChain.getBlockNum(); block++) {
		for (int pos = 0; pos < optLen[block]; pos++) {
			optAln[block][1][pos] = ca2len - 1 - optAln[block][1][pos];
		}
	}

	afpChain.setOptAln(optAln);
}
 
Example 5
Source File: AFPChainXMLParser.java    From biojava with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**  replace the PDB res nums with atom positions:
 *
 * @param afpChain
 * @param ca1
 * @param ca2
 */
public static void rebuildAFPChain(AFPChain afpChain, Atom[] ca1, Atom[] ca2){

	if ( afpChain.getAlgorithmName() == null) {
		afpChain.setAlgorithmName(DEFAULT_ALGORITHM_NAME);
	}
	if ( afpChain.getVersion() == null){
		afpChain.setVersion("1.0");
	}

	int blockNum  = afpChain.getBlockNum();
	int ca1Length = afpChain.getCa1Length();
	int ca2Length = afpChain.getCa2Length();

	int minLength = Math.min(ca1Length, ca2Length);
	int[][][] optAln = new int[blockNum][2][minLength];

	int[][][] blockResList = afpChain.getBlockResList();
	if ( blockResList == null){
		blockResList = new int[blockNum][2][minLength];
	}
	int[] optLen = afpChain.getOptLen();

	String[][][] pdbAln = afpChain.getPdbAln();
	int[] verifiedOptLen = null;
	if ( optLen != null)
	  verifiedOptLen = afpChain.getOptLen().clone();
	else {
		logger.warn("did not find optimal alignment, building up empty alignment.");
		optLen = new int[1];
		optLen[0] = 0;
	}
	for (int blockNr = 0 ; blockNr < blockNum ; blockNr++){

		//System.out.println("got block " + blockNr + " size: " + optLen[blockNr]);
		int verifiedEQR = -1;
		for ( int eqrNr = 0 ; eqrNr < optLen[blockNr] ; eqrNr++ ){
			String pdbResnum1 = pdbAln[blockNr][0][eqrNr];
			String pdbResnum2 = pdbAln[blockNr][1][eqrNr];

			//System.out.println(blockNr + " " + eqrNr + " got resnum: " + pdbResnum1 + " " + pdbResnum2);
			String[] spl1 = pdbResnum1.split(":");
			String[] spl2 = pdbResnum2.split(":");

			String chain1 = spl1[0];
			String pdbres1 = spl1[1];

			String chain2 = spl2[0];
			String pdbres2 = spl2[1];

			int pos1 = getPositionForPDBresunm(pdbres1,chain1,ca1);
			int pos2 = getPositionForPDBresunm(pdbres2,chain2,ca2);

			if ( pos1 == -1 || pos2 == -1 ){
				// this can happen when parsing old files that contained Calcium atoms...
				logger.warn("pos1: {} (residue {}), pos2: {} (residue {}), should never be -1. Probably parsing an old file.",
						pos1, pdbResnum1, pos2, pdbResnum2);
				verifiedOptLen[blockNr]-- ;
				continue;
			}

			verifiedEQR++;
			//System.out.println(blockNr + " " + eqrNr + " " + pos1 + " " + pos2);
			optAln[blockNr][0][verifiedEQR] = pos1;
			optAln[blockNr][1][verifiedEQR] = pos2;
			blockResList[blockNr][0][verifiedEQR] = pos1;
			blockResList[blockNr][1][verifiedEQR] = pos2;
		}
	}

	afpChain.setOptLen(verifiedOptLen);
	afpChain.setOptAln(optAln);
	afpChain.setBlockResList(blockResList);
	// build up alignment image:
	AFPAlignmentDisplay.getAlign(afpChain, ca1, ca2);


}
 
Example 6
Source File: CeCPMain.java    From biojava with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Swaps the order of structures in an AFPChain
 * @param a
 * @return
 */
public AFPChain invertAlignment(AFPChain a) {
	String name1 = a.getName1();
	String name2 = a.getName2();
	a.setName1(name2);
	a.setName2(name1);

	int len1 = a.getCa1Length();
	a.setCa1Length( a.getCa2Length() );
	a.setCa2Length( len1 );

	int beg1 = a.getAlnbeg1();
	a.setAlnbeg1(a.getAlnbeg2());
	a.setAlnbeg2(beg1);

	char[] alnseq1 = a.getAlnseq1();
	a.setAlnseq1(a.getAlnseq2());
	a.setAlnseq2(alnseq1);

	Matrix distab1 = a.getDisTable1();
	a.setDisTable1(a.getDisTable2());
	a.setDisTable2(distab1);

	int[] focusRes1 = a.getFocusRes1();
	a.setFocusRes1(a.getFocusRes2());
	a.setFocusRes2(focusRes1);

	//What are aftIndex and befIndex used for? How are they indexed?
	//a.getAfpAftIndex()


	String[][][] pdbAln = a.getPdbAln();
	if( pdbAln != null) {
		for(int block = 0; block < a.getBlockNum(); block++) {
			String[] paln1 = pdbAln[block][0];
			pdbAln[block][0] = pdbAln[block][1];
			pdbAln[block][1] = paln1;
		}
	}

	int[][][] optAln = a.getOptAln();
	if( optAln != null ) {
		for(int block = 0; block < a.getBlockNum(); block++) {
			int[] aln1 = optAln[block][0];
			optAln[block][0] = optAln[block][1];
			optAln[block][1] = aln1;
		}
	}
	a.setOptAln(optAln); // triggers invalidate()

	Matrix distmat = a.getDistanceMatrix();
	if(distmat != null)
		a.setDistanceMatrix(distmat.transpose());


	// invert the rotation matrices
	Matrix[] blockRotMat = a.getBlockRotationMatrix();
	Atom[] shiftVec = a.getBlockShiftVector();
	if( blockRotMat != null) {
		for(int block = 0; block < a.getBlockNum(); block++) {
			if(blockRotMat[block] != null) {
				// if y=x*A+b, then x=y*inv(A)-b*inv(A)
				blockRotMat[block] = blockRotMat[block].inverse();

				Calc.rotate(shiftVec[block],blockRotMat[block]);
				shiftVec[block] = Calc.invert(shiftVec[block]);
			}
		}
	}

	return a;
}
 
Example 7
Source File: AlignmentTools.java    From biojava with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * It replaces an optimal alignment of an AFPChain and calculates all the new alignment scores and variables.
 */
public static AFPChain replaceOptAln(int[][][] newAlgn, AFPChain afpChain, Atom[] ca1, Atom[] ca2) throws StructureException {

	//The order is the number of groups in the newAlgn
	int order = newAlgn.length;

	//Calculate the alignment length from all the subunits lengths
	int[] optLens = new int[order];
	for(int s=0;s<order;s++) {
		optLens[s] = newAlgn[s][0].length;
	}
	int optLength = 0;
	for(int s=0;s<order;s++) {
		optLength += optLens[s];
	}

	//Create a copy of the original AFPChain and set everything needed for the structure update
	AFPChain copyAFP = (AFPChain) afpChain.clone();

	//Set the new parameters of the optimal alignment
	copyAFP.setOptLength(optLength);
	copyAFP.setOptLen(optLens);
	copyAFP.setOptAln(newAlgn);

	//Set the block information of the new alignment
	copyAFP.setBlockNum(order);
	copyAFP.setBlockSize(optLens);
	copyAFP.setBlockResList(newAlgn);
	copyAFP.setBlockResSize(optLens);
	copyAFP.setBlockGap(calculateBlockGap(newAlgn));

	//Recalculate properties: superposition, tm-score, etc
	Atom[] ca2clone = StructureTools.cloneAtomArray(ca2); // don't modify ca2 positions
	AlignmentTools.updateSuperposition(copyAFP, ca1, ca2clone);

	//It re-does the sequence alignment strings from the OptAlgn information only
	copyAFP.setAlnsymb(null);
	AFPAlignmentDisplay.getAlign(copyAFP, ca1, ca2clone);

	return copyAFP;
}
 
Example 8
Source File: TestAFPChainConversion.java    From biojava with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Test
public void testAFPconversion() throws Exception{

	//Fill an AFPChain with the general information
	AFPChain afp = new AFPChain("algorithm");
	afp.setName1("name1");
	afp.setName2("name2");
	afp.setVersion("1.0");
	afp.setCalculationTime(System.currentTimeMillis());
	//Generate a optimal alignment with 3 blocks and 5 residues per block
	int[][][] optAln = new int[3][][];
	for (int b=0; b<optAln.length; b++){
		int[][] block = new int[2][];
		for (int c=0; c<block.length; c++){
			int[] residues = {b+5,b+6,b+7,b+8,b+9};
			block[c] = residues;
		}
		optAln[b] = block;
	}
	afp.setOptAln(optAln);
	afp.setBlockNum(optAln.length);
	//Set the rotation matrix and shift to random numbers
	double[][] mat = {{0.13,1.5,0.84},{1.3,0.44,2.3},{1.0,1.2,2.03}};
	Matrix rot = new Matrix(mat);
	Atom shift = new AtomImpl();
	shift.setX(0.44);
	shift.setY(0.21);
	shift.setZ(0.89);
	Matrix[] blockRot = {rot,rot,rot};
	afp.setBlockRotationMatrix(blockRot);
	Atom[] blockShift = {shift,shift,shift};
	afp.setBlockShiftVector(blockShift);

	//Convert the AFPChain into a MultipleAlignment (without Atoms)
	MultipleAlignmentEnsemble ensemble =
			new MultipleAlignmentEnsembleImpl(afp,null,null,true);
	MultipleAlignment msa = ensemble.getMultipleAlignment(0);

	//Test for all the information
	assertEquals(afp.getName1(),ensemble.getStructureIdentifiers().get(0).getIdentifier());
	assertEquals(afp.getName2(), ensemble.getStructureIdentifiers().get(1).getIdentifier());
	assertEquals(afp.getAlgorithmName(), ensemble.getAlgorithmName());
	assertEquals(afp.getVersion(),ensemble.getVersion());
	assertTrue(ensemble.getCalculationTime().equals(
			afp.getCalculationTime()));
	assertEquals(afp.getBlockNum(), msa.getBlockSets().size());
	for (int b = 0; b<afp.getBlockNum(); b++){
		assertEquals(Calc.getTransformation(
				afp.getBlockRotationMatrix()[b],
				afp.getBlockShiftVector()[b]),
				msa.getBlockSet(b).getTransformations().get(1));
	}

	//Test for the scores
	assertEquals(msa.getScore(MultipleAlignmentScorer.CE_SCORE),
			(Double) afp.getAlignScore());
	assertEquals(msa.getScore(MultipleAlignmentScorer.AVGTM_SCORE),
			(Double) afp.getTMScore());
	assertEquals(msa.getScore(MultipleAlignmentScorer.RMSD),
			(Double) afp.getTotalRmsdOpt());


	//Test for the optimal alignment
	for (int b=0; b<3; b++){
		for (int c=0; c<2; c++){
			for (int res=0; res<5; res++){
				Integer afpRes = afp.getOptAln()[b][c][res];
				assertEquals(afpRes, msa.getBlock(b).
						getAlignRes().get(c).get(res));
			}
		}
	}
}
 
Example 9
Source File: TestSimilarityCalc.java    From biojava with GNU Lesser General Public License v2.1 2 votes vote down vote up
@Test
public void testSimilarityDisplay(){

	String name1 = "1CDG.A";
	String name2 = "1TIM.A";

	AtomCache cache = new AtomCache();

	Structure structure1 = null;
	Structure structure2 = null;

	try {

		StructureAlignment algorithm = StructureAlignmentFactory.getAlgorithm(SmithWaterman3Daligner.algorithmName);

		SmithWaterman3DParameters params = new SmithWaterman3DParameters();

		structure1 = cache.getStructure(name1);
		structure2 = cache.getStructure(name2);

		Atom[] ca1 = StructureTools.getAtomCAArray(structure1);
		Atom[] ca2 = StructureTools.getAtomCAArray(structure2);


		AFPChain afpChain = algorithm.align(ca1, ca2, params);

		afpChain.setName1(name1);
		afpChain.setName2(name2);


		assertTrue(afpChain.getAlnLength() == 71);

		assertTrue(afpChain.getAlnLength() == 71);
		assertTrue(afpChain.getSimilarity() > .57);
		assertTrue(afpChain.getSimilarity() <= .6);

		// this calls the internal invalidate method
		afpChain.setOptAln(afpChain.getOptAln());

		assertTrue("wrong similarity score : " + afpChain.getSimilarity(), afpChain.getSimilarity() > .57);
		assertTrue("wrong similarity score : " + afpChain.getSimilarity(), afpChain.getSimilarity() <= .6);

		assertTrue(afpChain.getSimilarity() > .58);
		assertTrue(afpChain.getSimilarity() < .59);
		assertTrue("similarity score is " + afpChain.getSimilarity()  , afpChain.getSimilarity() > .46);


	} catch (Exception e){

		fail(e.getMessage());
	}

}