From f21eb611044169a07a7d4c6e2f69dd772fa7c872 Mon Sep 17 00:00:00 2001 From: Jim Procter Date: Thu, 12 Feb 2015 16:49:32 +0000 Subject: [PATCH] JAL-1350 cloned method and tested equivalence for using column selection to subdivide groups --- src/jalview/analysis/Grouping.java | 82 ++++++++++++++++++++++++++++ test/jalview/analysis/GroupingTest.java | 89 +++++++++++++++++++++++++++++++ 2 files changed, 171 insertions(+) create mode 100644 test/jalview/analysis/GroupingTest.java diff --git a/src/jalview/analysis/Grouping.java b/src/jalview/analysis/Grouping.java index 7b6dda1..ea10c9a 100644 --- a/src/jalview/analysis/Grouping.java +++ b/src/jalview/analysis/Grouping.java @@ -102,6 +102,88 @@ public class Grouping } /** + * Divide the given sequences based on the equivalence of characters at + * selected columns If exgroups is provided, existing groups will be + * subdivided. + * + * @param sequences + * @param columnSelection + * @param list + * @return + */ + public static SequenceGroup[] makeGroupsFromCols(SequenceI[] sequences, + ColumnSelection cs, List list) + { + // TODO: determine how to get/recover input data for group generation + Map> gps = new HashMap>(); + Map pgroup = new HashMap(); + if (list != null) + { + for (SequenceGroup sg : list) + { + for (SequenceI sq : sg.getSequences(null)) + { + pgroup.put(sq.toString(), sg); + } + } + } + int[] spos = new int[cs.getSelected().size()]; + int width = -1; + int i = 0; + for (Integer pos : cs.getSelected()) + { + spos[i++] = pos.intValue(); + } + ; + for (i = 0; i < sequences.length; i++) + { + int slen = sequences[i].getLength(); + if (width < slen) + { + width = slen; + } + + SequenceGroup pgp = pgroup.get(((Object) sequences[i]).toString()); + StringBuilder schar = new StringBuilder(); + if (pgp != null) + { + schar.append(pgp.getName() + ":"); + } + for (int p : spos) + { + if (p >= slen) + { + schar.append("~"); + } + else + { + schar.append(sequences[i].getCharAt(p)); + } + } + List svec = gps.get(schar.toString()); + if (svec == null) + { + svec = new ArrayList(); + gps.put(schar.toString(), svec); + } + svec.add(sequences[i]); + } + // make some groups + SequenceGroup[] groups = new SequenceGroup[gps.size()]; + i = 0; + for (String key : gps.keySet()) + { + SequenceGroup group = new SequenceGroup(gps.get(key), "Subseq: " + + key, null, true, true, false, 0, width - 1); + + groups[i++] = group; + } + gps.clear(); + pgroup.clear(); + return groups; + } + + /** * subdivide the given sequences based on the distribution of features * * @param featureLabels diff --git a/test/jalview/analysis/GroupingTest.java b/test/jalview/analysis/GroupingTest.java new file mode 100644 index 0000000..61bd480 --- /dev/null +++ b/test/jalview/analysis/GroupingTest.java @@ -0,0 +1,89 @@ +package jalview.analysis; + +import static org.junit.Assert.fail; +import jalview.datamodel.Alignment; +import jalview.datamodel.AlignmentI; +import jalview.datamodel.ColumnSelection; +import jalview.datamodel.Sequence; +import jalview.datamodel.SequenceGroup; +import jalview.datamodel.SequenceI; + +import java.util.ArrayList; +import java.util.Arrays; + +import org.junit.Assert; +import org.junit.Test; + +public class GroupingTest +{ + Sequence s1 = new Sequence("s1", "AAAADDDDEEEE"); + + Sequence s2 = new Sequence("s2", "AAAADDDDEEEE"); + + Sequence s3 = new Sequence("s3", "ACAADDEDEEEE"); + + Sequence s4 = new Sequence("s4", "AAAADDEDEEEE"); + + Sequence s5 = new Sequence("s5", "AAAADDEDTTEE"); + + SequenceGroup sg1 = new SequenceGroup(Arrays.asList(new SequenceI[] + { s1, s2 }), "Group1", null, false, false, false, 0, 5); + + SequenceGroup sg2 = new SequenceGroup(Arrays.asList(new SequenceI[] + { s3, s4, s5 }), "Group2", null, false, false, false, 0, 5); + + AlignmentI alignment = new Alignment(new SequenceI[] + { s1, s2, s3, s4, s5 }); + + int[] positions = new int[] + { 1, 7, 9 }; + + @Test + public void testMakeGroupsWithBoth() + { + ArrayList str = new ArrayList(); + for (SequenceI s : alignment.getSequences()) + { + StringBuilder sb = new StringBuilder(); + for (int p : positions) + { + sb.append(s.getCharAt(p)); + } + str.add(sb.toString()); + } + SequenceGroup[] seqgroupsString = Grouping.makeGroupsFrom( + alignment.getSequencesArray(), + str.toArray(new String[str.size()]), + Arrays.asList(new SequenceGroup[] + { sg1, sg2 })); + ColumnSelection cs = new ColumnSelection(); + for (int p : positions) + { + cs.addElement(p); + } + SequenceGroup[] seqgroupsColSel = Grouping.makeGroupsFromCols( + alignment.getSequencesArray(), cs, + Arrays.asList(new SequenceGroup[] + { sg1, sg2 })); + Assert.assertEquals(seqgroupsString.length, seqgroupsColSel.length); + for (int p = 0; p < seqgroupsString.length; p++) + { + Assert.assertEquals(seqgroupsString[p].getName(), + seqgroupsColSel[p].getName()); + Assert.assertArrayEquals( + seqgroupsString[p].getSequencesInOrder(alignment), + seqgroupsColSel[p].getSequencesInOrder(alignment)); + if (seqgroupsString[p].getSequences().contains(s2)) + { + Assert.assertTrue(seqgroupsString[p].getSize() == 2); + } + } + } + + @Test + public void testMakeGroupsFromCols() + { + fail("Not yet implemented"); // TODO + } + +} -- 1.7.10.2