/**
 *  Reads sequence and fasta header from NCBI database using given accession number
 *  Internet robot, fastaReaderNCBI, that uses form, located in page
 *  http://www.ncbi.nlm.nih.gov/entrez/.
 *  It is your responsibility to find and cite appropriate references
 *  from this page.
 *  Generated 2002.5.7  by Sight 2.0, Ulm university.
 */
package impl.sequence_readers;

import Sight.Agents.*;
import Sight.Agents.Signals.*;
import Sight.Structures.*;
import Sight.dds.*;
import java.io.*;
import Sight.Agents.util.*;

public class fastaReaderNCBI extends Sight.Agents.Prototypes.sightGeneratedAgent implements Serializable {
 /* Data structure to pass query to this robot. */
  public class Request extends Sight.Request implements java.io.Serializable {
   /** default=XP_134428 */
   public String Id="XP_134428"; // Parameter
       /** 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("url:identifier", request.Id); // Parameter
    map.add("connect:master", "http://www.ncbi.nlm.nih.gov/entrez/"); // connect
    map.add("connect:action", "get"); // connect
    map.add("url:prefix", "http://www.ncbi.nlm.nih.gov/entrez/viewer.fcgi?txt=on&view=fasta&val="); // connect
    map.add("url:suffix", ""); // connect
    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. .*/
  public String getFormURL() {
     return ""; }

  /** URL to refer. http://www.ncbi.nlm.nih.gov/entrez/.*/
  public String getMasterURL() {
     return "http://www.ncbi.nlm.nih.gov/entrez/"; }

    /** Type definitions for Request. */
   public dStructure getRequestDds() {
    return new Sight.dds.Records(false,
     new dField[] {
        new dField("Id","CharSequence",null,"XP_134428")                 }
    );
   }
 /* 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 {
        public CharSequence Header;
        public CharSequence Sequence;
      };
      /** 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.Header+" ";
              case 1:  return ""+r.Header+" ";
              default: return r.Header+"<br><textarea rows=\"3\" cols=\"80\">"+
              r.Sequence+"</textarea>";
            }
          };
      }

    /** Type definitions for Result. */
   public dStructure getResultDds() {
    return new Sight.dds.Records(true,
     new dField[] {
        new dField("Header","CharSequence",null,""),
        new dField("Sequence","CharSequence",null,"")                 }
    );
   }


   /** Get result from the given html content. Done manually. */
  public Sight.Result resultFromString(String html) throws Exception {
    try {
     Result r = new Result();
     int a = html.indexOf(">");
     int b = html.indexOf("\n");
     if (html.indexOf(">",b)>=0)
      throw new FatalAgentException("Multiple entries not supported.");
     Record R = new Record();
     R.Header = html.substring(a,b).trim();
     R.Sequence = getPlainSequence(html.substring(b+1));
     r.a = new Record[1];
     r.a[0] = R;
     return r;
     } catch (ArrayIndexOutOfBoundsException exc)
        { throw new FatalAgentException("Not a FASTA format."); }
   };

  public Class getRecordClass() { return Record.class; };

  public fastaReaderNCBI() {
   setDescription("Reads sequence and fasta header from NCBI database using given accession number");
   };

  /** Public instance of agent for user. */
  public static fastaReaderNCBI Public = new fastaReaderNCBI();
  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 modify and assign request fields in accordance with task:
        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();
         };
   }
;
  }