Skip to content

Commit 30cc0b4

Browse files
committed
Added new methods to the AlignmentTools class: a method to calculate the
gaps in each block of an optimal alignment and a method to replace the optimal alignment of an AFPChain.
1 parent bc96c51 commit 30cc0b4

1 file changed

Lines changed: 94 additions & 0 deletions

File tree

biojava-structure/src/main/java/org/biojava/nbio/structure/align/util/AlignmentTools.java

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,58 @@ public static AFPChain splitBlocksByTopology(AFPChain a, Atom[] ca1, Atom[] ca2)
672672

673673
return replaceOptAln(a, ca1, ca2, blocks.size(), newBlockLens, newOptAln);
674674
}
675+
676+
/**
677+
* It returns an AFPChain with a segmented optimal alignment, which means that it has <order of symmetry> blocks of
678+
* aligned subunits. The original AFPChain is not modified.
679+
*
680+
* INPUT: the optimal alignment in a triple list (same format as optAln of AFPChain) and the Atom[] arrays.
681+
* OUTPUT: the optimal AFPChain alignment object divided into the subunits.
682+
*/
683+
public static AFPChain replaceOptAln(int[][][] newAlgn, AFPChain afpChain, Atom[] ca1, Atom[] ca2) throws StructureException {
684+
685+
//The order is the number of groups in the newAlgn
686+
int order = newAlgn.length;
687+
688+
//Calculate the alignment length from all the subunits lengths
689+
int[] optLens = new int[order];
690+
for(int s=0;s<order;s++) {
691+
optLens[s] = newAlgn[s][0].length;
692+
}
693+
int optLength = 0;
694+
for(int s=0;s<order;s++) {
695+
optLength += optLens[s];
696+
}
697+
698+
/*//Print the sizes to check correctness
699+
System.out.println("Number of subunits: "+newAlgn.length);
700+
System.out.println("Subunit length: "+newAlgn[0][0].length);*/
701+
702+
//Create a copy of the original AFPChain and set everything needed for the structure update
703+
AFPChain copyAFP = (AFPChain) afpChain.clone();
704+
705+
//Set the new parameters of the optimal alignment
706+
copyAFP.setOptLength(optLength);
707+
copyAFP.setOptLen(optLens);
708+
copyAFP.setOptAln(newAlgn);
709+
710+
//Set the block information of the new alignment
711+
copyAFP.setBlockNum(order);
712+
copyAFP.setBlockSize(optLens);
713+
copyAFP.setBlockResList(newAlgn);
714+
copyAFP.setBlockResSize(optLens);
715+
copyAFP.setBlockGap(calculateBlockGap(newAlgn));
716+
717+
//Recalculate properties: superposition, tm-score, etc
718+
Atom[] ca2clone = StructureTools.cloneCAArray(ca2); // don't modify ca1 positions
719+
AlignmentTools.updateSuperposition(copyAFP, ca1, ca2clone);
720+
721+
//It re-does the sequence alignment strings from the OptAlgn information only
722+
copyAFP.setAlnsymb(null);
723+
AFPAlignmentDisplay.getAlign(copyAFP, ca1, ca2clone);
724+
725+
return copyAFP;
726+
}
675727

676728
/**
677729
* Takes an AFPChain and replaces the optimal alignment based on an alignment map
@@ -1031,4 +1083,46 @@ public static Map<Integer, Integer> fromConciseAlignmentString(String string) {
10311083
}
10321084
return map;
10331085
}
1086+
1087+
/**
1088+
* Method that calculates the number of gaps in each subunit block of an optimal AFP alignment.
1089+
*
1090+
* INPUT: an optimal alignment in the format int[][][].
1091+
* OUTPUT: an int[] array of <order> length containing the gaps in each block as int[block].
1092+
*/
1093+
public static int[] calculateBlockGap(int[][][] optAln){
1094+
1095+
//Initialize the array to be returned
1096+
int [] blockGap = new int[optAln.length];
1097+
1098+
//Loop for every block and look in both chains for non-contiguous residues.
1099+
for (int i=0; i<optAln.length; i++){
1100+
int gaps = 0; //the number of gaps in that block
1101+
int last1 = 0; //the last residue position in chain 1
1102+
int last2 = 0; //the last residue position in chain 2
1103+
//Loop for every position in the block
1104+
for (int j=0; j<optAln[i][0].length; j++){
1105+
//If the first position is evaluated initialize the last positions
1106+
if (j==0){
1107+
last1 = optAln[i][0][j];
1108+
last2 = optAln[i][1][j];
1109+
}
1110+
else{
1111+
//If one of the positions or both are not contiguous increment the number of gaps
1112+
if (optAln[i][0][j] > last1+1 || optAln[i][1][j] > last2+1){
1113+
gaps++;
1114+
last1 = optAln[i][0][j];
1115+
last2 = optAln[i][1][j];
1116+
}
1117+
//Otherwise just set the last position to the current one
1118+
else{
1119+
last1 = optAln[i][0][j];
1120+
last2 = optAln[i][1][j];
1121+
}
1122+
}
1123+
}
1124+
blockGap[i] = gaps;
1125+
}
1126+
return blockGap;
1127+
}
10341128
}

0 commit comments

Comments
 (0)