/*

Simple Example
of User Supplied Java Class

*/

/**
*   (C) 1996, GeneBee group of Belozersky Institute, 
*             Moscow State University
*
*       Example of user supplied Java class to demonstrate
*       customized screening of GenBank for simple logical 
*       condition on keywords associated with sequences 
*       
*/

import genebee.bank.*;                          // Include GeneBee package classes

public                                          // Mandatory Java keyword 
class MyCustomer                                // You may choose other name instead of "mycustom"
                                                //   if you want
extends Customer {                              // Mandatory class to extend


public void run()                               // The method you should implement
{
        RefSet setA = new RefSet();             // Creation of two empty sets
        RefSet setB = new RefSet();             //   of references to bank sequences
        RefSet mySet;

        setA.init(GBT.MAMMALIANS);              // Fill the sets with ALL references to sequences 
        setB.init(GBT.MAMMALIANS);              //    of GenBank section devoted to mammalians

        setA.filter(FKT.ORGANISM, "BAT");       // Reduce (filtering out) the setA up to the set 
                                                //   of references to the sequences with 
                                                //   keyword fltrA="BAT" in the OS field
                                                //   (description of Organism Species)

        System.out.println(                     // How many seqs were found with "BAT" in the OS fields
                "Number of sequences with 'BAT': " +
                setA.size());   
                                                                                                        
        setB.filter(FKT.ORGANISM, "FRUIT");     // The same with setB... 
        System.out.println(
                "Number of sequences with 'FRUIT': " + 
                setB.size());                   //   ...

        mySet = and(setA, setB);                // Place the intersection of the filtered out
                                                //   sets into the mySet
        System.out.println(                     // Print out how many sequences have both "BAT"
                "Number of sequences in intersection: " +
                mySet.size());                  //   and "FRUIT" strings in the OS fields
        setA.destroy();                         // There is no need in setA,setB more
        setB.destroy();                         //    they should be explicitly destroyed!

        SequenceIterator it =                   // Preparing the item-by-item access to the
                new SequenceIterator(mySet,0);  //   sequences referenced by mySet
                                                //   (SQL cursor if you know what we mean)

        Sequence mySeq;                         // The full data of current Sequence would be
                                                //   placed into mySeq
        int cnt = 0;                            // The counter of the sequences in the intersection set
 
/* Looking through the mySet one-by-one sequence but no more than 100 */

        while ( (mySeq = it.next()) != null  && cnt < 100 )     
        {
                byte[] sq ;                     // The text of the current sequence
                String id ;                     // Its identifier

                try {                           // There are possibility of unpleasant surprises
                        sq = mySeq.getSq();     // ...while retrieving the sequence text
                        id = mySeq.getId();     // ...and/or its identifier
                } catch (Exception e) {         // If any then
                        System.out.println(     //   print out diagnostics
                                "Error during Getting Sequence text!");
                        e.printStackTrace();    //   with the context of the event
                        return; 
                }
                                                // May be there would be less catastrophic
                if(sq == null) {                //   failure
                        System.out.println(     // ...then also print out diagnostics
                                "Getting Sequence text failed!");
                        break;                  //    and get out the loop
                }

                System.out.println("");         // Delimiter between sequences - blank line
                System.out.println("Sequence  id = " +
                        id + ".");              // Print out the identifier of current sequence
                System.out.println("Sequence len = " + 
                        sq.length + ".");       //    and the length of its text

                if (sq.length>0 )               // If not empty print out the sequence text
                {                               //   byte-by-byte (no more then 70 chars of it)  
                        System.out.print("Sequence text: ");
                        for(int k=0; k< min(70,sq.length); k++) { 
                                System.out.print((char)sq[k]); 
                                                // Remember about two-byte nature of chars in Java!
                        }       
                        System.out.println("...");
                }
        
        /**** Begin Info Printing ****/

                InfoItem[] info =               // Retrieve the full collection of seq info lines
                        mySeq.getInfo();
                if(info == null) {              // Ordinary diagnostics
                        System.out.println("Getting Sequence INFO failed!");
                        break;
                }
                                                // How many lines for the seq are retrieved
                System.out.println("Total Info lines = "+ info.length+".");
                for (int j = 0; j < info.length; j++) {
                        String abbr = InfoItem.abbr(info[j].type);      // Get 2-chars name of info line
                        if (abbr == null ||     
                                !( abbr.equals("KW") || abbr.equals("OC") ||
                                abbr.equals("DE") || abbr.equals("OS") )                                )
                        {
                                continue;       // Print only appropriate infos.
                        }
                        System.out.print("<TT><FONT COLOR=\"orange\">"+
                                        InfoItem.abbr(info[j].type)+
                                        "</FONT></TT>: " );
                                                // Pay attention to HTML-formatting the output
                        System.out.println(info[j].text);       
                }
        /**** End of Info printing *****/
                
                mySeq.destroy();                // The explicit destructor should be called!

                cnt++;                          // One more sequence was displayed
        }

        it.destroy();                           // The explicit destructor should be called!
        mySet.destroy();                        //   -""-

        System.out.print( "total " + cnt + "\n" );
}

int min(int a, int b)                           // You may implement any methods aside of run() in your java-class
{
        if(a > b)
                return b;
        else
                return a;
}

}