Skip to content
Snippets Groups Projects
Commit 2c8d2c1e authored by Christopher Schankula's avatar Christopher Schankula :earth_africa:
Browse files

Merge branch 'TeamA' into 'master'

Merge Team A with master

See merge request schankuc/2XB3!8
parents 2ae8d8ef e20b4e0d
No related branches found
No related tags found
No related merge requests found
Showing
with 1704 additions and 9 deletions
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="lib" path="json-simple-1.1.1.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
......@@ -7,4 +7,5 @@
*.blg
*DS_Store*
*.csv
.idea/
\ No newline at end of file
*.ser
.idea/
File added
public class HelloWorld {
// Abbreviated Print line Function
private static void println(String line) {
System.out.println(line);
}
public static void main(String[] args) {
System.out.println("I really want a monkey!");
}
......
import java.io.FileNotFoundException;
import org.json.simple.parser.ParseException;
import data.BioTree;
import data.DataStore;
import data.FileProcessor;
import data.Record;
import search.BasicSearch;
import sort.KDT;
public class Main {
public static void main(String[] args) {
printLogo();
//load data
try {
BioTree.init("data/biotree/");
DataStore.records = new KDT<Record>("data/kdt.ser");
} catch (Exception e0) {
try {
BioTree.init();
FileProcessor.initProcessing();
} catch (NumberFormatException | ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BioTree.write("data/biotree/");
DataStore.records.writeToFile("data/kdt.ser");
};
BasicSearch.init();
}
private static void printLogo() {
System.out.println("======== TRAWLEXPERT ALPHA v1 ========");
System.out.println(" _...----.\r\n" +
" _, _,-'_...--'`/\r\n" +
" ,^__\\__ ,' ,' /(\r\n" +
" _,.' | | `-._; \\/\r\n" +
" ,-' | / / \\ \\-. \\\r\n" +
" ,' \\ \\ \\ < / < \\ ,:\r\n" +
" / | < `.\\ > / \\ ,' | _,-'/\r\n" +
" / / \\ \\ \\ \\/ _; ,-' ;\r\n" +
" / `. \\ / \\ /`<__,' \\ /|\r\n" +
" ; (O) > | > < \\ \\`^. / :|\r\n" +
" / :\\ |_.--. < ,` / / ( > :|\r\n" +
" >) ;/,=' `.\\ \\ | / / __/ \\ ;|\r\n" +
" \\ ,' |) ,'/ / | ) |/-.`. / \\|\r\n" +
" `. ,' | `--=='` > | `./`-.\\ `-._ :\r\n" +
" `. / / \\ \\ ;`.__/ `-.\\\r\n" +
" `-._ \\ |_ : < _; < SSt\r\n" +
" ``'.:``-._____\\_,-'-..___,\\\r\n" +
" \\`.|`._\\ `--..__ `._\r\n" +
" `-' `````````");
System.out.println("Loading.......");
}
}
package data;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import org.json.simple.parser.ParseException;
import search.BST;
import sort.KDT;
public class BioTree implements Serializable {
/**
*
*/
private static final long serialVersionUID = 4291273291916906661L;
//FIXME: replace with a single kd-tree
private static BST<Integer, TaxonNode> idNodes = new BST<Integer, TaxonNode>();
private static BST<String, TaxonNode> strNodes = new BST<String, TaxonNode>();
private static BST<String, Integer> incorrectNames = new BST<String, Integer>();
public static void main(String[] args) throws IOException, ParseException {
BioTree.init("data/biotree");
System.out.println(idNodes.get(2));
Iterable<Integer> children = getNonEmptyChildren(159512);
for (Integer i: children)
System.out.println(i);
}
/**
* Initialize species abstract object
*/
public static void init() {
idNodes = new BST<Integer, TaxonNode>();
strNodes = new BST<String, TaxonNode>();
incorrectNames = new BST<String, Integer>();
}
/**
* Reads the BioTree from a file written by write().
* TODO: Implement
*
* @param fn
* Filename to read from
* @return
*/
public static void init(String fn) {
BST<Integer, TaxonNode> idNodes = null;
try {
FileInputStream fileIn = new FileInputStream(fn+"/idnodes.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
idNodes = (BST<Integer, TaxonNode>) in.readObject();
in.close();
fileIn.close();
} catch (IOException i) {
i.printStackTrace();
} catch (ClassNotFoundException c) {
System.out.println("Employee class not found");
c.printStackTrace();
}
BioTree.idNodes = idNodes;
BST<String, TaxonNode> strNodes = null;
try {
FileInputStream fileIn = new FileInputStream(fn+"/strNodes.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
strNodes = (BST<String, TaxonNode>) in.readObject();
in.close();
fileIn.close();
} catch (IOException i) {
i.printStackTrace();
} catch (ClassNotFoundException c) {
System.out.println("Employee class not found");
c.printStackTrace();
}
BioTree.strNodes = strNodes;
BST<String, Integer> incorrectNames = null;
try {
FileInputStream fileIn = new FileInputStream(fn+"/incorNames.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
incorrectNames = (BST<String, Integer>) in.readObject();
in.close();
fileIn.close();
} catch (IOException i) {
i.printStackTrace();
} catch (ClassNotFoundException c) {
System.out.println("Employee class not found");
c.printStackTrace();
}
BioTree.incorrectNames = incorrectNames;
}
/**
* Writes the BioTree BST to a file.
* TODO: Implement
*
* @param dir
* Filename to write to
*/
public static void write(String dir) {
//https://examples.javacodegeeks.com/core-java/io/file/check-if-directory-exists/
File d = new File(dir);
if (!d.exists())
d.mkdirs();
try {
FileOutputStream fileOut =
new FileOutputStream(dir+"/idNodes.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(BioTree.idNodes);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in /tmp/kdtree.ser");
} catch (IOException i) {
i.printStackTrace();
}
try {
FileOutputStream fileOut =
new FileOutputStream(dir+"/strNodes.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(BioTree.strNodes);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in /tmp/kdtree.ser");
} catch (IOException i) {
i.printStackTrace();
}
try {
FileOutputStream fileOut =
new FileOutputStream(dir+"/incorNames.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(BioTree.incorrectNames);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in /tmp/kdtree.ser");
} catch (IOException i) {
i.printStackTrace();
}
}
/**
* Process a record. Adds classification to tree if it doesn't exist.
* Returns the taxonId of the new / existing record.
*
* @param taxonId The taxonId of the possible new entry
* @return taxonId of new species entry
*/
public static Integer processRecord(int taxonId) {
//pass taxonId directly to function to add / increment it
if (processTaxonId(taxonId)) return null;
return taxonId;
}
/**
* Process a record. Adds classification to tree if it doesn't exist.
* Returns the taxonId of the new / existing record.
*
* @param scientificName The scientific name of the possible new entry
* @return taxonId of new / existing entry
* @throws IOException
* @throws ParseException
*/
public static Integer processRecord(String scientificName) throws IOException, ParseException {
//reverse lookup based on name, try adding the found taxonId.
System.out.println("Processing " + scientificName);
TaxonNode res = strNodes.get(scientificName);
if (res == null) {
System.out.println("Not already found.");
Integer incorrectNameId = incorrectNames.get(scientificName);
if (incorrectNameId != null) if (incorrectNameId == -1) return null;
res = idNodes.get(incorrectNameId);
}
Integer taxonId = null;
//if the taxonId was not found in the local database
if (res == null) {
try {
taxonId = WormsAPI.nameToID(scientificName);
} catch (Exception e) {
taxonId = WormsAPI.nameToRecordID(scientificName);
if (taxonId != null)
incorrectNames.put(scientificName, taxonId);
else {
incorrectNames.put(scientificName, -1);
}
}
}
else
taxonId = res.getTaxonId();
if (taxonId == null) return null;
if (taxonId == -999)
taxonId = WormsAPI.nameToRecordID(scientificName);
if (processTaxonId(taxonId)) return null;
return taxonId;
}
/**
* Process a new entry if it doesn't exist. If it does exist, increment the number
* of Records for this classification by one.
* @param taxonId New / existing TaxonID to add / increment count thereof.
* @return true if the process failed, false if nothing went wrong
*/
private static boolean processTaxonId(int taxonId) {
TaxonNode[] newNodes = null; //possible eventual new nodes
TaxonNode tx = idNodes.get(taxonId); //search tree to see if the node exists already
System.out.println("tx" + tx);
if (tx != null) { //if it does exist, increment its count
tx.incCount();
}
else { //otherwise, perform API call to get tree
try {
newNodes = WormsAPI.idToClassification(taxonId);
} catch (IOException | ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (newNodes == null) return true;
newNodes[newNodes.length - 1].incCount(); //one of the new nodes exists
for (int i = newNodes.length - 1; i >= 0; i--) { //iterate over all node starting from lowest child
tx = newNodes[i];
TaxonNode current = idNodes.get(tx.getTaxonId());
TaxonNode parent = null;
if (i > 0) { //if this is not the highest up find its parent
parent = idNodes.get(newNodes[i - 1].getTaxonId()); //the parent is either already in existence
if (parent == null) parent = newNodes[i - 1]; //or is is the old one that will be added later
}
if (current == null) { //if this node is not found, add it
System.out.println("Put: " + tx.getTaxonId());
idNodes.put(tx.getTaxonId(), tx); //put it in the search structure
strNodes.put(tx.getName(), tx);
tx.setParent(parent); //set its parent to the last
if (parent != null) parent.addChild(tx); //if a parent exists, add it as a child to its parent
} else
//stop loop if this node already exists in the tree (all its parents must exist too!)
break;
}
}
return false;
}
/**
* Get the species at a given index (taxonId). This assumes that the
* node already exists or else it will return null. As such, it is best
* to use this function once all the data has been parsed and the BioTree
* has been built.
*
* @param i
* The speciesid (index) of the species.
* @return The Species object.
*/
public static TaxonNode getTaxonRecord(int taxonId) {
return idNodes.get(taxonId);
}
public static Iterable<Integer> getNonEmptyChildren(int taxonId){
ArrayList<Integer> result = new ArrayList<Integer>();
getNonEmptyChildren(idNodes.get(taxonId), result);
return result;
}
private static void getNonEmptyChildren(TaxonNode txNode, ArrayList<Integer> result) {
if (txNode == null) return;
if (txNode.getCount() > 0) result.add(txNode.getTaxonId());
for (TaxonNode tx: txNode.getChildren()) {
getNonEmptyChildren(tx, result);
}
}
public static void printTree() {
printTree(idNodes.get(2), 0);
}
public static void printTree(int taxonId) {
TaxonNode txNode = idNodes.get(taxonId);
if (txNode == null) return;
printTree(txNode, 0);
}
public static void printTree(String scientificName) {
TaxonNode txNode = strNodes.get(scientificName);
if (txNode == null)
try {
int taxonId = WormsAPI.nameToRecordID(scientificName);
txNode = idNodes.get(taxonId);
} catch (IOException | ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (txNode == null) return;
printTree(txNode, 0);
}
/**
* Print a taxonNode's tree starting at the supplied root.
* @param tx
* @param level
*/
private static void printTree(TaxonNode tx, int level) {
String padd = new String(new char[level * 4]).replace('\0', ' ');
System.out.format(padd + "%s %d\n", tx.getName(), tx.getCount());
for (TaxonNode tx2: tx.getChildren())
printTree(tx2, level + 1);
}
}
package data;
import java.util.ArrayList;
import sort.GeneralCompare;
import sort.KDT;
/**
* Module for storing Record objects in a central place.
* @author Christopher W. Schankula
*
*/
public class DataStore {
public static KDT<Record> records;
public static void init(Record[] recs) {
GeneralCompare<Record> ax0 = (r0,r1) -> ((Record) r0).getDate().compareTo(((Record) r1).getDate());
GeneralCompare<Record> ax1 = (r0,r1) -> ((Record) r0).getTaxonId() - ((Record) r1).getTaxonId();
GeneralCompare<Record> ax2 = (r0,r1) -> Float.compare(((Record) r0).getLatitude(), ((Record) r1).getLatitude());
GeneralCompare<Record> ax3 = (r0,r1) -> Float.compare(((Record) r0).getLongitude(), ((Record) r1).getLongitude());
ArrayList<GeneralCompare<Record>> axes = new ArrayList<GeneralCompare<Record>>(4);
axes.add(ax0);axes.add(ax1);axes.add(ax2);axes.add(ax3);
records = new KDT<Record>(axes, recs);
System.out.println("K:" + records.getK());
System.out.println("H:" + records.height());
System.out.println("S:" + records.size());
}
}
package data;
import java.io.Serializable;
/**
* Date ADT for Records
*/
public class Date implements Comparable<Date>, Serializable {
private static final long serialVersionUID = -5256185778691324647L;
private final int year;
private final int month;
private final int day;
/**
* Initializes the object
*/
public Date (int year, int month, int day) {
this.year= year;
this.month= month;
this.day = day;
}
/**
* Gets the year
*
* @return The year of the date
*/
public int getYear() {
return this.year;
}
/**
* Gets the month
*
* @return The month of the date
*/
public int getMonth() {
return this.month;
}
/**
* Gets the day
*
* @return The day of the date
*/
public int getDay() {
return this.day;
}
/**
* Gets the day
* @param other -date object that it's comparing to
* @return comparedToValue -1 if greater, -1 if smaller and 0 if equal
*/
public int compareTo(Date other) {
if (this.year > other.getYear()) return 1;
else if (this.year < other.getYear()) return -1;
else {
if (this.month > other.getMonth()) return 1;
else if (this.month < other.getMonth()) return -1;
else {
if (this.day > other.getDay()) return 1;
else if (this.day < other.getDay()) return -1;
else return 0;
}
}
}
}
package data;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner; // Testing ONLY
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.simple.parser.ParseException;
import search.BasicSearch;
/**
* This class reads and parses files in the format of occurences.csv
* It provides methods to get chunks of data.
*
* Notes:
* - Processes one row at a time
* - Parsed data is in string format
* - TOO MANY FUNCTION CALLS?
*/
public class FileProcessor {
private static String path = "src/occurrence.csv";
private static ArrayList<Record> al = new ArrayList<Record>(1000); //temporary storage for records
/**
* Gets the path string
*
* @return Path String
*/
public String getPath() {
return path;
}
/**
* Sets the path string
*/
public void setPath(String newPath) {
path = newPath;
}
/**
* Initialize Processing.
* Reads file at path.
* Calls parse() for each line
* @throws ParseException
* @throws NumberFormatException
*/
public static void initProcessing() throws NumberFormatException, ParseException {
FileReader fr;
BufferedReader br;
try {
fr = new FileReader(path);
br = new BufferedReader(fr);
Scanner s = new Scanner(System.in); //Testing ONLY
String currentLine;
br.readLine(); // Reads Past Field Names
int i = 0;
while ((currentLine = br.readLine()) != null) {
//System.out.println(currentLine); //Testing ONLY for checking one line at a time
i++;
System.out.println("Processed line " + i);
parse(currentLine);
}
//initialize the storage of records
DataStore.init(al.toArray(new Record[al.size()]));
al = null; //free temporary memory storage of Record objects now that they're in the KDTree.
br.close();
fr.close();
//s.nextLine(); //Testing ONLY for checking one line at a time
s.close();//Testing ONLY
}
catch (IOException e) {
e.printStackTrace();
}
}
/**
* Parses data from string
* Calls createObject on successful regex matching
*
* @param currentLine, a line/row of data
* @throws IOException
* @throws ParseException
* @throws NumberFormatException
*/
private static void parse(String currentLine) throws IOException, NumberFormatException, ParseException {
/* Regex Pattern Grouping Guide
* Retrieve String Groups with: matchEventId.group(x):
* group 0: full matched string
* group 1: occurrenceId
* group 2: individualCount
* group 3: eventId
* group 4: year
* group 5: month
* group 6: date
* group 7: latitude
* group 8: longitude
* group 9: taxonId
* group 10: Scientific Name
*/
Pattern patternEventId = Pattern.compile("([^,]+)?,([^,]+)?,[^,]+,[^,]+,[^,]+OP_ID (\\d+)?,(\\d+)?-(\\d+)?-(\\d+)?,(\\d+.\\d+)?,(-\\d+.\\d+)?,[^,\\d]+(\\d+)?,([^,]+)?");
Matcher matchEventId = patternEventId.matcher(currentLine);
if(matchEventId.find()) {
// Testing ONLY Print lines
// System.out.println("Full String:" + matchEventId.group(0));
// System.out.println("Occurence Id:" + matchEventId.group(1));
// System.out.println("Ind. Count:" + matchEventId.group(2));
// System.out.println("event Id:" + matchEventId.group(3));
// System.out.println("Year:" + matchEventId.group(4));
// System.out.println("Month:" + matchEventId.group(5));
// System.out.println("Day:" + matchEventId.group(6));
// System.out.println("lat:" + matchEventId.group(7));
// System.out.println("long:" + matchEventId.group(8));
// System.out.println("tax Id:" + matchEventId.group(9));
// System.out.println("Scientific Name:" + matchEventId.group(10));
createObjects(matchEventId);
}
else {
System.out.println("Regex Matching Failed.");
}
}
/**
* Calls BioTree's processRecord and another method to create a Record
*
* @param matchEventId
* @throws ParseException
* @throws NumberFormatException
*/
public static void createObjects(Matcher matchEventId) throws NumberFormatException, ParseException {
// Call BioTree
Record rec = null;
// if(matchEventId.group(9) != null) {
// try {
// BioTree.processRecord(Integer.parseInt(matchEventId.group(9)));
// rec = createRecord(Integer.parseInt(matchEventId.group(3)), matchEventId.group(1), Integer.parseInt(matchEventId.group(9)), Integer.parseInt(matchEventId.group(2)), Float.parseFloat(matchEventId.group(7)), Float.parseFloat(matchEventId.group(8)), Integer.parseInt(matchEventId.group(4)), Integer.parseInt(matchEventId.group(5)), Integer.parseInt(matchEventId.group(6)));
// } catch(NullPointerException e) {
// System.out.println("NPE:" + e);
// }
// }
// else if (!matchEventId.group(10).equals("NA")) {
int errorCount=0; // TESTING ONLY
if (!matchEventId.group(10).equals("NA")) {
try{
al.add(createRecord(Integer.parseInt(matchEventId.group(3)), matchEventId.group(1), BioTree.processRecord(matchEventId.group(10)), Integer.parseInt(matchEventId.group(2)), Float.parseFloat(matchEventId.group(7)), Float.parseFloat(matchEventId.group(8)), Integer.parseInt(matchEventId.group(4)), Integer.parseInt(matchEventId.group(5)), Integer.parseInt(matchEventId.group(6))));
} catch(IOException e) {
System.out.println("Input Error:" + e);
} catch(NullPointerException e) {
System.out.println("Null Pointer Error:" + e);
}
}
//do something with rec
// I was thinking to just do something with the record the moment is created? Maybe saves a bit of time?
}
/**
* Create a Record Object
*
* @param eventId
* @param occurId
* @param taxonId
* @param individualCount
* @param latitude
* @param longitude
* @param year
* @param month
* @param day
* @return
*/
public static Record createRecord(int eventId, String occurId, int taxonId, int individualCount, float latitude, float longitude, int year, int month, int day) {
return new Record(eventId, occurId, taxonId, individualCount, latitude, longitude, year, month, day);
}
public static void main(String[] args) throws NumberFormatException, ParseException {
initProcessing();
}
}
package data;
import java.io.Serializable;
public class Record implements Comparable<Record>, Serializable {
/**
*
*/
private static final long serialVersionUID = 3564218991166319058L;
private final int eventId;
private final String occurId;
private final int taxonId;
private final int individualCount;
private final float latitude;
private final float longitude;
private final Date recDate;
// public static void main(String[] args) {
// ///test main
// Record record = new Record(1111,"11",11,111,(float) 94.56, 94,1998,1,9);
// System.out.println(record.toString());
// }
/**
* Initialize Record abstract object
*/
public Record( int eventId, String occurId, int taxonId, int individualCount, float latitude, float longitude, int year, int month, int day) {
this.eventId = eventId;
this.occurId = occurId;
this.taxonId = taxonId;
this.individualCount = individualCount;
this.latitude = latitude;
this.longitude = longitude;
this.recDate = new Date(year,month,day);
}
/**
* Gets the event ID
*
* @return The eventid of the record
*/
public int getEventId() {
return eventId;
}
/**
* Gets the occurrence ID
*
* @return The occurrence ID of the record
*/
public String getOccurId() {
return occurId;
}
/**
* Gets the taxon ID
*
* @return The taxon ID of the record
*/
public int getTaxonId() {
return taxonId;
}
/**
* Gets the individual count of the record
*
* @return The individual count of the record
*/
public int getCount() {
return individualCount;
}
/**
* Gets latitude of the record
*
* @return The latitude of the record
*/
public float getLatitude() {
return latitude;
}
/**
* Gets longitude of the record
*
* @return The longitude of the record
*/
public float getLongitude() {
return longitude;
}
/**
* Gets date of the record
*
* @return The date of the record
*/
public Date getDate() {
return recDate;
}
public String toString() {
String format = "|%1$-45s|%2$-15s|%3$-15s|%4$-15s|%5$-15s|%6$-15s\n";
String num = String.format(format, BioTree.getTaxonRecord(this.taxonId).getName(),this.individualCount,this.longitude, this.latitude, this.recDate.getYear(),this.recDate.getMonth(),this.recDate.getDay());
for (int i = 0; i < 15*10 ;i++ ) {
num += "_";
}
return num;
}
@Override
public int compareTo(Record arg0) {
// TODO Auto-generated method stub
return 0;
}
}
package data;
import java.io.Serializable;
import java.util.ArrayList;
/**
* This class implements TaxonNode ADT
* The BioTree class uses these nodes to construct its tree.
* An implicit taxonomic hierarchy is constructed via. the parent and children properties.
*
* Used the following source(s):
* https://stackoverflow.com/questions/2697182/how-to-use-an-array-list
*/
public class TaxonNode implements Serializable {
/**
*
*/
private static final long serialVersionUID = -317741592166253773L;
private final int taxonId;
private final TaxonType taxonType;
private final String name;
private TaxonNode parent;
private ArrayList<TaxonNode> children = new ArrayList<TaxonNode>();
private int childrenCount;
/**
* TaxonNode constructor
*
* @param taxonId
* @param taxonType
* @param name
*/
public TaxonNode(int taxonId, TaxonType taxonType, String name) {
this.taxonId = taxonId;
this.taxonType = taxonType;
this.name = name;
this.parent = null;
this.children = new ArrayList<TaxonNode>();
this.childrenCount = 0;
}
/**
* Gets the TaxonNode's Id
*
* @return taxonId
*/
public int getTaxonId() {
return this.taxonId;
}
/**
* Gets the TaxonNode's Type
*
* @return taxonType
*/
public TaxonType getTaxonType() {
return this.taxonType;
}
/**
* Gets the TaxonNode's Name
*
* @return Name
*/
public String getName() {
return this.name;
}
/**
* Gets the TaxonNode's Parent
*
* @return parent
*/
public TaxonNode getParent() {
return this.parent;
}
/**
* Gets the TaxonNode's Arraylist of Children
*
* @return Arraylist of children
*/
public Iterable<TaxonNode> getChildren() {
return this.children;
}
/**
* Gets the TaxonNode's Children Count
*
* @return Children Count
*/
public int getCount() {
return this.childrenCount;
}
/**
* Sets the TaxonNode's Parent
*
* @param parent
*/
public void setParent(TaxonNode parent) {
this.parent = parent;
}
/**
* Adds a new child to the children list
*
* @param newChild
*/
public void addChild(TaxonNode newChild) {
this.children.add(newChild);
}
/**
* Increments the children count
*/
public void incCount() {
this.childrenCount ++;
}
/**
* Converts the properties of the taxonNode to String format
*
* @return String of TaxonNode properties
*/
public String toString() {
String s = "";
s += String.format("%-20s%s\n", "Scientific Name:", name);
s += String.format("%-20s%s\n", "Taxon Type:", taxonType);
s += String.format("%-20s%d\n", "Taxon ID:", taxonId);
if (parent != null) s += String.format("%-20s%d\n", "Parent:", parent.getTaxonId());
s += String.format("%-20s", "Children:");
for (TaxonNode tx: children) {
s += String.format("%d ", tx.getTaxonId());
}
s += "\n";
s += String.format("%-20s%d\n", "Count:", childrenCount);
return s;
}
}
package data;
/**
* Enumeration of classifications of Taxa.
* @author Christopher W. Schankula
*
*/
//Changed to only capitalizing the first character
public enum TaxonType {
Kingdom, Phylum, Class, Order,
Family, Genus, Species, Subspecies;
public String toString() {
return name();
}
}
package data;
import static org.junit.Assert.*;
import java.io.IOException;
import org.junit.Before;
import org.junit.Test;
public class TestBioTree {
@Before
public static void main(String[] args) throws IOException {
BioTree.init();
BioTree.processRecord(125125);
BioTree.processRecord(125125);
BioTree.processRecord(125125);
BioTree.processRecord(125125);
BioTree.processRecord(125125);
BioTree.processRecord(125125);
BioTree.processRecord(125123);
BioTree.processRecord(125122);
BioTree.processRecord(125392);
BioTree.processRecord(125391);
BioTree.processRecord(600000000);
//System.out.println(nodes.size());
//System.out.println(nodes.get(123207));
//Iterable<Integer> keys = nodes.keys();
//for (Integer i: keys) {
// System.out.println(nodes.get(i));
//System.out.println(String.format("%-26s %s", nodes.get(i).getName(), nodes.get(i).getTaxonType()));
//}
BioTree.printTree();
System.out.println(WormsAPI.nameToID("Hello"));
}
}
package data;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
//Need to add library
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import java.util.ArrayList;
import java.util.Iterator;
public class WormsAPI {
public static void main(String[] args) throws IOException, ParseException {
// small test
//System.out.println(nameToID("Neogobius melanostomus"));
//System.out.println(nameToRecordID("Neogobius melanostomus"));
/*
* TaxonNode[] taxnodes = idToClassification(126916);
*
* for (int i =0; i < taxnodes.length;i++) {
* System.out.println(taxnodes[i].getTaxonId());
* System.out.println(taxnodes[i].getTaxonType());
* System.out.println(taxnodes[i].getName()); }
*/
}
/**
* Search the WORMS database by scientific name to return Aphia (taxon) ID. This
* must be an exact name or it will fail. Use fuzzyNameToID if the name is not
* exact.
*
* @param scientificName
* Scientific name of taxon (family, genus, species, etc)
* @return Aphia (taxon) ID of given scientific name.
* @throws Exception
*/
public static Integer nameToID(String scientificName) throws Exception {
System.out.println("nameToID: " + scientificName);
scientificName = repSpaces(scientificName);
String resp = makeRequest(
String.format("http://marinespecies.org/rest/AphiaIDByName/%s?marine_only=false", scientificName));
if (resp.length() == 0)
throw new Exception("Name not found in WORMS database.");
return Integer.parseInt(resp);
}
/**
* Search the WORMS database by fuzzy scientific name (a slightly misspelled
* name). This has the advantage of being more flexible but it can be less
* accurate and it is slower. If you have the actual scientific name, use
* nameToID() instead.
*
* @param fuzzyName
* Fuzzy scientific name of taxon (family, genus, species, etc)
* @return Aphia (taxon) ID of given scientific name.
* @throws IOException
* @throws ParseException
*/
public static Integer fuzzyNameToID(String fuzzyName) throws IOException, ParseException {
//System.out.println("Fuzzy name: " + fuzzyName);
fuzzyName = repSpaces(fuzzyName);
String resp = makeRequest(String.format(
"http://marinespecies.org/rest/AphiaRecordsByMatchNames?scientificnames%%5B%%5D=%s&marine_only=false",
fuzzyName));
if (resp.length() == 0) return null;
JSONParser parser = new JSONParser();
JSONArray json = (JSONArray) parser.parse(resp);
JSONObject first = (JSONObject)((JSONArray)json.get(0)).get(0);
return (int) (long) first.get("AphiaID");
}
/**
* Search the WORMS database by a scientific name from multiple records
*
* @param Name
* @return Aphia (taxon) ID of given scientific name.
* @throws IOException
* @throws ParseException
*/
public static Integer nameToRecordID(String Name) throws IOException, ParseException {
//System.out.println( Name);
Name = repSpaces(Name);
String resp = makeRequest(String.format(
"http://marinespecies.org/rest/AphiaRecordsByName/%s?like=true&marine_only=false&offset=1",
Name));
if (resp.length() == 0) return null;
JSONParser parser = new JSONParser();
JSONArray json = (JSONArray) parser.parse(resp);
int id = (int) (long) ((JSONObject)json.get(0)).get("AphiaID");
return id;
}
/**
* Search by taxonId (AphiaID) and return bioclassification of that and above.
*
* /AphiaClassificationByAphiaID/{ID}
*
* @throws IOException
* @throws ParseException
*/
public static TaxonNode[] idToClassification(int taxonId) throws IOException, ParseException {
System.out.println("idToClassification " + taxonId);
String resp = makeRequest(
String.format("http://marinespecies.org/rest/AphiaClassificationByAphiaID/%d", taxonId));
if (resp.length() == 0) return null;
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(resp);
// Assume length of 8 based on number of taxontypes
TaxonNode[] taxnodes = new TaxonNode[8];
int arraysize = parseIdCall(taxnodes, json, 0);
TaxonNode[] copiedArray = new TaxonNode[arraysize];
System.arraycopy(taxnodes, 0, copiedArray, 0, arraysize);
return copiedArray;
}
/**
* Perform a GET request to the given URL and return the content of the
* response.
*
* @param url
* The URL to which to make a request.
* @return The content returned by the server (if successful).
* @throws IOException
*/
private static String makeRequest(String url) throws IOException {
// Request method adapted from http://www.baeldung.com/java-http-request
// create new URL instance
URL urll = new URL(url);
// create and set up connection
HttpURLConnection con = (HttpURLConnection) urll.openConnection();
con.setRequestMethod("GET");
int status = con.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
con.disconnect();
return content.toString();
}
/**
* Helper to replace a space with the correct one for URLs.
*
* @param in
* Input string
* @return String with spaces replaced by "%20" for spaces in URLs.
*/
private static String repSpaces(String in) {
return in.replaceAll(" ", "%20");
}
/**
* Parses the json request recursively and also keeping track of the array
* length
*
* @param nodes
* the TaxonNode array
* @param current
* current rank level of JSON file
* @param n
* index of where the TaxonNode is being stored at
* @return arraysize number of elements added within array
*/
private static int parseIdCall(TaxonNode[] nodes, JSONObject current, int n) {
boolean checktype = false;
TaxonNode curNode = null;
int arraysize = n;
// Checks if rank matches the TaxonType Enum
for (TaxonType c : TaxonType.values()) {
if (c.name().equals((String) current.get("rank"))) {
checktype = true;
break;
} else
checktype = false;
}
JSONObject child = (JSONObject) current.get("child");
if (checktype == true) {
curNode = new TaxonNode((int) (long) current.get("AphiaID"), TaxonType.valueOf((String) current.get("rank")),
(String) current.get("scientificname"));
nodes[n] = curNode;
n++;
}
// If child is null, return
if ((JSONObject) current.get("child") == null)
return n;
arraysize = parseIdCall(nodes, child, n);
return arraysize;
}
}
package sandbox;
public class Point implements Comparable<Point> {
import java.io.Serializable;
public class Point implements Comparable<Point>, Serializable {
/**
*
*/
private static final long serialVersionUID = 5361956730616676054L;
private final int x;
private final int y;
......
package search;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.NoSuchElementException;
import sort.KDT;
public class BST<Key extends Comparable<Key>, Value> implements Serializable {
/**
*
*/
public static void main(String[] args) {
//BST<Integer,Integer> bst = new BST<Integer, Integer>();
//bst.put(75, 9);
//bst.writeToFile("bst.ser");
BST<Integer, Integer> bst = new BST<Integer, Integer>("bst.ser");
System.out.println(bst.get(75));
}
private static final long serialVersionUID = 8775155124761510511L;
private Node root;
public BST(String fn) {
BST<Key,Value> bst = null;
try {
FileInputStream fileIn = new FileInputStream(fn);
ObjectInputStream in = new ObjectInputStream(fileIn);
bst = (BST<Key,Value>) in.readObject();
in.close();
fileIn.close();
} catch (IOException i) {
i.printStackTrace();
} catch (ClassNotFoundException c) {
System.out.println("Employee class not found");
c.printStackTrace();
}
this.root = bst.root;
}
public BST() {
root = null;
}
private class Node implements Serializable {
/**
*
*/
private static final long serialVersionUID = -8145778479611668151L;
private Key key;
private Value val;
private Node left, right;
private int N;
public Node(Key key, Value val, int N) {
this.key = key;
this.val = val;
this.N = N;
}
}
public int size() {
return size(root);
}
private int size(Node x) {
if (x == null) return 0;
else return x.N;
}
public Value get(Key key) { if (key == null) return null; return get(root, key); }
private Value get(Node x, Key key) {
if (x == null) return null;
int cmp = key.compareTo(x.key);
if (cmp < 0) return get(x.left, key);
else if (cmp > 0) return get(x.right, key);
else return x.val;
}
public void put(Key key, Value val) {
root = put(root, key, val);
}
private Node put(Node x, Key key, Value val) {
if (x == null) return new Node(key, val, 1);
int cmp = key.compareTo(x.key);
if (cmp < 0) x.left = put(x.left, key, val);
else if (cmp > 0) x.right = put(x.right, key, val);
else x.val = val;
x.N = size(x.left) + size(x.right) + 1;
return x;
}
public boolean isEmpty() {
return root == null;
}
public Key min() {
if (isEmpty()) throw new NoSuchElementException();
Node x = min(root);
return x.key;
}
private Node min(Node x) {
if (x.left == null) return x;
return min(x.left);
}
public Key max() {
if (isEmpty()) throw new NoSuchElementException();
Node x = max(root);
return x.key;
}
private Node max(Node x) {
if (x.right == null) return x;
return max(x.right);
}
public Key floor(Key key) {
Node x = floor(root, key);
if (x == null) throw new NoSuchElementException();
return x.key;
}
private Node floor(Node x, Key key) {
if (x == null) return null;
int cmp = key.compareTo(x.key);
if (cmp == 0) return x;
if (cmp < 0) return floor(x.left, key);
Node t = floor(x.right, key);
if (t != null) return t;
else return x;
}
public Key select(int k) {
if (k < 0 || k >= size()) throw new IllegalArgumentException();
Node x = select(root, k);
return x.key;
}
private Node select(Node x, int k) {
if (x == null) return null;
int t = size(x.left);
if (t > k) return select(x.left, k);
else if (t < k) return select(x.right, k - t -1);
else return x;
}
public int rank(Key key) {
return rank(key, root);
}
private int rank(Key key, Node x) {
if (x == null) return 0;
int cmp = key.compareTo(x.key);
if (cmp < 0) return rank(key, x.left);
else if (cmp > 0) return 1 + size(x.left) + rank(key, x.right);
else return size(x.left);
}
public void deleteMin() {
if (isEmpty()) throw new NoSuchElementException();
root = deleteMin(root);
}
private Node deleteMin(Node x) {
if (x.left == null) return x.right;
x.left = deleteMin(x.left);
x.N = size(x.left) + size(x.right) + 1;
return x;
}
public void delete(Key key) {
root = delete(root, key);
}
private Node delete(Node x, Key key) {
if (x == null) return null;
int cmp = key.compareTo(x.key);
if (cmp < 0) x.left = delete(x.left, key);
else if (cmp > 0) x.right = delete(x.right, key);
else {
if (x.right == null) return x.left;
if (x.left == null) return x.right;
Node t = x;
x = min(t.right);
x.right = deleteMin(t.right);
x.left = t.left;
}
x.N = size(x.left) + size(x.right) + 1;
return x;
}
public Iterable<Key> keys() {
return keys(min(), max());
}
public Iterable<Key> keys(Key lo, Key hi) {
ArrayList<Key> al = new ArrayList<Key>();
keys(root, al, lo, hi);
return al;
}
private void keys(Node x, ArrayList<Key> al, Key lo, Key hi) {
if (x == null) return;
int cmplo = lo.compareTo(x.key);
int cmphi = hi.compareTo(x.key);
if (cmplo < 0) keys(x.left, al, lo, hi);
if (cmplo <= 0 && cmphi >= 0) al.add(x.key);
if (cmphi > 0) keys(x.right, al, lo, hi);
}
public int height() {
return height(root);
}
private int height(Node x) {
if (x == null) return 0;
else return Math.max(height (x.left), height(x.right)) + 1;
}
public void writeToFile(String fn) {
try {
FileOutputStream fileOut =
new FileOutputStream(fn);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(this);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in /tmp/kdtree.ser");
} catch (IOException i) {
i.printStackTrace();
}
}
}
package search;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.simple.parser.ParseException;
import utils.Stopwatch;
import data.BioTree;
import data.DataStore;
import data.Date;
import data.Record;
import data.WormsAPI;
import sort.Bound;
import sort.GeneralRange;
import sort.RangeHelper;
public class BasicSearch {
public static void init() {
System.out.println("Welcome!");
while(true) {
System.out.println("Main Menu");
System.out.println("Available commands:");
System.out.println("\ttree [taxonId / scientific name]");
System.out.println("\trecords (taxonId / scientific name) [-t start end]");
System.out.print("> ");
Pattern pat = Pattern.compile("([a-zA-Z]+)[ ]?([0-9a-zA-Z ]+[0-9a-zA-Z])?[ ]?[-]?([a-zA-Z])?[ ]?([A-Za-z0-9]+)?[ ]?([A-Za-z0-9]+)?[ ]?([A-Za-z0-9]+)?[ ]?([A-Za-z0-9]+)?[ ]?([A-Za-z0-9]+)?[ ]?");
Scanner s = new Scanner(System.in);
String line = s.nextLine();
Matcher matcher = pat.matcher(line);
if (!matcher.find()) continue;
//tree
//tree taxonId
//tree scientific name
//records taxonId
//records scientific name
String command = matcher.group(1);
if (command.equals("records"))
rangeSearch(matcher);
else if (command.equals("tree"))
printTree(matcher);
}
}
private static void rangeSearch(Matcher matcher) {
Integer start = null;
Integer end = null;
GeneralRange<Record> a0 = RangeHelper.date(Bound.ANY);
if (matcher.group(3) != null)
if (matcher.group(3).equals("t")) {
if (matcher.group(4) != null)
start = Integer.parseInt(matcher.group(4));
if (matcher.group(5) != null)
end = Integer.parseInt(matcher.group(5));
Date lower = new Date(start,01,01);
Date upper = new Date(end+1,01,01);
a0 = RangeHelper.date(Bound.LOWHIGH, lower, upper);
}
Integer taxonId = null;
try {
taxonId = Integer.parseInt(matcher.group(2));
} catch (NumberFormatException e) {
if (taxonId == null) {
try {
taxonId = WormsAPI.nameToRecordID(matcher.group(2));
} catch (IOException e1) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e1) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
GeneralRange<Record> a2 = r -> 0;
GeneralRange<Record> a3 = r -> 0;
GeneralRange<Record> a1;
Stopwatch sw = new Stopwatch();
Iterable<Integer> searches = BioTree.getNonEmptyChildren(taxonId);
ArrayList<Record> results = new ArrayList<Record>();
for (Integer txId: searches) {
a1 = RangeHelper.taxonID(Bound.EQUALS, txId);
ArrayList<GeneralRange<Record>> axes = new ArrayList<GeneralRange<Record>>();
axes.add(a0);axes.add(a1);axes.add(a2);axes.add(a3);
results.addAll((Collection<? extends Record>) DataStore.records.rangeSearch(axes));
}
double elapsed = sw.elapsedTime();
System.out.println("Found " + ((ArrayList<Record>) results).size() + " records in " + elapsed + " seconds.");
while(true) {
System.out.println("Available commands: list, histogram, sum, exit");
System.out.print("> ");
Scanner s = new Scanner(System.in);
String command = s.nextLine();
if (command.equals("list"))
printRecords(results);
else if (command.equals("histogram")) {
Histogram.printHistogram(Histogram.histogram(results));
} else if (command.equals("exit"))
return;
else if (command.equals("sum")) {
int sum = Histogram.sum(results);
System.out.println(sum);
}
}
}
private static void printRecords(Iterable<Record> results) {
String format = "|%1$-45s|%2$-15s|%3$-15s|%4$-15s|%5$-15s|%6$-15s\n";
System.out.format(format, "Scientific Name", "IndividualCount", "Latitude", "Longitude","Year","Month","Day");
for (Record r: results) {
System.out.println(r);
}
}
private static void printTree(Matcher matcher) {
Integer taxonId;
String name;
if (matcher.group(2) == null)
BioTree.printTree();
else {
name = matcher.group(2);
try {
taxonId = Integer.parseInt(name);
BioTree.printTree(taxonId);
} catch (Exception e) {
BioTree.printTree(name);
}
}
System.out.println();
}
}
package search;
import com.sun.org.apache.bcel.internal.generic.NEW;
import data.Record;
public class Histogram {
/**
* Generates a BST where each node contains the number of occurrences (value) in
* a year (key)
*
* @param record
* - An iterable of records
* @return - The BST
*/
public static BST<Integer, Integer> histogram(Iterable<Record> record) {
BST<Integer, Integer> tree = new BST<Integer, Integer>();
for (Record rec : record) {
int year = rec.getDate().getYear();
int count = 0;
if (tree.get(year) != null) {
count = tree.get(year);
count += rec.getCount();
tree.put(year, count);
} else
tree.put(year, rec.getCount());
}
return tree;
}
/**
* Prints a histogram based on a BST of records
*
* @param record -An BST of records
*/
public static void printHistogram(BST<Integer,Integer> record) {
int max = 0;
int scale = 100;
Iterable<Integer> results = record.keys();
for (Integer year: results) {
if (max < record.get(year)) max =record.get(year);
}
System.out.println(" |" + (new String(new char[scale]).replace('\0', '-')) + "|");
String format = "%1$-5d|%2$-" + (scale + 1) + "s";
for (Integer year: results) {
String s = "=";
int loopc = (int) ((float)(record.get(year)/ (float) max) * scale);
for (int j=0; j< loopc; j++) {
s+="=";
}
System.out.format(format, year, s);
System.out.println("| " + record.get(year));
}
System.out.format("Scale: one = is %d individuals.\n", max / scale);
}
public static int sum(Iterable<Record> records) {
int sum = 0;
for (Record r: records)
sum += r.getCount();
return sum;
}
public static void main(String[] args) {
}
}
package sort;
public enum Bound {
LOWER, UPPER, LOWHIGH, ANY, EQUALS;
}
package sort;
public interface GeneralCompare<T> {
import java.io.Serializable;
public interface GeneralCompare<T> extends Serializable {
/**
* Compare two Comparable elements based on an arbitrary ordering definition.
* @param a1 The first value to be compared.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment