2.17.2: 2011-06-17

net.sf.basedb.core
Class SpotBatcher

java.lang.Object
  extended by net.sf.basedb.core.AbstractBatcher
      extended by net.sf.basedb.core.SpotBatcher
All Implemented Interfaces:
Batcher

public class SpotBatcher
extends AbstractBatcher

Batcher class for per-spot intensity values for bioassaysets. This batcher is normally used when a new bioassayset has been created that uses a different cube and/or layer to store it's data than the source bioassayset.

Code example: Swap the intensities of channel 1 and 2

// Get source bioassayset
DbControl dc = ...
BioAssaySet source = ...

// Create new transformation and bioassayset to hold the
// new intensities, we use the same data cube as the source
// but a different layer
Transformation t = source.newTransformation();
dc.saveItem(t);
BioAssaySet bas = t.newProduct(null, "new", true);
dc.saveItem(bas);

// Create a query that selects the data of
// the source bioassayset
DynamicSpotQuery query = source.getSpotData();

// Execute the query, and use the batcher to swap the
// intensities
SpotBatcher batcher = bas.getSpotBatcher();
DynamicResultIterator result = query.iterate(dc);
int column = result.getIndex(VirtualColumn.COLUMN.getName());
int position = result.getIndex(VirtualColumn.POSITION.getName());
int ch1 = result.getIndex(VirtualColumn.channel(1).getName());
int ch2 = result.getIndex(VirtualColumn.channel(2).getName());
while (result.hasNext())
{
   SqlResult r = result.next();
   batcher.insert(r.getShort(column), r.getInt(position), 
      r.getFloat(ch2), r.getFloat(ch1));
}

// Close and commit
batcher.close();
dc.commit();

Version:
2.0
Author:
Nicklas
See Also:
BioAssaySet.getSpotBatcher()
Last modified
$Date: 2009-06-23 11:47:46 +0200 (Tue, 23 Jun 2009) $

Field Summary
private  BioAssaySet bioAssaySet
          The bioassayset this batcher inserts data for.
private  long bytes
          The number of bytes this batcher has added to the experiment.
private  long bytesPerRow
          The number of bytes used by a single row.
private  int numChannels
          The number of channels used in the raw data type of the experiment where the bioassayset belongs.
private  int queuedInsertCount
          The number of queued inserts.
private  PreparedStatement spotSql
          The statement that inserts spot data.
private  String spotSqlStatement
          The SQL string for the spotSql statement.
private  int totalInsertCount
          The total number of inserts done by this batcher.
 
Fields inherited from class net.sf.basedb.core.AbstractBatcher
debugSqlEnabled, logSql
 
Constructor Summary
SpotBatcher(DbControl dc, BioAssaySet bioAssaySet)
          Create a new spot batcher for a bioassayset.
 
Method Summary
private  String buildInsertSelectSql(String selectSql)
          Build the INSERT INTO ...
private  void buildSpotSql()
          Builds the insert SQL statement.
 void flush()
          Flush the batcher and send all remaining items in memory to the database.
 int insert(AbstractSqlQuery query)
          Insert spot intensities using a query.
 void insert(short column, int position, float... intensities)
          Insert intensities for a given column/position coordinate.
(package private)  void onBeforeClose()
          Update disk usage and close open SQL statements
 
Methods inherited from class net.sf.basedb.core.AbstractBatcher
analyzeTable, close, getBatchSize, getDbControl, getSessionControl, isClosed, setBatchSize, setDbControl, updateLastAccess
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

bioAssaySet

private final BioAssaySet bioAssaySet
The bioassayset this batcher inserts data for.


numChannels

private final int numChannels
The number of channels used in the raw data type of the experiment where the bioassayset belongs.


queuedInsertCount

private int queuedInsertCount
The number of queued inserts. Used to check when a flush is needed.


totalInsertCount

private int totalInsertCount
The total number of inserts done by this batcher.


bytes

private long bytes
The number of bytes this batcher has added to the experiment.


bytesPerRow

private final long bytesPerRow
The number of bytes used by a single row.


spotSql

private PreparedStatement spotSql
The statement that inserts spot data.
INSERT INTO Dynamic#PerSpot (cube, layer, column, position, ch1, ch2, ...)
VALUES (cube, layer, ?, ?, ?, ?, ...)


spotSqlStatement

private String spotSqlStatement
The SQL string for the spotSql statement.

Constructor Detail

SpotBatcher

SpotBatcher(DbControl dc,
            BioAssaySet bioAssaySet)
Create a new spot batcher for a bioassayset. It is assumed that the calling code has checked that the bioassayset is allowed to receive more data (ie. it's data cube layer hasn't been committed to the database).

Method Detail

flush

public void flush()
           throws BaseException
Description copied from interface: Batcher
Flush the batcher and send all remaining items in memory to the database.

Throws:
BaseException - If there is an error
See Also:
Batcher.close()

onBeforeClose

void onBeforeClose()
             throws BaseException
Update disk usage and close open SQL statements

Overrides:
onBeforeClose in class AbstractBatcher
Throws:
BaseException

insert

public void insert(short column,
                   int position,
                   float... intensities)
            throws BaseException
Insert intensities for a given column/position coordinate.

Parameters:
column - The column coordinate
position - The position coordinate
intensities - An array holding the intensities for the coordinate, the length must match the number of channels of the raw data type
Throws:
InvalidDataException - If the intensities array is null or of the incorrect length
BaseException - If there is another error

insert

public int insert(AbstractSqlQuery query)
           throws BaseException
Insert spot intensities using a query. The query MUST select data in the following order: column, position, ch1, ch2, ...

Internally this method converts the query into a INSERT INTO ... SELECT ... query, making it very fast, since the data for each spot doesn't have to be retreived, processed and then inserted one row at a time. It is possible to call this method multiple times with the same or different query.

Here is an alternative to the example above:

// Get source bioassayset
DbControl dc = ...
BioAssaySet source = ...

// Create new transformation and bioassayset to hold the
// new intensities, we use the same data cube as the source
// but a different layer
Transformation t = source.newTransformation();
dc.saveItem(t);
BioAssaySet bas = t.newProduct(null, "new", true);
dc.saveItem(bas);

// Create a query that selects the data of
// the source bioassayset, swap the order of the channels
DynamicSpotQuery query = source.getSpotData();
query.select(Dynamic.select(VirtualColumn.COLUMN));
query.select(Dynamic.select(VirtualColumn.POSITION));
query.select(Dynamic.select(VirtualColumn.channel(2)));
query.select(Dynamic.select(VirtualColumn.channel(1)));

// Execute the query, and use the batcher to insert the result
SpotBatcher batcher = bas.getSpotBatcher();
batcher.insert(query);

// Close and commit
batcher.close();
dc.commit();

Parameters:
query - The query that selects the data to be inserted
Returns:
int The number of spots inserted
Throws:
BaseException - If there is an error

buildSpotSql

private void buildSpotSql()
                   throws SQLException,
                          BaseException
Builds the insert SQL statement. Also creates the Dynamic#PerSpot table if it doesn't exists.

Throws:
SQLException
BaseException
See Also:
BatchUtil.buildInsertSql(VirtualDb, VirtualTable, VirtualColumn[], Object[])

buildInsertSelectSql

private String buildInsertSelectSql(String selectSql)
                             throws BaseException
Build the INSERT INTO ... SELECT ... statment. Also creates the Dynamic#PerSpot table if it doesn't exists.

Parameters:
selectSql - The SELECT part of the query
Throws:
BaseException
See Also:
BatchUtil.buildInsertSelectSql(VirtualDb, VirtualTable, VirtualColumn[], String)

2.17.2: 2011-06-17