src/org/gel/mauve/summary/output/IslandGeneFeatureWriter.java

Go to the documentation of this file.
00001 package org.gel.mauve.summary.output;
00002 
00003 import java.util.Collections;
00004 import java.util.Hashtable;
00005 import java.util.Iterator;
00006 import java.util.ListIterator;
00007 import java.util.Vector;
00008 
00009 import org.biojava.bio.seq.Feature;
00010 import org.biojava.bio.seq.StrandedFeature;
00011 import org.biojava.bio.symbol.Location;
00012 import org.gel.air.util.MathUtils;
00013 import org.gel.mauve.BaseViewerModel;
00014 import org.gel.mauve.MauveHelperFunctions;
00015 import org.gel.mauve.analysis.Segment;
00016 
00017 public class IslandGeneFeatureWriter extends IslandFeatureWriter {
00018         
00019         public static final String PERCENT = "prct_on_is";
00020         public static final int ISLAND_COL = -2;
00021         public static final int PERCENT_COL = -1;
00022         public static final String BACKBONE_MASK = "backbone_mask";
00023         public static StringBuffer ids = new StringBuffer ();
00024         public static int buffer_count;
00025         
00026         protected BaseViewerModel model;
00027         protected ListIterator iterator;
00028         protected Feature cur_feat;
00029         protected double cur_percent;
00030         protected double minimum_percent;
00031         protected int [][] num_per_multiplicity;
00032         protected boolean backbone_instead;
00033         protected int [] num_features;
00034         
00035         public static final String ISLAND_GENE = "island_gene";
00036         
00037         
00038         protected IslandGeneFeatureWriter (SegmentDataProcessor processor) {
00039                 super (MauveHelperFunctions.getSeqPartOfFile (processor) + (processor.get (
00040                                 BACKBONE_MASK) != null ? "backbone" : "island") + "_genes", processor);
00041         }
00042         
00043         protected void initSubClassParticulars (Hashtable args) {
00044                 super.initSubClassParticulars (args);
00045                 model = (BaseViewerModel) args.get (MODEL);
00046                 if (args.get (MINIMUM_PERCENT_CONTAINED) != null)
00047                         minimum_percent = ((Double) args.get (
00048                                         MINIMUM_PERCENT_CONTAINED)).doubleValue ();
00049                 else {
00050                         minimum_percent = DEFAULT_MIN_PERCENT_CONTAINED;
00051                         args.put (MINIMUM_PERCENT_CONTAINED, new Double (minimum_percent));
00052                 }
00053                 if (args.get (BACKBONE_MASK) != null)
00054                         backbone_instead = true;
00055                 num_per_multiplicity = ((int [][]) args.get (NUM_GENES_PER_MULT));
00056                 Iterator itty = MauveHelperFunctions.getFeatures (model, seq_index);
00057                 Vector vector = new Vector ();
00058                 while (itty.hasNext ())
00059                         vector.add (itty.next ());
00060                 Collections.sort (vector, MauveHelperFunctions.FEATURE_COMPARATOR);
00061                 num_features = (int []) args.get (TOTAL_GENES);
00062                 num_features [seq_index] = vector.size ();
00063                 System.out.println ("seq: " + seq_index + " features: " + num_features [seq_index]);
00064                 iterator = vector.listIterator ();
00065                 if (iterator.hasNext ())
00066                         cur_feat = (Feature) iterator.next ();
00067         }
00068         
00069         public Vector setColumnHeaders () {
00070                 Vector vect = super.setColumnHeaders ();
00071                 vect.remove (vect.size () - 1);
00072                 vect.add (0, PERCENT);
00073                 vect.add (0, ISLAND);
00074                 vect.add (Segment.MULTIPLICITY_STRING);
00075                 return vect;
00076         }
00077         
00078         protected String getData (int col, int row) {
00079                 col -= 2;
00080                 Location loci = cur_feat.getLocation ();
00081                 long value = 0;
00082                 switch (col) {
00083                         case ISLAND_COL:
00084                                 return current.typed_id;
00085                         case PERCENT_COL:
00086                                 return MauveHelperFunctions.doubleToString (cur_percent, 2);
00087                         case TYPE:
00088                                 return ISLAND_GENE;
00089                         case LABEL:
00090                                 String id = MauveHelperFunctions.getUniqueId (cur_feat);
00091                                 if (!backbone_instead && current.multiplicityType () < multiplicity << 1) {
00092                                         buffer_count++;
00093                                         ids.append (id.substring (5));
00094                                         ids.append (',');
00095                                 }
00096                                 return id;
00097                         case CONTIG:
00098                                 return contig_handler.getContigName (seq_index, loci.getMin ());
00099                         case STRAND:
00100                                 if (!(cur_feat instanceof StrandedFeature))
00101                                         System.out.println ("bad cast");
00102                                 return ((StrandedFeature) cur_feat).getStrand () == 
00103                                         StrandedFeature.NEGATIVE ? COMPLEMENT : FORWARD;
00104                         case LEFT:
00105                                 value = loci.getMin ();
00106                                 break;
00107                         case RIGHT:
00108                                 value = loci.getMax ();
00109                                 break;
00110                         case MULTIPLICITY_INDEX:
00111                                 String mult = MauveHelperFunctions.getReadableMultiplicity (current);
00112                                 performComplexIteration ();
00113                                 return mult;
00114                 }
00115                 return adjustForContigs (seq_index, value) + "";
00116         }
00117         
00118         public boolean badType (Feature feat) {
00119                 String type = feat.getType ().toLowerCase ();
00120                 if (type.indexOf ("rna") > -1 || type.indexOf ("gene") > -1 || 
00121                                 type.indexOf ("cds") > -1 || type.indexOf ("asap") > -1)
00122                         return false;
00123                 else {
00124                         num_features [seq_index]--;
00125                         return true;
00126                 }
00127         }
00128         
00129         public void printData () {
00130                 if (cur_feat != null)
00131                         super.printData();
00132         }
00133 
00134         public boolean shouldPrintRow (int row) {
00135                 Location loci = cur_feat.getLocation ();
00136                 boolean print = false;
00137                 while ((badType (cur_feat) || loci.getMax () <= current.left [seq_index]) &&
00138                                 iterator.hasNext ()) {
00139                         //cur_feat = (Feature) iterator.next ();
00140                         performComplexIteration ();
00141                         if (cur_feat != null)
00142                                 loci = cur_feat.getLocation ();
00143                         else
00144                                 loci = null;
00145                 }
00146                 if (loci != null && shouldPrintSegment (row) && loci.getMin () < 
00147                                 current.right [seq_index]) {
00148                         if (cur_feat instanceof StrandedFeature) {
00149                                 cur_percent = MathUtils.percentContained (loci.getMin (), loci.getMax (), 
00150                                                 current.left [seq_index], current.right [seq_index]);
00151                                 if (!(cur_percent >= minimum_percent)) {
00152                                         if (loci.getMax () < current.right [seq_index] || 
00153                                                         current.nexts [seq_index] == Segment.END) {
00154                                                 performComplexIteration ();
00155                                         }
00156                                         else {
00157                                                 current = current.nexts [seq_index];
00158                                         }
00159                                 }
00160                                 else {
00161                                         print = true;
00162                                         num_per_multiplicity [seq_index][(int) current.multiplicityType () - 1] += 1;
00163                                 }
00164                         }
00165                 }
00166                 return print;
00167         }
00168         
00169         protected void performComplexIteration () {
00170                 cur_feat = iterator.hasNext () ? (Feature) iterator.next () : null;
00171                 while (cur_feat != null && cur_feat.getLocation ().getMin () < 
00172                                 current.left [seq_index] && current.prevs [seq_index] != Segment.END)
00173                         current = current.prevs [seq_index];
00174         }
00175         
00176         protected boolean shouldPrintSegment (int row) {
00177                 if (!backbone_instead)
00178                         return super.shouldPrintRow (row);
00179                 else
00180                         return current.multiplicityType () == all_seq_multiplicity ? true : false;
00181         }
00182         
00183         protected boolean moreRowsToPrint () {
00184                 if (cur_feat == null)
00185                         return false;
00186                 Location loci = cur_feat.getLocation ();
00187                 if (loci.getMin () >= current.right [seq_index] || !shouldPrintSegment (row_number))
00188                         return super.moreRowsToPrint ();
00189                 else
00190                         return true;
00191         }
00192         
00193         public static void printIslandsAsFeatures (SegmentDataProcessor processor) {
00194                 int count = ((Object []) processor.get (FIRSTS)).length;
00195                 long all_mult = ((Long) processor.get (ALL_MULTIPLICITY)).longValue ();
00196                 processor.put (NUM_GENES_PER_MULT, new int [count][(int) all_mult]);
00197                 processor.put (TOTAL_GENES, new int [count]);
00198                 for (int i = 0; i < count; i++) {
00199                         processor.put (SEQUENCE_INDEX, new Integer (i));
00200                         new IslandGeneFeatureWriter (processor);
00201                         if (i == count - 1 && processor.get (BACKBONE_MASK) == null) {
00202                                 processor.put (BACKBONE_MASK, new Object ());
00203                                 i = -1;
00204                         }
00205                 }
00206         }
00207 
00208 }

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