Java tutorial
/* * Copyright 2015 EMBL-European Bioinformatics Institute * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.ensembl.gti.seqstore; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileReader; import java.io.InputStreamReader; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.zip.GZIPInputStream; import org.apache.commons.lang.time.StopWatch; import org.ensembl.gti.seqstore.database.EnaCramSeqStore; import org.ensembl.gti.seqstore.database.JdbcSeqStore; import org.ensembl.gti.seqstore.database.SeqStore; import org.ensembl.gti.seqstore.database.cramstore.EnaCramSubmissionHandler; import org.ensembl.gti.seqstore.server.MetaDataServer; import org.ensembl.gti.seqstore.server.MetaDataServer.MetaDataServerOptions; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.beust.jcommander.JCommander; import com.beust.jcommander.Parameter; public class TestSeqStore { public static class TestSeqStoreOpts extends MetaDataServerOptions { public TestSeqStoreOpts() { } public TestSeqStoreOpts(Properties properties) { super(properties); } @Parameter(names = { "-implementation", "-i" }, description = "Implementation") private String implementation = "jdbc"; @Parameter(names = { "-files", "-f" }, description = "Sequence files", required = true) private List<String> files; public String getImplementation() { return implementation; } public void setImplementation(String implementation) { this.implementation = implementation; } public List<String> getFiles() { return files; } public void setFiles(List<String> files) { this.files = files; } } public static final SeqStore buildSeqStore(TestSeqStoreOpts opts) { SeqStore store = null; switch (opts.getImplementation()) { case "jdbc": store = new JdbcSeqStore( MetaDataServer.buildDataSource(opts.getDbUri(), opts.getDbUser(), opts.getDbPass())); break; case "ena": store = new EnaCramSeqStore( MetaDataServer.buildDataSource(opts.getDbUri(), opts.getDbUser(), opts.getDbPass()), new EnaCramSubmissionHandler(opts.getFtpUri(), opts.getSubmitUri(), opts.getCentre(), opts.getSubmitUser(), opts.getSubmitPass())); break; } return store; } public static void main(String[] args) throws Exception { TestSeqStoreOpts opts = new TestSeqStoreOpts(); new JCommander(opts, args); Logger log = LoggerFactory.getLogger(TestSeqStore.class); log.info("Building store " + opts.getImplementation()); SeqStore store = buildSeqStore(opts); log.info("Built store of type " + store.getClass().getSimpleName() + ": starting session"); long sessionId = store.startSession("testseqstore"); StopWatch watch = new StopWatch(); Map<String, Long> times = new HashMap<>(); for (String file : opts.getFiles()) { log.info("Processing " + file); long time = 0; int n = 0; BufferedReader reader = null; if (file.endsWith(".gz")) { reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(file)))); } else { reader = new BufferedReader(new FileReader(file)); } String line; StringBuilder seq = null; while ((line = reader.readLine()) != null) { if (line.charAt(0) == '>') { n++; time += processSeq(watch, seq, store, sessionId); seq = new StringBuilder(); } else if (seq != null) { seq.append(line.trim()); } } n++; time += processSeq(watch, seq, store, sessionId); reader.close(); times.put(file, time); double secs = 1.0 * time / 1000; log.info(String.format("Completed processing %s: %d sequences in %.3f s (%.3f seq/s) ", file, n, secs, n / secs)); } log.info("Completed test"); } private static long processSeq(StopWatch watch, StringBuilder seq, SeqStore store, long sessionId) { long time = 0; if (seq != null && seq.length() > 0) { String str = seq.toString(); watch.reset(); watch.start(); store.storeSequence(sessionId, str); watch.stop(); time = watch.getTime(); } return time; } }