Example usage for java.lang String substring

List of usage examples for java.lang String substring

Introduction

In this page you can find the example usage for java.lang String substring.

Prototype

public String substring(int beginIndex, int endIndex) 

Source Link

Document

Returns a string that is a substring of this string.

Usage

From source file:edu.msu.cme.rdp.graph.sandbox.KmerStartsFromKnown.java

public static void main(String[] args) throws Exception {
    final KmerStartsWriter out;
    final boolean translQuery;
    final int wordSize;
    final int translTable;

    try {//www . j a v a  2s. c  om
        CommandLine cmdLine = new PosixParser().parse(options, args);
        args = cmdLine.getArgs();

        if (args.length < 2) {
            throw new Exception("Unexpected number of arguments");
        }

        if (cmdLine.hasOption("out")) {
            out = new KmerStartsWriter(cmdLine.getOptionValue("out"));
        } else {
            out = new KmerStartsWriter(System.out);
        }

        if (cmdLine.hasOption("transl-table")) {
            translTable = Integer.valueOf(cmdLine.getOptionValue("transl-table"));
        } else {
            translTable = 11;
        }

        translQuery = cmdLine.hasOption("transl-kmer");
        wordSize = Integer.valueOf(args[0]);

    } catch (Exception e) {
        new HelpFormatter().printHelp("KmerStartsFromKnown <word_size> [name=]<ref_file> ...", options);
        System.err.println(e.getMessage());
        System.exit(1);
        throw new RuntimeException("Stupid jvm"); //While this will never get thrown it is required to make sure javac doesn't get confused about uninitialized variables
    }

    long startTime = System.currentTimeMillis();

    /*
     * if (args.length == 4) { maxThreads = Integer.valueOf(args[3]); } else
     * {
     */

    //}

    System.err.println("Starting kmer mapping at " + new Date());
    System.err.println("*  References:              " + Arrays.asList(args));
    System.err.println("*  Kmer length:             " + wordSize);

    for (int index = 1; index < args.length; index++) {
        String refName;
        String refFileName = args[index];
        if (refFileName.contains("=")) {
            String[] lexemes = refFileName.split("=");
            refName = lexemes[0];
            refFileName = lexemes[1];
        } else {
            String tmpName = new File(refFileName).getName();
            if (tmpName.contains(".")) {
                refName = tmpName.substring(0, tmpName.lastIndexOf("."));
            } else {
                refName = tmpName;
            }
        }

        File refFile = new File(refFileName);

        if (SeqUtils.guessSequenceType(refFile) != SequenceType.Nucleotide) {
            throw new Exception("Reference file " + refFile + " contains " + SeqUtils.guessFileFormat(refFile)
                    + " sequences but expected nucleotide sequences");
        }

        SequenceReader seqReader = new SequenceReader(refFile);
        Sequence seq;

        while ((seq = seqReader.readNextSequence()) != null) {
            if (seq.getSeqName().startsWith("#")) {
                continue;
            }
            ModelPositionKmerGenerator kmers = new ModelPositionKmerGenerator(seq.getSeqString(), wordSize,
                    SequenceType.Nucleotide);

            for (char[] charmer : kmers) {
                int pos = kmers.getModelPosition() - 1;
                if (translQuery) {
                    if (pos % 3 != 0) {
                        continue;
                    } else {
                        pos /= 3;
                    }
                }

                String kmer = new String(charmer);

                out.write(new KmerStart(refName, seq.getSeqName(), seq.getSeqName(), kmer, 1, pos, translQuery,
                        (translQuery ? ProteinUtils.getInstance().translateToProtein(kmer, true, translTable)
                                : null)));

            }
        }
        seqReader.close();
    }
    out.close();
}

From source file:MarkVowels.java

public static void main(String[] args) {
    System.out.print("Enter a string: ");
    String s = sc.nextLine();
    String originalString = s;//from ww  w .  ja  va  2 s.co  m

    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        if ((c == 'A') || (c == 'a') || (c == 'E') || (c == 'e') || (c == 'I') || (c == 'i') || (c == 'O')
                || (c == 'o') || (c == 'U') || (c == 'u')) {
            String front = s.substring(0, i);
            String back = s.substring(i + 1);
            s = front + "*" + back;
        }
    }
    System.out.println(originalString);
    System.out.println(s);
}

From source file:mil.tatrc.physiology.biogears.verification.ScenarioPlotTool.java

public static void main(String[] args) {
    if (args.length < 1) {
        Log.fatal("Expected inputs : [results file path] [result in results file NOT to plot] ");
        return;//from w w w .j a v  a 2s .com
    }
    File f = new File(args[0]);
    if (!f.exists()) {
        Log.fatal("Input file cannot be found");
        return;
    }
    String reportDir = "./graph_results/" + f.getName();
    reportDir = reportDir.substring(0, reportDir.lastIndexOf(".")) + "/";
    ScenarioPlotTool t = new ScenarioPlotTool();
    t.graphResults(args[0], reportDir);
}

From source file:ComputeResult.java

public static void main(String[] args) {
    String original = "software";
    StringBuilder result = new StringBuilder("hi");
    int index = original.indexOf('a');

    /*1*/ result.setCharAt(0, original.charAt(0));
    /*2*/ result.setCharAt(1, original.charAt(original.length() - 1));
    /*3*/ result.insert(1, original.charAt(4));
    /*4*/ result.append(original.substring(1, 4));
    /*5*/ result.insert(3, (original.substring(index, index + 2) + " "));

    System.out.println(result);//from   ww w  .j av a2 s.  c  o m
}

From source file:RegionMatchesDemo.java

public static void main(String[] args) {

    String searchMe = "Green Eggs and Ham";
    String findMe = "Eggs";
    int len = findMe.length();
    boolean foundIt = false;

    int i = 0;//  w  w w .j a  v  a  2s  . c  o  m
    while (!searchMe.regionMatches(i, findMe, 0, len)) {
        i++;
        foundIt = true;
    }
    if (foundIt) {
        System.out.println(searchMe.substring(i, i + len));
    }
}

From source file:mil.tatrc.physiology.utilities.csv.plots.CSVPlotTool.java

public static void main(String[] args) {
    if (args.length < 1) {
        Log.fatal("Expected inputs : [results file path]");
        return;/*w  ww  .  j a  va 2 s  .com*/
    }
    File f = new File(args[0]);
    if (!f.exists()) {
        Log.fatal("Input file cannot be found");
        return;
    }
    String reportDir = "./graph_results/" + f.getName();
    reportDir = reportDir.substring(0, reportDir.lastIndexOf(".")) + "/";
    CSVPlotTool t = new CSVPlotTool();
    t.graphResults(args[0], reportDir);
}

From source file:com.meli.client.controller.AppController.java

public static void main(String[] args) {
        String query = "";
        String fileName = "";
        String countryCode = "";
        String option = "";

        for (int i = 0; i < args.length - 1; i++) {
            option = args[i];//from   w w w.j a  v  a  2 s  .c  o  m

            if (option.length() >= 2) {
                String argVal = option.substring(0, 2);

                if (!args[i + 1].isEmpty() && !args[i + 1].matches("-[qfc]")) {
                    if ("-q".equals(argVal)) {
                        query = args[i + 1];
                        continue;
                    }
                    if ("-f".equals(argVal)) {
                        fileName = args[i + 1];
                        continue;
                    }
                    if ("-c".equals(argVal))
                        countryCode = args[i + 1];

                }
            }
        }

        if (!fileName.isEmpty())
            processQueryFile(fileName, countryCode);

        if (!query.isEmpty())
            doApiCalls(query, countryCode);

    }

From source file:SparkKMer.java

public static void main(String[] args) throws Exception {
    //Setup//from   w ww . j  a v a 2s  . c om
    SparkConf sparkConf = new SparkConf().setAppName("SparkKMer");
    JavaSparkContext jsc = new JavaSparkContext(sparkConf);
    //Agrument parsing
    if (args.length < 2) {
        System.err.println("Usage: SparkKMer <accession> <kmer-length>");
        System.exit(1);
    }
    final String acc = args[0];
    final int KMER_LENGTH = Integer.parseInt(args[1]);

    //Check accession and split
    ReadCollection run = gov.nih.nlm.ncbi.ngs.NGS.openReadCollection(acc);
    long numreads = run.getReadCount();

    //Slice the job
    int chunk = 20000; /** amount of reads per 1 map operation **/
    int slices = (int) (numreads / chunk / 1);
    if (slices == 0)
        slices = 1;
    List<LongRange> sub = new ArrayList<LongRange>();
    for (long first = 1; first <= numreads;) {
        long last = first + chunk - 1;
        if (last > numreads)
            last = numreads;
        sub.add(new LongRange(first, last));
        first = last + 1;
    }
    System.err.println("Prepared ranges: \n" + sub);

    JavaRDD<LongRange> jobs = jsc.parallelize(sub, slices);
    //Map
    //
    JavaRDD<String> kmers = jobs.flatMap(new FlatMapFunction<LongRange, String>() {
        ReadCollection run = null;

        @Override
        public Iterable<String> call(LongRange s) {
            //Executes on task nodes
            List<String> ret = new ArrayList<String>();
            try {
                long first = s.getMinimumLong();
                long last = s.getMaximumLong();
                if (run == null) {
                    run = gov.nih.nlm.ncbi.ngs.NGS.openReadCollection(acc);
                }
                ReadIterator it = run.getReadRange(first, last - first + 1, Read.all);
                while (it.nextRead()) {
                    //iterate through fragments
                    while (it.nextFragment()) {
                        String bases = it.getFragmentBases();
                        //iterate through kmers
                        for (int i = 0; i < bases.length() - KMER_LENGTH; i++) {
                            ret.add(bases.substring(i, i + KMER_LENGTH));
                        }
                    }
                }
            } catch (ErrorMsg x) {
                System.err.println(x.toString());
                x.printStackTrace();
            }
            return ret;
        }
    });
    //Initiate kmer counting;
    JavaPairRDD<String, Integer> kmer_ones = kmers.mapToPair(new PairFunction<String, String, Integer>() {
        @Override
        public Tuple2<String, Integer> call(String s) {
            return new Tuple2<String, Integer>(s, 1);
        }
    });
    //Reduce counts
    JavaPairRDD<String, Integer> counts = kmer_ones.reduceByKey(new Function2<Integer, Integer, Integer>() {
        @Override
        public Integer call(Integer i1, Integer i2) {
            return i1 + i2;
        }
    });
    //Collect the output
    List<Tuple2<String, Integer>> output = counts.collect();
    for (Tuple2<String, Integer> tuple : output) {
        System.out.println(tuple._1() + ": " + tuple._2());
    }
    jsc.stop();
}

From source file:Main.java

public static void main(String[] args) {
    String searchMe = "Green Eggs and Ham";
    String findMe = "Eggs";
    int searchMeLength = searchMe.length();
    int findMeLength = findMe.length();
    boolean foundIt = false;
    for (int i = 0; i <= (searchMeLength - findMeLength); i++) {
        if (searchMe.regionMatches(i, findMe, 0, findMeLength)) {
            foundIt = true;//from   w  w w . j a v  a2s.c  om
            System.out.println(searchMe.substring(i, i + findMeLength));
            break;
        }
    }
    if (!foundIt)
        System.out.println("No match found.");
}

From source file:org.eclipse.swt.snippets.SnippetLauncher.java

public static void main(String[] args) {
    File sourceDir = SnippetsConfig.SNIPPETS_SOURCE_DIR;
    boolean hasSource = sourceDir.exists();
    int count = 500;
    if (hasSource) {
        File[] files = sourceDir.listFiles();
        if (files.length > 0)
            count = files.length;/*from ww w.  j  a va 2s  . c o m*/
    }
    for (int i = 1; i < count; i++) {
        if (SnippetsConfig.isPrintingSnippet(i))
            continue; // avoid printing to printer
        String className = "Snippet" + i;
        Class<?> clazz = null;
        try {
            clazz = Class.forName(SnippetsConfig.SNIPPETS_PACKAGE + "." + className);
        } catch (ClassNotFoundException e) {
        }
        if (clazz != null) {
            System.out.println("\n" + clazz.getName());
            if (hasSource) {
                File sourceFile = new File(sourceDir, className + ".java");
                try (FileReader reader = new FileReader(sourceFile);) {
                    char[] buffer = new char[(int) sourceFile.length()];
                    reader.read(buffer);
                    String source = String.valueOf(buffer);
                    int start = source.indexOf("package");
                    start = source.indexOf("/*", start);
                    int end = source.indexOf("* For a list of all");
                    System.out.println(source.substring(start, end - 3));
                    boolean skip = false;
                    String platform = SWT.getPlatform();
                    if (source.contains("PocketPC")) {
                        platform = "PocketPC";
                        skip = true;
                    } else if (source.contains("OpenGL")) {
                        platform = "OpenGL";
                        skip = true;
                    } else if (source.contains("JavaXPCOM")) {
                        platform = "JavaXPCOM";
                        skip = true;
                    } else {
                        String[] platforms = { "win32", "gtk" };
                        for (int p = 0; p < platforms.length; p++) {
                            if (!platforms[p].equals(platform) && source.contains("." + platforms[p])) {
                                platform = platforms[p];
                                skip = true;
                                break;
                            }
                        }
                    }
                    if (skip) {
                        System.out.println("...skipping " + platform + " example...");
                        continue;
                    }
                } catch (Exception e) {
                }
            }
            Method method = null;
            String[] param = SnippetsConfig.getSnippetArguments(i);
            try {
                method = clazz.getMethod("main", param.getClass());
            } catch (NoSuchMethodException e) {
                System.out.println("   Did not find main(String [])");
            }
            if (method != null) {
                try {
                    method.invoke(clazz, new Object[] { param });
                } catch (IllegalAccessException e) {
                    System.out.println("   Failed to launch (illegal access)");
                } catch (IllegalArgumentException e) {
                    System.out.println("   Failed to launch (illegal argument to main)");
                } catch (InvocationTargetException e) {
                    System.out.println("   Exception in Snippet: " + e.getTargetException());
                }
            }
        }
    }
}