List of usage examples for org.apache.commons.lang ArrayUtils subarray
public static boolean[] subarray(boolean[] array, int startIndexInclusive, int endIndexExclusive)
Produces a new boolean
array containing the elements between the start and end indices.
From source file:org.ncic.bioinfo.sparkseq.algorithms.utils.haplotype.Haplotype.java
public Haplotype insertAllele(final Allele refAllele, final Allele altAllele, final int refInsertLocation, final int genomicInsertLocation) { // refInsertLocation is in ref haplotype offset coordinates NOT genomic coordinates final int haplotypeInsertLocation = ReadUtils.getReadCoordinateForReferenceCoordinate( alignmentStartHapwrtRef, cigar, refInsertLocation, ReadUtils.ClippingTail.RIGHT_TAIL, true); final byte[] myBases = this.getBases(); if (haplotypeInsertLocation == -1 || haplotypeInsertLocation + refAllele.length() >= myBases.length) { // desired change falls inside deletion so don't bother creating a new haplotype return null; }/*from w w w . j a va 2 s . co m*/ byte[] newHaplotypeBases = new byte[] {}; newHaplotypeBases = ArrayUtils.addAll(newHaplotypeBases, ArrayUtils.subarray(myBases, 0, haplotypeInsertLocation)); // bases before the variant newHaplotypeBases = ArrayUtils.addAll(newHaplotypeBases, altAllele.getBases()); // the alt allele of the variant newHaplotypeBases = ArrayUtils.addAll(newHaplotypeBases, ArrayUtils.subarray(myBases, haplotypeInsertLocation + refAllele.length(), myBases.length)); // bases after the variant return new Haplotype(newHaplotypeBases); }
From source file:org.ncic.bioinfo.sparkseq.algorithms.walker.haplotypecaller.HaplotypeCaller.java
private byte[] getTrimedRefBases(ActiveRegion originalRegion, ActiveRegion trimedRegion, byte[] originalRefBases) { GenomeLoc originalLoc = originalRegion.getExtendedLoc(); GenomeLoc trimedLoc = trimedRegion.getExtendedLoc(); if (originalLoc.getStart() > trimedLoc.getStart() || originalLoc.getStop() < trimedLoc.getStop()) { throw new PipelineException("Error !! implement error when get trimed ref base"); }//from w w w .j av a2s . c o m int startIdx = trimedLoc.getStart() - originalLoc.getStart(); int endIdx = originalRefBases.length + trimedLoc.getStop() - originalLoc.getStop(); return ArrayUtils.subarray(originalRefBases, startIdx, endIdx); }
From source file:org.openanzo.client.cli.AnzoConsole.java
AnzoConsole() { try {//from w w w . ja v a 2s.c o m dcw = CommandLineInterface.DEFAULT_CONSOLE; if (dcw.cr != null) { dcw.cr.setBellEnabled(true); dcw.cr.setPrompt("Anzo>"); dcw.cr.setHistoryEnabled(true); } dcw.writeOutput( "Anzo Command Line Client. \nCopyright (c) 2009 Cambridge Semantics Inc and others. All rights reserved."); String version = null; if (CommandLineInterface.class.getProtectionDomain() != null && CommandLineInterface.class.getProtectionDomain().getCodeSource() != null && CommandLineInterface.class.getProtectionDomain().getCodeSource().getLocation() != null) { ProtectionDomain domain = CommandLineInterface.class.getProtectionDomain(); CodeSource source = domain.getCodeSource(); URL location = source.getLocation(); if (location != null) { File file = new File(location.toURI()); if (file.exists() && file.getName().toLowerCase().endsWith(".jar")) { JarFile jar = new JarFile(file); version = jar.getManifest().getMainAttributes().getValue("Bundle-Version"); if (version == null) { version = jar.getManifest().getMainAttributes().getValue("Implementation-Build"); } } } } if (version == null) { version = CommandLineInterface.class.getPackage().getImplementationVersion(); } dcw.writeOutput("Version: " + ((version == null) ? "Unknown" : version)); dcw.writeOutput("Type help for usage"); HashMap<String, Completer> completers = new HashMap<String, Completer>(); completers.put("exit", new NullCompleter()); completers.put("quit", new NullCompleter()); completers.put("connect", new NullCompleter()); completers.put("disconnect", new NullCompleter()); completers.put("trace", new StringsCompleter("on", "off")); Options global = CommandLineInterface.getGlobalOptions(); for (SubCommand sc : CommandLineInterface.subcommands) { String command = sc.getName(); ArrayList<String> subcommands = new ArrayList<String>(); for (Object o : sc.getOptions().getOptions()) { Option options = (Option) o; subcommands.add("-" + options.getOpt()); subcommands.add("--" + options.getLongOpt()); } for (Object o : global.getOptions()) { Option options = (Option) o; subcommands.add("-" + options.getOpt()); subcommands.add("--" + options.getLongOpt()); } completers.put(command, new StringsCompleter(subcommands)); } if (dcw.cr != null) { dcw.cr.addCompleter(new CLICompleter(completers)); } boolean showStackTrace = false; while (true) { String command = dcw.readLine("Anzo>"); if (command != null) { if (EXIT.toLowerCase().equals(command.trim().toLowerCase()) || QUIT.toLowerCase().equals(command.trim().toLowerCase())) { if (context != null && context.client != null && context.client.isConnected()) { context.client.disconnect(); context.client.close(); dcw.writeOutput("Disonnected from:" + context.host); } System.exit(0); } String arguments[] = stringToArgs(command); try { if (arguments.length > 0) { String subcommand = arguments[0]; if ("connect".equals(subcommand.toLowerCase())) { Options options = new Options(); CommandLineInterface.appendGlobalOptions(options); CommandLineParser parser = new PosixParser(); String[] subcommandArgs = (String[]) ArrayUtils.subarray(arguments, 1, arguments.length); CommandLine cl = parser.parse(options, subcommandArgs); if (context == null) { context = CommandLineInterface.createContext(dcw, cl, options, arguments); } if (!context.client.isConnected()) { context.client.connect(); dcw.writeOutput("Connected to:" + context.host); } } else if ("disconnect".equals(subcommand.toLowerCase())) { if (context != null && context.client != null && context.client.isConnected()) { context.client.disconnect(); context.client.close(); dcw.writeOutput("Disonnected from:" + context.host); } else { dcw.writeOutput("Not connected to:" + context.host); } context = null; } else if ("trace".equals(subcommand.toLowerCase())) { String[] subcommandArgs = (String[]) ArrayUtils.subarray(arguments, 1, arguments.length); if (subcommandArgs.length == 0) { dcw.writeOutput("Show Stack Trace:" + showStackTrace); } else { String flag = subcommandArgs[0]; if (flag.equals("on")) showStackTrace = true; else if (flag.equals("off")) showStackTrace = false; else showStackTrace = Boolean.parseBoolean(flag); } if (context != null) { context.showTrace = showStackTrace; } } else if ("version".equals(subcommand.toLowerCase())) { String header = CommandLineInterface.generateVersionHeader(); dcw.writeOutput(header); } else { CommandLineInterface.processCommand(context, false, arguments); } } } catch (AnzoException e) { if (e.getErrorCode() == ExceptionConstants.COMBUS.JMS_CONNECT_FAILED) { dcw.writeError("Connection failed."); if (showStackTrace) dcw.printException(e, showStackTrace); } else { dcw.printException(e, showStackTrace); } } catch (AnzoRuntimeException e) { dcw.printException(e, showStackTrace); } } } } catch (Exception e) { e.printStackTrace(); System.exit(0); } }
From source file:org.openanzo.client.cli.CommandContext.java
private static Map<String, String> findMinimumPrefixMap(Map<String, String> prefixes, Collection<Statement> stmts) { String[] values = new String[stmts.size() * 5]; int i = 0;/*from w w w.j a v a 2 s .co m*/ for (Statement stmt : stmts) { Resource subject = stmt.getSubject(); if (subject instanceof URI) { values[i++] = subject.toString(); } values[i++] = stmt.getPredicate().toString(); Value object = stmt.getObject(); if (object instanceof TypedLiteral) { values[i++] = ((TypedLiteral) object).getDatatypeURI().toString(); } else if (object instanceof URI) { values[i++] = object.toString(); } if (stmt.getNamedGraphUri() != null) values[i++] = stmt.getNamedGraphUri().toString(); } values = (String[]) ArrayUtils.subarray(values, 0, i); Arrays.sort(values); HashMap<String, String> results = new HashMap<String, String>(); for (Map.Entry<String, String> entry : prefixes.entrySet()) { if (isUsed(entry.getValue(), values, i)) { results.put(entry.getKey(), entry.getValue()); } } return results; }
From source file:org.openanzo.client.cli.CommandLineInterface.java
static int processCommand(CommandContext context, boolean exitOnException, String... arguments) { IConsole cw = context != null ? context.getConsoleWriter() : null; if (cw == null) { cw = DEFAULT_CONSOLE;/*www .ja v a 2 s. c om*/ } boolean beep = false; String subcommand = arguments[0]; try { for (SubCommand sc : subcommands) { if (sc.getName().equals(subcommand)) { boolean showStackTrace = false; boolean pauseOnExit = false; try { Options options = sc.getOptions(); appendGlobalOptions(options); CommandLineParser parser = new PosixParser(); String[] subcommandArgs = (String[]) ArrayUtils.subarray(arguments, 1, arguments.length); CommandLine cl = parser.parse(options, subcommandArgs); pauseOnExit = cl.hasOption(PAUSE_EXIT_OPTION.getOpt()); if (context == null) { context = createContext(DEFAULT_CONSOLE, cl, options, arguments); } // set up the trust manager boolean trustAll = cl.hasOption(TRUST_OPTION.getOpt()); showStackTrace = cl.hasOption(SHOW_TRACE_OPTION.getOpt()); beep = cl.hasOption(BEEP_OPTION.getOpt()); TrustManager[] myTMs = new TrustManager[] { new AnzoTrustManager(trustAll, showStackTrace) }; SSLContext ctx = SSLContext.getInstance("TLS"); ctx.init(null, myTMs, new java.security.SecureRandom()); SSLContext.setDefault(ctx); return sc.invoke(cl, context, context.getAnzoClient()); } catch (ParseException e) { cw.writeError("error: "); cw.printException(e, showStackTrace); HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("anzo", sc.getOptions()); if (exitOnException) exitOnError(1, pauseOnExit); } catch (InvalidArgumentException e) { cw.printException(e, showStackTrace); sc.printHelp(cw); if (exitOnException) exitOnError(1, pauseOnExit); } catch (CommandException ae) { if (ae.getCause() != null && ae.getCause() instanceof AnzoException) { if (((AnzoException) ae.getCause()) .getErrorCode() == ExceptionConstants.COMBUS.JMS_CONNECT_FAILED) { cw.writeError("Connection failed."); } } cw.printException(ae, showStackTrace); if (exitOnException) exitOnError(1, pauseOnExit); } catch (UpdateServerException e) { cw.writeError(e.getUserMessage()); for (List<AnzoException> exceptions : e.getErrors()) { if (exceptions != null) { for (AnzoException ae : exceptions) { if (ae.getErrorCode() == ExceptionConstants.COMBUS.JMS_CONNECT_FAILED) cw.writeError("Connection failed."); cw.printException(ae, showStackTrace); } } } if (exitOnException) exitOnError(1, pauseOnExit); } catch (AnzoException e) { if (e.getErrorCode() == ExceptionConstants.COMBUS.JMS_CONNECT_FAILED) cw.writeError("Connection failed."); cw.printException(e, showStackTrace); if (exitOnException) exitOnError(1, pauseOnExit); } catch (AnzoRuntimeException e) { cw.printException(e, showStackTrace); sc.printHelp(cw); if (exitOnException) exitOnError(1, pauseOnExit); } catch (NoSuchAlgorithmException e) { cw.writeError("Error with ssl:"); cw.printException(e, showStackTrace); if (exitOnException) exitOnError(1, pauseOnExit); } catch (KeyManagementException e) { cw.writeError("Error with ssl:"); cw.printException(e, showStackTrace); if (exitOnException) exitOnError(1, pauseOnExit); } } } if (subcommand.startsWith("-")) { cw.writeError("Option not allowed before subcommand: " + subcommand); } else if (subcommand.equals("help")) { if (arguments.length < 2) { printHelp(cw); } else { String helpSubcommand = arguments[1]; for (SubCommand sc : subcommands) { if (sc.getName().equals(helpSubcommand)) { sc.printHelp(cw); return 0; } } cw.writeError("unrecognized subcommand: " + helpSubcommand); } } else { cw.writeError("unrecognized subcommand: " + subcommand); } } finally { if (cw != null && beep) { cw.beep(); } } return 0; }
From source file:org.opencastproject.util.data.Monadics.java
/** Constructor function optimized for arrays. */ public static <A> ListMonadic<A> mlist(final A... as) { return new ListMonadic<A>() { @Override// w w w .j a v a 2 s. c o m public <B> ListMonadic<B> fmap(Function<A, B> f) { final List<B> target = newListBuilder(as.length); for (A a : as) target.add(f.apply(a)); return mlist(target); } @Override public <B, M extends Iterable<B>> ListMonadic<B> bind(Function<A, M> f) { final List<B> target = newListBuilder(); for (A a : as) appendTo(target, f.apply(a)); return mlist(target); } @Override public ListMonadic<A> filter(Function<A, Boolean> p) { List<A> target = newListBuilder(as.length); for (A a : as) { if (p.apply(a)) { target.add(a); } } return mlist(target); } @Override public Option<A> find(Function<A, Boolean> p) { for (A a : as) { if (p.apply(a)) return some(a); } return none(); } @Override public boolean exists(Function<A, Boolean> p) { for (A a : as) { if (p.apply(a)) return true; } return false; } @Override public <B> B foldl(B zero, Function2<B, A, B> f) { B fold = zero; for (A a : as) { fold = f.apply(fold, a); } return fold; } @Override public A reducel(Function2<A, A, A> f) { if (as.length == 0) { throw new RuntimeException("Cannot reduce an empty list"); } else { A fold = as[0]; for (int i = 1; i < as.length; i++) { fold = f.apply(fold, as[i]); } return fold; } } @Override public Option<A> headOpt() { return as.length != 0 ? some(as[0]) : Option.<A>none(); } @Override public A head() { return as[0]; } @Override public A last() { return as[as.length - 1]; } @Override public Option<A> lastOpt() { return as.length > 0 ? some(last()) : Option.<A>none(); } @Override public Option<A> option() { return as.length == 1 ? some(as[0]) : Option.<A>none(); } @Override public ListMonadic<A> tail() { if (as.length <= 1) return mlist(); return (ListMonadic<A>) mlist(ArrayUtils.subarray(as, 1, as.length)); } @Override public ListMonadic<A> take(int n) { return (ListMonadic<A>) mlist(ArrayUtils.subarray(as, 0, n)); } @Override public <M extends Iterable<A>> ListMonadic<A> concat(M bs) { final List<A> t = newListBuilder(as.length); return mlist(appendTo(appendToA(t, as), bs)); } @Override public <X extends A> ListMonadic<A> cons(X a) { return mlist(Collections.<A, List>concat(Collections.<A>list(a), Collections.<A>list(as))); } @Override public <B> ListMonadic<B> inspect(Function<List<A>, List<B>> f) { return mlist(f.apply(value())); } @Override public ListMonadic<A> each(Function<A, Void> e) { for (A a : as) { e.apply(a); } return mlist(as); } @Override public ListMonadic<A> eachIndex(Function2<A, Integer, Void> e) { int i = 0; for (A a : as) { e.apply(a, i++); } return this; } @Override public <B, M extends Iterable<B>> ListMonadic<Tuple<A, B>> zip(M m) { final List<Tuple<A, B>> target = newListBuilder(); int i = 0; final Iterator<B> mi = m.iterator(); while (i < as.length && mi.hasNext()) { target.add(tuple(as[i++], mi.next())); } return mlist(target); } @Override public <B> ListMonadic<Tuple<A, B>> zip(B[] bs) { final List<Tuple<A, B>> target = newListBuilder(min(as.length, bs.length)); int i = 0; while (i < as.length && i < bs.length) { target.add(tuple(as[i], bs[i])); i++; } return mlist(target); } @Override public <B> ListMonadic<Tuple<A, B>> zip(Iterator<B> bs) { final List<Tuple<A, B>> target = newListBuilder(as.length); int i = 0; while (i < as.length && bs.hasNext()) { target.add(tuple(as[i++], bs.next())); } return mlist(target); } @Override public ListMonadic<A> sort(Comparator<A> c) { final List<A> target = list(as); java.util.Collections.sort(target, c); return mlist(target); } @Override public Iterator<A> iterator() { return Collections.iterator(as); } @Override public List<A> value() { return asList(as); } }; }
From source file:org.opencb.biodata.formats.variant.annotation.io.VepFormatReader.java
private void parseVariantFromIdField(Map<String, String> parsedVariant, String[] variantIdFields) { try {/*from w w w. j a v a 2 s . c o m*/ // Some VEP examples: // 1_718787_-/T 1:718786-718787 T ... // 1_718787_T/- 1:718787 - ... // 1_718788_T/A 1:718788 A ... String[] leftVariantFields = variantIdFields[0].split("_"); // Chr id containing _ if (leftVariantFields.length > 3) { parsedVariant.put("chromosome", String.join("_", (String[]) ArrayUtils.subarray(leftVariantFields, 0, leftVariantFields.length - 2))); } else { parsedVariant.put("chromosome", leftVariantFields[0]); } parsedVariant.put("start", leftVariantFields[leftVariantFields.length - 2]); parsedVariant.put("reference", leftVariantFields[leftVariantFields.length - 1]); // parsedVariant.put("alternative", variantIdFields[1]); } catch (ArrayIndexOutOfBoundsException e) { throw new IllegalArgumentException( "Unexpected variant format for column 1: " + variantIdFields.toString()); } }
From source file:org.openhab.binding.homematic.internal.communicator.message.BinRpcMessage.java
/** * Decodes a BIN-RPC message from the given InputStream. *///from w w w.ja v a2 s .c o m public BinRpcMessage(InputStream is, boolean methodHeader, String encoding) throws IOException { this.encoding = encoding; byte sig[] = new byte[8]; int length = is.read(sig, 0, 4); if (length != 4) { throw new EOFException("Only " + length + " bytes received reading signature"); } validateBinXSignature(sig); length = is.read(sig, 4, 4); if (length != 4) { throw new EOFException("Only " + length + " bytes received reading message length"); } int datasize = (new BigInteger(ArrayUtils.subarray(sig, 4, 8))).intValue(); byte[] message = ArrayUtils.addAll(sig, IOUtils.toByteArray(is, datasize)); decodeMessage(message, methodHeader); }
From source file:org.openhab.binding.upb.internal.UPBReader.java
/** * {@inheritDoc}//from w ww.j a va 2 s . c o m */ @Override public void run() { byte[] buffer = new byte[256]; try { for (int len = -1; (len = inputStream.read(buffer)) >= 0;) { if (len > 0) { logger.debug("Received: {}", ArrayUtils.subarray(buffer, 0, len)); } addData(buffer, len); if (Thread.interrupted()) { break; } } } catch (Exception e) { logger.debug("Failed to read input stream.", e); } logger.debug("UPBReader stopped."); }
From source file:org.openhab.binding.zigbee.internal.protocol.serialmessage.GetVersionMessageClass.java
@Override public boolean handleResponse(ZigbeeController zController, SerialMessage lastSentMessage, SerialMessage incomingMessage) { ZigbeeLibraryType = incomingMessage.getMessagePayloadByte(12); ZigbeeVersion = new String(ArrayUtils.subarray(incomingMessage.getMessagePayload(), 0, 11)); logger.debug(String.format("Got MessageGetVersion response. Version = %s, Library Type = 0x%02X", ZigbeeVersion, ZigbeeLibraryType)); checkTransactionComplete(lastSentMessage, incomingMessage); return true;//from w w w .j a v a2 s . c om }