@Test
  public void testParse() throws Exception {

    // generate a sample JSON array
    StringBuilder sample = new StringBuilder();
    sample.append("[");
    int i = 0;
    final int N = 5000;
    for (; i < N; i++) {
      sample.append(i);
      sample.append(",");
    }
    sample.append(i);
    sample.append("]");

    String json = sample.toString();

    StopWatch timer = new StopWatch();

    for (int n = 0; n < 500; n++) {
      JSONPullParser parser = new JSONPullParser(json);

      JSONEvent e;
      while ((e = parser.next()) != JSONEvent.EndJSON) {}
    }
    _logger.info("time: " + timer.getElapsedTime());
  }
示例#2
0
public class DTDParserTest {
  Logger _logger = Logger.getLogger(DTDParserTest.class);

  @Before
  public void setUp() throws Exception {}

  @After
  public void tearDown() throws Exception {}

  @Test
  public void parse() throws IOException, RecognitionException {
    DTDParser parser =
        DTDParserUtil.createParser(
            FileResource.find(DTDParserTest.class, "auction.dtd").openStream());
    DTDParser.dtd_return r = parser.dtd();
    CommonTree t = (CommonTree) r.getTree();
    _logger.debug(ANTLRUtil.parseTree(t, DTDParser.tokenNames));
  }

  @Test
  public void parseLine() throws IOException, RecognitionException {
    DTDParser parser =
        DTDParserUtil.createParser(
            FileResource.find(DTDParserTest.class, "simple.dtd").openStream());
    DTDParser.dtd_return r = parser.dtd();
    CommonTree t = (CommonTree) r.getTree();
    _logger.debug(ANTLRUtil.parseTree(t, DTDParser.tokenNames));
  }
}
 public Alignment bandedAlign(String ref, String query) throws Exception {
   ACGTSequence r = new ACGTSequence(ref);
   ACGTSequence q = new ACGTSequence(query);
   Alignment alignment = SmithWatermanAligner.bandedAlign(r, q);
   _logger.debug(alignment);
   return alignment;
 }
  @Test
  public void testLexerPerformance() throws Exception {

    // generate a sample JSON array
    StringBuilder sample = new StringBuilder();
    sample.append("[");
    int i = 0;
    final int N = 5000;
    for (; i < N; i++) {
      sample.append(i);
      sample.append(",");
    }
    sample.append(i);
    sample.append("]");

    String json = sample.toString();

    StopWatch timer = new StopWatch();

    for (int n = 0; n < 500; n++) {
      JSONLexer lexer = new JSONLexer(json);

      Token t;
      while ((t = lexer.nextToken()) != null) {}
    }
    _logger.info("time: " + timer.getElapsedTime());
  }
 @Test
 public void scan() throws Exception {
   ACGTSequence q =
       new ACGTSequence(
           "TTTATACGTGTTTGAATAACTTGGCCAAATCGCCGAGAAGGAATAGAATACTGGACGACATTGTACATATTTTCCAAAAAATCAGAAAGTAGATGACGGG");
   PrefixScan scan =
       PrefixScan.scanRead(fmIndex, q, Strand.FORWARD, new StaircaseFilter(q.length(), 7));
   _logger.debug(scan);
 }
示例#6
0
 @Test
 public void parseLine() throws IOException, RecognitionException {
   DTDParser parser =
       DTDParserUtil.createParser(
           FileResource.find(DTDParserTest.class, "simple.dtd").openStream());
   DTDParser.dtd_return r = parser.dtd();
   CommonTree t = (CommonTree) r.getTree();
   _logger.debug(ANTLRUtil.parseTree(t, DTDParser.tokenNames));
 }
  public static void main(String[] args) throws OptionParserException, IOException {
    Config config = new Config();
    OptionParser optionParser = new OptionParser(config);

    optionParser.parse(args);
    if (config.displayHelp) {
      optionParser.printUsage();
    }

    if (config.verbose) {
      Logger.getRootLogger().setLogLevel(LogLevel.ALL);
    }

    if (config.searchFolderName == null) {
      System.err.println("no search folder is specified");
      return;
    }

    _logger.debug("search folder = " + config.searchFolderName);

    if (config.packageName == null || config.className == null) {
      System.err.println("no package or class name is given");
      return;
    }

    // search track classes
    File searchFolder = new File(config.searchFolderName);
    TrackResourceFinder finder = new TrackResourceFinder(searchFolder);
    finder.enter(searchFolder);

    // template
    Template template =
        new Template(
            FileResource.find(TrackLoaderGenerator.class, "TrackLoader.java.template")
                .openStream());
    Properties p = new Properties();
    p.put("trackClasses", finder.trackClassFile);
    p.put("trackGroupClasses", finder.trackGroupClassFile);
    p.put("packageName", config.packageName);
    p.put("className", config.className);
    String result = template.apply(p);
    System.out.print(result);
  }
    public void enter(File directory) {
      assert (directory.isDirectory());

      _logger.trace("enter the directory: " + directory.getName());
      File[] fileList = directory.listFiles();
      for (File file : fileList) {
        if (file.isDirectory()) {
          enter(file);
        } else {
          if (file.getName().endsWith("Track.class") && !file.getName().equals("Track.class")) {
            String className = getClassName(file);
            _logger.trace("found a track class: " + className);
            trackClassFile.add(className);
          }
          if (file.getName().endsWith("TrackGroup.class")) {
            String className = getClassName(file);
            _logger.trace("found a track group class: " + className);
            trackGroupClassFile.add(className);
          }
        }
      }
    }
  HashedArrayList<String, String> parse(String file) throws Exception {
    JSONStreamReader reader =
        new JSONStreamReader(FileResource.open(JSONStreamReaderTest.class, file));
    TreeEvent e;

    HashedArrayList<String, String> data = new HashedArrayList<String, String>();

    while ((e = reader.next()) != null) {
      _logger.debug(e);

      if (e.isVisit()) data.put(e.nodeName, e.nodeValue);
    }

    return data;
  }
public class PrefixScanTest {
  private static Logger _logger = Logger.getLogger(PrefixScanTest.class);

  private FMIndexOnGenome fmIndex;

  @Before
  public void setUp() throws Exception {
    PackedFASTA fasta =
        PackFasta.encode(new FASTAPullParser(FileResource.open(PackFastaTest.class, "sample2.fa")));
    fmIndex = FMIndexOnGenome.buildFromSequence("sample", fasta.sequence);
  }

  @Test
  public void scan() throws Exception {
    ACGTSequence q =
        new ACGTSequence(
            "TTTATACGTGTTTGAATAACTTGGCCAAATCGCCGAGAAGGAATAGAATACTGGACGACATTGTACATATTTTCCAAAAAATCAGAAAGTAGATGACGGG");
    PrefixScan scan =
        PrefixScan.scanRead(fmIndex, q, Strand.FORWARD, new StaircaseFilter(q.length(), 7));
    _logger.debug(scan);
  }
}
  @Test
  public void performanceOfCSVSplit() throws Exception {

    StopWatch s = new StopWatch();

    String line = null;
    final int N = 1;
    Pattern p = Pattern.compile(",");

    s.reset();
    for (int i = 0; i < N; i++) {
      BufferedReader br =
          new BufferedReader(
              new InputStreamReader(
                  FileResource.find(SilkWalkerTest.class, "scaffold1.silk").openStream()));

      while ((line = br.readLine()) != null) {
        ArrayList<String> csv = StringUtil.splitCSV(line);
      }
    }
    _logger.info("OpenCSV split:" + s.getElapsedTime());
  }
public class JSONStreamReaderTest {
  private static Logger _logger = Logger.getLogger(JSONStreamReaderTest.class);

  @Before
  public void setUp() throws Exception {}

  @After
  public void tearDown() throws Exception {}

  HashedArrayList<String, String> parse(String file) throws Exception {
    JSONStreamReader reader =
        new JSONStreamReader(FileResource.open(JSONStreamReaderTest.class, file));
    TreeEvent e;

    HashedArrayList<String, String> data = new HashedArrayList<String, String>();

    while ((e = reader.next()) != null) {
      _logger.debug(e);

      if (e.isVisit()) data.put(e.nodeName, e.nodeValue);
    }

    return data;
  }

  @Test
  public void testNext() throws Exception {
    HashedArrayList<String, String> data = parse("sample.json");

    assertEquals("Leo", data.get("name").get(0));
    assertEquals("100", data.get("id").get(0));
  }

  @Test
  public void testArray() throws Exception {
    HashedArrayList<String, String> data = parse("array.json");

    assertEquals(2, data.get("author").size());
    assertEquals("leo", data.get("author").get(0));
    assertEquals("yui", data.get("author").get(1));

    assertEquals("Relational-Style XML Query", data.get("title").get(0));
    assertNull(data.get("paper").get(0));
  }

  @Test
  public void testParse() throws Exception {

    // generate a sample JSON array
    StringBuilder sample = new StringBuilder();
    sample.append("[");
    int i = 0;
    final int N = 5000;
    for (; i < N; i++) {
      sample.append(i);
      sample.append(",");
    }
    sample.append(i);
    sample.append("]");

    String json = sample.toString();

    StopWatch timer = new StopWatch();

    for (int n = 0; n < 500; n++) {
      JSONPullParser parser = new JSONPullParser(json);

      JSONEvent e;
      while ((e = parser.next()) != JSONEvent.EndJSON) {}
    }
    _logger.info("time: " + timer.getElapsedTime());
  }

  @Test
  public void testLexerPerformance() throws Exception {

    // generate a sample JSON array
    StringBuilder sample = new StringBuilder();
    sample.append("[");
    int i = 0;
    final int N = 5000;
    for (; i < N; i++) {
      sample.append(i);
      sample.append(",");
    }
    sample.append(i);
    sample.append("]");

    String json = sample.toString();

    StopWatch timer = new StopWatch();

    for (int n = 0; n < 500; n++) {
      JSONLexer lexer = new JSONLexer(json);

      Token t;
      while ((t = lexer.nextToken()) != null) {}
    }
    _logger.info("time: " + timer.getElapsedTime());
  }
}
public class TrackLoaderGenerator {
  private static Logger _logger = Logger.getLogger(TrackLoaderGenerator.class);

  @Usage(
      command = "> java -cp bin org.utgenome.gwt.utgb.TrackLoaderGenerator [option] search_folder")
  public static class Config {

    @Option(symbol = "h", longName = "help", description = "display help messages")
    boolean displayHelp = false;

    @Option(symbol = "v", longName = "verbose", description = "display verbose messages")
    boolean verbose = false;

    @Option(symbol = "p", longName = "package", varName = "NAME", description = "package name")
    String packageName = null;

    @Option(symbol = "c", longName = "class", varName = "NAME", description = "class name")
    String className = null;

    @Argument String searchFolderName = null;
  }

  public static void main(String[] args) throws OptionParserException, IOException {
    Config config = new Config();
    OptionParser optionParser = new OptionParser(config);

    optionParser.parse(args);
    if (config.displayHelp) {
      optionParser.printUsage();
    }

    if (config.verbose) {
      Logger.getRootLogger().setLogLevel(LogLevel.ALL);
    }

    if (config.searchFolderName == null) {
      System.err.println("no search folder is specified");
      return;
    }

    _logger.debug("search folder = " + config.searchFolderName);

    if (config.packageName == null || config.className == null) {
      System.err.println("no package or class name is given");
      return;
    }

    // search track classes
    File searchFolder = new File(config.searchFolderName);
    TrackResourceFinder finder = new TrackResourceFinder(searchFolder);
    finder.enter(searchFolder);

    // template
    Template template =
        new Template(
            FileResource.find(TrackLoaderGenerator.class, "TrackLoader.java.template")
                .openStream());
    Properties p = new Properties();
    p.put("trackClasses", finder.trackClassFile);
    p.put("trackGroupClasses", finder.trackGroupClassFile);
    p.put("packageName", config.packageName);
    p.put("className", config.className);
    String result = template.apply(p);
    System.out.print(result);
  }

  static class TrackResourceFinder {
    ArrayList<String> trackClassFile = new ArrayList<String>();
    ArrayList<String> trackGroupClassFile = new ArrayList<String>();

    File searchFolder;

    public TrackResourceFinder(File searchFolder) {
      if (!searchFolder.isDirectory())
        throw new IllegalArgumentException(searchFolder.getName() + " is not a directory");
      this.searchFolder = searchFolder;
    }

    private String getClassName(File classFile) {
      String classFilePath =
          classFile.getAbsolutePath().replace(searchFolder.getAbsolutePath() + File.separator, "");
      return classFilePath.replace(File.separator, ".").replace(".class", "");
    }

    public void enter(File directory) {
      assert (directory.isDirectory());

      _logger.trace("enter the directory: " + directory.getName());
      File[] fileList = directory.listFiles();
      for (File file : fileList) {
        if (file.isDirectory()) {
          enter(file);
        } else {
          if (file.getName().endsWith("Track.class") && !file.getName().equals("Track.class")) {
            String className = getClassName(file);
            _logger.trace("found a track class: " + className);
            trackClassFile.add(className);
          }
          if (file.getName().endsWith("TrackGroup.class")) {
            String className = getClassName(file);
            _logger.trace("found a track group class: " + className);
            trackGroupClassFile.add(className);
          }
        }
      }
    }
  }
}
  @Test
  public void perfTest() throws Exception {
    StopWatch s = new StopWatch();

    File in = FileResource.copyToTemp(SilkWalkerTest.class, "scaffold1.silk", new File("target"));
    final int N = 2;

    {
      s.reset();
      int lineCount = 0;
      char[] buf = new char[8192];
      for (int i = 0; i < N; i++) {
        BufferedReader fb = new BufferedReader(new FileReader(in));
        for (int readSize = 0; (readSize = fb.read(buf)) != -1; ) {
          for (int c = 0; c < readSize; c++) {
            if (buf[c] == '\r') {
              lineCount++;
              if (c + 1 < readSize && buf[c + 1] == '\n') {
                c++;
              }
            } else if (buf[c] == '\n') {
              lineCount++;
            }
          }
        }
      }
      _logger.info(String.format("BufferedReader: %.2f", s.getElapsedTime()));
    }

    {
      s.reset();
      int lineCount = 0;
      for (int i = 0; i < N; i++) {
        BufferedScanner fb = new BufferedScanner(new FileInputStream(in));
        for (CharSequence line; (line = fb.nextLine()) != null; lineCount++) {}
      }
      _logger.info(
          String.format("BufferedScanner nextLine: %.2f, line:%d", s.getElapsedTime(), lineCount));
    }
    {
      s.reset();
      int lineCount = 0;
      for (int i = 0; i < N; i++) {
        BufferedReader fb = new BufferedReader(new FileReader(in));
        for (String line; (line = fb.readLine()) != null; lineCount++) {}
      }
      _logger.info(
          String.format("BufferedReader readLine: %.2f, line:%d", s.getElapsedTime(), lineCount));
    }

    {
      s.reset();
      int lineCount = 0;
      for (int i = 0; i < N; i++) {
        BufferedScanner fb = new BufferedScanner(new FileInputStream(in));
        for (CharSequence line; (line = fb.nextLine()) != null; lineCount++) {
          String st = line.toString();
        }
      }
      _logger.info(
          String.format(
              "BufferedScanner nextLine (with String conversion): %.2f, line:%d",
              s.getElapsedTime(), lineCount));
    }

    {
      s.reset();
      int lineCount = 0;
      for (int i = 0; i < N; i++) {
        BufferedScanner fb = new BufferedScanner(new FileReader(in));
        for (CharSequence line; (line = fb.nextLine()) != null; lineCount++) {}
      }
      _logger.info(
          String.format(
              "BufferedScanner nextLine with Reader input: %.2f, line:%d",
              s.getElapsedTime(), lineCount));
    }

    {
      s.reset();
      int lineCount = 0;
      char[] buf = new char[8192];
      for (int i = 0; i < N; i++) {
        FastBufferedReader fb = new FastBufferedReader(new FileReader(in));
        for (int readSize = 0; (readSize = fb.read(buf)) != -1; ) {
          for (int c = 0; c < readSize; c++) {
            if (buf[c] == '\r') {
              lineCount++;
              if (c + 1 < readSize && buf[c + 1] == '\n') {
                c++;
              }
            } else if (buf[c] == '\n') {
              lineCount++;
            }
          }
        }
      }
      _logger.info(String.format("FastBufferedReader: %.2f", s.getElapsedTime()));
    }

    {
      s.reset();
      int lineCount = 0;
      byte[] buf = new byte[8192];
      for (int i = 0; i < N; i++) {
        FastBufferedInputStream fb = new FastBufferedInputStream(new FileInputStream(in));
        for (int readSize = 0; (readSize = fb.read(buf)) != -1; ) {
          for (int c = 0; c < readSize; c++) {
            if (buf[c] == '\r') {
              lineCount++;
              if (c + 1 < readSize && buf[c + 1] == '\n') {
                c++;
              }
            } else if (buf[c] == '\n') {
              lineCount++;
            }
          }
        }
      }
      _logger.info(String.format("FastBufferedInputStream: %.2f", s.getElapsedTime()));
    }

    {
      s.reset();
      int lineCount = 0;
      byte[] buf = new byte[8192];
      for (int i = 0; i < N; i++) {
        BufferedInputStream fb = new BufferedInputStream(new FileInputStream(in));
        for (int readSize = 0; (readSize = fb.read(buf)) != -1; ) {
          for (int c = 0; c < readSize; c++) {
            if (buf[c] == '\r') {
              lineCount++;
              if (c + 1 < readSize && buf[c + 1] == '\n') {
                c++;
              }
            } else if (buf[c] == '\n') {
              lineCount++;
            }
          }
        }
      }
      _logger.info(String.format("BufferedInputStream: %.2f", s.getElapsedTime()));
    }
  }
public class FastBufferedReaderTest {

  private static Logger _logger = Logger.getLogger(FastBufferedReaderTest.class);

  @Before
  public void setUp() throws Exception {}

  @After
  public void tearDown() throws Exception {}

  @Test
  public void performanceOfCSVSplit() throws Exception {

    StopWatch s = new StopWatch();

    String line = null;
    final int N = 1;
    Pattern p = Pattern.compile(",");

    s.reset();
    for (int i = 0; i < N; i++) {
      BufferedReader br =
          new BufferedReader(
              new InputStreamReader(
                  FileResource.find(SilkWalkerTest.class, "scaffold1.silk").openStream()));

      while ((line = br.readLine()) != null) {
        ArrayList<String> csv = StringUtil.splitCSV(line);
      }
    }
    _logger.info("OpenCSV split:" + s.getElapsedTime());
  }

  @Test
  public void performanceOfTabSplit() throws Exception {

    StopWatch s = new StopWatch();

    String line = null;
    final int N = 1;

    Pattern p = Pattern.compile("\t");

    s.reset();
    for (int i = 0; i < N; i++) {
      BufferedReader br =
          new BufferedReader(
              new InputStreamReader(
                  FileResource.find(SilkWalkerTest.class, "scaffold1.silk").openStream()));

      while ((line = br.readLine()) != null) {
        String[] tab = p.split(line);
      }
    }
    _logger.info("default tab split:" + s.getElapsedTime());

    s.reset();
    for (int i = 0; i < N; i++) {
      BufferedReader br =
          new BufferedReader(
              new InputStreamReader(
                  FileResource.find(SilkWalkerTest.class, "scaffold1.silk").openStream()));

      while ((line = br.readLine()) != null) {
        ArrayList<String> tokens = StringUtil.splitAtTab(line);
      }
    }
    _logger.info("my tab split:" + s.getElapsedTime());

    s.reset();
    for (int i = 0; i < N; i++) {
      BufferedReader br =
          new BufferedReader(
              new InputStreamReader(
                  FileResource.find(SilkWalkerTest.class, "scaffold1.silk").openStream()));

      while ((line = br.readLine()) != null) {
        StringTokenizer t = new StringTokenizer(line, "\t");
        ArrayList<String> tokens = new ArrayList<String>();
        while (t.hasMoreTokens()) {
          tokens.add(t.nextToken());
        }
      }
    }
    _logger.info("tokenizer tab split:" + s.getElapsedTime());

    s.reset();
    for (int i = 0; i < N; i++) {
      FastBufferedReader fb =
          new FastBufferedReader(
              new InputStreamReader(
                  FileResource.find(SilkWalkerTest.class, "scaffold1.silk").openStream()));

      while ((line = fb.readLine()) != null) {
        ArrayList<String> tab = StringUtil.splitAtTab(line);
      }
    }
    _logger.info("fast reader:" + s.getElapsedTime());
  }

  @Test
  public void perfTest() throws Exception {
    StopWatch s = new StopWatch();

    File in = FileResource.copyToTemp(SilkWalkerTest.class, "scaffold1.silk", new File("target"));
    final int N = 2;

    {
      s.reset();
      int lineCount = 0;
      char[] buf = new char[8192];
      for (int i = 0; i < N; i++) {
        BufferedReader fb = new BufferedReader(new FileReader(in));
        for (int readSize = 0; (readSize = fb.read(buf)) != -1; ) {
          for (int c = 0; c < readSize; c++) {
            if (buf[c] == '\r') {
              lineCount++;
              if (c + 1 < readSize && buf[c + 1] == '\n') {
                c++;
              }
            } else if (buf[c] == '\n') {
              lineCount++;
            }
          }
        }
      }
      _logger.info(String.format("BufferedReader: %.2f", s.getElapsedTime()));
    }

    {
      s.reset();
      int lineCount = 0;
      for (int i = 0; i < N; i++) {
        BufferedScanner fb = new BufferedScanner(new FileInputStream(in));
        for (CharSequence line; (line = fb.nextLine()) != null; lineCount++) {}
      }
      _logger.info(
          String.format("BufferedScanner nextLine: %.2f, line:%d", s.getElapsedTime(), lineCount));
    }
    {
      s.reset();
      int lineCount = 0;
      for (int i = 0; i < N; i++) {
        BufferedReader fb = new BufferedReader(new FileReader(in));
        for (String line; (line = fb.readLine()) != null; lineCount++) {}
      }
      _logger.info(
          String.format("BufferedReader readLine: %.2f, line:%d", s.getElapsedTime(), lineCount));
    }

    {
      s.reset();
      int lineCount = 0;
      for (int i = 0; i < N; i++) {
        BufferedScanner fb = new BufferedScanner(new FileInputStream(in));
        for (CharSequence line; (line = fb.nextLine()) != null; lineCount++) {
          String st = line.toString();
        }
      }
      _logger.info(
          String.format(
              "BufferedScanner nextLine (with String conversion): %.2f, line:%d",
              s.getElapsedTime(), lineCount));
    }

    {
      s.reset();
      int lineCount = 0;
      for (int i = 0; i < N; i++) {
        BufferedScanner fb = new BufferedScanner(new FileReader(in));
        for (CharSequence line; (line = fb.nextLine()) != null; lineCount++) {}
      }
      _logger.info(
          String.format(
              "BufferedScanner nextLine with Reader input: %.2f, line:%d",
              s.getElapsedTime(), lineCount));
    }

    {
      s.reset();
      int lineCount = 0;
      char[] buf = new char[8192];
      for (int i = 0; i < N; i++) {
        FastBufferedReader fb = new FastBufferedReader(new FileReader(in));
        for (int readSize = 0; (readSize = fb.read(buf)) != -1; ) {
          for (int c = 0; c < readSize; c++) {
            if (buf[c] == '\r') {
              lineCount++;
              if (c + 1 < readSize && buf[c + 1] == '\n') {
                c++;
              }
            } else if (buf[c] == '\n') {
              lineCount++;
            }
          }
        }
      }
      _logger.info(String.format("FastBufferedReader: %.2f", s.getElapsedTime()));
    }

    {
      s.reset();
      int lineCount = 0;
      byte[] buf = new byte[8192];
      for (int i = 0; i < N; i++) {
        FastBufferedInputStream fb = new FastBufferedInputStream(new FileInputStream(in));
        for (int readSize = 0; (readSize = fb.read(buf)) != -1; ) {
          for (int c = 0; c < readSize; c++) {
            if (buf[c] == '\r') {
              lineCount++;
              if (c + 1 < readSize && buf[c + 1] == '\n') {
                c++;
              }
            } else if (buf[c] == '\n') {
              lineCount++;
            }
          }
        }
      }
      _logger.info(String.format("FastBufferedInputStream: %.2f", s.getElapsedTime()));
    }

    {
      s.reset();
      int lineCount = 0;
      byte[] buf = new byte[8192];
      for (int i = 0; i < N; i++) {
        BufferedInputStream fb = new BufferedInputStream(new FileInputStream(in));
        for (int readSize = 0; (readSize = fb.read(buf)) != -1; ) {
          for (int c = 0; c < readSize; c++) {
            if (buf[c] == '\r') {
              lineCount++;
              if (c + 1 < readSize && buf[c + 1] == '\n') {
                c++;
              }
            } else if (buf[c] == '\n') {
              lineCount++;
            }
          }
        }
      }
      _logger.info(String.format("BufferedInputStream: %.2f", s.getElapsedTime()));
    }
  }
}
  @Test
  public void performanceOfTabSplit() throws Exception {

    StopWatch s = new StopWatch();

    String line = null;
    final int N = 1;

    Pattern p = Pattern.compile("\t");

    s.reset();
    for (int i = 0; i < N; i++) {
      BufferedReader br =
          new BufferedReader(
              new InputStreamReader(
                  FileResource.find(SilkWalkerTest.class, "scaffold1.silk").openStream()));

      while ((line = br.readLine()) != null) {
        String[] tab = p.split(line);
      }
    }
    _logger.info("default tab split:" + s.getElapsedTime());

    s.reset();
    for (int i = 0; i < N; i++) {
      BufferedReader br =
          new BufferedReader(
              new InputStreamReader(
                  FileResource.find(SilkWalkerTest.class, "scaffold1.silk").openStream()));

      while ((line = br.readLine()) != null) {
        ArrayList<String> tokens = StringUtil.splitAtTab(line);
      }
    }
    _logger.info("my tab split:" + s.getElapsedTime());

    s.reset();
    for (int i = 0; i < N; i++) {
      BufferedReader br =
          new BufferedReader(
              new InputStreamReader(
                  FileResource.find(SilkWalkerTest.class, "scaffold1.silk").openStream()));

      while ((line = br.readLine()) != null) {
        StringTokenizer t = new StringTokenizer(line, "\t");
        ArrayList<String> tokens = new ArrayList<String>();
        while (t.hasMoreTokens()) {
          tokens.add(t.nextToken());
        }
      }
    }
    _logger.info("tokenizer tab split:" + s.getElapsedTime());

    s.reset();
    for (int i = 0; i < N; i++) {
      FastBufferedReader fb =
          new FastBufferedReader(
              new InputStreamReader(
                  FileResource.find(SilkWalkerTest.class, "scaffold1.silk").openStream()));

      while ((line = fb.readLine()) != null) {
        ArrayList<String> tab = StringUtil.splitAtTab(line);
      }
    }
    _logger.info("fast reader:" + s.getElapsedTime());
  }
示例#17
0
  public void SAIS(LSeq SA) {

    StopWatch timer = new StopWatch();
    _logger.info("SAIS: N=" + SA.textSize());

    // Determin T[N-1]'s LS-type
    // T[i] is SType if T[i,_) < T[i+1,_)
    // T[i] is LType if T[i,_) > T[i+1,_)
    {
      long i = 0;
      for (; i < N; ++i) {
        long x = T.lookup((N + i - 1) % N);
        long y = T.lookup((N + i) % N);
        if (x == y) continue;
        if (x < y) {
          LS.set(N - 1, SType);
          break;
        } else {
          LS.set(N - 1, LType);
          break;
        }
      }
      if (i == N) {
        // When T = AAAA... , etc.
        LS.set(N - 1, LType);
      }
    }

    // T[i] is SType if T[i] < T[i+1] or T[i] = T[i+1] and T[i+1] is S-type
    // T[i] is LType if T[i] > T[i+1] or T[i] = T[i+1] and T[i+1] is L-type
    // Set the LS type of each character
    for (long i = N - 1; i > 0; --i) {
      long x = T.lookup(i);
      long y = T.lookup(i - 1);
      if (x < y) LS.set(i - 1, LType);
      else if (x > y) LS.set(i - 1, SType);
      else LS.set(i - 1, LS.get(i));
    }

    // Initialize the buckets.
    // A bucket is a container of the suffixes sharing the same first character
    {
      _logger.trace("Initialize the buckets");
      Arrays.fill(bucketEnd, 0);
      // Compute the size of each bucket
      for (long i = 0; i < N; ++i) {
        ++bucketEnd[(int) T.lookup(i)];
      }
      // Accumulate the character counts. The bucketStart holds the pointers to beginning of the
      // buckets in SA
      for (int i = 1; i < bucketEnd.length; ++i) {
        bucketEnd[i] += bucketEnd[i - 1];
      }
      _logger.trace("Done.");
    }

    // initialize the suffix array
    for (long i = 0; i < N; ++i) SA.set(i, N);

    // Step 1: reduce the problem by at least 1/2
    // Sort all the S-substrings

    // Find LMS characters
    long[] cursorInBucket = Arrays.copyOf(bucketEnd, bucketEnd.length);
    for (long i = 0; i < N; ++i) {
      if (isLMS(i)) SA.set(--cursorInBucket[(int) T.lookup(i)], i);
    }

    // Induced sorting LMS prefixes
    {
      _logger.trace(String.format("[N=%,d] induceSA", SA.textSize()));
      induceSA(SA);
      _logger.trace("Done.");
    }

    int numLMS = 0;
    // Compact all the sorted substrings into the first M items of SA
    // 2*M must be not larger than N
    for (long i = 0; i < N; ++i) {
      long si = SA.lookup(i);
      if (si != N && isLMS(si)) SA.set(numLMS++, si);
    }

    // Initialize the name array buffer
    for (long i = numLMS; i < N; ++i) SA.set(i, N);

    // Find the lexicographic names of the LMS substrings
    _logger.trace("Sorting LMS substrings: N=" + SA.textSize());
    int name = 1;
    SA.set(numLMS + (SA.lookup(0) / 2), name++);
    for (long i = 1; i < numLMS; ++i) {
      final long prev = SA.lookup(i - 1);
      final long current = SA.lookup(i);
      if (!isEqualLMSSubstring(prev, current)) {
        name++;
      }
      SA.set(numLMS + (current / 2), name - 1);
    }

    for (long i = N - 1, j = N - 1; i >= numLMS; --i) {
      if (SA.lookup(i) != N) SA.set(j--, SA.lookup(i) - 1);
    }

    // Step 2: solve the reduced problem
    // Create SA1, a view of SA[0, numLMS-1]
    _logger.trace("Solving the reduced problem: N=" + SA.textSize());
    LSeq SA1 = new ArrayWrap(SA, 0, numLMS);
    LSeq T1 = new ArrayWrap(SA, N - numLMS, numLMS);
    if (name - 1 != numLMS) {
      new CyclicSAIS(T1, name - 1).SAIS(SA1);
    } else {
      // When all LMS substrings have unique names
      for (long i = 0; i < numLMS; i++) SA1.set(T1.lookup(i), i);
    }

    // Step 3: Induce SA from SA1
    // Construct P1 using T1 buffer
    for (long i = 0, j = 0; i < N; ++i) {
      if (isLMS(i)) T1.set(j++, i); //
    }
    // Translate short name into the original index at T
    // SA1 now holds the LMS-substring indexes
    for (long i = 0; i < numLMS; ++i) {
      SA1.set(i, T1.lookup(SA1.lookup(i)));
    }

    // Step 3-1: Put all the items in SA1 into corresponding S-type buckets of SA

    // Clear SA[N1 .. N-1]
    for (long i = numLMS; i < N; ++i) {
      SA.set(i, N);
    }
    // Put SA1 contents into S-type buckets of SA
    System.arraycopy(bucketEnd, 0, cursorInBucket, 0, bucketEnd.length);
    for (int i = numLMS - 1; i >= 0; --i) {
      long si = SA1.lookup(i);
      SA.set(i, N);
      long index = --cursorInBucket[(int) T.lookup(si)];
      SA.set(index, si);
    }
    // SA.set(0, T.textSize() - 1);

    // Step 3-2, 3-3
    _logger.trace("Inducing SA from SA1: N=" + SA.textSize());
    induceSA(SA);

    _logger.info(String.format("done. %.2f sec.", timer.getElapsedTime()));
  }
public class SmithWatermanAlignerTest {
  private static Logger _logger = Logger.getLogger(SmithWatermanAlignerTest.class);

  public Alignment bandedAlign(String ref, String query) throws Exception {
    ACGTSequence r = new ACGTSequence(ref);
    ACGTSequence q = new ACGTSequence(query);
    Alignment alignment = SmithWatermanAligner.bandedAlign(r, q);
    _logger.debug(alignment);
    return alignment;
  }

  public Alignment bandedAlign(String ref, String query, AlignmentScoreConfig config)
      throws Exception {
    ACGTSequence r = new ACGTSequence(ref);
    ACGTSequence q = new ACGTSequence(query);
    Alignment alignment = SmithWatermanAligner.bandedAlign(r, q, config);
    _logger.debug(alignment);
    return alignment;
  }

  @Test
  public void align() throws Exception {
    Alignment alignment = bandedAlign("GATATAGAGATCTGGCCTAG", "TAGAGAT");
    assertEquals("7M", alignment.cigar.toString());
    assertEquals(4, alignment.pos);
    assertEquals(0, alignment.numMismatches);
  }

  @Test
  public void alignMismatch() throws Exception {
    Alignment alignment = bandedAlign("GATATAGAGATCTGGCCTAG", "TATACAGATCTGGCCTAG");
    assertEquals("4M1X13M", alignment.cigar.toString());
    assertEquals(2, alignment.pos);
    assertEquals(1, alignment.numMismatches);
  }

  @Test
  public void alignInsertion() throws Exception {
    AlignmentScoreConfig config = new AlignmentScoreConfig();
    config.gapOpenPenalty = 5;
    config.gapExtensionPenalty = 2;
    Alignment alignment =
        bandedAlign(
            "TATACCAAGATATAGATCTGGCAAGTGTGTTAT", "TACCAAGATATAGAGATCTGGCAAGTGTGTTAT", config);
    assertEquals("11M2I20M", alignment.cigar.toString());
    assertEquals(2, alignment.pos);
    assertEquals(2, alignment.numMismatches);
  }

  @Test
  public void alignDeletion() throws Exception {
    AlignmentScoreConfig config = new AlignmentScoreConfig();
    config.gapOpenPenalty = 5;
    config.gapExtensionPenalty = 2;
    Alignment alignment =
        bandedAlign(
            "TATACCAAGATATAGAGATCTGGCAAGTGTGTTAT", "TACCAAGATATAGATCTGGCAAGTGTGTTAT", config);
    assertEquals("11M2D20M", alignment.cigar.toString());
    assertEquals(2, alignment.pos);
    assertEquals(2, alignment.numMismatches);
  }

  @Test
  public void clippedAlignment() throws Exception {
    Alignment alignment = bandedAlign("ATTTGTATTATACCAAGAT", "ACCGGAAGGACCAAGAT");
    assertEquals("9S8M", alignment.cigar.toString());
    assertEquals(11, alignment.pos);
    assertEquals(0, alignment.numMismatches);
  }
}
示例#19
0
 public void addRelation(Relation relation) {
   _logger.debug("add relation: " + relation.toString());
 }
示例#20
0
/**
 * Suffix-array (SA) construction algorithm based on Induced Sorting (IS)
 *
 * @author leo
 */
public class CyclicSAIS {
  private final LSeq T;
  private final long N;
  private final int K;
  private final long[] bucketEnd;
  private LSType LS;

  private static final boolean LType = false;
  private static final boolean SType = true;

  public static class LSType {
    private static final int BIT_LENGTH = 32;
    private int[] bitVector;
    private long size;

    public LSType(long n) {
      int blockSize = (int) ((n + BIT_LENGTH - 1) / BIT_LENGTH);
      bitVector = new int[blockSize];
      this.size = n;
    }

    public void set(long index, boolean flag) {
      int pos = (int) (index / BIT_LENGTH);
      int offset = (int) (index % BIT_LENGTH);
      int mask = 0x01 << (offset - 1);

      if (flag) bitVector[pos] |= mask;
      else bitVector[pos] &= ~mask;
    }

    public boolean get(long index) {
      int pos = (int) (index / BIT_LENGTH);
      int offset = (int) (index % BIT_LENGTH);
      int mask = 0x01 << (offset - 1);
      return (bitVector[pos] & mask) != 0;
    }

    @Override
    public boolean equals(Object obj) {
      if (!BitVector.class.isInstance(obj)) return false;

      LSType other = LSType.class.cast(obj);

      // compare the size
      if (size() != other.size()) return false;

      // compare each byte
      int byteLength = byteLength();

      for (int i = 0; i < byteLength; i++) {
        if (this.bitVector[i] != other.bitVector[i]) return false;
      }

      return true;
    }

    private int byteLength() {
      return (int) ((size + BIT_LENGTH - 1) / BIT_LENGTH);
    }

    @Override
    public int hashCode() {
      int hashValue = 3;

      int byteLength = byteLength();
      for (int i = 0; i < byteLength; i++) {
        hashValue += hashValue * 137 + bitVector[i];
      }

      return hashValue % 1987;
    }

    public long size() {
      return size;
    }

    public void clear() {
      bitVector = null;
      size = 0;
    }

    @Override
    public String toString() {
      StringBuilder buf = new StringBuilder();
      for (int i = 0; i < size; i++) buf.append(get(i) ? "1" : "0");
      return buf.toString();
    }
  }

  static class ArrayWrap implements LSeq {

    private final LSeq seq;
    private final long offset;
    private final long length;

    public ArrayWrap(LSeq original, long offset) {
      this.seq = original;
      this.offset = offset;
      this.length = original.textSize() - offset;
    }

    public ArrayWrap(LSeq original, long offset, long length) {
      this.seq = original;
      this.offset = offset;
      this.length = length;
    }

    @Override
    public long lookup(long index) {
      return seq.lookup(index + offset);
    }

    @Override
    public long textSize() {
      return length;
    }

    @Override
    public void set(long index, long value) {
      seq.set(index + offset, value);
    }

    @Override
    public long increment(long index, long value) {
      return seq.increment(index + offset, value);
    }

    @Override
    public String toString() {
      StringBuilder w = new StringBuilder();
      w.append("[");
      for (long i = 0; i < length; ++i) {
        if (i != 0) w.append(", ");
        w.append(lookup(i));
      }
      w.append("]");
      return w.toString();
    }
  }

  public CyclicSAIS(LSeq T, int K) {
    this.T = T;
    this.N = T.textSize();
    this.K = K;
    this.bucketEnd = new long[K];
    LS = new LSType(N);
  }

  public static LSeq SAIS(LSeq T, int K) {
    LSeq SA;
    if (T.textSize() < Integer.MAX_VALUE) SA = new LSAIS.IntArray(new int[(int) T.textSize()], 0);
    else if (T.textSize() < Math.pow(2, 32)) {
      SA = new UInt32Array(T.textSize());
    } else if (T.textSize() < Math.pow(2, 39)) {
      SA = new Int40Array(T.textSize());
    } else throw new IllegalArgumentException("Cannot create SA array more than 3.2GB length");
    SAIS(T, SA, K);
    return SA;
  }

  public static LSeq SAIS(LSeq T, LSeq SA, int K) {
    CyclicSAIS sais = new CyclicSAIS(T, K);
    sais.SAIS(SA);
    return SA;
  }

  private static Logger _logger = Logger.getLogger(CyclicSAIS.class);

  public void SAIS(LSeq SA) {

    StopWatch timer = new StopWatch();
    _logger.info("SAIS: N=" + SA.textSize());

    // Determin T[N-1]'s LS-type
    // T[i] is SType if T[i,_) < T[i+1,_)
    // T[i] is LType if T[i,_) > T[i+1,_)
    {
      long i = 0;
      for (; i < N; ++i) {
        long x = T.lookup((N + i - 1) % N);
        long y = T.lookup((N + i) % N);
        if (x == y) continue;
        if (x < y) {
          LS.set(N - 1, SType);
          break;
        } else {
          LS.set(N - 1, LType);
          break;
        }
      }
      if (i == N) {
        // When T = AAAA... , etc.
        LS.set(N - 1, LType);
      }
    }

    // T[i] is SType if T[i] < T[i+1] or T[i] = T[i+1] and T[i+1] is S-type
    // T[i] is LType if T[i] > T[i+1] or T[i] = T[i+1] and T[i+1] is L-type
    // Set the LS type of each character
    for (long i = N - 1; i > 0; --i) {
      long x = T.lookup(i);
      long y = T.lookup(i - 1);
      if (x < y) LS.set(i - 1, LType);
      else if (x > y) LS.set(i - 1, SType);
      else LS.set(i - 1, LS.get(i));
    }

    // Initialize the buckets.
    // A bucket is a container of the suffixes sharing the same first character
    {
      _logger.trace("Initialize the buckets");
      Arrays.fill(bucketEnd, 0);
      // Compute the size of each bucket
      for (long i = 0; i < N; ++i) {
        ++bucketEnd[(int) T.lookup(i)];
      }
      // Accumulate the character counts. The bucketStart holds the pointers to beginning of the
      // buckets in SA
      for (int i = 1; i < bucketEnd.length; ++i) {
        bucketEnd[i] += bucketEnd[i - 1];
      }
      _logger.trace("Done.");
    }

    // initialize the suffix array
    for (long i = 0; i < N; ++i) SA.set(i, N);

    // Step 1: reduce the problem by at least 1/2
    // Sort all the S-substrings

    // Find LMS characters
    long[] cursorInBucket = Arrays.copyOf(bucketEnd, bucketEnd.length);
    for (long i = 0; i < N; ++i) {
      if (isLMS(i)) SA.set(--cursorInBucket[(int) T.lookup(i)], i);
    }

    // Induced sorting LMS prefixes
    {
      _logger.trace(String.format("[N=%,d] induceSA", SA.textSize()));
      induceSA(SA);
      _logger.trace("Done.");
    }

    int numLMS = 0;
    // Compact all the sorted substrings into the first M items of SA
    // 2*M must be not larger than N
    for (long i = 0; i < N; ++i) {
      long si = SA.lookup(i);
      if (si != N && isLMS(si)) SA.set(numLMS++, si);
    }

    // Initialize the name array buffer
    for (long i = numLMS; i < N; ++i) SA.set(i, N);

    // Find the lexicographic names of the LMS substrings
    _logger.trace("Sorting LMS substrings: N=" + SA.textSize());
    int name = 1;
    SA.set(numLMS + (SA.lookup(0) / 2), name++);
    for (long i = 1; i < numLMS; ++i) {
      final long prev = SA.lookup(i - 1);
      final long current = SA.lookup(i);
      if (!isEqualLMSSubstring(prev, current)) {
        name++;
      }
      SA.set(numLMS + (current / 2), name - 1);
    }

    for (long i = N - 1, j = N - 1; i >= numLMS; --i) {
      if (SA.lookup(i) != N) SA.set(j--, SA.lookup(i) - 1);
    }

    // Step 2: solve the reduced problem
    // Create SA1, a view of SA[0, numLMS-1]
    _logger.trace("Solving the reduced problem: N=" + SA.textSize());
    LSeq SA1 = new ArrayWrap(SA, 0, numLMS);
    LSeq T1 = new ArrayWrap(SA, N - numLMS, numLMS);
    if (name - 1 != numLMS) {
      new CyclicSAIS(T1, name - 1).SAIS(SA1);
    } else {
      // When all LMS substrings have unique names
      for (long i = 0; i < numLMS; i++) SA1.set(T1.lookup(i), i);
    }

    // Step 3: Induce SA from SA1
    // Construct P1 using T1 buffer
    for (long i = 0, j = 0; i < N; ++i) {
      if (isLMS(i)) T1.set(j++, i); //
    }
    // Translate short name into the original index at T
    // SA1 now holds the LMS-substring indexes
    for (long i = 0; i < numLMS; ++i) {
      SA1.set(i, T1.lookup(SA1.lookup(i)));
    }

    // Step 3-1: Put all the items in SA1 into corresponding S-type buckets of SA

    // Clear SA[N1 .. N-1]
    for (long i = numLMS; i < N; ++i) {
      SA.set(i, N);
    }
    // Put SA1 contents into S-type buckets of SA
    System.arraycopy(bucketEnd, 0, cursorInBucket, 0, bucketEnd.length);
    for (int i = numLMS - 1; i >= 0; --i) {
      long si = SA1.lookup(i);
      SA.set(i, N);
      long index = --cursorInBucket[(int) T.lookup(si)];
      SA.set(index, si);
    }
    // SA.set(0, T.textSize() - 1);

    // Step 3-2, 3-3
    _logger.trace("Inducing SA from SA1: N=" + SA.textSize());
    induceSA(SA);

    _logger.info(String.format("done. %.2f sec.", timer.getElapsedTime()));
  }

  boolean isLMS(long pos) {
    return LS.get(pos % N) == SType && LS.get((pos - 1 + N) % N) == LType;
  }

  private void induceSA(LSeq SA) {
    long[] cursorInBucket = Arrays.copyOf(bucketEnd, bucketEnd.length);

    // induce left
    for (long i = 0; i < N; ++i) {
      long si = SA.lookup(i);
      if (si == N) continue;
      si = (si - 1 + N) % N;
      if (LS.get(si) == LType) SA.set(cursorInBucket[(int) T.lookup(si) - 1]++, si);
    }

    // induce right
    System.arraycopy(bucketEnd, 0, cursorInBucket, 0, bucketEnd.length);
    for (long i = N - 1; i >= 0; --i) {
      long si = SA.lookup(i);
      if (si == N) continue;
      si = (si - 1 + N) % N;
      if (LS.get(si) == SType) SA.set(--cursorInBucket[(int) T.lookup(si)], si);
    }
  }

  boolean isEqualLMSSubstring(long pos1, long pos2) {
    boolean prevLS = SType;
    long offset = 0;
    for (; offset < N; ++offset) {
      long p1 = (pos1 + offset) % N;
      long p2 = (pos2 + offset) % N;
      if (T.lookup(p1) == T.lookup(p2) && LS.get(p1) == LS.get(p2)) {
        if (prevLS == LType && LS.get(p1) == SType) return true; // equal LMS substring
        prevLS = LS.get(p1);
        continue;
      } else return false;
    }
    return false;
  }
}
示例#21
0
/**
 * A DataModel manages the graph structure of an ER-diagram.
 *
 * <p>person:(id, name, address) || author:(book_id, author_id) |* |1 book:(r2:id, r2:title) ||
 * order:(id, book_id, customer_id, info) || customer:(id, name, address, phone)
 *
 * <p>query examples (person:name, book:title = "Gone with the wind")
 *
 * <p>(book:id = 10, customer:name, order:info)
 *
 * <p>select customer:id, customer:name, count(*) from (select customer left join order on
 * customer:id = order:customer_id)
 *
 * <p>Multidimensional Hash Table can support this type of query?
 *
 * @author leo
 */
public class DataModel {
  private static Logger _logger = Logger.getLogger(DataModel.class);

  private AdjacencyList<String, Relationship> _graph = new AdjacencyList<String, Relationship>();

  public void addNode(String nodeName) {
    if (!contains(nodeName)) _graph.addNode(nodeName);
  }

  public boolean contains(String nodeName) {
    return _graph.hasNode(nodeName);
  }

  public void connectNodes(String sourceNodeName, String destNodeName, Relationship rel)
      throws DBException {
    _graph.addEdge(sourceNodeName, destNodeName, rel);
  }

  public Set<String> getRootNodeNameSet() {
    TreeSet<String> rootNodeSet = new TreeSet<String>();

    for (String node : _graph.getNodeLabelSet()) {
      // if this node has no incoming edges
      if (_graph.getInEdgeSet(node).size() == 0) rootNodeSet.add(node);
    }

    return rootNodeSet;
  }

  @SuppressWarnings("unchecked")
  public List<String> destinationOf(String nodeName) {
    Collection<Integer> nodeIDSet = _graph.getDestNodeIDSetOf(_graph.getNodeID(nodeName));

    return CollectionUtil.collect(
        nodeIDSet,
        new Functor<Integer, String>() {
          public String apply(Integer nodeID) {
            return _graph.getNodeLabel(nodeID);
          }
        });
  }

  class PathSearch extends DepthFirstSearchBase<String, Relationship> {
    public List<String> getPath(String src, String dest) {
      run(_graph, src);

      // TODO improve by using Dijkstra's algorithm
      LinkedList<String> path = new LinkedList<String>();
      path.add(dest);
      String predecessor;
      String contextNode = dest;
      while (!(predecessor = getPredecessor(contextNode)).equals(contextNode)) {
        path.addFirst(predecessor);
        if (predecessor.equals(src)) {
          return path;
        }
        contextNode = predecessor;
      }

      if (!path.getFirst().equals(src)) return new LinkedList<String>();

      return path;
    }
  }

  public List<String> path(String src, String dest) {
    return new PathSearch().getPath(src, dest);
  }

  public boolean hasPath(String src, String dest) {
    return path(src, dest).size() > 0;
  }

  public void save(SQLiteAccess query) throws DBException {
    if (query.hasTable("node")) query.dropTable("node");
    if (query.hasTable("edge")) query.dropTable("edge");

    Relation nodeRelation = new Relation();
    nodeRelation.add(new DataTypeBase("id", TypeName.INTEGER, true, true));
    nodeRelation.add(new DataTypeBase("name", TypeName.STRING));
    query.createTable("node", nodeRelation);

    Relation edgeRelation = new Relation();
    edgeRelation.add(new DataTypeBase("id", TypeName.INTEGER, true, true));
    edgeRelation.add(new DataTypeBase("src", TypeName.INTEGER));
    edgeRelation.add(new DataTypeBase("dest", TypeName.INTEGER));
    edgeRelation.add(new DataTypeBase("relationship", TypeName.INTEGER));
    query.createTable("edge", edgeRelation);

    Vector<NodeData> nodeList = new Vector<NodeData>();
    for (int nodeID : _graph.getNodeIDSet()) {
      nodeList.add(new NodeData(nodeID, _graph.getNodeLabel(nodeID)));
    }

    Vector<EdgeData> edgeList = new Vector<EdgeData>();
    for (Edge edge : _graph.getEdgeSet()) {
      edgeList.add(
          new EdgeData(
              _graph.getEdgeID(edge),
              edge.getSourceNodeID(),
              edge.getDestNodeID(),
              _graph.getEdgeLabel(edge)));
    }

    for (NodeData node : nodeList) query.insert("node", node);
    for (EdgeData edge : edgeList) query.insert("edge", edge);
  }

  public void load(SQLiteAccess query) throws DBException {

    TreeSet<NodeData> nodeList = CollectionUtil.sort(query.amoebaQuery(NodeData.class, "node"));
    TreeSet<EdgeData> edgeList = CollectionUtil.sort(query.amoebaQuery(EdgeData.class, "edge"));

    for (NodeData node : nodeList) {
      _graph.addNode(node.getName());
    }
    for (EdgeData edge : edgeList) {
      _graph.addEdge(
          new Edge(edge.getSrc(), edge.getDest()), EdgeData.translate(edge.getRelationship()));
    }
  }

  public String toString() {
    return _graph.toString();
  }

  public void attachSQLiteDatabase(String sqliteDBFile) throws DBException {

    SQLiteAccess query = new SQLiteAccess(sqliteDBFile);

    Collection<String> tableList = query.getTableList();
    List<String> infoTableList =
        CollectionUtil.select(
            tableList,
            new Predicate<String>() {
              public boolean apply(String input) {
                return isInfoTable(input);
              }
            });
    List<String> dataTableList =
        CollectionUtil.select(
            tableList,
            new Predicate<String>() {
              public boolean apply(String input) {
                return !isInfoTable(input);
              }
            });

    // HashMap<String, HashMap<String, String>>
    for (String infoTable : infoTableList) {
      for (DataModelInfo info : query.amoebaQuery(DataModelInfo.class, infoTable)) {}
    }

    for (String dataTable : dataTableList) {
      Relation relation = new Relation();
      for (SQLiteDataTypeInfo dataType : query.getSQLiteDataTypeInfo(dataTable)) {
        relation.add(Relation.getDataType(dataType.getName(), dataType.getType()));
      }
      addRelation(relation);
    }
  }

  public void addRelation(Relation relation) {
    _logger.debug("add relation: " + relation.toString());
  }

  private static boolean isInfoTable(String tableName) {
    return tableName.endsWith("_info");
  }

  public boolean hasEdge(String src, String dest) {
    return _graph.hasEdge(src, dest);
  }

  public Relationship getRelationship(String src, String dest) {
    return _graph.getEdgeLabel(src, dest);
  }
}
示例#22
0
/** Web action: ScaleBar */
public class ScaleBar extends WebTrackBase {
  private static final long serialVersionUID = 1L;
  private static Logger _logger = Logger.getLogger(ScaleBar.class);

  /**
   * Describe your web action parameters here. Public fields in this class will be set using the web
   * request query parameters before calling handle().
   */

  /**
   * Predefined coordinate parameters for GenomeTrack. Uncomment the following lines if you want to
   * receive these parameter values.
   */
  // public String species;   /* human, mouse, etc. */
  // public String revision;  /* hg19, mm9 ... */
  // public String name;	    /* chr1, chr2, ... */
  public long range;

  public int width = 700; /* track pixel width */
  public int height = 20;

  private final int[] template = {5, 2, 1};
  private BufferedImage image;
  private Graphics2D g;

  /** Use dbGroup, dbName parameters to specify database contents to be accessed */
  // public String dbGroup;   /* database group */
  // public String dbName;    /* database name in the group */

  public ScaleBar() {}

  @Override
  public void handle(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    // write your own code to generate an web page here.
    image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    g = image.createGraphics();
    draw();

    response.setContentType("image/png");
    ImageIO.write(image, "png", response.getOutputStream());
    // Generate debug log messages. (log level: trace, debug, info, warn, error, fatal)
    // You can switch log level by specifying -Dloglevel=debug in the eclipse launch file, or
    // use -l (log level) option in the utgb command.
    // _logger.debug("debug message");
  }

  private void draw() {
    long half_range = range / 2;
    int digit = Long.toString(half_range).length();
    long unit = (long) Math.pow(10, digit - 1);

    for (int i = 0; i < template.length; i++) {
      if (half_range >= template[i] * unit) {
        unit *= template[i];
        break;
      }
    }
    double ratio = (double) unit / (double) range / 2.0;
    int x_left = (int) ((0.5 - ratio) * width + 0.5);
    int x_right = (int) ((0.5 + ratio) * width + 0.5);

    int x_1 = (int) ((0.5 - ratio * 0.8) * width + 0.5);
    int x_2 = (int) ((0.5 - ratio * 0.6) * width + 0.5);
    int x_3 = (int) ((0.5 - ratio * 0.4) * width + 0.5);
    int x_4 = (int) ((0.5 - ratio * 0.2) * width + 0.5);
    int x_c = (int) (0.5 * width + 0.5);
    int x_6 = (int) ((0.5 + ratio * 0.2) * width + 0.5);
    int x_7 = (int) ((0.5 + ratio * 0.4) * width + 0.5);
    int x_8 = (int) ((0.5 + ratio * 0.6) * width + 0.5);
    int x_9 = (int) ((0.5 + ratio * 0.8) * width + 0.5);

    int y_lr_u = height * 7 / 20;
    int y_lr_l = height * 13 / 20;
    int y_c_u = height * 2 / 5;
    int y_c_l = height * 3 / 5;
    ;
    int y_o_u = height * 9 / 20;
    ;
    int y_o_l = height * 11 / 20;

    int unit_digit = Long.toString(unit).length();
    int unit_remainder = (unit_digit - 1) % 3;
    unit_remainder++;
    int unit_quotient = (unit_digit - 1) / 3;

    String suffix = "";
    if (unit_quotient == 0) {
    } else if (unit_quotient == 1) {
      suffix = "k";
    } else if (unit_quotient == 2) {
      suffix = "M";
    } else if (unit_quotient == 3) {
      suffix = "G";
    } else if (unit_quotient == 4) {
      suffix = "T";
    }

    g.setColor(Color.BLACK);
    g.drawLine(x_left, height / 2, x_right, height / 2);

    g.drawLine(x_left, y_lr_u, x_left, y_lr_l);
    g.drawLine(x_right, y_lr_u, x_right, y_lr_l);

    g.drawLine(x_c, y_c_u, x_c, y_c_l);

    g.drawLine(x_1, y_o_u, x_1, y_o_l);
    g.drawLine(x_2, y_o_u, x_2, y_o_l);
    g.drawLine(x_3, y_o_u, x_3, y_o_l);
    g.drawLine(x_4, y_o_u, x_4, y_o_l);
    g.drawLine(x_6, y_o_u, x_6, y_o_l);
    g.drawLine(x_7, y_o_u, x_7, y_o_l);
    g.drawLine(x_8, y_o_u, x_8, y_o_l);
    g.drawLine(x_9, y_o_u, x_9, y_o_l);

    Font f = new Font("SansSerif", Font.PLAIN, 10);
    g.setFont(f);
    g.drawString(
        Long.toString(unit).substring(0, unit_remainder) + suffix + "b",
        x_right + 5,
        height / 2 + height / 15);
  }
}