From 750cb88e3685b6cf231163ebc31d4861d5d316d7 Mon Sep 17 00:00:00 2001 From: Jim Procter Date: Thu, 12 Feb 2015 16:46:44 +0000 Subject: [PATCH] JAL-1115 List rather than vector for sequence groups (minimal additional synchronized clauses added) --- src/jalview/datamodel/AlignmentView.java | 37 +++--- src/jalview/datamodel/SequenceGroup.java | 206 ++++++++++++++---------------- 2 files changed, 115 insertions(+), 128 deletions(-) diff --git a/src/jalview/datamodel/AlignmentView.java b/src/jalview/datamodel/AlignmentView.java index efb7c4a..2d234fe 100644 --- a/src/jalview/datamodel/AlignmentView.java +++ b/src/jalview/datamodel/AlignmentView.java @@ -24,7 +24,7 @@ import jalview.util.MessageManager; import jalview.util.ShiftList; import java.io.PrintStream; -import java.util.Enumeration; +import java.util.ArrayList; import java.util.List; import java.util.Vector; @@ -47,7 +47,7 @@ public class AlignmentView * one or more ScGroup objects, which are referenced by each seqCigar's group * membership */ - private Vector scGroups; + private List scGroups=null; private boolean isNa = false; @@ -133,27 +133,24 @@ public class AlignmentView selseqs = alignment.getSequencesArray(); } + List> seqsets=new ArrayList>(); // get the alignment's group list and make a copy - Vector grps = new Vector(); + List grps = new ArrayList(); List gg = alignment.getGroups(); grps.addAll(gg); ScGroup[] sgrps = null; boolean addedgps[] = null; if (grps != null) { - SequenceGroup sg; if (selection != null && selectedRegionOnly) { // trim annotation to the region being stored. // strip out any groups that do not actually intersect with the // visible and selected region int ssel = selection.getStartRes(), esel = selection.getEndRes(); - Vector isg = new Vector(); - Enumeration en = grps.elements(); - while (en.hasMoreElements()) + List isg = new ArrayList(); + for (SequenceGroup sg : grps) { - sg = (SequenceGroup) en.nextElement(); - if (!(sg.getStartRes() > esel || sg.getEndRes() < ssel)) { // adjust bounds of new group, if necessary. @@ -168,7 +165,7 @@ public class AlignmentView sg.setStartRes(sg.getStartRes() - ssel + 1); sg.setEndRes(sg.getEndRes() - ssel + 1); - isg.addElement(sg); + isg.add(sg); } } grps = isg; @@ -178,13 +175,13 @@ public class AlignmentView addedgps = new boolean[grps.size()]; for (int g = 0; g < sgrps.length; g++) { - sg = (SequenceGroup) grps.elementAt(g); + SequenceGroup sg = grps.get(g); sgrps[g] = new ScGroup(); sgrps[g].sg = new SequenceGroup(sg); addedgps[g] = false; - grps.setElementAt(sg.getSequences(null), g); + seqsets.set(g, sg.getSequences(null)); } - // grps now contains vectors (should be sets) for each group, so we can + // seqsets now contains vectors (should be sets) for each group, so we can // track when we've done with the group } int csi = 0; @@ -198,11 +195,11 @@ public class AlignmentView sequences[csi].setGroupMembership(selected); selected.addElement(sequences[csi]); } - if (grps != null) + if (seqsets != null) { for (int sg = 0; sg < sgrps.length; sg++) { - if (((Vector) grps.elementAt(sg)).contains(selseqs[i])) + if ((seqsets.get(sg)).contains(selseqs[i])) { sequences[csi].setGroupMembership(sgrps[sg]); sgrps[sg].sg.deleteSequence(selseqs[i], false); @@ -211,10 +208,10 @@ public class AlignmentView { if (scGroups == null) { - scGroups = new Vector(); + scGroups = new ArrayList(); } addedgps[sg] = true; - scGroups.addElement(sgrps[sg]); + scGroups.add(sgrps[sg]); } } } @@ -356,7 +353,7 @@ public class AlignmentView SequenceGroup[] nsg = new SequenceGroup[nvg]; for (int g = 0; g < nvg; g++) { - SequenceGroup sg = ((ScGroup) scGroups.elementAt(g)).sg; + SequenceGroup sg = scGroups.get(g).sg; if (r) { if (sg.getStartRes() > gend || sg.getEndRes() < gstart) @@ -442,7 +439,7 @@ public class AlignmentView for (int g = 0; g < nvg; g++) { if (nsg[g] != null - && sequences[nsq].isMemberOf(scGroups.elementAt(g))) + && sequences[nsq].isMemberOf(scGroups.get(g))) { nsg[g].addSequence(aln[nsq], false); } @@ -1038,7 +1035,7 @@ public class AlignmentView + " groups defined on the view."); for (int g = 0; g < view.scGroups.size(); g++) { - ScGroup sgr = (ScGroup) view.scGroups.elementAt(g); + ScGroup sgr = view.scGroups.get(g); os.println("Group " + g + ": Name = " + sgr.sg.getName() + " Contains " + sgr.seqs.size() + " Seqs."); os.println("This group runs from " + sgr.sg.getStartRes() + " to " diff --git a/src/jalview/datamodel/SequenceGroup.java b/src/jalview/datamodel/SequenceGroup.java index 752c6d4..c6b9cbd 100755 --- a/src/jalview/datamodel/SequenceGroup.java +++ b/src/jalview/datamodel/SequenceGroup.java @@ -27,7 +27,6 @@ import jalview.schemes.ResidueProperties; import java.awt.Color; import java.util.ArrayList; -import java.util.Enumeration; import java.util.Hashtable; import java.util.List; import java.util.Map; @@ -63,7 +62,7 @@ public class SequenceGroup implements AnnotatedCollectionI /** * group members */ - private Vector sequences = new Vector(); + private List sequences = new ArrayList(); /** * representative sequence for this group (if any) @@ -136,7 +135,7 @@ public class SequenceGroup implements AnnotatedCollectionI * @param end * last column of group */ - public SequenceGroup(Vector sequences, String groupName, + public SequenceGroup(List sequences, String groupName, ColourSchemeI scheme, boolean displayBoxes, boolean displayText, boolean colourText, int start, int end) { @@ -160,13 +159,8 @@ public class SequenceGroup implements AnnotatedCollectionI { if (seqsel != null) { - sequences = new Vector(); - Enumeration sq = seqsel.sequences.elements(); - while (sq.hasMoreElements()) - { - sequences.addElement(sq.nextElement()); - } - ; + sequences = new ArrayList(); + sequences.addAll(seqsel.sequences); if (seqsel.groupName != null) { groupName = new String(seqsel.groupName); @@ -310,16 +304,15 @@ public class SequenceGroup implements AnnotatedCollectionI { if (hiddenReps == null) { + // TODO: need a synchronizedCollection here ? return sequences; } else { - Vector allSequences = new Vector(); - SequenceI seq; - for (int i = 0; i < sequences.size(); i++) + List allSequences = new ArrayList(); + for (SequenceI seq : sequences) { - seq = sequences.elementAt(i); - allSequences.addElement(seq); + allSequences.add(seq); if (hiddenReps.containsKey(seq)) { SequenceCollectionI hsg = hiddenReps.get(seq); @@ -327,7 +320,7 @@ public class SequenceGroup implements AnnotatedCollectionI { if (seq2 != seq && !allSequences.contains(seq2)) { - allSequences.addElement(seq2); + allSequences.add(seq2); } } } @@ -471,14 +464,17 @@ public class SequenceGroup implements AnnotatedCollectionI */ public void addSequence(SequenceI s, boolean recalc) { - if (s != null && !sequences.contains(s)) + synchronized (sequences) { - sequences.addElement(s); - } + if (s != null && !sequences.contains(s)) + { + sequences.add(s); + } - if (recalc) - { - recalcConservation(); + if (recalc) + { + recalcConservation(); + } } } @@ -613,6 +609,8 @@ public class SequenceGroup implements AnnotatedCollectionI */ public void addOrRemove(SequenceI s, boolean recalc) { + synchronized (sequences) + { if (sequences.contains(s)) { deleteSequence(s, recalc); @@ -620,24 +618,28 @@ public class SequenceGroup implements AnnotatedCollectionI else { addSequence(s, recalc); + } } } /** - * DOCUMENT ME! + * remove * * @param s - * DOCUMENT ME! + * to be removed * @param recalc - * DOCUMENT ME! + * true means recalculate conservation */ public void deleteSequence(SequenceI s, boolean recalc) { - sequences.removeElement(s); - - if (recalc) + synchronized (sequences) { - recalcConservation(); + sequences.remove(s); + + if (recalc) + { + recalcConservation(); + } } } @@ -683,9 +685,7 @@ public class SequenceGroup implements AnnotatedCollectionI } /** - * DOCUMENT ME! - * - * @return DOCUMENT ME! + * @return number of sequences in group */ public int getSize() { @@ -693,23 +693,17 @@ public class SequenceGroup implements AnnotatedCollectionI } /** - * DOCUMENT ME! - * * @param i - * DOCUMENT ME! - * - * @return DOCUMENT ME! + * @return the ith sequence */ public SequenceI getSequenceAt(int i) { - return sequences.elementAt(i); + return sequences.get(i); } /** - * DOCUMENT ME! - * * @param state - * DOCUMENT ME! + * colourText */ public void setColourText(boolean state) { @@ -769,30 +763,26 @@ public class SequenceGroup implements AnnotatedCollectionI } /** - * DOCUMENT ME! + * computes the width of current set of sequences and returns it * * @return DOCUMENT ME! */ @Override public int getWidth() { - // MC This needs to get reset when characters are inserted and deleted - if (sequences.size() > 0) + synchronized (sequences) { - width = sequences.elementAt(0).getLength(); - } - - for (int i = 1; i < sequences.size(); i++) - { - SequenceI seq = sequences.elementAt(i); - - if (seq.getLength() > width) - { - width = seq.getLength(); + // MC This needs to get reset when characters are inserted and deleted + boolean first=true; + for (SequenceI seq:sequences) { + if (first || seq.getLength() > width) + { + width = seq.getLength(); + first = false; + } } + return width; } - - return width; } /** @@ -843,39 +833,42 @@ public class SequenceGroup implements AnnotatedCollectionI */ public SequenceI[] getSequencesInOrder(AlignmentI al, boolean trim) { - int sSize = sequences.size(); - int alHeight = al.getHeight(); + synchronized (sequences) + { + int sSize = sequences.size(); + int alHeight = al.getHeight(); - SequenceI[] seqs = new SequenceI[(trim) ? sSize : alHeight]; + SequenceI[] seqs = new SequenceI[(trim) ? sSize : alHeight]; - int index = 0; - for (int i = 0; i < alHeight && index < sSize; i++) - { - if (sequences.contains(al.getSequenceAt(i))) + int index = 0; + for (int i = 0; i < alHeight && index < sSize; i++) { - seqs[(trim) ? index : i] = al.getSequenceAt(i); - index++; + if (sequences.contains(al.getSequenceAt(i))) + { + seqs[(trim) ? index : i] = al.getSequenceAt(i); + index++; + } } - } - if (index == 0) - { - return null; - } - if (!trim) - { - return seqs; - } - if (index < seqs.length) - { - SequenceI[] dummy = seqs; - seqs = new SequenceI[index]; - while (--index >= 0) + if (index == 0) + { + return null; + } + if (!trim) + { + return seqs; + } + if (index < seqs.length) { - seqs[index] = dummy[index]; - dummy[index] = null; + SequenceI[] dummy = seqs; + seqs = new SequenceI[index]; + while (--index >= 0) + { + seqs[index] = dummy[index]; + dummy[index] = null; + } } + return seqs; } - return seqs; } /** @@ -989,23 +982,14 @@ public class SequenceGroup implements AnnotatedCollectionI { SequenceGroup sgroup = new SequenceGroup(this); SequenceI[] insect = getSequencesInOrder(alignment); - sgroup.sequences = new Vector(); + sgroup.sequences = new ArrayList(); for (int s = 0; insect != null && s < insect.length; s++) { if (map == null || map.containsKey(insect[s])) { - sgroup.sequences.addElement(insect[s]); + sgroup.sequences.add(insect[s]); } } - // Enumeration en =getSequences(hashtable).elements(); - // while (en.hasMoreElements()) - // { - // SequenceI elem = (SequenceI) en.nextElement(); - // if (alignment.getSequences().contains(elem)) - // { - // sgroup.addSequence(elem, false); - // } - // } return sgroup; } @@ -1225,27 +1209,30 @@ public class SequenceGroup implements AnnotatedCollectionI // TODO add in other methods like 'getAlignmentAnnotation(String label), // etc' ArrayList annot = new ArrayList(); - for (SequenceI seq : sequences) + synchronized (sequences) { - AlignmentAnnotation[] aa = seq.getAnnotation(); - if (aa != null) + for (SequenceI seq : sequences) { - for (AlignmentAnnotation al : aa) + AlignmentAnnotation[] aa = seq.getAnnotation(); + if (aa != null) { - if (al.groupRef == this) + for (AlignmentAnnotation al : aa) { - annot.add(al); + if (al.groupRef == this) + { + annot.add(al); + } } } } - } - if (consensus != null) - { - annot.add(consensus); - } - if (conservation != null) - { - annot.add(conservation); + if (consensus != null) + { + annot.add(consensus); + } + if (conservation != null) + { + annot.add(conservation); + } } return annot.toArray(new AlignmentAnnotation[0]); } @@ -1310,7 +1297,10 @@ public class SequenceGroup implements AnnotatedCollectionI public void clear() { - sequences.clear(); + synchronized (sequences) + { + sequences.clear(); + } } private AnnotatedCollectionI context; -- 1.7.10.2