00001 package org.gel.mauve.backbone; 00002 00003 import java.util.Arrays; 00004 import java.util.Comparator; 00005 import java.util.Vector; 00006 00007 import org.gel.mauve.Genome; 00008 import org.gel.mauve.XMFAAlignment; 00009 00010 public class BackboneList { 00014 static class BbLeftEndComparator implements Comparator { 00015 protected Genome g; 00016 00017 BbLeftEndComparator (Genome g) { 00018 this.g = g; 00019 } 00020 00021 public int compare (Object o_a, Object o_b) { 00022 Backbone a = (Backbone) o_a; 00023 Backbone b = (Backbone) o_b; 00024 boolean a_def = a.getSeqs ()[g.getSourceIndex ()]; 00025 boolean b_def = b.getSeqs ()[g.getSourceIndex ()]; 00026 if (!a_def && !b_def) 00027 return 0; 00028 if (!a_def) 00029 return -1; 00030 if (!b_def) 00031 return 1; 00032 long a_lend = a.getLeftEnd (g); 00033 long b_lend = b.getLeftEnd (g); 00034 if (a_lend == b_lend) 00035 return (int) (a.getRightEnd (g) - b.getRightEnd (g)); 00036 return (int) (a_lend - b_lend); 00037 } 00038 } 00039 00043 static class BbComparator implements Comparator { 00044 protected int seq; 00045 00046 BbComparator (int seq) { 00047 this.seq = seq; 00048 } 00049 00050 public int compare (Object o_a, Object o_b) { 00051 Backbone a = (Backbone) o_a; 00052 Backbone b = (Backbone) o_b; 00053 boolean a_def = a.getSeqs ()[seq]; 00054 boolean b_def = b.getSeqs ()[seq]; 00055 if (!a_def && !b_def) 00056 return 0; 00057 if (!a_def) 00058 return -1; 00059 if (!b_def) 00060 return 1; 00061 int a_lcb_id = a.getLcbIndex (); 00062 int b_lcb_id = b.getLcbIndex (); 00063 if (a_lcb_id != b_lcb_id) 00064 return a_lcb_id - b_lcb_id; 00065 return (int) (a.getLeftColumn () - b.getLeftColumn ()); 00066 } 00067 } 00068 00069 protected Vector seq_bb; 00070 00071 protected Backbone [] bb_array; 00072 00073 protected XMFAAlignment xmfa; 00074 00075 public void setXmfa (XMFAAlignment xmfa) { 00076 this.xmfa = xmfa; 00077 } 00078 00079 public void setBackbone (Backbone [] bb_array) { 00080 this.bb_array = bb_array; 00081 } 00082 00083 public void setSeqBackbone (Vector seq_bb) { 00084 this.seq_bb = seq_bb; 00085 } 00086 00087 public Backbone getNextBackbone (Genome g, long position) { 00088 Backbone [] seq_bb_array = (Backbone []) seq_bb.elementAt (g 00089 .getSourceIndex ()); 00090 if (seq_bb_array.length == 0) 00091 return null; 00092 if (position < 1) 00093 position = 1; 00094 if (position > g.getLength ()) 00095 position = g.getLength (); 00096 long [] lcb_and_column = xmfa.getLCBAndColumn (g, position); 00097 Backbone key = new Backbone (); 00098 key.setLcbIndex ((int) lcb_and_column[0]); 00099 key.setLeftColumn (lcb_and_column[1]); 00100 key.setLength (0); 00101 boolean [] seqs = new boolean [seq_bb.size ()]; 00102 seqs[g.getSourceIndex ()] = true; 00103 key.setSeqs (seqs); 00104 long [] left_ends = new long [seq_bb.size ()]; 00105 left_ends[g.getSourceIndex ()] = position; 00106 key.setLeftEnd (left_ends); 00107 key.setRightEnd (left_ends); 00108 BbLeftEndComparator comp = new BbLeftEndComparator (g); 00109 int indie = Arrays.binarySearch (seq_bb_array, key, comp); 00110 if (indie < 0) { 00111 indie = -indie - 1; 00112 // scan backwards to see whether the position was contained in any 00113 // previous 00114 // bb segments 00115 if (indie >= seq_bb_array.length 00116 || position < seq_bb_array[indie].getLeftEnd (g)) { 00117 int prev_i = indie - 1; 00118 while (prev_i >= 0) { 00119 if (seq_bb_array[prev_i].getLeftEnd (g) <= position 00120 && position <= seq_bb_array[prev_i].getRightEnd (g)) { 00121 indie = prev_i; // contained by the previous bb seg 00122 break; 00123 } 00124 if (prev_i > 0 00125 && seq_bb_array[prev_i].getLeftEnd (g) == seq_bb_array[prev_i - 1] 00126 .getLeftEnd (g)) 00127 prev_i--; 00128 else 00129 break; 00130 } 00131 } 00132 } 00133 00134 if (indie == seq_bb_array.length) 00135 return null; 00136 return seq_bb_array[indie]; 00137 } 00138 00139 public Backbone getBackbone (Genome g, long position) { 00140 Backbone bb = getNextBackbone (g, position); 00141 if (bb != null && bb.getLeftEnd (g) <= position 00142 && position <= bb.getRightEnd (g)) 00143 return bb; 00144 return null; 00145 } 00146 00147 public Backbone [] getBackboneArray () { 00148 return bb_array; 00149 } 00150 }
1.3.6