/**
 *
 *  Internet robot, TmPred, that uses form, located in page
 *  http://www.ch.embnet.org/software/TMPRED_form.html.
 *  It is your responsibility to find and cite appropriate references
 *  from this page.
 *  Generated by Sight 2.0, Ulm university.
 */
package impl.protein_analysers;

import Sight.*;
import Sight.Agents.*;
import Sight.Structures.*;
import Sight.dds.*;
import Sight.tools.xPlain.*;
import java.io.*;
import Sight.Agents.util.*;

public class TmPred extends Sight.Agents.Prototypes.preformattedResponseAgent implements Serializable {
 /* Data structure to pass query to this robot. */
  public class Request extends Sight.Request implements java.io.Serializable {
   public String Min="17"; // select
   public String Max="33"; // select
   public String Sequence; // required.
       /** Submit request, compute key by SHA digesting algorithm. */
       public Sight.Agents.Request submit() { return Sight.Agents.Request.submit(Public, this, getKey()); };
       /** Submit request with the known key. */
       public Sight.Agents.Request submit(String key)
        { return Sight.Agents.Request.submit(Public, this, key); };
       };

   /** Get the AMap data structure, required by post method. */
  protected AMap getFormParameters(Object req) {
    Request request = (Request) req;
    AMap map = new AMap();
    map.add("connect:url", "http://www.ch.embnet.org/cgi-bin/TMPRED_form_parser"); // connect
    map.add("connect:action", "POST"); // connect
    map.add("connect:master", "http://www.ch.embnet.org/software/TMPRED_form.html"); // connect
    map.add("outmode", "html"); // select
    map.add("min", request.Min); // select
    map.add("max", request.Max); // select
    map.add("comm", ""); // text
    map.add("format", "plain_text"); // select
    map.add("seq", request.Sequence); // textarea
    return map; };
  /** Get default request for this robot. */
  public static Request getDefaultRequest() {
     return Public.getRequest(); }
  private Request getRequest() { return new Request(); }; // must be non static

  /** Form URL to connect. http://www.ch.embnet.org/cgi-bin/TMPRED_form_parser.*/
  public String getFormURL() {
     return "http://www.ch.embnet.org/cgi-bin/TMPRED_form_parser"; }

  /** URL to refer. http://www.ch.embnet.org/software/TMPRED_form.html.*/
  public String getMasterURL() {
     return "http://www.ch.embnet.org/software/TMPRED_form.html"; }

    /** Type definitions for Request. */
   public dStructure getRequestDds() {
    return new Sight.dds.Records(false,
     new dField[] {
        new dField("Min","CharSequence","Minimal transmembrane segment length","17",new String[] {"14","16","17","18","19","20","21","22","23","24","25","26"},new String[] {"14","16","17 default","18","19","20","21","22","23","24","25","26"}),
        new dField("Max","CharSequence","Maximal transmembrane segment length","33",new String[] {"23","25","27","29","31","32","33","34","35","37","39","41"},new String[] {"23","25","27","29","31","32","33 default","34","35","37","39","41"}),
        new dField("Sequence","CharSequence","protein sequence","")                 }
    );
   }
 /* Record type, used in results for this robot. */
 /* The result of this robot is an array of records. */
  public static class Record extends Sight.Record implements Serializable {
        /** Region */
        public s_feature Region;
        /** Score */
        public CharSequence Score;
        /** Orientation */
        public CharSequence Orientation;
        /** Helix number. */
        public CharSequence Helix;
        /** Helix from. */
        public CharSequence From;
        /** Helix to. */
        public CharSequence To;
      };
      /** The result of this robot. */
      public static class Result extends Sight.Result implements Serializable {
         public Record[] a; // array of results.
         /** Create report for one record. */
         public CharSequence getReportForRecord(Object ro, int level)
          { Record r = (Record) ro;
           switch (level) {
              case 0:  return r.Region+" ";
              default: return r.Region+" "+" Score: "+r.Score+" "+" Orientation: "+r.Orientation+" ";
            }
          };
         /** Create report for the whole result */
        public String getReport(int verbosityLevel)
        { if (verbosityLevel==0) return a.length+" helixes";
          return getReport(getRecordArray(), verbosityLevel); };
      }

    /** Type definitions for Result. */
   public dStructure getResultDds() {
    return new Sight.dds.Records(true,
     new dField[] {
        new dField("Helix","CharSequence","Helix",""),
        new dField("Region","s_feature","Region",""),
        new dField("Score","CharSequence","Score",""),
        new dField("Orientation","CharSequence","Orientation",""),
        new dField("From","CharSequence","From",""),
        new dField("To","CharSequence","To",""),
        }
    );
   }

  public Sight.Record createRecord(String[] row) throws Exception
    { return fillRecord(row); };
  public Class getRecordClass() { return Record.class; };
   Record fillRecord(String[] fields) throws Exception {
        Record r = new Record();
        r.Region = new s_feature(fields[1]+".."+fields[2]);
        r.Score=fields[4];
        r.Orientation=fields[5];
        r.From = fields[1];
        r.To   = fields[2];
        return r;
   };

   /** Get result from the given html content. */
  public Sight.Result resultFromString(String html) throws Exception {
     Result r = new Result();
     r.a = (Record[]) createResultArray(html);
     for (int i = 0; i < r.a.length; i++) {
       r.a[i].Helix = ""+(i+1);
     }

     return r;
   };
  public TmPred() { signatures = new multiSignatures();
   signatures.far_start=null;
   signatures.top="# from   to length score orientation";
   signatures.zeroCases = new String[] {"probably no transmembrane protein","no possible model found"};
   signatures.bottom="\n------>";
   signatures.Columns = 6;
   setDescription("Prediction of transmembrane regions and protein Orientation (EMBnet-CH)");
   };
  /** Public instance of agent for user. */
  public static TmPred Public = new TmPred();
  public static Agent getAvailableAgent() { return Public; };

  /** main(..) method provided to check robot, launching it
   * as standalone program. */
  public static void main(String[] args) {
  Sight.Agents.util.Pind.showConsoles();
      try {
        Request request = getDefaultRequest();
        // (now you can modify 'request' fields if required)
        request.Sequence = "MPSSGRALLDSPLDSGSLTSLDSSVFCSEGEGEPLALGDCFTVNVGGSRFVLSQQALSCFPHTRLGKLAVVVASYRRPGALAAVPSPLELCDDANPVDNEYFFDRSSQAFRYVLHYYRTGRLHVMEQLCALSFLQEIQYWGIDELSIDSCCRDRYFRRKELSETLDFKKDTEDQESQHESEQDFSQGPCPTVRQKLWNILEKPGSSTAARIFGVISIIFVVVSIINMALMSAELSWLDLQLLEILEYVCISWFTGEFVLRFLCVRDRCRFLRKVPNIIDLLAILPFYITLLVESLSGSQTTQELENVGRIVQVLRLLRALRMLKLGRHSTGLRSLGMTITQCYEEVGLLLLFLSVGISIFSTVEYFAEQSIPDTTFTSVPCAWWWATTSMTTVGYGDIRPDTTTGKIVAFMCILSGILVLALPIAIINDRFSACYFTLKLKEAAVRQREALKKLTKNIATDSYISVNLRDVYARSIMEMLRLKGRERASTRSSGGDDFWF";
        Sight.Agents.Request submission = request.submit();
        // processing now started in separate thread:
        Result response = (Result) submission.getResult();
        // print report, verbosity level 2:
        System.out.println(response.getReport(2));
      } catch (Exception exc)
         { if (exc!=null) System.out.println(exc.getMessage());
           exc.printStackTrace();
         };
   }
  }