<bridge_layer schema="gadfly">
  <notes>
    Bridge layer for GadFly Database schema - allows chado to "masquerade"
    as a GadFly Schema, which means we can use (legacy) GadFly software; 
    GadFly is still in use at the BDGP/DHGP

    Activate this to make this bridge take precedence
    SET SEARCH_PATH TO gadfly,public;
    (note that placing gadfly first in the search path means
    that gadfly.dbxref takes precedence over public.dbxref)
  </notes>

  <relation id="seq_feature">
    <sql>
<![CDATA[
SELECT
  feature_id                    AS id,
  feature.name                  AS name,
  0                             AS created,
  CASE WHEN strand<0 THEN fmax ELSE fmin END AS "start",
  CASE WHEN strand<0 THEN fmin ELSE fmax END AS "end",
  0                             AS major_version,
  0                             AS minor_version,
  NULL                          AS primary_acc,
  NULL                          AS primary_dbname,
  t.name                        AS type,
  organism_id                   AS species_id,
  NULL                          AS annotation_id,
  feature_id                    AS seq_id,
  srcfeature_id                 AS src_seq_id
 FROM feature INNER JOIN featureloc USING (feature_id)
      INNER JOIN cvterm AS t ON (type_id=cvterm_id);
]]>
    </sql>
  </relation>

  <relation id="seq">
    <sql>SELECT
  feature_id                    AS id,
  0                             AS created,
  feature.name                  AS name,
  NULL                          AS molecule_type,
  NULL                          AS last_modified,
  residues,
  NULL                          AS description,
  seqlen                        AS length,
  0                             AS major_version,
  md5checksum
 FROM feature;
    </sql>
  </relation>

  <relation id="sf_produces_sf">
    <sql>SELECT
 produced_by_sf_id     AS subject_id,
 produces_sf_id        AS object_id
 FROM feature_relationship;
    </sql>
  </relation>

  <relation id="exon_rank">
    <sql>SELECT
 exon_sf_id              AS subject_id,
 transcript_sf_id        AS object_id,
 rank
 FROM feature_relationship;
    </sql>
  </relation>

  <relation id="analysis">
    <sql>SELECT
 analysis_id AS id,
 name,
 program,
 sourcename AS dbname,
 NULL AS descrption,
 0 AS created,
 0 AS last_modified
 FROM analysis;
    </sql>
  </relation>

  <relation id="sf_analysis">
    <sql>SELECT
 analysis_id,
 feature_id AS seq_feature_id,
 0 AS job_id
 FROM analysisfeature;
    </sql>
  </relation>

  <relation id="result_span">
    <sql>
<![CDATA[
SELECT
  f.feature_id                    AS id,
  CASE WHEN fl1.strand<0 THEN fl1.fmax ELSE fl1.fmin END AS "start1",
  CASE WHEN fl1.strand<0 THEN fl1.fmin ELSE fl1.fmax END AS "end1",
  CASE WHEN fl2.strand<0 THEN fl2.fmax ELSE fl2.fmin END AS "start1",
  CASE WHEN fl2.strand<0 THEN fl2.fmin ELSE fl2.fmax END AS "end1",
  t.name                        AS type,
  NULL                          AS annotation_id,
  fr.object_id                  AS rset_sf_id,
  0                             AS analysis_id,
  fl1.srcfeature_id             AS seq1_id,
  fl2.srcfeature_id             AS seq2_id,
  0                             AS expect_mantissa,
  0                             AS expect_exponent,
  fl1.phase                     AS query_frame,
  fl2.phase                     AS subject_frame,
  rawscore                      AS score,
  identity                      AS percent_identity
 FROM featyre AS f
 INNER JOIN featureloc AS fl1 ON (fl1.feature_id=f.feature_id)
 INNER JOIN featureloc AS fl2 ON (fl2.feature_id=f.feature_id)
 INNER JOIN cvterm AS t ON (type_id=cvterm_id)
 INNER JOIN feature_relationship AS fr ON (f.feature_id=fr.subject_id)
]]>
    </sql>
  </relation>

  <relation id="sf_property">
    <sql>SELECT
 feature_id AS seq_feature_id,
 t.name     AS p_key,
 value      AS p_val
FROM featureprop INNER JOIN cvterm AS t ON (cvterm_id=type_id);
    </sql>
  </relation>

  <relation id="sf_comment">
    <sql>SELECT
 feature_id AS seq_feature_id,
 0 AS is_internal,
 value AS comment
FROM featureprop INNER JOIN cvterm AS t ON (cvterm_id=type_id)
WHERE t.name='comment';
    </sql>
  </relation>

  <relation id="sf_description">
    <sql>SELECT
 feature_id AS seq_feature_id,
 0 AS is_internal,
 value AS comment
FROM featureprop INNER JOIN cvterm AS t ON (cvterm_id=type_id)
WHERE t.name='description';
    </sql>
  </relation>

  <relation id="result_span_data">
    <sql>SELECT
 result_span_id AS seq_feature_id,
 t.name     AS param_key,
 value      AS param_value
FROM featureprop INNER JOIN cvterm AS t ON (cvterm_id=type_id)
    </sql>
  </relation>

  <relation id="seq2dbxref">
    <sql>SELECT
 feature_id AS seq_id,
 dbxref_id
FROM feature_dbxref;
    </sql>
  </relation>

  <relation id="seq2taxon">
    <sql>SELECT
 feature_id AS seq_id,
 organism_id
FROM feature;
    </sql>
  </relation>

  <relation id="seq_feature2dbxref">
    <sql>SELECT
 feature_id AS feature_id,
 dbxref_id
FROM feature_dbxref;
    </sql>
  </relation>

  <relation id="feature2taxon">
    <sql>SELECT
 feature_id AS feature_id,
 organism_id
FROM feature;
    </sql>
  </relation>

  <relation id="dbxref">
    <notes>table name clash - be sure to prefix with schema name</notes>
    <sql>
 SELECT
  dbxref_id                          AS id,
  db.name                            AS dbname,
  dbxref.accession                   AS accession,
  CAST(NULL AS VARCHAR)              AS accessiontype,
  dbxref.description                 AS description
 FROM public.dbxref
  INNER JOIN public.db USING (db_id);
    </sql>
    <index>
CREATE INDEX dbxref_idx1 ON gadfly.dbxref (id);
CREATE INDEX dbxref_idx2 ON gadfly.dbxref (dbname);
CREATE INDEX dbxref_idx3 ON gadfly.dbxref (accession);
CREATE INDEX dbxref_idx4 ON gadfly.dbxref (accessiontype);
CREATE INDEX dbxref_idx5 ON gadfly.dbxref (description);
    </index>
  </relation>

  <relation id="taxon">
    <sql>SELECT
 organism_id AS id,
 0 AS parent_taxon_id,
 genus,
 species,
 common_name,
 '' AS taxon_code,
 '' AS embl_code
FROM organism;
    </sql>
  </relation>


</bridge_layer>
