annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/opt/bbmap-39.01-1/current/tax/TaxClient.java @ 68:5028fdace37b

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 16:23:26 -0400
parents
children
rev   line source
jpayne@68 1 package tax;
jpayne@68 2
jpayne@68 3 import java.io.PrintStream;
jpayne@68 4 import java.util.ArrayList;
jpayne@68 5
jpayne@68 6 import server.PercentEncoding;
jpayne@68 7 import server.ServerTools;
jpayne@68 8 import shared.Parse;
jpayne@68 9 import shared.PreParser;
jpayne@68 10 import shared.Shared;
jpayne@68 11 import shared.Timer;
jpayne@68 12 import shared.Tools;
jpayne@68 13 import structures.ByteBuilder;
jpayne@68 14 import structures.StringNum;
jpayne@68 15
jpayne@68 16 public class TaxClient {
jpayne@68 17
jpayne@68 18 public static void main(String[] args){
jpayne@68 19 Timer t=new Timer();
jpayne@68 20 {//Preparse block for help, config files, and outstream
jpayne@68 21 PreParser pp=new PreParser(args, null, false);
jpayne@68 22 args=pp.args;
jpayne@68 23 outstream=pp.outstream;
jpayne@68 24 }
jpayne@68 25
jpayne@68 26 ArrayList<Integer> gi=new ArrayList<Integer>();
jpayne@68 27 ArrayList<String> acc=new ArrayList<String>();
jpayne@68 28 ArrayList<String> name=new ArrayList<String>();
jpayne@68 29 ArrayList<String> header=new ArrayList<String>();
jpayne@68 30
jpayne@68 31 boolean slow=true;
jpayne@68 32
jpayne@68 33 //Parse each argument
jpayne@68 34 for(int i=0; i<args.length; i++){
jpayne@68 35 String arg=args[i];
jpayne@68 36
jpayne@68 37 //Break arguments into their constituent parts, in the form of "a=b"
jpayne@68 38 String[] split=arg.split("=");
jpayne@68 39 String a=split[0].toLowerCase();
jpayne@68 40 String b=split.length>1 ? split[1] : null;
jpayne@68 41
jpayne@68 42 if(a.equals("verbose")){
jpayne@68 43 verbose=Parse.parseBoolean(b);
jpayne@68 44 }else if(a.equals("name") || a.equals("names")){
jpayne@68 45 if(b!=null){
jpayne@68 46 for(String s : b.split(",")){
jpayne@68 47 name.add(s);
jpayne@68 48 }
jpayne@68 49 }
jpayne@68 50 }else if(a.equals("gi")){
jpayne@68 51 if(b!=null){
jpayne@68 52 for(String s : b.split(",")){
jpayne@68 53 gi.add(Integer.parseInt(s));
jpayne@68 54 }
jpayne@68 55 }
jpayne@68 56 }else if(a.equals("accession")){
jpayne@68 57 if(b!=null){
jpayne@68 58 for(String s : b.split(",")){
jpayne@68 59 acc.add(s);
jpayne@68 60 }
jpayne@68 61 }
jpayne@68 62 }else if(a.equals("header")){
jpayne@68 63 if(b!=null){
jpayne@68 64 for(String s : b.split(",")){
jpayne@68 65 header.add(s);
jpayne@68 66 }
jpayne@68 67 }
jpayne@68 68 }else if(a.equals("slow")){
jpayne@68 69 slow=Parse.parseBoolean(b);
jpayne@68 70 }else if(a.equals("fast")){
jpayne@68 71 slow=!Parse.parseBoolean(b);
jpayne@68 72 }else if(a.equals("parse_flag_goes_here")){
jpayne@68 73 long fake_variable=Parse.parseKMG(b);
jpayne@68 74 //Set a variable here
jpayne@68 75 }else{
jpayne@68 76 outstream.println("Unknown parameter "+args[i]);
jpayne@68 77 assert(false) : "Unknown parameter "+args[i];
jpayne@68 78 }
jpayne@68 79 }
jpayne@68 80
jpayne@68 81 if(slow){
jpayne@68 82 for(String s : name){
jpayne@68 83 outstream.println(s+"\t"+nameToTaxid(s));
jpayne@68 84 }
jpayne@68 85 for(Integer s : gi){
jpayne@68 86 outstream.println(s+"\t"+giToTaxid(s));
jpayne@68 87 }
jpayne@68 88 for(String s : acc){
jpayne@68 89 outstream.println(s+"\t"+accessionToTaxid(s));
jpayne@68 90 }
jpayne@68 91 }else{
jpayne@68 92 if(name.size()>0){
jpayne@68 93 int[] ids=nameToTaxidArray(name);
jpayne@68 94 for(int i=0; i<ids.length; i++){
jpayne@68 95 outstream.println(name.get(i)+"\t"+ids[i]);
jpayne@68 96 }
jpayne@68 97 }
jpayne@68 98 if(gi.size()>0){
jpayne@68 99 int[] ids=giToTaxidArray(gi);
jpayne@68 100 for(int i=0; i<ids.length; i++){
jpayne@68 101 outstream.println(gi.get(i)+"\t"+ids[i]);
jpayne@68 102 }
jpayne@68 103 }
jpayne@68 104 if(acc.size()>0){
jpayne@68 105 int[] ids=accessionToTaxidArray(acc);
jpayne@68 106 for(int i=0; i<ids.length; i++){
jpayne@68 107 outstream.println(acc.get(i)+"\t"+ids[i]);
jpayne@68 108 }
jpayne@68 109 }
jpayne@68 110 }
jpayne@68 111
jpayne@68 112 t.stopAndPrint();
jpayne@68 113 }
jpayne@68 114
jpayne@68 115 public static int accessionToTaxid(String accession){
jpayne@68 116 String s=sendAndReceive("pt/accession/",accession);
jpayne@68 117 if(s==null || s.length()<1 || !Tools.isDigitOrSign(s.charAt(0))){return -1;}
jpayne@68 118 return Integer.parseInt(s);
jpayne@68 119 }
jpayne@68 120
jpayne@68 121 public static int accessionToTaxidSpecificServer(String path, String accession){
jpayne@68 122 String s=sendAndReceiveSpecificServer(path,"pt/accession/",accession);
jpayne@68 123 if(s==null || s.length()<1 || !Tools.isDigitOrSign(s.charAt(0))){return -1;}
jpayne@68 124 return Integer.parseInt(s);
jpayne@68 125 }
jpayne@68 126
jpayne@68 127 public static int giToTaxid(int gi){
jpayne@68 128 String s=sendAndReceive("pt/gi/",Integer.toString(gi));
jpayne@68 129 if(s==null || s.length()<1 || !Tools.isDigitOrSign(s.charAt(0))){return -1;}
jpayne@68 130 return Integer.parseInt(s);
jpayne@68 131 }
jpayne@68 132
jpayne@68 133 public static int nameToTaxid(String name){
jpayne@68 134 String s=sendAndReceive("pt/name/",name.replace(' ', '_'));
jpayne@68 135 if(s==null || s.length()<1 || !Tools.isDigitOrSign(s.charAt(0))){return -1;}
jpayne@68 136 return Integer.parseInt(s);
jpayne@68 137 }
jpayne@68 138
jpayne@68 139 public static int headerToTaxid(String header){
jpayne@68 140 String s=sendAndReceive("pt/name/",header);
jpayne@68 141 if(s==null || s.length()<1 || !Tools.isDigitOrSign(s.charAt(0))){return -1;}
jpayne@68 142 return Integer.parseInt(s);
jpayne@68 143 }
jpayne@68 144
jpayne@68 145
jpayne@68 146 public static int[] accessionToTaxidArray(String accession){
jpayne@68 147 String s=sendAndReceive("pt/accession/",accession);
jpayne@68 148 // System.err.println("Sent "+accession+"\nReceived "+s);
jpayne@68 149 return splitOutput(s);
jpayne@68 150 }
jpayne@68 151
jpayne@68 152 public static int[] giToTaxidArray(String gi){
jpayne@68 153 String s=sendAndReceive("pt/gi/",gi);
jpayne@68 154 return splitOutput(s);
jpayne@68 155 }
jpayne@68 156
jpayne@68 157 public static int[] nameToTaxidArray(String name){
jpayne@68 158 String s=sendAndReceive("pt/name/",name.replace(' ', '_'));
jpayne@68 159 return splitOutput(s);
jpayne@68 160 }
jpayne@68 161
jpayne@68 162 public static int[] headerToTaxidArray(String header){
jpayne@68 163 String s=sendAndReceive("pt/header/",header);
jpayne@68 164 return splitOutput(s);
jpayne@68 165 }
jpayne@68 166
jpayne@68 167
jpayne@68 168 public static int[] accessionToTaxidArray(ArrayList<String> accession){
jpayne@68 169 String s=sendAndReceive("pt/accession/",fuse(accession));
jpayne@68 170 return splitOutput(s);
jpayne@68 171 }
jpayne@68 172
jpayne@68 173 public static int[] giToTaxidArray(ArrayList<Integer> gi){
jpayne@68 174 String s=sendAndReceive("pt/gi/",fuse(gi));
jpayne@68 175 return splitOutput(s);
jpayne@68 176 }
jpayne@68 177
jpayne@68 178 public static int[] nameToTaxidArray(ArrayList<String> name){
jpayne@68 179 String s=sendAndReceive("pt/name/",fuse(name).replace(' ', '_'));
jpayne@68 180 return splitOutput(s);
jpayne@68 181 }
jpayne@68 182
jpayne@68 183 public static int[] headerToTaxidArray(ArrayList<String> header){
jpayne@68 184 String s=sendAndReceive("pt/header/",fuse(header));
jpayne@68 185 return splitOutput(s);
jpayne@68 186 }
jpayne@68 187
jpayne@68 188 private static final int[] splitOutput(String s){
jpayne@68 189 if(s==null || s.length()<1 || !Tools.isDigitOrSign(s.charAt(0))){return null;}
jpayne@68 190 String[] split=s.split(",");
jpayne@68 191 int[] ret=new int[split.length];
jpayne@68 192 for(int i=0; i<split.length; i++){
jpayne@68 193 ret[i]=Integer.parseInt(split[i]);
jpayne@68 194 }
jpayne@68 195 return ret;
jpayne@68 196 }
jpayne@68 197
jpayne@68 198 private static final String sendAndReceive(String prefix, String message){
jpayne@68 199 String path=Shared.taxServer();
jpayne@68 200 return sendAndReceiveSpecificServer(path, prefix, message);
jpayne@68 201 }
jpayne@68 202
jpayne@68 203 private static final String sendAndReceiveSpecificServer(String path, String prefix, String message){
jpayne@68 204 String response=null;
jpayne@68 205 for(int i=0, millis=200; i<12 && (response==null || response.length()==0); i++) {
jpayne@68 206 if(i>0){
jpayne@68 207 System.err.println("Null response, retrying after "+millis+" ms.");
jpayne@68 208 try {
jpayne@68 209 Thread.sleep(millis);
jpayne@68 210 } catch (InterruptedException e) {
jpayne@68 211 e.printStackTrace();
jpayne@68 212 }
jpayne@68 213 millis*=2;
jpayne@68 214 }
jpayne@68 215 response=sendAndReceiveOnceSpecificServer(path, prefix, message);
jpayne@68 216 }
jpayne@68 217 return response;
jpayne@68 218 }
jpayne@68 219
jpayne@68 220 private static String sendAndReceiveOnce(String prefix, String message){
jpayne@68 221 String path=Shared.taxServer();
jpayne@68 222 return sendAndReceiveOnceSpecificServer(path, prefix, message);
jpayne@68 223 }
jpayne@68 224
jpayne@68 225 private static String sendAndReceiveOnceSpecificServer(String path, String prefix, String message){
jpayne@68 226 final String response;
jpayne@68 227 if(message.length()<2000){//NERSC Apache limit is around 8kb
jpayne@68 228 message=prefix+PercentEncoding.symbolToCode(message);
jpayne@68 229 ByteBuilder bb=ServerTools.readPage(path+message, false);
jpayne@68 230 response=(bb==null ? null : bb.toString());
jpayne@68 231 // System.err.println("S&R1 send:\n"+path+message);
jpayne@68 232 // System.err.println("S&R1 receive:\n"+response);
jpayne@68 233 }else{
jpayne@68 234 StringNum sn=ServerTools.sendAndReceive((prefix+message).getBytes(), path+"$POST");
jpayne@68 235 response=sn.s;
jpayne@68 236 // System.err.println("S&R2: "+message+"\n"+path+"$POST");
jpayne@68 237 }
jpayne@68 238 return response;
jpayne@68 239 }
jpayne@68 240
jpayne@68 241 private static String fuse(ArrayList<?> list){
jpayne@68 242 if(list==null || list.size()<0){return null;}
jpayne@68 243 StringBuilder sb=new StringBuilder();
jpayne@68 244 for(Object s : list){
jpayne@68 245 sb.append(s).append(',');
jpayne@68 246 }
jpayne@68 247 sb.setLength(sb.length()-1);
jpayne@68 248 return sb.toString();
jpayne@68 249 }
jpayne@68 250
jpayne@68 251 public static PrintStream outstream=System.err;
jpayne@68 252 public static boolean verbose=false;
jpayne@68 253
jpayne@68 254 }