Skip to content

Commit a0cdb6f

Browse files
authored
Merge pull request biojava#532 from sbliven/master
Refactor methods for generating alignment PDBs
2 parents e893ac6 + f63e46d commit a0cdb6f

File tree

8 files changed

+259
-246
lines changed

8 files changed

+259
-246
lines changed

biojava-structure-gui/src/main/java/org/biojava/nbio/structure/align/gui/DisplayAFP.java

Lines changed: 4 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,9 @@
3030
import javax.swing.JScrollPane;
3131

3232
import org.biojava.nbio.structure.Atom;
33-
import org.biojava.nbio.structure.Chain;
34-
import org.biojava.nbio.structure.ChainImpl;
3533
import org.biojava.nbio.structure.Group;
3634
import org.biojava.nbio.structure.Structure;
3735
import org.biojava.nbio.structure.StructureException;
38-
import org.biojava.nbio.structure.StructureImpl;
3936
import org.biojava.nbio.structure.StructureTools;
4037
import org.biojava.nbio.structure.align.gui.aligpanel.AligPanel;
4138
import org.biojava.nbio.structure.align.gui.aligpanel.StatusDisplay;
@@ -44,6 +41,7 @@
4441
import org.biojava.nbio.structure.align.gui.jmol.StructureAlignmentJmol;
4542
import org.biojava.nbio.structure.align.model.AFPChain;
4643
import org.biojava.nbio.structure.align.util.AFPAlignmentDisplay;
44+
import org.biojava.nbio.structure.align.util.AlignmentTools;
4745
import org.slf4j.Logger;
4846
import org.slf4j.LoggerFactory;
4947

@@ -413,75 +411,6 @@ private static final int getUngappedFatCatPos(AFPChain afpChain, int chainNr, in
413411
}
414412

415413

416-
/** get an artifical List of chains containing the Atoms and groups.
417-
* Does NOT rotate anything.
418-
* @param ca
419-
* @return a list of Chains that is built up from the Atoms in the ca array
420-
* @throws StructureException
421-
*/
422-
static final List<Chain> getAlignedModel(Atom[] ca){
423-
424-
List<Chain> model = new ArrayList<Chain>();
425-
for ( Atom a: ca){
426-
427-
Group g = a.getGroup();
428-
Chain parentC = g.getChain();
429-
430-
Chain newChain = null;
431-
for ( Chain c : model) {
432-
if ( c.getChainID().equals(parentC.getChainID())){
433-
newChain = c;
434-
break;
435-
}
436-
}
437-
if ( newChain == null){
438-
439-
newChain = new ChainImpl();
440-
441-
newChain.setChainID(parentC.getChainID());
442-
443-
model.add(newChain);
444-
}
445-
446-
newChain.addGroup(g);
447-
448-
}
449-
450-
return model;
451-
}
452-
453-
454-
/** Get an artifical Structure containing both chains.
455-
* Does NOT rotate anything
456-
* @param ca1
457-
* @param ca2
458-
* @return a structure object containing two models, one for each set of Atoms.
459-
* @throws StructureException
460-
*/
461-
public static final Structure getAlignedStructure(Atom[] ca1, Atom[] ca2) throws StructureException{
462-
463-
/* Previous implementation commented
464-
465-
Structure s = new StructureImpl();
466-
467-
468-
List<Chain>model1 = getAlignedModel(ca1);
469-
List<Chain>model2 = getAlignedModel(ca2);
470-
s.addModel(model1);
471-
s.addModel(model2);
472-
473-
return s;*/
474-
475-
Structure s = new StructureImpl();
476-
477-
List<Chain>model1 = getAlignedModel(ca1);
478-
s.addModel(model1);
479-
List<Chain> model2 = getAlignedModel(ca2);
480-
s.addModel(model2);
481-
482-
return s;
483-
}
484-
485414
/**
486415
* Returns the first atom for each group
487416
* @param ca
@@ -638,10 +567,10 @@ public static Structure createArtificalStructure(AFPChain afpChain, Atom[] ca1,
638567

639568

640569
if ( afpChain.getNrEQR() < 1){
641-
return DisplayAFP.getAlignedStructure(ca1, ca2);
570+
return AlignmentTools.getAlignedStructure(ca1, ca2);
642571
}
643572

644-
Group[] twistedGroups = StructureAlignmentDisplay.prepareGroupsForDisplay(afpChain,ca1, ca2);
573+
Group[] twistedGroups = AlignmentTools.prepareGroupsForDisplay(afpChain,ca1, ca2);
645574

646575
List<Atom> twistedAs = new ArrayList<Atom>();
647576

@@ -661,7 +590,7 @@ public static Structure createArtificalStructure(AFPChain afpChain, Atom[] ca1,
661590
Atom[] arr1 = DisplayAFP.getAtomArray(ca1, hetatms);
662591
Atom[] arr2 = DisplayAFP.getAtomArray(twistedAtoms, hetatms2);
663592

664-
Structure artificial = DisplayAFP.getAlignedStructure(arr1,arr2);
593+
Structure artificial = AlignmentTools.getAlignedStructure(arr1,arr2);
665594
return artificial;
666595
}
667596
}

biojava-structure-gui/src/main/java/org/biojava/nbio/structure/align/gui/MultipleAlignmentJmolDisplay.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -195,25 +195,4 @@ public static MultipleAlignmentJmol display(MultipleAlignment multAln)
195195
return jmol;
196196
}
197197

198-
/**
199-
* Get an artifical Structure containing a different model for every
200-
* input structure, so that the alignment result can be viewed in Jmol.
201-
* The Atoms have to be rotated beforehand.
202-
*
203-
* @param atomArrays an array of Atoms for every aligned structure
204-
* @return a structure object containing a set of models,
205-
* one for each input array of Atoms.
206-
* @throws StructureException
207-
*/
208-
public static final Structure getAlignedStructure(List<Atom[]> atomArrays)
209-
throws StructureException {
210-
211-
Structure s = new StructureImpl();
212-
for (int i=0; i<atomArrays.size(); i++){
213-
List<Chain> model = DisplayAFP.getAlignedModel(atomArrays.get(i));
214-
s.addModel(model);
215-
}
216-
return s;
217-
}
218-
219198
}

biojava-structure-gui/src/main/java/org/biojava/nbio/structure/align/gui/StructureAlignmentDisplay.java

Lines changed: 2 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.biojava.nbio.structure.align.fatcat.FatCatRigid;
3434
import org.biojava.nbio.structure.align.gui.jmol.StructureAlignmentJmol;
3535
import org.biojava.nbio.structure.align.model.AFPChain;
36+
import org.biojava.nbio.structure.align.util.AlignmentTools;
3637
import org.biojava.nbio.structure.jama.Matrix;
3738

3839
public class StructureAlignmentDisplay {
@@ -51,7 +52,7 @@ public static StructureAlignmentJmol display(AFPChain afpChain, Atom[] ca1, Atom
5152
throw new StructureException("length of atoms arrays is too short! " + ca1.length + "," + ca2.length);
5253
}
5354

54-
Group[] twistedGroups = prepareGroupsForDisplay(afpChain, ca1, ca2);
55+
Group[] twistedGroups = AlignmentTools.prepareGroupsForDisplay(afpChain, ca1, ca2);
5556

5657
List<Group> hetatms = StructureTools.getUnalignedGroups(ca1);
5758
List<Group> hetatms2 = StructureTools.getUnalignedGroups(ca2);
@@ -60,103 +61,4 @@ public static StructureAlignmentJmol display(AFPChain afpChain, Atom[] ca1, Atom
6061

6162
}
6263

63-
/** Rotate the Atoms/Groups so they are aligned for the 3D visualisation
64-
*
65-
* @param afpChain
66-
* @param ca1
67-
* @param ca2
68-
* @return an array of Groups that are transformed for 3D display
69-
* @throws StructureException
70-
*/
71-
public static Group[] prepareGroupsForDisplay(AFPChain afpChain, Atom[] ca1, Atom[] ca2) throws StructureException{
72-
73-
74-
if ( afpChain.getBlockRotationMatrix().length == 0 ) {
75-
// probably the alignment is too short!
76-
System.err.println("No rotation matrix found to rotate 2nd structure!");
77-
afpChain.setBlockRotationMatrix(new Matrix[]{Matrix.identity(3, 3)});
78-
afpChain.setBlockShiftVector(new Atom[]{new AtomImpl()});
79-
}
80-
81-
// List of groups to be rotated according to the alignment
82-
Group[] twistedGroups = new Group[ ca2.length];
83-
84-
//int blockNum = afpChain.getBlockNum();
85-
86-
int i = -1;
87-
88-
// List of groups from the structure not included in ca2 (e.g. ligands)
89-
// Will be rotated according to first block
90-
List<Group> hetatms2 = StructureTools.getUnalignedGroups(ca2);
91-
92-
if ( (afpChain.getAlgorithmName().equals(FatCatRigid.algorithmName) ) || (afpChain.getAlgorithmName().equals(FatCatFlexible.algorithmName) ) ){
93-
94-
for (Atom a: ca2){
95-
i++;
96-
twistedGroups[i]=a.getGroup();
97-
98-
}
99-
100-
twistedGroups = AFPTwister.twistOptimized(afpChain, ca1, ca2);
101-
102-
//} else if (( blockNum == 1 ) || (afpChain.getAlgorithmName().equals(CeCPMain.algorithmName))) {
103-
} else {
104-
105-
Matrix m = afpChain.getBlockRotationMatrix()[ 0];
106-
Atom shift = afpChain.getBlockShiftVector() [ 0 ];
107-
108-
shiftCA2(afpChain, ca2, m,shift, twistedGroups);
109-
110-
}
111-
112-
if ( afpChain.getBlockNum() > 0){
113-
114-
// Superimpose ligands relative to the first block
115-
if( hetatms2.size() > 0 ) {
116-
117-
if ( afpChain.getBlockRotationMatrix().length > 0 ) {
118-
119-
Matrix m1 = afpChain.getBlockRotationMatrix()[0];
120-
//m1.print(3,3);
121-
Atom vector1 = afpChain.getBlockShiftVector()[0];
122-
//System.out.println("shift vector:" + vector1);
123-
124-
for ( Group g : hetatms2){
125-
Calc.rotate(g, m1);
126-
Calc.shift(g,vector1);
127-
}
128-
}
129-
}
130-
}
131-
132-
return twistedGroups;
133-
}
134-
135-
/** only shift CA positions.
136-
*
137-
*/
138-
public static void shiftCA2(AFPChain afpChain, Atom[] ca2, Matrix m, Atom shift, Group[] twistedGroups) {
139-
140-
int i = -1;
141-
for (Atom a: ca2){
142-
i++;
143-
Group g = a.getGroup();
144-
145-
Calc.rotate(g,m);
146-
Calc.shift(g, shift);
147-
148-
if (g.hasAltLoc()){
149-
for (Group alt: g.getAltLocs()){
150-
for (Atom alta : alt.getAtoms()){
151-
if ( g.getAtoms().contains(alta))
152-
continue;
153-
Calc.rotate(alta,m);
154-
Calc.shift(alta,shift);
155-
}
156-
}
157-
}
158-
twistedGroups[i]=g;
159-
}
160-
}
161-
16264
}

biojava-structure-gui/src/main/java/org/biojava/nbio/structure/align/gui/jmol/MultipleAlignmentJmol.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -360,22 +360,9 @@ protected void initCoords() {
360360
return;
361361
}
362362
}
363-
PDBHeader header = new PDBHeader();
364-
String title = multAln.getEnsemble().getAlgorithmName() + " V."
365-
+ multAln.getEnsemble().getVersion() + " : ";
366-
367-
for (StructureIdentifier name : multAln.getEnsemble()
368-
.getStructureIdentifiers()) {
369-
title += name.getIdentifier() + " ";
370-
}
371-
Structure artificial = MultipleAlignmentJmolDisplay
372-
.getAlignedStructure(transformedAtoms);
373-
374-
artificial.setPDBHeader(header);
363+
Structure artificial = MultipleAlignmentTools.toMultimodelStructure(multAln, transformedAtoms);
375364
setStructure(artificial);
376-
header.setTitle(title);
377-
logger.info(title);
378-
365+
logger.info(artificial.getPDBHeader().getTitle());
379366
} catch (StructureException e) {
380367
e.printStackTrace();
381368
}

biojava-structure-gui/src/main/java/org/biojava/nbio/structure/align/gui/jmol/StructureAlignmentJmol.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.biojava.nbio.structure.align.gui.MenuCreator;
3030
import org.biojava.nbio.structure.align.model.AFPChain;
3131
import org.biojava.nbio.structure.align.model.AfpChainWriter;
32+
import org.biojava.nbio.structure.align.util.AlignmentTools;
3233
import org.biojava.nbio.structure.align.util.AtomCache;
3334
import org.biojava.nbio.structure.align.util.ResourceManager;
3435
import org.biojava.nbio.structure.align.util.UserConfiguration;
@@ -286,7 +287,7 @@ protected void initCoords(){
286287
return;
287288
}
288289
}
289-
Structure artificial = DisplayAFP.getAlignedStructure(ca1,ca2);
290+
Structure artificial = AlignmentTools.getAlignedStructure(ca1,ca2);
290291
PDBHeader header = new PDBHeader();
291292
String title = afpChain.getAlgorithmName() + " V." +afpChain.getVersion() + " : " + afpChain.getName1() + " vs. " + afpChain.getName2();
292293
header.setTitle(title);

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,18 @@
4545
import org.biojava.nbio.structure.AminoAcid;
4646
import org.biojava.nbio.structure.Atom;
4747
import org.biojava.nbio.structure.Calc;
48+
import org.biojava.nbio.structure.Chain;
4849
import org.biojava.nbio.structure.Group;
50+
import org.biojava.nbio.structure.PDBHeader;
51+
import org.biojava.nbio.structure.Structure;
52+
import org.biojava.nbio.structure.StructureException;
53+
import org.biojava.nbio.structure.StructureIdentifier;
54+
import org.biojava.nbio.structure.StructureImpl;
4955
import org.biojava.nbio.structure.StructureTools;
5056
import org.biojava.nbio.structure.align.multiple.Block;
5157
import org.biojava.nbio.structure.align.multiple.BlockSet;
5258
import org.biojava.nbio.structure.align.multiple.MultipleAlignment;
59+
import org.biojava.nbio.structure.align.util.AlignmentTools;
5360
import org.biojava.nbio.structure.jama.Matrix;
5461
import org.forester.evoinference.matrix.distance.BasicSymmetricalDistanceMatrix;
5562
import org.forester.phylogeny.Phylogeny;
@@ -813,7 +820,44 @@ public static MultipleSequenceAlignment<ProteinSequence, AminoAcidCompound> toPr
813820
}
814821
return msa;
815822
}
823+
824+
public static Structure toMultimodelStructure(MultipleAlignment multAln, List<Atom[]> transformedAtoms) throws StructureException {
825+
PDBHeader header = new PDBHeader();
826+
String title = multAln.getEnsemble().getAlgorithmName() + " V."
827+
+ multAln.getEnsemble().getVersion() + " : ";
828+
829+
for (StructureIdentifier name : multAln.getEnsemble()
830+
.getStructureIdentifiers()) {
831+
title += name.getIdentifier() + " ";
832+
}
833+
Structure artificial = getAlignedStructure(transformedAtoms);
834+
835+
artificial.setPDBHeader(header);
836+
header.setTitle(title);
837+
return artificial;
838+
}
816839

840+
/**
841+
* Get an artificial Structure containing a different model for every
842+
* input structure, so that the alignment result can be viewed in Jmol.
843+
* The Atoms have to be rotated beforehand.
844+
*
845+
* @param atomArrays an array of Atoms for every aligned structure
846+
* @return a structure object containing a set of models,
847+
* one for each input array of Atoms.
848+
* @throws StructureException
849+
*/
850+
public static final Structure getAlignedStructure(List<Atom[]> atomArrays)
851+
throws StructureException {
852+
853+
Structure s = new StructureImpl();
854+
for (int i=0; i<atomArrays.size(); i++){
855+
List<Chain> model = AlignmentTools.getAlignedModel(atomArrays.get(i));
856+
s.addModel(model);
857+
}
858+
return s;
859+
}
860+
817861
/**
818862
* Calculate the RMSD matrix of a MultipleAlignment, that is, entry (i,j) of
819863
* the matrix contains the RMSD between structures i and j.

0 commit comments

Comments
 (0)