jpayne@68: package tax; jpayne@68: jpayne@68: import java.io.PrintStream; jpayne@68: import java.util.ArrayList; jpayne@68: jpayne@68: import server.PercentEncoding; jpayne@68: import server.ServerTools; jpayne@68: import shared.Parse; jpayne@68: import shared.PreParser; jpayne@68: import shared.Shared; jpayne@68: import shared.Timer; jpayne@68: import shared.Tools; jpayne@68: import structures.ByteBuilder; jpayne@68: import structures.StringNum; jpayne@68: jpayne@68: public class TaxClient { jpayne@68: jpayne@68: public static void main(String[] args){ jpayne@68: Timer t=new Timer(); jpayne@68: {//Preparse block for help, config files, and outstream jpayne@68: PreParser pp=new PreParser(args, null, false); jpayne@68: args=pp.args; jpayne@68: outstream=pp.outstream; jpayne@68: } jpayne@68: jpayne@68: ArrayList gi=new ArrayList(); jpayne@68: ArrayList acc=new ArrayList(); jpayne@68: ArrayList name=new ArrayList(); jpayne@68: ArrayList header=new ArrayList(); jpayne@68: jpayne@68: boolean slow=true; jpayne@68: jpayne@68: //Parse each argument jpayne@68: for(int i=0; i1 ? split[1] : null; jpayne@68: jpayne@68: if(a.equals("verbose")){ jpayne@68: verbose=Parse.parseBoolean(b); jpayne@68: }else if(a.equals("name") || a.equals("names")){ jpayne@68: if(b!=null){ jpayne@68: for(String s : b.split(",")){ jpayne@68: name.add(s); jpayne@68: } jpayne@68: } jpayne@68: }else if(a.equals("gi")){ jpayne@68: if(b!=null){ jpayne@68: for(String s : b.split(",")){ jpayne@68: gi.add(Integer.parseInt(s)); jpayne@68: } jpayne@68: } jpayne@68: }else if(a.equals("accession")){ jpayne@68: if(b!=null){ jpayne@68: for(String s : b.split(",")){ jpayne@68: acc.add(s); jpayne@68: } jpayne@68: } jpayne@68: }else if(a.equals("header")){ jpayne@68: if(b!=null){ jpayne@68: for(String s : b.split(",")){ jpayne@68: header.add(s); jpayne@68: } jpayne@68: } jpayne@68: }else if(a.equals("slow")){ jpayne@68: slow=Parse.parseBoolean(b); jpayne@68: }else if(a.equals("fast")){ jpayne@68: slow=!Parse.parseBoolean(b); jpayne@68: }else if(a.equals("parse_flag_goes_here")){ jpayne@68: long fake_variable=Parse.parseKMG(b); jpayne@68: //Set a variable here jpayne@68: }else{ jpayne@68: outstream.println("Unknown parameter "+args[i]); jpayne@68: assert(false) : "Unknown parameter "+args[i]; jpayne@68: } jpayne@68: } jpayne@68: jpayne@68: if(slow){ jpayne@68: for(String s : name){ jpayne@68: outstream.println(s+"\t"+nameToTaxid(s)); jpayne@68: } jpayne@68: for(Integer s : gi){ jpayne@68: outstream.println(s+"\t"+giToTaxid(s)); jpayne@68: } jpayne@68: for(String s : acc){ jpayne@68: outstream.println(s+"\t"+accessionToTaxid(s)); jpayne@68: } jpayne@68: }else{ jpayne@68: if(name.size()>0){ jpayne@68: int[] ids=nameToTaxidArray(name); jpayne@68: for(int i=0; i0){ jpayne@68: int[] ids=giToTaxidArray(gi); jpayne@68: for(int i=0; i0){ jpayne@68: int[] ids=accessionToTaxidArray(acc); jpayne@68: for(int i=0; i accession){ jpayne@68: String s=sendAndReceive("pt/accession/",fuse(accession)); jpayne@68: return splitOutput(s); jpayne@68: } jpayne@68: jpayne@68: public static int[] giToTaxidArray(ArrayList gi){ jpayne@68: String s=sendAndReceive("pt/gi/",fuse(gi)); jpayne@68: return splitOutput(s); jpayne@68: } jpayne@68: jpayne@68: public static int[] nameToTaxidArray(ArrayList name){ jpayne@68: String s=sendAndReceive("pt/name/",fuse(name).replace(' ', '_')); jpayne@68: return splitOutput(s); jpayne@68: } jpayne@68: jpayne@68: public static int[] headerToTaxidArray(ArrayList header){ jpayne@68: String s=sendAndReceive("pt/header/",fuse(header)); jpayne@68: return splitOutput(s); jpayne@68: } jpayne@68: jpayne@68: private static final int[] splitOutput(String s){ jpayne@68: if(s==null || s.length()<1 || !Tools.isDigitOrSign(s.charAt(0))){return null;} jpayne@68: String[] split=s.split(","); jpayne@68: int[] ret=new int[split.length]; jpayne@68: for(int i=0; i0){ jpayne@68: System.err.println("Null response, retrying after "+millis+" ms."); jpayne@68: try { jpayne@68: Thread.sleep(millis); jpayne@68: } catch (InterruptedException e) { jpayne@68: e.printStackTrace(); jpayne@68: } jpayne@68: millis*=2; jpayne@68: } jpayne@68: response=sendAndReceiveOnceSpecificServer(path, prefix, message); jpayne@68: } jpayne@68: return response; jpayne@68: } jpayne@68: jpayne@68: private static String sendAndReceiveOnce(String prefix, String message){ jpayne@68: String path=Shared.taxServer(); jpayne@68: return sendAndReceiveOnceSpecificServer(path, prefix, message); jpayne@68: } jpayne@68: jpayne@68: private static String sendAndReceiveOnceSpecificServer(String path, String prefix, String message){ jpayne@68: final String response; jpayne@68: if(message.length()<2000){//NERSC Apache limit is around 8kb jpayne@68: message=prefix+PercentEncoding.symbolToCode(message); jpayne@68: ByteBuilder bb=ServerTools.readPage(path+message, false); jpayne@68: response=(bb==null ? null : bb.toString()); jpayne@68: // System.err.println("S&R1 send:\n"+path+message); jpayne@68: // System.err.println("S&R1 receive:\n"+response); jpayne@68: }else{ jpayne@68: StringNum sn=ServerTools.sendAndReceive((prefix+message).getBytes(), path+"$POST"); jpayne@68: response=sn.s; jpayne@68: // System.err.println("S&R2: "+message+"\n"+path+"$POST"); jpayne@68: } jpayne@68: return response; jpayne@68: } jpayne@68: jpayne@68: private static String fuse(ArrayList list){ jpayne@68: if(list==null || list.size()<0){return null;} jpayne@68: StringBuilder sb=new StringBuilder(); jpayne@68: for(Object s : list){ jpayne@68: sb.append(s).append(','); jpayne@68: } jpayne@68: sb.setLength(sb.length()-1); jpayne@68: return sb.toString(); jpayne@68: } jpayne@68: jpayne@68: public static PrintStream outstream=System.err; jpayne@68: public static boolean verbose=false; jpayne@68: jpayne@68: }