Changeset 4926


Ignore:
Timestamp:
May 11, 2009, 1:01:45 PM (12 years ago)
Author:
Nicklas Nordborg
Message:

References #742: Refactor BioAssaySetExporter?

Added some more support functionality into superclasses.

Location:
trunk/src/core/net/sf/basedb/util
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/util/basefile/BaseFileWriter.java

    r4925 r4926  
    2626
    2727import net.sf.basedb.core.InvalidDataException;
     28import net.sf.basedb.util.export.TableWriter;
    2829
    2930/**
     
    5051*/
    5152public class BaseFileWriter
    52   extends PrintWriter
     53  extends TableWriter
    5354{
    5455  private String metadataPrefix = "";
    55   private String dataSeparator = "\t";
    56   private String nullValue = "";
    5756 
    5857  private String currentSection;
     
    104103  }
    105104 
    106   /**
    107     The data separator string is written between
    108     each data column in the output. The default value is 'tab'.
    109     @return The current value of the data separator string
    110   */
    111   public String getDataSeparator()
    112   {
    113     return dataSeparator;
    114   }
    115  
    116   /**
    117     Change the data separator string.
    118     @param dataSeparator The new data separator
    119   */
    120   public void setDataSeparator(String dataSeparator)
    121   {
    122     this.dataSeparator = dataSeparator;
    123   }
    124  
    125   /**
    126     The null value string is written to the output instead of
    127     null data values. The default value is an empty string.
    128     @return The current value of the null value string
    129   */
    130   public String getNullValue()
    131   {
    132     return nullValue;
    133   }
    134  
    135   /**
    136     Change the null value string.
    137     @param nullValue The new null value string
    138   */
    139   public void setNullValue(String nullValue)
    140   {
    141     this.nullValue = nullValue;
    142   }
    143 
    144105  /**
    145106    The metadata prefix is written to the beginning of
     
    259220  {
    260221    if (!isWritingData()) return;
    261     boolean first = true;
    262     String ds = getDataSeparator();
    263     String nv = getNullValue();
    264     for (Object d : data)
    265     {
    266       if (!first) print(ds);
    267       if (d != null) print(d == null ? nv : d.toString());
    268       first = false;
    269     }
    270     println();
     222    tablePrintData(data);
    271223  }
    272224 
  • trunk/src/core/net/sf/basedb/util/export/spotdata/AbstractBioAssaySetExporter.java

    r4925 r4926  
    109109  private List<BioAssay> bioAssays;
    110110  private Map<Integer, Object[]> reporterCache;
     111  private Map<Short, Integer> assayIndexMap;
    111112 
    112113  protected AbstractBioAssaySetExporter()
     
    495496    // Create and prepare query
    496497    DynamicPositionQuery query = source.getPositionData();
    497     query.joinReporters(JoinType.INNER);
     498    query.joinReporters(JoinType.LEFT);
    498499   
    499500    // Add reporter fields
     
    584585 
    585586  /**
     587    Prepare a cache that maps the bioassay column ({@link BioAssay#getDataCubeColumnNo()})
     588    value to an index in the 'data' array this is generated. The map is generated as
     589    follows:
     590    <ul>
     591    <li>first assay -&gt; firstIndex
     592    <li>second assay -&gt; firstIndex + columnsPerAssay
     593    <li>third assay -&gt; firtsIndex + 2 * columnsPerAssay
     594    <li>... and so on
     595    </ul>
     596    <p>
     597    Use {@link #getAssayIndex(short)} to get the indexed values.
     598   
     599    @param assays The assays that should be mapped
     600    @param firstIndex The index of the first assay
     601    @param columnsPerAssay Number of columns for each assay
     602  */
     603  protected void prepareAssayIndexMap(List<BioAssay> assays, int firstIndex, int columnsPerAssay)
     604  {
     605    if (assayIndexMap == null) assayIndexMap = new HashMap<Short, Integer>(assays.size());
     606    int index = firstIndex;
     607    for (BioAssay ba : assays)
     608    {
     609      assayIndexMap.put(ba.getDataCubeColumnNo(), index);
     610      index += columnsPerAssay;
     611    }
     612  }
     613 
     614  /**
     615    Get the cached index value for an assay when you know the assay's column
     616    number.
     617    @param column The column number ({@link BioAssay#getDataCubeColumnNo()})
     618    @return The index of the first position in the 'data' array that
     619      data for the assay should be written to.
     620    @see #prepareAssayIndexMap(List, int, int)
     621  */
     622  protected int getAssayIndex(short column)
     623  {
     624    return assayIndexMap.get(column);
     625  }
     626 
     627  /**
    586628    Copy cached reporter data into a data array.
    587629    @param key The cache key (=position or reporter id)
  • trunk/src/core/net/sf/basedb/util/export/spotdata/BaseFileExporter.java

    r4925 r4926  
    2727import java.util.Arrays;
    2828import java.util.Collection;
    29 import java.util.HashMap;
    3029import java.util.LinkedHashMap;
    3130import java.util.List;
     
    390389    int numColumnsPerRow = numReporterFields + numAssaysPerRow * numSpotFields;
    391390    Object[] data = new Object[numColumnsPerRow];
    392     Map<Short, Integer> indexMap = null;
     391    // Map assay column number to index in the 'data' array
    393392    if (numAssaysPerRow > 1)
    394393    {
    395       indexMap = new HashMap<Short, Integer>();
    396       int i = 0;
    397       for (BioAssay ba : assays)
    398       {
    399         indexMap.put(ba.getDataCubeColumnNo(), numReporterFields + i * numSpotFields);
    400         ++i;
    401       }
     394      prepareAssayIndexMap(assays, numReporterFields, numSpotFields);
    402395    }
    403396    Formatter[] formatters = new Formatter[numSpotFields+1];
     
    448441        {
    449442          short column = result.getShort(colIndex);
    450           index = indexMap.get(column);
     443          index = getAssayIndex(column);
    451444        }
    452445        for (int i = 1; i <= numSpotFields; ++i)
  • trunk/src/core/net/sf/basedb/util/export/spotdata/MatrixBaseFileExporter.java

    r4925 r4926  
    118118  protected void endExport(RuntimeException e)
    119119  {
    120     setProgress(100, "Export complete. " + spotCount + " spots done");
     120    if (e == null) setProgress(100, "Export complete. " + spotCount + " spots done");
    121121    out = null;
    122122    bioAssays = null;
  • trunk/src/core/net/sf/basedb/util/export/spotdata/SerialBaseFileExporter.java

    r4925 r4926  
    131131  protected void endExport(RuntimeException e)
    132132  {
    133     setProgress(100, "Export complete. " + spotCount + " spots done");
     133    if (e == null) setProgress(100, "Export complete. " + spotCount + " spots done");
    134134    out = null;
    135135    bioAssays = null;
Note: See TracChangeset for help on using the changeset viewer.