Skip to content
Snippets Groups Projects
Commit ca9c6a5d authored by Ray Liu's avatar Ray Liu
Browse files

Merge branches

parent 58622f20
No related branches found
No related tags found
No related merge requests found
package biotree;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
......
package record;
package biotree;
public class Record {
private final int eventId;
......
package biotree;
public class Species {
private final String species;
private final String bClass;
private final String order;
private final String family;
private final String genus;
public Species(String bClass, String order, String family, String genus, String species) {
this.species = species;
this.bClass = bClass;
this.order = order;
this.family = family;
this.genus = genus;
}
public String getSpecies() {
return this.species;
}
public String getBClass() {
return this.bClass;
}
public String getOrder() {
return this.order;
}
public String getFamily() {
return this.family;
}
public String getGenus() {
return this.genus;
}
public String toString() {
return bClass + " " + order + " " + family + " " + genus + " " + species;
}
}
package biotree;
public class TaxonNode {
}
package record;
/**
* Enumeration of classifications of Taxa.
* @author Christopher W. Schankula
*
*/
public enum TaxonType {
KINGDOM, PHYLUM, CLASS, ORDER,
FAMILY, GENUS, SPECIES, SUBSPECIES;
}
package biotree;
/**
* Enumeration of classifications of Taxa.
* @author Christopher W. Schankula
*
*/
public enum TaxonType {
KINGDOM, PHYLUM, CLASS, ORDER,
FAMILY, GENUS, SPECIES, SUBSPECIES;
}
package biotree;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import record.TaxonType;
import tuple.Tuple;
public class WormsAPI {
public static void main(String[] args) throws IOException {
//small test
System.out.println(nameToID("Neogobius melanostomus"));
}
/**
* 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 IOException
*/
public static int nameToID(String scientificName) throws IOException{
scientificName = repSpaces(scientificName);
String resp = makeRequest(String.format("http://marinespecies.org/rest/AphiaIDByName/%s?marine_only=false", scientificName));
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
*/
public static int fuzzyNameToID(String fuzzyName) throws IOException{
fuzzyName = repSpaces(fuzzyName);
String resp = makeRequest(String.format("http://marinespecies.org/rest/AphiaRecordsByMatchNames?scientificnames%5B%5D=%s&marine_only=true", fuzzyName));
//TODO: finish this function.
return 123123;
}
/**
* Search by taxonId (AphiaID) and return bioclassification of that and above.
* http://marinespecies.org/rest/
* /AphiaClassificationByAphiaID/{ID}
*/
public static Tuple<TaxonType,Integer>[] idToClassification(int taxonId) {
return null;
}
/**
* 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");
}
}
package biotree;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import tuple.Tuple;
public class WormsAPI {
public static void main(String[] args) throws IOException {
//small test
System.out.println(nameToID("Neogobius melanostomus"));
}
/**
* 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 IOException
*/
public static int nameToID(String scientificName) throws IOException{
scientificName = repSpaces(scientificName);
String resp = makeRequest(String.format("http://marinespecies.org/rest/AphiaIDByName/%s?marine_only=false", scientificName));
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
*/
public static int fuzzyNameToID(String fuzzyName) throws IOException{
fuzzyName = repSpaces(fuzzyName);
String resp = makeRequest(String.format("http://marinespecies.org/rest/AphiaRecordsByMatchNames?scientificnames%5B%5D=%s&marine_only=true", fuzzyName));
//TODO: finish this function.
return 123123;
}
/**
* Search by taxonId (AphiaID) and return bioclassification of that and above.
* http://marinespecies.org/rest/
* /AphiaClassificationByAphiaID/{ID}
*/
public static Tuple<TaxonType,Integer>[] idToClassification(int taxonId) {
return null;
}
/**
* 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");
}
}
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