/**
 *
 *  Internet robot, EST_reader, 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.8.2  by Sight 2.0, Ulm university.
 *
 *  This robot receives response in XML, transforms using XSLT,
 *  inserted at the end of this text and then gets the result
 *  from transformed document.
 */
package impl.rna_analysers;

import Sight.Agents.*;
import Sight.Agents.Redirectors.*;
import Sight.Structures.*;
import Sight.dds.*;
import java.io.*;
import org.w3c.dom.*;

public class EST_reader extends Sight.Agents.Prototypes.xml_based_agent implements Serializable {
 /* Data structure to pass query to this robot. */
  public class Request extends Sight.Request implements java.io.Serializable {
   /** default=22519907 */
   public String Id="22519907"; // 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();

    String Gi = request.Id.toLowerCase();
    if (Gi.indexOf("|")>=0)
    // if multiple provided, take gi:
     {
       String left = "gi|";
       int a = Gi.indexOf(left);
       if (a<0) { Sight.io.Output.e("EST_reader: "+"Invalid gi: "+Gi); return null; }
       a = a+left.length();
       int b = Gi.indexOf("|",a);
       if (b<0) { Sight.io.Output.e("EST_reader: "+"Invalid gi: "+Gi); return null; }
       Gi = Gi.substring(a,b);
     };

    map.add("url:identifier", Gi); // 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?view=xml&txt=on&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

    /** Type definitions for Request. */
   public dStructure getRequestDds() {
    return new Sight.dds.Records(false,
     new dField[] {
        new dField("Id","CharSequence",null,"22519907")                 }
    );
   }
 /* 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 Id;
        public CharSequence Header;
        public CharSequence Tissue;
        public CharSequence Sequence;
        public CharSequence Cell;
      };
      /** 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;
           StringBuffer b = new StringBuffer();

           if (r.Id!=null)
            { b.append(r.Id);
            };
           if (r.Tissue!=null)
            {
              b.append(' ');
              b.append(r.Tissue);
            };
           if (r.Cell!=null)
            {
              b.append(' ');
              b.append(r.Cell);
            };

            if (level>=1 && r.Header!=null)
             { b.append(' ');
               b.append(r.Header);
             };

            if (level>=2 && r.Sequence!=null)
             {
               b.append("<textarea>");
               b.append(r.Sequence);
               b.append("</textarea>");
             };

            return b;
          };
     }

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

/* Required structure: parent nodes (name ignored), inside them -
   child nodes (can be with parameters), representing fields.
   */
 public Sight.Record createRecord(Node xml)
 {
  Record r = new Record();
  r. Id = getXMLField("Id", xml);
  r. Header = getXMLField("Header", xml);
  r. Tissue = getXMLField("Tissue", xml);
  r. Sequence = getXMLField("Sequence", xml);
  r. Cell = getXMLField("Cell", xml);
  return r;
 };

  /** main(..) method provided to check robot quickly, launching it
   * as standalone program. */
  public static void main(String[] args) {
       try {
         Request request = getDefaultRequest();
         // (now you can modify 'request' fields if required)
         Sight.Agents.Request submission = request.submit();
         // processing now started in separate thread:
         Result response = (Result) submission.getResult();

         try {
         // to format report, we need an agent itself, because
         // Document cannot be easily extended by extra methods.
         String html = EST_reader .getAvailableAgent().getReport(response, 2);
         System.out.println("Report: "+html);
         }
         catch (java.lang.UnsupportedOperationException ui)
          { System.out.println(ui.getMessage()); };
       } catch (Exception exc)
          { if (exc!=null) System.out.println(exc.getMessage());
            exc.printStackTrace();
          };
  }

  public EST_reader() {
   setDescription("Reads NCBI EST records.");
   setFormURL("http://www.ncbi.nlm.nih.gov/entrez/");
   setMasterURL("http://www.ncbi.nlm.nih.gov/entrez/");
   // define pre scan section.
   preScan = new preScan();
   cn_maxConnectionLength = 80000000; // abort after 8000 Mb by default.
   }

  /** Public instance of agent for user. */
  public static EST_reader Public = new EST_reader();
  public static Agent getAvailableAgent() { return Public; };
  public Class getRecordClass() { return Record.class; };
   /** Get result from the given XML document */
  public Sight.Result resultFromDocument(Document node) throws Exception {
     Result r = new Result();
     r.a = (Record[]) createResultArray(node);
     return r;
   };

public String getXSLT() {
      return getXMLFromSelf("Sight:xml-xsl-insert-1");
   };
// This insert will be loaded runtime:
/* <Sight:xml-xsl-insert-1>
<?xml version="1.0"?>
<!-- See http://www.w3.org/TR/1999/REC-xslt-19991116 and http://www.w3.org/TR/1999/REC-xpath-19991116
 for transform language description. -->
<!-- $comment -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="xml" indent="yes"/>
   <!-- This will add required starting and closing tags. -->
   <xsl:template match="/">
   <Sight>
      <xsl:apply-templates/>
   </Sight>
   </xsl:template>
   <xsl:template match="text()|@*"></xsl:template>
   <!-- // mathes the path anywhere in the document: -->
   <xsl:template match="//SubSource/SubSource_subtype[@value='tissue-type']">
     <Sight-record>
       <Tissue>
        <xsl:copy-of select="following-sibling::SubSource_name/text()"/>
       </Tissue>
     </Sight-record>
   </xsl:template>
   <xsl:template match="//SubSource/SubSource_subtype[@value='cell-type']">
     <Sight-record>
       <Cell>
        <xsl:copy-of select="following-sibling::SubSource_name/text()"/>
       </Cell>
     </Sight-record>
   </xsl:template>
   <xsl:template match="//SubSource/SubSource_subtype[@value='sex']">
     <Sight-record>
       <Sex>
        <xsl:copy-of select="following-sibling::SubSource_name/text()"/>
       </Sex>
     </Sight-record>
   </xsl:template>
   <xsl:template match="//SubSource/SubSource_subtype[@value='dev-stage']">
     <Sight-record>
       <Stage>
        <xsl:copy-of select="following-sibling::SubSource_name/text()"/>
       </Stage>
     </Sight-record>
   </xsl:template>
   <xsl:template match="//Bioseq/Bioseq_id/Seq-id/Seq-id_gi">
     <Sight-record>
       <Id>
        <xsl:copy-of select="text()"/>
       </Id>
     </Sight-record>
   </xsl:template>
   <xsl:template match="//IUPACna">
     <Sight-record>
       <Sequence>
        <xsl:copy-of select="text()"/>
       </Sequence>
     </Sight-record>
   </xsl:template>
   <xsl:template match="//Seqdesc_title">
     <Sight-record>
       <Header>
        <xsl:copy-of select="text()"/>
       </Header>
     </Sight-record>
   </xsl:template>
</xsl:stylesheet>
</Sight:xml-xsl-insert-1> */
}