// Copyright(c) 2006, Shinichi Morishita. All Rights Reserved. public class Chapter1_SimpleStringSearch { // Brute-force search public static void bruteSearch( int[] target, int[] query ) { // "target.length" returns the size of the array target. int targetLen = target.length; int queryLen = query.length; for(int i = 0; i + queryLen <= targetLen; i++){ // i++ means i=i+1. int j = 0; while(j < queryLen && target[i+j] == query[j]) j = j+1; if(j == queryLen) System.out.print(i+" "); } System.out.println(); } // Brute-force search with integers public static void intBruteSearch( int[] target, int[] query ) { // Exit if the target is shorter than the query. if(target.length < query.length) return; // Generate k-mer integer representations of the target and query. int[] intTarget = generateIntTarget(target, query.length); int intQuery = generateIntQuery(query); // Search intTarget for the query. for(int i = 0; i < intTarget.length; i++) if(intTarget[i] == intQuery) System.out.print(i+" "); System.out.println(); } public static int[] generateIntTarget( int[] target, int queryLen){ // Exit if the target is shorter than the query. if(target.length < queryLen) return null; int intTargetLen = target.length-queryLen+1; int[] intTarget = new int[intTargetLen]; // Generate intTarget array. int tmp = 0; int encode = 1; for(int i = 0; i < intTargetLen; i++){ if(i == 0){ // Initialize variables. for(int j=0; j= pivot. for(i++; target[i] < pivot; i++){} // Move from the right until hitting a value =< pivot. for(j--; pivot < target[j] && i < j; j--){} if (i >= j) break; // Exchange a[i] and a[j]. tmp = target[i]; target[i] = target[j]; target[j] = tmp; tmp = posTarget[i]; posTarget[i] = posTarget[j]; posTarget[j] = tmp; } // Exchange a[i] and a[r]. tmp = target[i]; target[i] = target[right]; target[right] = tmp; tmp = posTarget[i]; posTarget[i] = posTarget[right]; posTarget[right] = tmp; return i; } public static int[] nucleotides2IntArray(String targetString){ int targetLen = targetString.length(); int[] targetIntArray = new int[targetLen]; for(int i = 0; i < targetLen; i++) targetIntArray[i] = encode_char(targetString.charAt(i)); return targetIntArray; } public static int encode_char( char c ){ switch (c){ case 'A': return 0; case 'C': return 1; case 'G': return 2; case 'T': return 3; default: return -1; } } public static int[] generateRandomNumbers(int targetLen){ // Generate an array in which elements are selected from {0,1,2,3} at random. int[] target = new int[targetLen]; for(int i=0; i