import uk.ac.ebi.sbo.sbows.SBOProvider;
import uk.ac.ebi.sbo.sbows.SBOProviderServiceLocator;
import uk.ac.ebi.sbo.sbows.SBOProviderService;

import uk.ac.ebi.sbo.castorExport.*;

import java.io.*;
import iotools.*;

/********************************************************
 * retrieve all terms from SBO given by accession #s
 *******************************************************/

public class SBO
{
  public SBO() {}

  public void getTerms()
  {
    try
    {
      Configuration config = new Configuration();

      SBOProviderServiceLocator service = new SBOProviderServiceLocator();

      //--- set endpoint adress
//      service.setSBOQueryEndpointAddress("http://www.ebi.ac.uk/compneur-srv/sbo-main/services/SBOQuery");
      service.setSBOQueryEndpointAddress("http://www.ebi.ac.uk/sbo/main/services/SBOQuery");

      SBOProvider port = service.getSBOQuery();
System.out.println(port);

      // ------------------------------------------------------------
      // --- INPUT: a file of SBO IDs for terms to be fetched
      // ------------------------------------------------------------
      String     SBO_id_file = config.path() + "\\input\\" + config.input_SBO();
      EasyReader SBO_id_in   = new EasyReader(SBO_id_file);

      // ------------------------------------------------------------
      // --- OUTPUT: a file of SBO IDs mapped to their terms
      // ------------------------------------------------------------
      String     SBO_term_file = config.path() + "\\output\\" + config.output_SBO();
      FileWriter SBO_term_out  = new FileWriter(SBO_term_file);

      while (!SBO_id_in.eof())
      {
        int SBO_id = SBO_id_in.readInt();

        // --- get term by its accession number
        uk.ac.ebi.sbo.castorExport.Term term = port.getTermById(SBO_id);
System.out.println(term);

        String termId   = term.getId();
        String termName = term.getName();
        String id2term  = termId + "\t" + termName + "\n";
        System.out.print(id2term);
        SBO_term_out.write(id2term);

        Synonym[] termSynonym = term.getSynonym();
        for(int i = 0; i < termSynonym.length; i++)
        {
          termName = termSynonym[i].getSynonym_text();
          id2term  = termId + "\t" + termName + "\n";
          System.out.print(id2term);
          SBO_term_out.write(id2term);
        }
      }

      SBO_id_in.close();
      SBO_term_out.close();

    } // --- end of try
    catch (Exception e) {System.out.println(e.toString());}

  } // --- end of main
} // --- end of class

