1 package jalview.analysis;
3 import java.util.ArrayList;
4 import java.util.Hashtable;
7 public class SecStrConsensus {
11 * Internal class to represent a simple base-pair.
13 * [JBPNote: ^^ is that Anne Menard or Ya(w)nn Ponty, I wonder ! ]
15 public static class SimpleBP{
23 public SimpleBP(int i5, int i3)
28 public void setBP5(int i5)
33 public void setBP3(int i3)
48 public String toString()
50 return "("+bp5+","+bp3+")";
55 public static int[] extractConsensus(ArrayList<ArrayList<SimpleBP>> bps)
57 // We do not currently know the length of the alignment
58 // => Estimate it as the biggest index of a base-pair plus one.
60 for (ArrayList<SimpleBP> strs : bps)
62 for (SimpleBP bp : strs)
65 maxlength = Math.max(1+Math.max(bp.bp5, bp.bp3), maxlength);
69 // Now we have a good estimate for length, allocate and initialize data
70 // to be fed to the dynamic programming procedure.
71 ArrayList<Hashtable<Integer,Double>> seq = new ArrayList<Hashtable<Integer,Double>>();
72 for (int i=0;i<maxlength;i++)
73 { seq.add(new Hashtable<Integer,Double>()); }
74 for (ArrayList<SimpleBP> strs : bps)
76 for (SimpleBP bp : strs)
80 Hashtable<Integer,Double> h = seq.get(i);
81 if (!h.containsKey(j))
85 h.put(j, h.get(j)+1.);
88 // At this point, seq contains, at each position i, a hashtable which associates,
89 // to each possible end j, the number of time a base-pair (i,j) occurs in the alignment
91 // We can now run the dynamic programming procedure on this data
92 double[][] mat = fillMatrix(seq);
93 ArrayList<SimpleBP> res = backtrack(mat,seq);
95 // Convert it to an array, ie finalres[i] = j >= 0 iff a base-pair (i,j) is present
96 // in the consensus, or -1 otherwise
97 int[] finalres = new int[seq.size()];
98 for (int i=0;i<seq.size();i++)
100 for (SimpleBP bp : res)
102 finalres[bp.bp5] = bp.bp3;
103 finalres[bp.bp3] = bp.bp5;
109 private static boolean canBasePair(ArrayList<Hashtable<Integer,Double>> seq, int i, int k)
111 return seq.get(i).containsKey(k);
114 // Returns the score of a potential base-pair, ie the number of structures in which it is found.
115 private static double basePairScore(ArrayList<Hashtable<Integer,Double>> seq, int i, int k)
117 return seq.get(i).get(k);
121 private static double[][] fillMatrix(ArrayList<Hashtable<Integer,Double>> seq)
124 double[][] tab = new double[n][n];
125 for(int m=1;m<=n;m++)
127 for(int i=0;i<n-m+1;i++)
133 tab[i][j] = Math.max(tab[i][j], tab[i+1][j]);
134 for (int k=i+1;k<=j;k++)
136 if (canBasePair(seq,i,k))
141 fact1 = tab[i+1][k-1];
148 tab[i][j] = Math.max(tab[i][j],basePairScore(seq,i,k)+fact1+fact2);
157 private static ArrayList<SimpleBP> backtrack(double[][] tab,ArrayList<Hashtable<Integer,Double>> seq)
159 return backtrack(tab,seq,0,seq.size()-1);
162 private static ArrayList<SimpleBP> backtrack(double[][] tab,ArrayList<Hashtable<Integer,Double>> seq, int i, int j)
164 ArrayList<SimpleBP> result = new ArrayList<SimpleBP>();
167 ArrayList<Integer> indices = new ArrayList<Integer>();
169 for (int k=i+1;k<=j;k++)
173 for (int k : indices)
177 if (tab[i][j] == tab[i+1][j])
179 result = backtrack(tab, seq, i+1,j);
184 if (canBasePair(seq,i,k))
189 fact1 = tab[i+1][k-1];
196 if (tab[i][j]==basePairScore(seq,i,k)+fact1+fact2)
198 result = backtrack(tab, seq, i+1,k-1);
199 result.addAll(backtrack(tab, seq, k+1,j));
200 result.add(new SimpleBP(i,k));