src/org/gel/mauve/analysis/BrokenCDS.java

Go to the documentation of this file.
00001 package org.gel.mauve.analysis;
00002 
00003 import java.text.NumberFormat;
00004 import java.util.Iterator;
00005 import java.util.TreeMap;
00006 import java.util.TreeSet;
00007 import java.util.Vector;
00008 
00009 public class BrokenCDS {
00010         
00011         private LiteWeightFeature cds;
00012         
00013         
00014         private TreeMap<Integer, Character> prmtrStops;
00015         
00022         private TreeMap<Integer,char[]> naSubs;
00023         
00024         private TreeMap<Integer, char[]> aaSubs;
00025         
00026         private TreeMap<Integer,Integer> frmShfts;
00027         
00028         private TreeSet<Integer> insStops;
00029         
00031         private Vector<int[]> bfSegs;
00032         
00033         private Vector<int[]> gapSegs;
00034 
00035         private double aaSubRate;
00036         
00037         public BrokenCDS(LiteWeightFeature cds){
00038                 if (cds.getLength() % 3 != 0) {
00039                         throw new IllegalArgumentException("CDS length must be divisible by 3.");
00040                 }
00041                 this.cds = cds;
00042                 prmtrStops = new TreeMap<Integer,Character>();
00043                 naSubs = new TreeMap<Integer, char[]>();
00044                 aaSubs = new TreeMap<Integer, char[]>();
00045                 frmShfts = new TreeMap<Integer, Integer>();
00046                 bfSegs = new Vector<int[]>();
00047                 gapSegs = new Vector<int[]>();
00048                 insStops = new TreeSet<Integer>();
00049         }
00050         
00051         public void addNASubstitution(int pos, char from, char to){
00052                 char[] pat = {from, to};
00053                 naSubs.put(pos, pat);
00054         }
00055         
00056         public void addAASubstitution(int pos, char from, char to) {
00057                 char[] pat = {from, to};
00058                 aaSubs.put(pos, pat);
00059         }
00060         
00061         public void setAASubRate(double perc) {
00062                 if (perc < 0 || perc > 1) {
00063                         throw new IllegalArgumentException("Error rates must be between 0-1, inclusive.");
00064                 }
00065                 aaSubRate = perc;
00066         }
00067         
00068         public void addPrmtrStop(int pos, char from){
00069                 prmtrStops.put(pos,from);
00070         }
00071         
00072         public void addInsertionStop(int pos){
00073                 insStops.add(pos);
00074         }
00075         
00076         public void addBFSegment(int[] range) {
00077                 if (bfSegs.contains(range)){
00078                         System.out.flush();
00079                 }
00080                 bfSegs.add(range);
00081         }
00082         
00083         public void addGapSegment(int[] range) {
00084                 gapSegs.add(range);
00085         }
00086         
00087         public void addFrameShift(int pos, int length) throws IllegalArgumentException{
00088                 if (length !=1 && length !=2){
00089                         throw new IllegalArgumentException("Illegal length: " + length + " - Frameshifts can only be of length 1 or 2.");
00090                 } else {
00091                         frmShfts.put(pos, length);
00092                 }
00093         }
00094         
00095         public String toString(){
00096                 /*FeatureID Peptide_Length PercIncAAs BrokenFrameSegments GapSegments Subst_Positions Subst_Patterns PrmtrStop_Positions PrmtrStop_OrigRes Frame_Shift_Stops*/
00097                 StringBuilder sb = new StringBuilder();
00098                 NumberFormat nf = NumberFormat.getInstance();
00099                 nf.setMaximumFractionDigits(6);
00100                 
00101                 sb.append(cds.getID()+"\t"+getPeptideLength()+"\t"+nf.format(aaSubRate));
00102                 
00103                 String tmp = sb.toString();
00104                 if (bfSegs.size() > 0) {
00105                         Iterator<int[]> bfIt = bfSegs.iterator();
00106                         boolean first = true;
00107                         while(bfIt.hasNext()){
00108                                 int[] seg = bfIt.next();
00109                                 if (first) {
00110                                         first = false;
00111                                         sb.append("\t["+seg[0]+","+seg[1]+"]");
00112                                 } else {
00113                                         sb.append(",["+seg[0]+","+seg[1]+"]");
00114                                 }
00115                         }
00116                 } else {
00117                         sb.append("\t-");
00118                 }
00119                 
00120                 tmp = sb.toString();
00121                 if (gapSegs.size() > 0) {
00122                         Iterator<int[]> gapIt = gapSegs.iterator();
00123                         boolean first = true;
00124                         while (gapIt.hasNext()) {
00125                                 int[] seg = gapIt.next();
00126                                 if (first) {
00127                                         first = false;
00128                                         sb.append("\t["+seg[0]+","+seg[1]+"]");
00129                                 } else {
00130                                         sb.append(",["+seg[0]+","+seg[1]+"]");
00131                                 }
00132                         }
00133                 } else {
00134                         sb.append("\t-");
00135                 }
00136                 
00137                 
00138                 tmp = sb.toString();
00139                 if (aaSubs.size() > 0) {
00140                         Iterator<Integer> it = aaSubs.keySet().iterator();
00141                         StringBuilder subPos = new StringBuilder();
00142                         StringBuilder subPat = new StringBuilder();
00143                         boolean first = true;
00144                         while(it.hasNext()){
00145                                 int pos = it.next();
00146                                 char[] pat = aaSubs.get(pos);
00147                                 if (first){
00148                                         subPat.append(pat[0] +"->"+ pat[1]);
00149                                         subPos.append(pos);
00150                                         first = false;
00151                                 } else {
00152                                         subPat.append(","+pat[0] +"->"+ pat[1]);
00153                                         subPos.append(","+pos);
00154                                 }
00155                         }
00156                         sb.append("\t"+subPos.toString()+"\t"+subPat.toString());
00157                 } else {
00158                         sb.append("\t-\t-");
00159                 }
00160                 if (prmtrStops.size() > 0) {
00161                         Iterator<Integer> it = prmtrStops.keySet().iterator();
00162                         StringBuilder subPos = new StringBuilder();
00163                         StringBuilder subPat = new StringBuilder();
00164                         subPat = new StringBuilder();
00165                         boolean first = true;
00166                         while(it.hasNext()){
00167                                 int pos = it.next();
00168                                 char c = prmtrStops.get(pos);
00169                                 if (first) {
00170                                         subPos.append(pos);
00171                                         subPat.append(c);
00172                                         first = false;
00173                                 } else {
00174                                         subPos.append(","+pos);
00175                                         subPat.append(","+c);
00176                                 }
00177                         }
00178                         sb.append("\t"+subPos.toString()+"\t"+subPat.toString());
00179                 } else {
00180                         sb.append("\t-\t-");
00181                 }
00182                 
00183                 if (insStops.size() > 0) {
00184                         Iterator<Integer> it = insStops.iterator();
00185                         boolean first = true;
00186                         while (it.hasNext()) {
00187                                 int pos = it.next();
00188                                 if (first) {
00189                                         sb.append("\t"+Integer.toString(pos));
00190                                         first = false;
00191                                 } else {
00192                                         sb.append(","+Integer.toString(pos));
00193                                 }
00194                         }
00195                 }
00196                 
00197                 return sb.toString();
00198         }
00199         
00200         public int getPeptideLength(){
00201                 return (cds.getRight() - cds.getLeft() + 1) / 3;
00202         }
00203         
00204         public double getPercIncPeptides(){
00205                 return aaSubRate;
00206         }
00207         
00208 }

Generated on Mon Aug 19 06:03:40 2013 for Mauve by doxygen 1.3.6