Exemplo n.º 1
0
  public static void main(String argv[]) throws Exception {
    RetsSession session = new RetsSession("http://www.dis.com:6103/rets/login");

    if (!session.Login("Joe", "Schmoe")) {
      System.out.println("Invalid login");
      System.exit(2);
    }

    System.out.println("Action: " + session.GetAction());
    RetsVersion version = session.GetDetectedRetsVersion();

    System.out.println("RETS Version: " + ((version == RetsVersion.RETS_1_5) ? "1.5" : "1.0"));

    SearchRequest searchRequest =
        session.CreateSearchRequest("Property", "RES", "(ListPrice=300000-)");

    searchRequest.SetSelect("ListingID,ListPrice,Beds,City");
    searchRequest.SetLimit(SearchRequest.LIMIT_DEFAULT);
    searchRequest.SetOffset(SearchRequest.OFFSET_NONE);
    searchRequest.SetCountType(SearchRequest.CountType.RECORD_COUNT_AND_RESULTS);
    searchRequest.SetStandardNames(true);
    searchRequest.SetRestrictedIndicator("XXXX");
    searchRequest.SetFormatType(SearchRequest.FormatType.COMPACT);
    SearchResultSet results = session.Search(searchRequest);

    System.out.println("Record count: " + results.GetCount());

    StringVector columns = null;

    while (results.HasNext()) {
      if (columns == null) {
        columns = results.GetColumns();
      }
      for (int i = 0; i < columns.size(); i++) {
        System.out.format("%15s: %s\n", columns.get(i), results.GetString(columns.get(i)));
      }
      System.out.println();
    }

    LogoutResponse logout = session.Logout();

    System.out.println("Billing info: " + logout.GetBillingInfo());
    System.out.println("Logout Message:  " + logout.GetLogoutMessage());
    System.out.println("Connect time: " + logout.GetConnectTime());
  }
  /**
   * Add a new qualifier to a list of qualifiers
   *
   * @param qualifiers
   * @param newQualifier
   */
  private void addNewQualifier(QualifierVector qualifiers, Qualifier newQualifier) {
    Qualifier qualifier;
    if ((qualifier = qualifiers.getQualifierByName(newQualifier.getName())) != null) {
      final StringVector newValues = newQualifier.getValues();
      final StringVector values = qualifier.getValues();

      if (newValues == null) return;
      for (int j = 0; j < newValues.size(); j++) {
        String newValue = (String) newValues.get(j);
        if (!values.contains(newValue)) qualifier.addValue(newValue);
      }
    } else qualifiers.addElement(newQualifier);
  }
Exemplo n.º 3
0
  public static void main(String argv[]) throws Exception {
    RetsSession session = new RetsSession("http://demo.crt.realtors.org:6103/rets/login");

    if (!session.Login("Joe", "Schmoe")) {
      System.out.println("Invalid login");
      System.exit(2);
    }

    System.out.println("Action: " + session.GetAction());
    RetsVersion version = session.GetDetectedRetsVersion();

    System.out.println("RETS Version: " + ((version == RetsVersion.RETS_1_5) ? "1.5" : "1.0"));

    SearchRequest searchRequest =
        session.CreateSearchRequest("Property", "RES", "(ListPrice=300000-)");

    searchRequest.SetSelect("ListingID,ListPrice,Beds,City");
    searchRequest.SetLimit(SearchRequest.LIMIT_DEFAULT);
    searchRequest.SetOffset(SearchRequest.OFFSET_NONE);
    searchRequest.SetCountType(SearchRequest.CountType.RECORD_COUNT_AND_RESULTS);
    searchRequest.SetFormatType(SearchRequest.FormatType.COMPACT);
    searchRequest.SetStandardNames(true);

    try {
      File f = new File("rawsearch.xml");
      FileOutputStream fop = new FileOutputStream(f);
      byte[] data = session.SearchAsArray(searchRequest);
      fop.write(data);
      fop.flush();
      fop.close();
    } catch (IOException e) {
    }

    LogoutResponse logout = session.Logout();

    SearchResultSet results = new SearchResultSet();

    // Reopen the file now for input
    try {
      File f = new File("rawsearch.xml");
      byte[] buffer = new byte[(int) f.length()];

      FileInputStream fip = new FileInputStream(f);

      int offset = 0;
      int numRead = 0;

      while (offset < buffer.length
          && (numRead = fip.read(buffer, offset, buffer.length - offset)) >= 0) offset += numRead;

      results.SetEncoding(EncodingType.RETS_XML_DEFAULT_ENCODING);
      results.SetDataAsArray(buffer);
    } catch (IOException e) {
    }

    System.out.println("Record count: " + results.GetCount());

    StringVector columns = results.GetColumns();

    while (results.HasNext()) {
      if (columns == null) {
        columns = results.GetColumns();
      }
      for (int i = 0; i < columns.size(); i++) {
        System.out.format("%15s: %s\n", columns.get(i), results.GetString(columns.get(i)));
      }
      System.out.println();
    }

    /*
     * Prototype for returning data in a stream.
    try
    {
        File f=new File("foobarty");
        FileOutputStream fop=new FileOutputStream(f);
        CppInputStream data = session.SearchAsStream(searchRequest);
        byte [] buf = new byte[30];
        int len;
        while ((len = data.read(buf, 0, 30)) > 0)
        {
            fop.write(buf, 0, len);
        }
        fop.flush();
        fop.close();
    }
    catch (IOException e) {}
     * end prototype */

  }
  /** Read key and qualifier mappings for CHADO to EMBL */
  private static void initDatabaseMappings() {
    InputStream keyStream = Options.class.getResourceAsStream("/key_mapping");
    if (keyStream == null) keyStream = Options.class.getResourceAsStream("/etc/key_mapping");

    InputStream qualifierStream = Options.class.getResourceAsStream("/qualifier_mapping");
    if (qualifierStream == null)
      qualifierStream = Options.class.getResourceAsStream("/etc/qualifier_mapping");

    final Properties keyMapProperties = new Properties();
    final Properties qualifierMapProperties = new Properties();
    try {
      keyMapProperties.load(keyStream);
      qualifierMapProperties.load(qualifierStream);

      if (System.getProperty("nohistory") != null)
        qualifierMapProperties.setProperty("history", "");
    } catch (IOException e) {
      e.printStackTrace();
    }

    // parse the keyMapProperties
    DATABASE_MAP_KEYS = new Object[keyMapProperties.size()][3];
    final Enumeration keysenum = keyMapProperties.propertyNames();
    int n = 0;
    while (keysenum.hasMoreElements()) {
      String current_map_name = (String) keysenum.nextElement();

      final StringVector property_values =
          Options.getPropertyValues(keyMapProperties, current_map_name);

      DATABASE_MAP_KEYS[n][0] = current_map_name;
      DATABASE_MAP_KEYS[n][1] = property_values.get(0);
      if (property_values.size() == 2) {
        String qualifierString[] = ((String) property_values.get(1)).split("=");
        final uk.ac.sanger.artemis.io.Qualifier qualifier;
        if (qualifierString.length == 2)
          qualifier = new uk.ac.sanger.artemis.io.Qualifier(qualifierString[0], qualifierString[1]);
        else qualifier = new uk.ac.sanger.artemis.io.Qualifier(qualifierString[0]);
        DATABASE_MAP_KEYS[n][2] = qualifier;
      } else DATABASE_MAP_KEYS[n][2] = null;
      n++;
    }

    // parse the qualifier mappings
    Enumeration qualifiersenum = qualifierMapProperties.propertyNames();
    n = 0;

    Vector qualifiersToRemove = new Vector();
    while (qualifiersenum.hasMoreElements()) {
      String current_map_name = (String) qualifiersenum.nextElement();
      final StringVector property_values =
          Options.getPropertyValues(qualifierMapProperties, current_map_name);
      if (property_values == null || property_values.size() == 0)
        qualifiersToRemove.add(current_map_name);
      else n++;
    }

    DATABASE_QUALIFIERS_TO_MAP = new String[n][2];
    DATABASE_QUALIFIERS_TO_REMOVE = qualifiersToRemove.toArray();

    qualifiersenum = qualifierMapProperties.propertyNames();
    n = 0;

    while (qualifiersenum.hasMoreElements()) {
      String current_map_name = (String) qualifiersenum.nextElement();
      final StringVector property_values =
          Options.getPropertyValues(qualifierMapProperties, current_map_name);
      if (property_values != null && property_values.size() > 0) {
        DATABASE_QUALIFIERS_TO_MAP[n][0] = current_map_name;
        DATABASE_QUALIFIERS_TO_MAP[n][1] = (String) property_values.get(0);
        n++;
      }
    }
  }
  /**
   * Merge qualifiers
   *
   * @param qualifiers
   * @param newQualifiers
   */
  private void combineQualifiers(
      final QualifierVector qualifiers, final QualifierVector newQualifiers, final boolean isGene) {
    for (int i = 0; i < newQualifiers.size(); i++) {
      Qualifier newQualifier = (Qualifier) newQualifiers.get(i);

      if (newQualifier.getName().equals("ID") && !isGene) {
        continue;
      }

      // convert GO evidence to codes (e.g. ND=No biological Data available)
      if (newQualifier.getName().equals("GO")) {
        final StringVector newValues = newQualifier.getValues();
        final StringVector tmpNewValues = new StringVector();
        for (int j = 0; j < newValues.size(); j++) {
          String val = GoBox.getEvidenceCodeGoTextFromText((String) newValues.get(j));
          tmpNewValues.add(val);
        }

        newQualifier = new Qualifier("GO", tmpNewValues);
      }

      if (newQualifier.getName().equals("product")) {
        final StringVector newValues = newQualifier.getValues();
        final StringVector tmpNewValues = new StringVector();
        for (int j = 0; j < newValues.size(); j++) {
          String val = (String) newValues.get(j);

          int ind = 0;
          if ((ind = val.indexOf(";db_xref=")) > -1) val = val.substring(0, ind);

          if ((ind = val.indexOf(";evidence=")) > -1) val = val.substring(0, ind);

          if (val.startsWith("term=")) val = val.substring(5, val.length());

          if (val.endsWith(";")) val = val.substring(0, val.length() - 1);

          tmpNewValues.add(val);
        }

        newQualifier = new Qualifier("product", tmpNewValues);
      }

      if (newQualifier.getName().equals("orthologous_to")
          || newQualifier.getName().equals("paralogous_to")) {
        final StringVector newValues = newQualifier.getValues();
        final StringVector tmpNewValues = new StringVector();
        for (int j = 0; j < newValues.size(); j++) {
          if (!newValues.get(j).equals("")) tmpNewValues.add(newValues.get(j));
        }
        if (tmpNewValues.size() == 0) continue;

        Pattern p = Pattern.compile("\\w+:link=\\w+");
        for (int j = 0; j < tmpNewValues.size(); j++) {
          String valueStr = (String) tmpNewValues.get(j);
          String newValueStr;
          int indexEnd = valueStr.indexOf(';');
          String endStr = "";
          if (indexEnd > -1) endStr = valueStr.substring(indexEnd);
          Matcher m = p.matcher(valueStr);
          while (m.find()) {
            int index = valueStr.indexOf("link=", m.start());
            newValueStr =
                valueStr.substring(m.start(), index)
                    + valueStr.substring(index + 5, m.end())
                    + endStr;
            if (newQualifier.getName().equals("orthologous_to"))
              newQualifier = new Qualifier("orthologous_to", newValueStr);
            else newQualifier = new Qualifier("paralogous_to", newValueStr);
            qualifiers.addElement(newQualifier);
          }
        }
        continue;
      }

      addNewQualifier(qualifiers, newQualifier);
    }
  }