import charite.christo.strap.StrapProtein;
import charite.christo.protein.ProteinParser;
import charite.christo.protein.Protein;
import charite.christo.ChUtils;
import charite.christo.ByteArray;
import java.util.ArrayList;
import java.io.File;
import static java.lang.System.*;
/*
  java  DemoProteinParser2 hs_EscherichiaColi.pdb
  Imagine your protein files have a format which is not supported by STRAP.
  This demo explains, how a protein parser can be implemented and used.

  The text is given in charite.christo.ByteArray.  It has similar
  features like java.lang.String or StringBuffer, but is faster and
  keeps a reference of the positions of line ends.  The byte array may
  be longer than the valid text.  The valid text starts at index
  getBegin() and ends at getEnd().
*/
public class DemoProteinParser2 {
    public static void main(String argv[]) {
        out.println("This is the protein file parsed with the built-in parsers ");
        final  StrapProtein p1= StrapProtein.newInstance(new java.io.File(argv[0]));
        out.println("p1.getResidueType()="+p1.getResidueTypeAsString());
        /* Create a protein object */
        final  StrapProtein p2=new StrapProtein();
        /* Reads the text of the file given by the first  command line parameter */
        /* Evaluates the text and set the amino acid sequence in the protein object */
        final long options=0;
        p2.setName("p2");
        p2.parse(ChUtils.readBytes(new File(argv[0])),new ProteinParser[]{new MyParser()},options);
        /* Check the amino acid sequence */
        out.println("\nThis is the same protein file parsed with the self-made parser");
        out.println("p2.getResidueType()="+p2.getResidueTypeAsString());
        exit(0);
    }

    static class MyParser implements ProteinParser {
        /* This is the self-made parser. It is rather stupid. */
        /* Irrespectively of the contents of the protein file text it sets the same amino acid sequence. */
        public boolean parse(Protein p, long options, ByteArray byteArray) {
            final int ends[]=byteArray.getLineEnds();
            final int begin=byteArray.getBegin();
            final int end=byteArray.getEnd();
            final byte[] text=byteArray.getBytes();
            out.println(" The text contains "+(end-begin)+" characters and "+ends.length+" lines.");
            p.setResidueType("HelloWorld");
            /* By  returning true it indicates that parsing was successful, */
            /* False would say that it is the wrong protein file type for this parser. */
            return true;
        }
    }
}