package org.biomoby.service.test;

import org.biomoby.shared.MobyDataType;
import org.biomoby.shared.data.*;
import org.biomoby.service.*;

/**
 * Simple service used for exemplary and testing purposes.
 */

@mobyService(name="ConvertAAtoFASTA_AA",
             type="FormatConversion", 
	     provider="moby.ucalgary.ca", 
	     author="gordonp@ucalgary.ca",
	     in={"inseq:AminoAcidSequence"},
	     out={"outseq:FASTA_AA"},
	     description={"Converts amino acid objects into FastA formatted records, ", 
			  "primarily to increase inter-service compatibility"})

public class ConvertAAtoFASTA_AA extends MobyServlet{

    /**
     * Builds a FastA record from a MOBY AminoAcidSequence object.  Demonstrates
     * how to access fields of a MOBY object, and how to build a new object.
     */
    public void processRequest(MobyDataJob request, MobyDataJob result) throws Exception{
         // The input parameter for this method is registered as "inseq"
         MobyDataComposite aaSeqObject = (MobyDataComposite) request.get("inseq");

         // SequenceString is a member of incoming AminoAcidSequence object
         MobyDataString aaStringObject = (MobyDataString) aaSeqObject.get("SequenceString");  
         String aaString = aaStringObject.toString();

         // Do the reformatting (divide seq into 50 char chunks per line)
         String fastaString = ">" + aaSeqObject.getId() + "\n" +
                              aaString.replaceAll("(.{1,50})", "$1\n");

         MobyDataType fastaType = MobyDataType.getDataType("FASTA_AA");
         MobyDataComposite fastaObject = new MobyDataComposite(fastaType, 
                                                               aaSeqObject.getPrimaryNamespace(), 
                                                               aaSeqObject.getId(),
                                                               fastaString);

         // Set the result that will be passed back to the client
         result.put("outseq", fastaObject);
    }
}
