/* Need to special-case some URLs.  It's yucky to do this,
   * but that's the only way this will work for some of the Drosophila
   * result types.  The assumption in the code is that there is one URL
   * per type, but for us it depends on which database the hit is on. */
  protected String getURLPrefix(FeatureProperty prop, SeqFeatureI f, String id) {
    String defaultURL = prop.getURLString();

    if (!(f instanceof FeaturePair)) return (defaultURL);

    FeaturePair fp = (FeaturePair) f;
    SeqFeatureI fs = (SeqFeatureI) fp.getRefFeature();
    String database = fs.getDatabase();

    String url = defaultURL;

    /* Here we go--a bunch of special cases.
     * It sucks that these are in the code--
     * these should be in the style file or something! */
    String nucleotide = "nucleotide";
    String protein = "protein";
    String ENTREZ_N =
        "http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?cmd=search&db="
            + nucleotide
            + "&doptcmdl=GenBank&term=";
    String ENTREZ_P =
        "http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?cmd=search&db="
            + protein
            + "&doptcmdl=GenBank&term=";
    // In most cases, the IDs for BLASTX Similarity to Fly seem to work better
    // with the default SWALL URL; however, the AA ones seem not to be in SWALL
    // and we have to look in Entrez.  Yuck.
    //    if (prop.typeEquals("BLASTX Similarity to Fly") && id.startsWith("AA")) {
    if (prop.getDisplayType().startsWith("BLASTX") && id.startsWith("AA")) {
      url = ENTREZ_P;
    } else if (prop.typeEquals("Rodent") && (database.indexOf("unigene") >= 0)) {
      url = ENTREZ_N;
    } else if (prop.typeEquals("Insect") && (database.indexOf("dbEST") >= 0)) {
      url = ENTREZ_N;
    }
    // This isn't working right yet.  The tRNA ids look like:
    // gb|AE002593|AE002593.trna5-ArgTCG
    // and we want just the part between the first two ||s.
    else {
      String acc;
      if (prop.typeEquals("tRNA-result") && (database.indexOf("na_tRNA.dros") >= 0)) {
        acc = id.substring(id.indexOf("|") + 1);
        acc = acc.substring(0, acc.indexOf("|"));
      } else if (prop.typeEquals("New Fly Sequence")) {
        // These accs look like gi||gb|AY135216|AY135216
        acc = id;
        if (acc.indexOf("|") >= 0) acc = acc.substring(acc.lastIndexOf("|") + 1);

        /* We've already appended the correct acc,
         * but the URL constructor will automatically
         * slap the uncorrected one at the end.
         * The last # is so that when the whole ID is appended at the end, it
         * won't have any effect. */
        url = ENTREZ_N + acc + "#";
      }
    }
    return url;
  }
  /** For annotations, try to find the appropriate identifier to add to the base URL. */
  protected String generateAnnotURL(AnnotatedFeatureI g, String baseAnnotURL) {
    if (baseAnnotURL == null || baseAnnotURL.equals("")) return null;

    Hashtable id_hash = new Hashtable();

    String id;
    for (int i = 0; i < g.getDbXrefs().size(); i++) {
      DbXref ref = (DbXref) g.getDbXrefs().elementAt(i);
      id = ref.getIdValue();
      // Apparently, if the id starts with FBan we want to replace "FBan" with "CG".
      if (id.startsWith("FBan")) {
        id = id.substring("FBan".length());
        try {
          int cg_num = Integer.parseInt(id);
          id = "CG" + cg_num;
          id_hash.put(id, id);
        } catch (Exception e) {
        }
      } else if (id.startsWith("FB")) { // FBgn, FBab, etc.
        id_hash.put(id, id);
      } else if (id.startsWith("TE")) { // FBgn, FBab, etc.
        id_hash.put(id, id);
      }
    }

    id = getCG(g.getId(), "CG");
    if (id != null) id_hash.put(id, id);
    else {
      id = getCG(g.getId(), "CR");
      if (id != null) id_hash.put(id, id);
      else {
        id = getCG(g.getId(), "FB");
        if (id != null) id_hash.put(id, id);
      }
    }

    id = getCG(g.getName(), "CG");
    if (id != null) id_hash.put(id, id);
    else {
      id = getCG(g.getName(), "CR");
      if (id != null) id_hash.put(id, id);
    }

    for (int i = 0; i < g.getSynonyms().size(); i++) {
      Synonym syn = (Synonym) (g.getSynonyms().elementAt(i));
      id = getCG(syn.getName(), "CG");
      if (id != null) id_hash.put(id, id);
      else {
        id = getCG(syn.getName(), "CR");
        if (id != null) id_hash.put(id, id);
        else {
          id = getCG(g.getId(), "FB");
          if (id != null) id_hash.put(id, id);
        }
      }
    }

    // The code here was glomming together all the elements into one
    // big |ed string, but that's not what the FlyBase annotation report CGI wants.
    // An FB or CG or CR identifier should be sufficient.
    StringBuffer query_str = new StringBuffer();
    for (Enumeration e = id_hash.elements(); e.hasMoreElements(); ) {
      String namepart = (String) e.nextElement();
      //      System.out.println("Considering namepart " + namepart); // DEL
      if (namepart.indexOf("FB") == 0
          || namepart.indexOf("CG") == 0
          || namepart.indexOf("CR") == 0) {
        if (query_str.length() > 0) query_str.replace(0, query_str.length(), namepart);
        else query_str.append(namepart);
        break;
      }
      if (query_str.length() > 0) query_str.append('|');
      query_str.append(namepart);
    }

    // if not FlyBase ID, use the annotation's regular ID
    if (query_str.length() == 0) {
      query_str.append(g.getId());
    }

    String url = null;
    //    System.out.println("generateAnnotUrl: query = " + query_str); // DEL
    if (query_str.length() > 0) {
      // if a custom URL is setup for this type in the tiers file, use that - otherwise use the
      // global
      // one setup in the style file
      FeatureProperty fp = Config.getPropertyScheme().getFeatureProperty(g.getTopLevelType());
      if (fp.getURLString() != null && fp.getURLString().length() > 0) {
        url = fp.getURLString() + g.getId();
      } else {
        url = baseAnnotURL + query_str.toString();
      }
    }

    return url;
  }