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

The following examples show how to use org.biojava.nbio.structure.align.model.AFPChain#setDistanceMatrix() . 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: CeCPMain.java    From biojava with GNU Lesser General Public License v2.1 6 votes vote down vote up
/** Circular permutation specific code to be run after the standard CE alignment
 *
 * @param afpChain The finished alignment
 * @param ca1 CA atoms of the first protein
 * @param ca2m A duplicated copy of the second protein
 * @param calculator The CECalculator used to create afpChain
 * @param param Parameters
 * @throws StructureException
 */
public static AFPChain postProcessAlignment(AFPChain afpChain, Atom[] ca1, Atom[] ca2m,CECalculator calculator, CECPParameters param ) throws StructureException{

	// remove bottom half of the matrix
	Matrix doubledMatrix = afpChain.getDistanceMatrix();

	// the matrix can be null if the alignment is too short.
	if ( doubledMatrix != null ) {
		assert(doubledMatrix.getRowDimension() == ca1.length);
		assert(doubledMatrix.getColumnDimension() == ca2m.length);

		Matrix singleMatrix = doubledMatrix.getMatrix(0, ca1.length-1, 0, (ca2m.length/2)-1);
		assert(singleMatrix.getRowDimension() == ca1.length);
		assert(singleMatrix.getColumnDimension() == (ca2m.length/2));

		afpChain.setDistanceMatrix(singleMatrix);
	}
	// Check for circular permutations
	int alignLen = afpChain.getOptLength();
	if ( alignLen > 0) {
		afpChain = filterDuplicateAFPs(afpChain,calculator,ca1,ca2m,param);
	}
	return afpChain;
}
 
Example 2
Source File: CEMirrorSymm.java    From symmetry with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void postProcessAlignment(AFPChain afpChain) {
	if (mirrorSequence) {
		// reverse the optAln
		reverseOptAln(afpChain);

		// reverse the distance matrix
		afpChain.setDistanceMatrix(reverseMatrixCols(afpChain
				.getDistanceMatrix()));

		// reverse the ca2 matrix
		Matrix distMat2 = afpChain.getDisTable2();
		distMat2 = reverseMatrixRows(distMat2);
		distMat2 = reverseMatrixCols(distMat2);
		afpChain.setDisTable2(distMat2);
	}

}
 
Example 3
Source File: OptimalCECPMain.java    From biojava with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Permute the second protein of afpChain by the specified number of residues.
 * @param afpChain Input alignment
 * @param cp Amount leftwards (or rightward, if negative) to shift the
 * @return A new alignment equivalent to afpChain after the permutations
 */
private static void permuteAFPChain(AFPChain afpChain, int cp) {

	int ca2len = afpChain.getCa2Length();

	//fix up cp to be positive
	if(cp == 0) {
		return;
	}
	if(cp < 0) {
		cp = ca2len+cp;
	}
	if(cp < 0 || cp >= ca2len) {
		throw new ArrayIndexOutOfBoundsException(
				"Permutation point ("+cp+") must be between -ca2.length and ca2.length-1" );
	}

	// Fix up optAln
	permuteOptAln(afpChain,cp);

	if(afpChain.getBlockNum() > 1)
		afpChain.setSequentialAlignment(false);
	// fix up matrices
	// ca1 corresponds to row indices, while ca2 corresponds to column indices.
	afpChain.setDistanceMatrix(permuteMatrix(afpChain.getDistanceMatrix(),0,-cp));
	// this is square, so permute both
	afpChain.setDisTable2(permuteMatrix(afpChain.getDisTable2(),-cp,-cp));

	//TODO fix up other AFP parameters?

}
 
Example 4
Source File: CeSymm.java    From biojava with GNU Lesser General Public License v2.1 4 votes vote down vote up
private static Matrix align(AFPChain afpChain, Atom[] ca1, Atom[] ca2,
		CESymmParameters params, Matrix origM, CECalculator calculator,
		int counter) throws StructureException {

	int fragmentLength = params.getWinSize();
	Atom[] ca2clone = StructureTools.cloneAtomArray(ca2);

	int rows = ca1.length;
	int cols = ca2.length;

	// Matrix that tracks similarity of a fragment of length fragmentLength
	// starting a position i,j.

	int blankWindowSize = fragmentLength;
	if (origM == null) {

		// Build alignment ca1 to ca2-ca2
		afpChain = calculator.extractFragments(afpChain, ca1, ca2clone);

		origM = SymmetryTools.blankOutPreviousAlignment(afpChain, ca2,
				rows, cols, calculator, null, blankWindowSize);

	} else {
		// we are doing an iteration on a previous alignment
		// mask the previous alignment
		origM = SymmetryTools.blankOutPreviousAlignment(afpChain, ca2,
				rows, cols, calculator, origM, blankWindowSize);
	}

	Matrix clone = (Matrix) origM.clone();

	// that's the matrix to run the alignment on..
	calculator.setMatMatrix(clone.getArray());

	calculator.traceFragmentMatrix(afpChain, ca1, ca2clone);

	final Matrix origMfinal = (Matrix) origM.clone();
	// Add a matrix listener to keep the blacked zones in max.
	calculator.addMatrixListener(new MatrixListener() {

		@Override
		public double[][] matrixInOptimizer(double[][] max) {

			// Check every entry of origM for blacked out regions
			for (int i = 0; i < max.length; i++) {
				for (int j = 0; j < max[i].length; j++) {
					if (origMfinal.getArray()[i][j] > 1e9) {
						max[i][j] = -origMfinal.getArray()[i][j];
					}
				}
			}
			return max;
		}

		@Override
		public boolean[][] initializeBreakFlag(boolean[][] brkFlag) {

			return brkFlag;
		}
	});

	calculator.nextStep(afpChain, ca1, ca2clone);

	afpChain.setAlgorithmName(algorithmName);
	afpChain.setVersion(version);

	afpChain.setDistanceMatrix(origM);

	return origMfinal;

}
 
Example 5
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 6
Source File: CeMain.java    From biojava with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Align ca2 onto ca1.
 */
@Override
public AFPChain align(Atom[] ca1, Atom[] ca2, Object param) throws StructureException{
	if ( ! (param instanceof CeParameters))
		throw new IllegalArgumentException("CE algorithm needs an object of call CeParameters as argument.");

	params = (CeParameters) param;

	// we don't want to rotate input atoms, do we?
	ca2clone = new Atom[ca2.length];

	int pos = 0;
	for (Atom a : ca2){
		Group g = (Group)a.getGroup().clone(); // works because each group has only a CA atom

		ca2clone[pos] = g.getAtom(a.getName());

		pos++;
	}

	calculator = new CECalculator(params);

	//Build alignment ca1 to ca2-ca2
	AFPChain afpChain = new AFPChain(algorithmName);
	afpChain = calculator.extractFragments(afpChain, ca1, ca2clone);
	calculator.traceFragmentMatrix( afpChain,ca1, ca2clone);
	calculator.nextStep( afpChain,ca1, ca2clone);

	afpChain.setAlgorithmName(getAlgorithmName());
	afpChain.setVersion(version);

	// Try to guess names

	if (ca1.length!=0 && ca1[0].getGroup().getChain()!=null && ca1[0].getGroup().getChain().getStructure()!=null)
		afpChain.setName1(ca1[0].getGroup().getChain().getStructure().getName());

	if (ca2.length!=0 && ca2[0].getGroup().getChain()!=null && ca2[0].getGroup().getChain().getStructure()!=null)
		afpChain.setName2(ca2[0].getGroup().getChain().getStructure().getName());

	if ( afpChain.getNrEQR() == 0)
	   return afpChain;

	// Set the distance matrix

	int winSize = params.getWinSize();
	int winSizeComb1 = (winSize-1)*(winSize-2)/2;
	double[][] m = calculator.initSumOfDistances(ca1.length, ca2.length, winSize, winSizeComb1, ca1, ca2clone);
	afpChain.setDistanceMatrix(new Matrix(m));
	afpChain.setSequentialAlignment(true);

	return afpChain;
}