From: jprocter Date: Fri, 21 Aug 2009 10:57:20 +0000 (+0000) Subject: routines for creating groups over an alignment X-Git-Tag: Release_2_5~216 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=87deef0fa44f3aa7d7d86ac7241a9d8c93b2408f;p=jalview.git routines for creating groups over an alignment --- diff --git a/src/jalview/analysis/Grouping.java b/src/jalview/analysis/Grouping.java new file mode 100644 index 0000000..bba668d --- /dev/null +++ b/src/jalview/analysis/Grouping.java @@ -0,0 +1,273 @@ +package jalview.analysis; + +import jalview.datamodel.AlignmentI; +import jalview.datamodel.SequenceFeature; +import jalview.datamodel.SequenceGroup; +import jalview.datamodel.SequenceI; + +import java.util.Enumeration; +import java.util.Hashtable; +import java.util.Vector; + +/** + * various methods for defining groups on an alignment based on some other properties + * @author JimP + * + */ +public class Grouping +{ + /** + * Divide the given sequences based on the equivalence of their corresponding selectedChars string. If exgroups is provided, existing groups will be subdivided. + * @param sequences + * @param selectedChars + * @param exgroups + * @return + */ + public static SequenceGroup[] makeGroupsFrom(SequenceI[] sequences, String[] selectedChars, Vector exgroups) + { + // TODO: determine how to get/recover input data for group generation + Hashtable gps = new Hashtable(); + int width = 0,i; + Hashtable pgroup = new Hashtable(); + if (exgroups!=null) + { + SequenceGroup sg; + for (Enumeration g=exgroups.elements(); g.hasMoreElements(); ) + { + sg = (SequenceGroup) g.nextElement(); + for (Enumeration sq = sg.getSequences(null).elements(); sq.hasMoreElements(); ) + pgroup.put(sq.nextElement().toString(), sg); + } + } + for (i = 0; i < sequences.length; i++) + { + String schar = selectedChars[i]; + SequenceGroup pgp = (SequenceGroup) pgroup.get(((Object) sequences[i]).toString()); + if (pgp!=null) + { + schar = pgp.getName()+":"+schar; + } + Vector svec = (Vector) gps.get(schar); + if (svec == null) + { + svec = new Vector(); + gps.put(schar, svec); + } + if (width sstop) + || + // or ignore based on selection criteria + (featureLabels != null && !AlignmentSorter.containsIgnoreCase(sf[f].type, featureLabels)) + || (groupLabels != null + // problem here: we cannot eliminate null feature group features + && (sf[f].getFeatureGroup() != null + && !AlignmentSorter.containsIgnoreCase(sf[f].getFeatureGroup(), groupLabels)))) + { + // forget about this feature + sf[f] = null; + n--; + } else { + // or, also take a look at the scores if necessary. + if (!ignoreScore && sf[f].getScore()!=Float.NaN) + { + if (seqScores[i]==0) + { + hasScores++; + } + seqScores[i]++; + hasScore[i] = true; + scores[i] += sf[f].getScore(); // take the first instance of this + // score. + } + } + } + SequenceFeature[] fs; + feats[i] = fs = new SequenceFeature[n]; + if (n>0) + { + n=0; + for (int f=0;f scores[i]) + { + min = scores[i]; + } + } + } + } + + if (method==FEATURE_SCORE) + { + if (hasScores == 0) + { + return; // do nothing - no scores present to sort by. + } + // pad score matrix + if (hasScores < seqs.length) + { + for (int i = 0; i < seqs.length; i++) + { + if (!hasScore[i]) + { + scores[i] = (max + i); + } else { + int nf=(feats[i]==null) ? 0 :((SequenceFeature[]) feats[i]).length; + System.err.println("Sorting on Score: seq "+seqs[i].getName()+ " Feats: "+nf+" Score : "+scores[i]); + } + } + } + + jalview.util.QuickSort.sort(scores, seqs); + } + else + if (method==FEATURE_DENSITY) + { + + // break ties between equivalent numbers for adjacent sequences by adding 1/Nseq*i on the original order + double fr = 0.9/(1.0*seqs.length); + for (int i=0;i