/**
 *
 *  THIS AGENT IS NOT COMPLETE.
 *  It is skeleton for a wrapper that computes result from request.
 *  You must complete the go(..) method self dependently.
 *  Generated 2002.9.21  by Sight 2.0, Ulm university.
 */
package impl.manipulators;

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

public class translator extends Sight.Agents.Prototypes.Wrapper implements Serializable {
  /* Data structure to pass query to this robot. */
  public class Request extends Sight.Request implements java.io.Serializable {
        /** RNA sequence to translate */
        public CharSequence rna_sequence;
        /** Frame to translate */
        public CharSequence Frame;
       /** 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 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(true,
     new dField[] {
        new dField("Frame","CharSequence","Frame to translate","0",
         new String[] {"First","all 3 on plus strand","++"},
         new String[] {"0","++","+-"}
                  ),
        new dField("rna_sequence","CharSequence","RNA sequence to translate","")
       });
   };

 /* 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 {
        /** Translated protein sequence */
        public CharSequence protein_Sequence;
        /** Frame of the translated protein sequence */
        public CharSequence Frame;
      };
      /** 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.protein_Sequence.length()+" aa, "+r.Frame;
              default: return r.protein_Sequence.length()+" aa, "+r.Frame+
              "<br><textarea>"+r.protein_Sequence+"</textarea>";
            }
          };
      }
    /** Type definitions for Result. */
   public dStructure getResultDds() {
    return new Sight.dds.Records(false,
     new dField[] {
        new dField("protein_Sequence","CharSequence","Translated protein sequence",""),
        new dField("Frame","CharSequence","Frame of the translated protein sequence","")                 }
    );
   }

   private Record translate(String seq, String hdr)
    {
      Record r = new Record();
      r.protein_Sequence = translator.translate(seq);
      r.Frame = hdr;
      return r;
    };

   Sight.util.Translator translator = new Sight.util.Translator();
   /** Implementation of the central go method.  */
   public Object go(Object o_request) throws Exception
    {
     /** @todo: complete the go(...) method for this wrapper. */
     Request request = (Request) o_request;
     Result  result  = new Result();

     ArrayList list = new ArrayList();
     String seq = request.rna_sequence.toString();


     if (request.Frame.equals("0"))
      {
        list.add(translate(seq,"0"));
      } else
     if (request.Frame.equals("++"))
       {
        list.add(translate(seq,"+.0"));
        list.add(translate(seq.substring(1),"+.1"));
        list.add(translate(seq.substring(2),"+.2"));
       } else
     if (request.Frame.equals("+-"))
       {
        list.add(translate(seq,"+.0"));
        list.add(translate(seq.substring(1),"+.1"));
        list.add(translate(seq.substring(2),"+.2"));
        seq = Sight.util.na.reverse_complement(seq);
        list.add(translate(seq,"-.0"));
        list.add(translate(seq.substring(1),"-.1"));
        list.add(translate(seq.substring(2),"-.2"));
       };

     result.a = new Record[list.size()];
     for (int i = 0; i < result.a.length; i++) {
       result.a[i] = (Record) list.get(i);
     }

     // set the agent signature:
     result.setAgentSignature("Translate "+request.Frame);
     return result;
    };

  public translator() {
   setDescription("Translate rna sequence into protein, using standard (human) genetic code.");
   setMasterURL("");
   // this robot newer uses caching:
   setWriteCache(false);
   setReadCache(false);
   };
  /** Public instance of agent for user. */
  public static translator Public = new translator();
  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:
        request.Frame = "+-";
        request.rna_sequence="GGTGACAACGTTTGGAAGGGCCAGGGCGACCGGCAGTGTG";
        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();
         };
   }
;
  }