Changeset 561


Ignore:
Timestamp:
Jan 30, 2008, 2:29:20 PM (13 years ago)
Author:
Martin Svensson
Message:

References #90 The plug-in can now be used to import features to an array design.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/net/sf/basedb/illumina/src/net/sf/basedb/illumina/plugins/BgxFeatureImporter.java

    r556 r561  
    2525package net.sf.basedb.illumina.plugins;
    2626
    27 import java.util.Collections;
    28 import java.util.Set;
    29 
    3027import net.sf.basedb.core.BaseException;
    31 import net.sf.basedb.core.Item;
    32 import net.sf.basedb.core.ProgressReporter;
    33 import net.sf.basedb.core.RequestInformation;
     28import net.sf.basedb.core.FeatureIdentificationMethod;
     29import net.sf.basedb.core.InvalidDataException;
     30import net.sf.basedb.core.Job;
     31import net.sf.basedb.core.ParameterType;
     32import net.sf.basedb.core.PermissionDeniedException;
     33import net.sf.basedb.core.PluginConfiguration;
     34import net.sf.basedb.core.PluginDefinition;
     35import net.sf.basedb.core.SessionControl;
    3436import net.sf.basedb.core.plugin.About;
    3537import net.sf.basedb.core.plugin.AboutImpl;
    36 import net.sf.basedb.core.plugin.AbstractPlugin;
    37 import net.sf.basedb.core.plugin.GuiContext;
    3838import net.sf.basedb.core.plugin.InteractivePlugin;
    39 import net.sf.basedb.core.plugin.Plugin;
    40 import net.sf.basedb.core.plugin.Request;
    41 import net.sf.basedb.core.plugin.Response;
     39import net.sf.basedb.core.plugin.ParameterValues;
    4240import net.sf.basedb.illumina.Illumina;
     41import net.sf.basedb.plugins.ReporterMapFlatFileImporter;
     42import net.sf.basedb.util.FileUtil;
     43import net.sf.basedb.util.InputStreamTracker;
     44import net.sf.basedb.util.parser.FlatFileParser;
     45import net.sf.basedb.util.parser.WrappedConfigureByExample;
     46import net.sf.basedb.util.parser.FlatFileParser.Line;
     47
     48import java.io.IOException;
     49import java.io.InputStream;
     50import java.io.PushbackInputStream;
     51import java.util.HashMap;
     52import java.util.List;
     53import java.util.Map;
     54import java.util.regex.Pattern;
     55import java.util.zip.GZIPInputStream;
    4356
    4457/**
    45   Importer plug-in that Illumina BGX files. This plug-in can
    46   import reporter annotations and features to array designs.
    47   It supports both compressed and uncompressed BGX files.
     58  Plug-in that imports features from Illumina BGX-file.
     59  Both compressed(gzip) and uncompressed files are supported.
     60  Mostly of the work is done by the
     61  {@link net.sf.basedb.plugins.ReporterMapFlatFileImporter},
     62  while this class adds support for compressed files.
     63 
     64  @author Martin, Nicklas
     65  @version 2.6
    4866
    4967*/
    5068public class BgxFeatureImporter
    51   extends AbstractPlugin
    52   implements InteractivePlugin
     69  extends ReporterMapFlatFileImporter
     70  implements InteractivePlugin, WrappedConfigureByExample
    5371{
    5472
    5573  private static final About about = new AboutImpl(
    56     "Illumina BGX importer",
    57     "TODO",
     74    "Illumina BGX feature importer",
     75    "Plugin that imports features from a Illumina BGX-file." +
     76    "Both compressed(gzip) and uncompressed files are supported.",
    5877    Illumina.VERSION,
    5978    Illumina.COPYRIGHT,
     
    6382  );
    6483 
    65   private static final Set<GuiContext> guiContexts =
    66     Collections.singleton(new GuiContext(Item.ARRAYDESIGN, GuiContext.Type.ITEM));
    67  
    68  
     84  /**
     85    Needed for progress reporting when reading from a compressed
     86    BGX file.
     87  */
     88  private InputStreamTracker tracker;
     89   
     90  /**
     91    The parser being used.
     92   */
     93  private FlatFileParser ffp;
     94 
     95  /**
     96    The section we are currently parsing.
     97  */
     98  private Section currentSection;
     99
     100  private ParameterValuesProxy confProxy;
     101   
    69102  public BgxFeatureImporter()
    70103  {}
     
    79112  }
    80113  @Override
    81   public boolean supportsConfigurations()
    82   {
    83     return false;
    84   }
    85   @Override
    86   public boolean requiresConfiguration()
    87   {
    88     return false;
    89   }
    90   public Plugin.MainType getMainType()
    91   {
    92     return Plugin.MainType.IMPORT;
    93   }
    94   public void run(Request request, Response response, ProgressReporter progress)
    95   {
    96     String command = request.getCommand();
    97     response.setDone("Nothing has been done! Only testing");
     114  public void init(SessionControl sc, ParameterValues configuration, ParameterValues job)
     115  {
     116    this.confProxy = new ParameterValuesProxy(configuration);
     117    super.init(sc, this.confProxy, job);
    98118  }
    99119  // -------------------------------------------
     120 
    100121  /*
    101122    From the InteractivePlugin interface
    102123    -------------------------------------------
    103124  */
    104   public Set<GuiContext> getGuiContexts()
    105   {
    106     return guiContexts;
    107   }
    108   public String isInContext(GuiContext context, Object item)
    109   {
    110     String message = null;
    111     if (guiContexts.contains(context))
    112     {
     125  /**
     126    If the input stream is a gzip:ed stream, wraps it in a GZIPInputStream.
     127  */
     128  @Override
     129  public InputStream wrapInputStream(InputStream in)
     130    throws IOException
     131  {
     132    tracker = new InputStreamTracker(in);
     133    PushbackInputStream pin = new PushbackInputStream(tracker, 2);
     134    boolean isGzip = FileUtil.checkMagicNumber(pin, new byte[] { 0x1f, (byte)0x8b });
     135    if (isGzip)
     136    {
     137      in = new GZIPInputStream(pin);
    113138    }
    114139    else
    115140    {
    116       message = "Unsupported context: " + context;
    117     }
    118     return message;
    119   }
    120 
    121   public void configure(GuiContext context, Request request, Response response)
    122   {
    123     response.setDone("Nothing really. Just testing");
    124   }
    125 
    126   public RequestInformation getRequestInformation(GuiContext context, String command)
     141      in = pin;
     142    }
     143    return in;
     144  }
     145 
     146  /**
     147    Get the number of byte from the compressed file.
     148  */
     149  @Override
     150  protected long getNumBytes(FlatFileParser ffp)
     151  {
     152    return tracker.getNumRead();
     153  }
     154 
     155  /**
     156    We need to set the regular expression for matching each new section.
     157  */
     158  @Override
     159  protected FlatFileParser getInitializedFlatFileParser()
    127160    throws BaseException
    128161  {
    129     RequestInformation requestInformation = null;
    130     if (command.equals(Request.COMMAND_CONFIGURE_JOB))
    131     {
    132       requestInformation = getConfigureJobParameters();
    133     }
    134     return requestInformation;
    135   }
    136   // -------------------------------------------
    137 
    138   private RequestInformation getConfigureJobParameters()
    139   {
    140     return new RequestInformation
    141     (
    142       Request.COMMAND_CONFIGURE_JOB,
    143       "Select import options",
    144       about.getDescription(),
    145       null
    146     );
    147 
    148   }
    149  
     162    ffp = super.getInitializedFlatFileParser();
     163    ffp.setSectionRegexp(Pattern.compile("\\[(.+)\\]"));
     164    return ffp;
     165  }
     166 
     167  @Override
     168  protected void handleSection(Line line)
     169    throws BaseException
     170  {
     171    currentSection = Section.getByName(line.name());
     172    if (currentSection == Section.CONTROLS)
     173    {
     174      // Change regular expressions to match the data in this section
     175      ffp.setDataHeaderRegexp(Pattern.compile("Probe_Id\\tArray_Address_Id.*"));
     176      ffp.setIgnoreNonExistingColumns(true);
     177      Map<String, Object> override = new HashMap<String, Object>();
     178      override.put("featureIdentification", FeatureIdentificationMethod.FEATURE_ID);
     179      override.put("reporterIdColumnMapping", "\\Probe_Id\\");
     180      override.put("featureIdColumnMapping", "\\Array_Address_Id\\");
     181      confProxy.setOverride(override);
     182    }
     183  }
     184 
     185  /*
     186    Enum to hold the sections in a BGX file.
     187   */
     188  private enum Section
     189  {
     190    PROBES("Probes"),
     191    CONTROLS("Controls"),
     192    OTHER(null);
     193   
     194    private static final Map<String, Section> sections = new HashMap<String, Section>();
     195    static
     196    {
     197      for (Section s : Section.values())
     198      {
     199        sections.put(s.name, s);
     200      }
     201    }
     202   
     203    private String name;
     204    private Section(String name)
     205    {
     206      this.name = name;
     207    }
     208   
     209   
     210    private static Section getByName(String name)
     211    {
     212      Section s = sections.get(name);
     213      if (s == null) s = Section.OTHER;
     214      return s;
     215    }
     216   
     217  }
     218 
     219 
     220  /**
     221    Proxy for the plugin configuration parameters. Forwards
     222    everything to the original parameters, except when the
     223    'override' is set and then only for 'ColumnMapping' parameters.
     224    This is needed so we can use different column mappings when parsing
     225    the [Controls] section of the BGX file. The regular configuration
     226    parameters only apply to the [Probes] section.
     227  */
     228  private static class ParameterValuesProxy
     229    implements ParameterValues
     230  {
     231   
     232    private ParameterValues params;
     233    private Map<String, Object> override;
     234   
     235    private ParameterValuesProxy(ParameterValues params)
     236    {
     237      this.params = params;
     238    }
     239   
     240    /*
     241      From the ParameterValues interface
     242      -------------------------------------------
     243    */
     244    @Override
     245    public int getId()
     246    {
     247      return params.getId();
     248    }
     249    @Override
     250    public Job getJob()
     251    {
     252      return params.getJob();
     253    }
     254    @Override
     255    public PluginConfiguration getPluginConfiguration()
     256    {
     257      return params.getPluginConfiguration();
     258    }
     259    @Override
     260    public PluginDefinition getPluginDefinition()
     261    {
     262      return params.getPluginDefinition();
     263    }
     264    @Override
     265    public Object getValue(String name)
     266      throws PermissionDeniedException, BaseException
     267    {
     268      return override != null && name.contains("ColumnMapping") ?
     269        override.get(name) : params.getValue(name);
     270    }
     271    @Override
     272    public List<?> getValues(String name)
     273      throws PermissionDeniedException, BaseException
     274    {
     275      return params.getValues(name);
     276    }
     277    @Override
     278    public <T> void setValue(String name, ParameterType<T> type, T value)
     279      throws PermissionDeniedException, InvalidDataException, BaseException
     280    {
     281      params.setValue(name, type, value);
     282    }
     283    @Override
     284    public <T> void setValues(String name, ParameterType<T> type, List<T> values)
     285      throws PermissionDeniedException, InvalidDataException, BaseException
     286    {
     287      params.setValues(name, type, values);
     288    }
     289    // -------------------------------------------
     290   
     291    private void setOverride(Map<String, Object> override)
     292    {
     293      this.override = override;
     294    }
     295   
     296  }
     297
    150298}
    151299
Note: See TracChangeset for help on using the changeset viewer.