From 97e37bce240ce2430ca838bfb3060bd06dfe1711 Mon Sep 17 00:00:00 2001 From: Jim Procter Date: Mon, 20 Oct 2014 09:27:57 +0100 Subject: [PATCH] JAL-674 refactor method to match and transfer annotation between aligned sequences --- src/MCview/PDBfile.java | 85 +----------------------------------- src/jalview/analysis/AlignSeq.java | 81 ++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 83 deletions(-) diff --git a/src/MCview/PDBfile.java b/src/MCview/PDBfile.java index 086c4d9..19e13e7 100755 --- a/src/MCview/PDBfile.java +++ b/src/MCview/PDBfile.java @@ -279,7 +279,7 @@ public class PDBfile extends jalview.io.AlignFile {}).invoke(jmf)); cl.getMethod("addAnnotations", new Class[] { Alignment.class }).invoke(jmf, al); - replaceMatchingSeqsWith(seqs, annotations, prot, al, AlignSeq.PEP, false); + AlignSeq.replaceMatchingSeqsWith(seqs, annotations, prot, al, AlignSeq.PEP, false); } } catch (ClassNotFoundException q) { @@ -307,7 +307,7 @@ public class PDBfile extends jalview.io.AlignFile new Class[] { FileParse.class }).invoke(annotate3d, new Object[] { new FileParse(getDataName(), type) })); - replaceMatchingSeqsWith(seqs, annotations, rna, al, AlignSeq.DNA, false); + AlignSeq.replaceMatchingSeqsWith(seqs, annotations, rna, al, AlignSeq.DNA, false); } } catch (ClassNotFoundException x) { @@ -317,87 +317,6 @@ public class PDBfile extends jalview.io.AlignFile } /** - * matches ochains against al and populates seqs with the best match between - * each ochain and the set in al - * - * @param ochains - * @param al - * @param dnaOrProtein - * @param removeOldAnnots when true, old annotation is cleared before new annotation transferred - */ - public static void replaceMatchingSeqsWith(List seqs, List annotations, List ochains, - AlignmentI al, String dnaOrProtein, boolean removeOldAnnots) - { - if (al != null && al.getHeight() > 0) - { - ArrayList matches = new ArrayList(); - ArrayList aligns = new ArrayList(); - - for (SequenceI sq : ochains) - { - SequenceI bestm = null; - AlignSeq bestaseq = null; - int bestscore = 0; - for (SequenceI msq : al.getSequences()) - { - AlignSeq aseq = AlignSeq.doGlobalNWAlignment(msq, sq, - dnaOrProtein); - if (bestm == null || aseq.getMaxScore() > bestscore) - { - bestscore = aseq.getMaxScore(); - bestaseq = aseq; - bestm = msq; - } - } - System.out.println("Best Score for " + (matches.size() + 1) + " :" - + bestscore); - matches.add(bestm); - aligns.add(bestaseq); - al.deleteSequence(bestm); - } - for (int p = 0, pSize = seqs.size(); p < pSize; p++) - { - SequenceI sq, sp = seqs.get(p); - int q; - if ((q = ochains.indexOf(sp)) > -1) - { - seqs.set(p, sq = matches.get(q)); - sq.setName(sp.getName()); - sq.setDescription(sp.getDescription()); - Mapping sp2sq; - sq.transferAnnotation(sp, sp2sq = aligns.get(q).getMappingFromS1(false)); - int inspos = -1; - for (int ap = 0; ap < annotations.size();) - { - if (annotations.get(ap).sequenceRef == sp) - { - if (inspos == -1) - { - inspos = ap; - } - if (removeOldAnnots) { - annotations.remove(ap); - } else { - AlignmentAnnotation alan = annotations.get(ap); - alan.liftOver(sq, sp2sq); - alan.setSequenceRef(sq); - } - } - else - { - ap++; - } - } - if (sq.getAnnotation() != null) - { - annotations.addAll(inspos, Arrays.asList(sq.getAnnotation())); - } - } - } - } - } - - /** * make a friendly ID string. * * @param dataName diff --git a/src/jalview/analysis/AlignSeq.java b/src/jalview/analysis/AlignSeq.java index 4460985..9815b97 100755 --- a/src/jalview/analysis/AlignSeq.java +++ b/src/jalview/analysis/AlignSeq.java @@ -1024,6 +1024,87 @@ public class AlignSeq } /** + * matches ochains against al and populates seqs with the best match between + * each ochain and the set in al + * + * @param ochains + * @param al + * @param dnaOrProtein + * @param removeOldAnnots when true, old annotation is cleared before new annotation transferred + */ + public static void replaceMatchingSeqsWith(List seqs, List annotations, List ochains, + AlignmentI al, String dnaOrProtein, boolean removeOldAnnots) + { + if (al != null && al.getHeight() > 0) + { + ArrayList matches = new ArrayList(); + ArrayList aligns = new ArrayList(); + + for (SequenceI sq : ochains) + { + SequenceI bestm = null; + AlignSeq bestaseq = null; + int bestscore = 0; + for (SequenceI msq : al.getSequences()) + { + AlignSeq aseq = doGlobalNWAlignment(msq, sq, + dnaOrProtein); + if (bestm == null || aseq.getMaxScore() > bestscore) + { + bestscore = aseq.getMaxScore(); + bestaseq = aseq; + bestm = msq; + } + } + System.out.println("Best Score for " + (matches.size() + 1) + " :" + + bestscore); + matches.add(bestm); + aligns.add(bestaseq); + al.deleteSequence(bestm); + } + for (int p = 0, pSize = seqs.size(); p < pSize; p++) + { + SequenceI sq, sp = seqs.get(p); + int q; + if ((q = ochains.indexOf(sp)) > -1) + { + seqs.set(p, sq = matches.get(q)); + sq.setName(sp.getName()); + sq.setDescription(sp.getDescription()); + Mapping sp2sq; + sq.transferAnnotation(sp, sp2sq = aligns.get(q).getMappingFromS1(false)); + int inspos = -1; + for (int ap = 0; ap < annotations.size();) + { + if (annotations.get(ap).sequenceRef == sp) + { + if (inspos == -1) + { + inspos = ap; + } + if (removeOldAnnots) { + annotations.remove(ap); + } else { + AlignmentAnnotation alan = annotations.get(ap); + alan.liftOver(sq, sp2sq); + alan.setSequenceRef(sq); + } + } + else + { + ap++; + } + } + if (sq.getAnnotation() != null) + { + annotations.addAll(inspos, Arrays.asList(sq.getAnnotation())); + } + } + } + } + } + + /** * compute the PID vector used by the redundancy filter. * * @param originalSequences -- 1.7.10.2