This repository has been archived by the owner on Dec 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeneBankSearch.java
111 lines (95 loc) · 3.51 KB
/
GeneBankSearch.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Scanner;
/**
* Searches a provides gene sequence file for a certain set of sequences.
* The sequences are then prnted to the console if found.
*
* @author Ben Mcavoy, Nick Figura
*/
public class GeneBankSearch {
static int cacheCapacity, metaSeqLength, debugLevel, cacheSize;
static String btreeFileName, queryFileName, metadataFileName;
static long searchedKey;
static Cache cache;
static BTree bt;
/**
* Main entery point for GeneBankSearch. Command line arguments are:
* GeneBankSearch <0/1 with/without Cache> <btree file> <query file> <Cache Size> [<debug level>]
*
* @param args command line arguements
*/
public static void main(String args[]){
parseArgs(args);
//setup BTree for search
try {
BTree bt = new BTree(new File(btreeFileName), new File(metadataFileName), cache);
if(debugLevel == 0){
System.out.println("Btree File:" + btreeFileName);
System.out.println("Metadata File:" + metadataFileName);
System.out.println("Query File:" + queryFileName);
if(debugLevel == 1) System.out.println("Debug Level : " + debugLevel);
System.out.println();
}
//search for gene subsequences
Scanner queryScanner = new Scanner(new File(queryFileName));
String curLine = "";
do{
curLine = queryScanner.nextLine();
long k = bt.sequenceToLong(curLine);
BTreeNode searchKey = bt.search(bt.root, k);
if(searchKey == null) return;
for(int i = 0; i < searchKey.keys.length; i++){
if(searchKey.keys[i].key == k){
System.out.println(bt.longToSequence(k, metaSeqLength) + " " + searchKey.keys[i].freq);
}
}
}while(queryScanner.hasNextLine());
}catch(FileNotFoundException e){
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
//GeneBankSearch <0/1 with/without Cache> <btree file> <query file> <Cache Size> [<debug level>]
/**
* Stores the arguments from the command line. Also verifies that
* the corret arguments were passed.
*
* @param args arguments from the cmmand line.
*/
public static void parseArgs(String args[]){
debugLevel = 0;
try {
if(args.length > 5 || args.length < 4)
printUsage();
if(args.length == 5 && Integer.parseInt(args[4]) != 0)
printUsage();
if(args[0].equals("1")){
cacheSize = Integer.parseInt(args[3]);
cache = new Cache(cacheCapacity);
}else if(args[0].equals("0")){
cache = null;
}else{
printUsage();
}
btreeFileName = args[1];
queryFileName = args[2];
metadataFileName = btreeFileName.replace("data", "metadata");
metaSeqLength = Integer.parseInt(metadataFileName.split("\\.")[4]);
}catch(NumberFormatException e){
printUsage();
}
}
/**
* Usage message to be printed if correct arguements are not passed.
*/
private static void printUsage(){
System.err.println("Usage: Java GeneBankSearch "
+ "<0/1(no/with Cache)> <Btree File>"
+ " <Query File> <Cache Size> [<Debug level>]");
System.exit(1);
}
}