--creates several items that Apollo expects to be in the database:
-- a property type cv and terms
-- a pub type cv and terms
-- a pubrelationship type cv and terms
-- a pubprop type cv and terms

--Rice    EST_match 
--Rice    cDNA_match
--Maize   EST_match
--Maize   cross_genome_match
--Rice    protein_match
--FgenesH:Monocot

insert into analysis (name,program,programversion,sourcename)
   values ('Rice_EST_match','blat','1','Rice_EST');
insert into analysis (name,program,programversion,sourcename)
   values ('Rice_cDNA_match','blat','1','Rice_cDNA');
insert into analysis (name,program,programversion,sourcename)
   values ('Maize_EST_match','blat','1','Maize_EST');
insert into analysis (name,program,programversion,sourcename)
   values ('Rice_protein_match','blat','1','Rice_protein');
insert into analysis (name,program,programversion,sourcename)
   values ('Maize_cross_genome_match','blat','1','Maize_genome');
insert into analysis (name,program,programversion,sourcename)
   values ('FgenesH_Monocot','FgenesH','1','na');
insert into analysis (name,program,programversion,sourcename)
   values ('Sorghum_EST_match','blat','1','Sourghum_EST');
insert into analysis (name,program,programversion,sourcename)
   values ('Barley_EST_match','blat','1','Barley_EST');
insert into analysis (name,program,programversion,sourcename)
   values ('Wheat_EST_match','blat','1','Wheat_EST');
insert into analysis (name,program,programversion,sourcename)
   values ('Rice_cross_genome_match','blat','1','Rice_genome');
insert into analysis (name,program,programversion,sourcename)
   values ('Millet_EST_match','blat','1','Millet_EST');
insert into analysis (name,program,programversion,sourcename)
   values ('Rice_Japonica_BACend_nucleotide_match','blat','1','Rice_Japonica_BACend');
insert into analysis (name,program,programversion,sourcename)
   values ('riceSativa_bacends_omap_nucleotide_match','blat','1','riceSativa_bacends_omap');
insert into analysis (name,program,programversion,sourcename)
   values ('Rice_Australiensis_BACend_nucleotide_match','blat','1','Rice_Australiensis_BACend');
insert into analysis (name,program,programversion,sourcename)
   values ('riceAustraliensis_bacends_omap_nucleotide_match','blat','1','riceAustraliensis_bacends_omap');
insert into analysis (name,program,programversion,sourcename)
   values ('Rice_Punctata_BACend_nucleotide_match','blat','1','Rice_Punctata_BACend');
insert into analysis (name,program,programversion,sourcename)
   values ('ricePunctata_bacends_omap_nucleotide_match','blat','1','ricePunctata_bacends_omap');
insert into analysis (name,program,programversion,sourcename)
   values ('Maize_MAGI_ISU_cross_genome_match','blat','1','Maize_MAGI_ISU'); 
insert into analysis (name,program,programversion,sourcename)
   values ('Maize_hi_cot_Bennetzen_cross_genome_match','blat','1','Maize_hi_cot_Bennetzen');
insert into analysis (name,program,programversion,sourcename)
   values ('Maize_meth_filt_hi_cot_cluster_cross_genome_match','blat','1','Maize_meth_filt_hi_cot_cluster');
insert into analysis (name,program,programversion,sourcename)
   values ('Maize_hi_cot_TIGR_cross_genome_match','blat','1','Maize_hi_cot_TIGR');
insert into analysis (name,program,programversion,sourcename)
   values ('Maize_BACend_cross_genome_match','blat','1','Maize_BACend');
insert into analysis (name,program,programversion,sourcename)
   values ('Rice_Nivara_BACend_nucleotide_match','blat','1','Rice_Nivara_BACend');
insert into analysis (name,program,programversion,sourcename)
   values ('riceNivara_bacends_omap_nucleotide_match','blat','1','riceNivara_bacends_omap');
insert into analysis (name,program,programversion,sourcename)
   values ('Rice_Rufipogon_BACend_nucleotide_match','blat','1','Rice_Rufipogon_BACend');
insert into analysis (name,program,programversion,sourcename)
   values ('riceRufipogon_bacends_omap_nucleotide_match','blat','1','riceRufipogon_bacends_omap');
insert into analysis (name,program,programversion,sourcename)
   values ('Sorghum_orion_cross_genome_match','blat','1','Sorghum_orion');
insert into analysis (name,program,programversion,sourcename)
   values ('Maize_meth_filt_CSHL_Mccombie_cross_genome_match','blat','1','Maize_meth_filt_CSHL_Mccombie');
insert into analysis (name,program,programversion,sourcename)
   values ('Maize_meth_filt_TIGR_cross_genome_match','blat','1','Maize_meth_filt_TIGR');
insert into analysis (name,program,programversion,sourcename)
   values ('Ryegrass_Sequence_cross_genome_match','blat','1','Ryegrass_Sequence');
insert into analysis (name,program,programversion,sourcename)
   values ('Ryegrass_Assembly_cross_genome_match','blat','1','Ryegrass_Assembly');
insert into analysis (name,program,programversion,sourcename)
   values ('Rice_Glaberrima_BACend_nucleotide_match','blat','1','Rice_Glaberrima_BACend');
insert into analysis (name,program,programversion,sourcename)
   values ('riceGlaberrima_bacends_omap_nucleotide_match','blat','1','riceGlaberrima_bacends_omap');
insert into analysis (name,program,programversion,sourcename)
   values ('Rice_unmapped_BAC_back_nucleotide_match','blat','1','Rice_unmapped_BAC_back');
insert into analysis (name,program,programversion,sourcename)
   values ('Rice_ArrayTarget_Affy57K_cDNA_match','blat','1','Rice_ArrayTarget_Affy57K');
insert into analysis (name,program,programversion,sourcename)
   values ('Rice_GI_EST_match','blat','1','Rice_GI');
insert into analysis (name,program,programversion,sourcename)
   values ('Rice_jap_cDNA_KOME_cDNA_match','blat','1','Rice_jap_cDNA_KOME');
insert into analysis (name,program,programversion,sourcename)
   values ('Rice_Brachyantha_BACend_nucleotide_match','blat','1','Rice_Brachyantha_BACend');
insert into analysis (name,program,programversion,sourcename)
   values ('riceBrachyantha_bacends_omap_nucleotide_match','blat','1','riceBrachyantha_bacends_omap');
insert into analysis (name,program,programversion,sourcename)
   values ('Osativa_TUG_PlantGDB_EST_match','blat','1','Osativa_TUG_PlantGDB');
insert into analysis (name,program,programversion,sourcename)
   values ('Rice_tug_EST_match','blat','1','Rice_tug');
insert into analysis (name,program,programversion,sourcename)
   values ('Rice_Alta_BACend_nucleotide_match','blat','1','Rice_Alta_BACend');
insert into analysis (name,program,programversion,sourcename)
   values ('riceAlta_bacends_omap_nucleotide_match','blat','1','riceAlta_bacends_omap');
insert into analysis (name,program,programversion,sourcename)
   values ('Sorghum_cluster_Pratt_EST_match','blat','1','Sorghum_cluster_Pratt');
insert into analysis (name,program,programversion,sourcename)
   values ('Maize_est_EST_match','blat','1','Maize_est');
insert into analysis (name,program,programversion,sourcename)
   values ('Maize_GI_EST_match','blat','1','Maize_GI');
insert into analysis (name,program,programversion,sourcename)
   values ('Maize_ArrayTarget_NSF58K_cDNA_match','blat','1','Maize_ArrayTarget_NSF58K');
insert into analysis (name,program,programversion,sourcename)
   values ('Zmays_TUG_PlantGDB_EST_match','blat','1','Zmays_TUG_PlantGDB');
insert into analysis (name,program,programversion,sourcename)
   values ('Wheat_est_EST_match','blat','1','Wheat_est');
insert into analysis (name,program,programversion,sourcename)
   values ('Wheat_GI_EST_match','blat','1','Wheat_GI');
insert into analysis (name,program,programversion,sourcename)
   values ('Wheat_tug_EST_match','blat','1','Wheat_tug');
insert into analysis (name,program,programversion,sourcename)
   values ('Taestivum_TUG_PlantGDB_EST_match','blat','1','Taestivum_TUG_PlantGDB');
insert into analysis (name,program,programversion,sourcename)
   values ('Wheat_ArrayTarget_Affy61K_cDNA_match','blat','1','Wheat_ArrayTarget_Affy61K');
insert into analysis (name,program,programversion,sourcename)
   values ('Barley_est_EST_match','blat','1','Barley_est');
insert into analysis (name,program,programversion,sourcename)
   values ('Barley_GI_EST_match','blat','1','Barley_GI');
insert into analysis (name,program,programversion,sourcename)
   values ('Barley_ArrayTarget_Affy22K_cDNA_match','blat','1','Barley_ArrayTarget_Affy22K');
insert into analysis (name,program,programversion,sourcename)
   values ('Hvulgare_TUG_PlantGDB_EST_match','blat','1','Hvulgare_TUG_PlantGDB');
insert into analysis (name,program,programversion,sourcename)
   values ('Sugarcane_est_EST_match','blat','1','Sugarcane_est');
insert into analysis (name,program,programversion,sourcename)
   values ('Barley_tug_EST_match','blat','1','Barley_tug');
insert into analysis (name,program,programversion,sourcename)
   values ('Maize_markers_cross_genome_match','blat','1','Maize_markers');
insert into analysis (name,program,programversion,sourcename)
   values ('Rice_ind_est_EST_match','blat','1','Rice_ind_est');
insert into analysis (name,program,programversion,sourcename)
   values ('Rice_ind_cluster_EST_match','blat','1','Rice_ind_cluster');
insert into analysis (name,program,programversion,sourcename)
   values ('Sorghum_GI_EST_match','blat','1','Sorghum_GI');
insert into analysis (name,program,programversion,sourcename)
   values ('Rice_est_EST_match','blat','1','Rice_est');
insert into analysis (name,program,programversion,sourcename)
   values ('Sorghum_CDNA_cDNA_match','blat','1','Sorghum_CDNA');
insert into analysis (name,program,programversion,sourcename)
   values ('Sorghum_est_EST_match','blat','1','Sorghum_est');
insert into analysis (name,program,programversion,sourcename)
   values ('Sbicolor_TUG_PlantGDB_EST_match','blat','1','Sbicolor_TUG_PlantGDB');
insert into analysis (name,program,programversion,sourcename)
   values ('Maize_tug_EST_match','blat','1','Maize_tug');
insert into analysis (name,program,programversion,sourcename)
   values ('Maize_ArrayTarget_Affy18K_cDNA_match','blat','1','Maize_ArrayTarget_Affy18K');
insert into analysis (name,program,programversion,sourcename)
   values ('Sorghum_tug_EST_match','blat','1','Sorghum_tug');
insert into analysis (name,program,programversion,sourcename)
   values ('Maize_cornsensus_cDNA_match','blat','1','Maize_cornsensus');
insert into analysis (name,program,programversion,sourcename)
   values ('Millet_est_EST_match','blat','1','Millet_est');
insert into analysis (name,program,programversion,sourcename)
   values ('Maize_CDs_cDNA_match','blat','1','Maize_CDs');
insert into analysis (name,program,programversion,sourcename)
   values ('Maize_Unigene_Panzea_EST_match','blat','1','Maize_Unigene_Panzea');
insert into analysis (name,program,programversion,sourcename)
   values ('Wheat_markers_cross_genome_match','blat','1','Wheat_markers');
insert into analysis (name,program,programversion,sourcename)
   values ('Rice_BAC_nucleotide_match','blat','1','Rice_BAC');
insert into analysis (name,program,programversion,sourcename)
   values ('Rice_unmapped_BAC_front_nucleotide_match','blat','1','Rice_unmapped_BAC_front');
insert into analysis (name,program,programversion,sourcename)
   values ('Sorghum_Markers_cross_genome_match','blat','1','Sorghum_Markers');
insert into analysis (name,program,programversion,sourcename)
   values ('Rice_CDS_cDNA_match','blat','1','Rice_CDS');
insert into analysis (name,program,programversion,sourcename)
   values ('Sorghum_gss-read_Klein_cross_genome_match','blat','1','Sorghum_gss-read_Klein');
insert into analysis (name,program,programversion,sourcename)
   values ('RiceAlta_BACend_OMAP_nucleotide_match','blat','1','RiceAlta_BACend_OMAP');
insert into analysis (name,program,programversion,sourcename)
   values ('RiceBrachyantha_BACend_OMAP_nucleotide_match','blat','1','RiceBrachyantha_BACend_OMAP');
insert into analysis (name,program,programversion,sourcename)
   values ('RiceJaponica_BACend_OMAP_nucleotide_match','blat','1','RiceJaponica_BACend_OMAP');
insert into analysis (name,program,programversion,sourcename)
   values ('RiceAustraliensis_BACend_OMAP_nucleotide_match','blat','1','RiceAustraliensis_BACend_OMAP');
insert into analysis (name,program,programversion,sourcename)
   values ('RiceCoarctata_BACend_OMAP_nucleotide_match','blat','1','RiceCoarctata_BACend_OMAP');
insert into analysis (name,program,programversion,sourcename)
   values ('RicePunctata_BACend_OMAP_nucleotide_match','blat','1','RicePunctata_BACend_OMAP');
insert into analysis (name,program,programversion,sourcename)
   values ('RiceGlaberrima_BACend_OMAP_nucleotide_match','blat','1','RiceGlaberrima_BACend_OMAP');
insert into analysis (name,program,programversion,sourcename)
   values ('RiceNivara_BACend_OMAP_nucleotide_match','blat','1','RiceNivara_BACend_OMAP');
insert into analysis (name,program,programversion,sourcename)
   values ('Rice_ESTCluster_PlantGDB_EST_match','blat','1','Rice_ESTCluster_PlantGDB');
insert into analysis (name,program,programversion,sourcename)
   values ('Barley_ESTCluster_PlantGDB_EST_match','blat','1','Barley_ESTCluster_PlantGDB');
insert into analysis (name,program,programversion,sourcename)
   values ('Wheat_ESTCluster_PlantGDB_EST_match','blat','1','Wheat_ESTCluster_PlantGDB');
insert into analysis (name,program,programversion,sourcename)
   values ('Sorghum_ESTCluster3P_LGBPratt_EST_match','blat','1','Sorghum_ESTCluster3P_LGBPratt');
insert into analysis (name,program,programversion,sourcename)
   values ('Sorghum_ESTCluster_PlantGDB_EST_match','blat','1','Sorghum_ESTCluster_PlantGDB');
insert into analysis (name,program,programversion,sourcename)
   values ('Maize_ESTCluster_PlantGDB_EST_match','blat','1','Maize_ESTCluster_PlantGDB');
insert into analysis (name,program,programversion,sourcename)
   values ('RiceRufipogon_BACend_OMAP_nucleotide_match','blat','1','RiceRufipogon_BACend_OMAP');
insert into analysis (name,program,programversion,sourcename)
   values ('Maize_Seq_Panzea_EST_match','blat','1','Maize_Seq_Panzea');
insert into analysis (name,program,programversion,sourcename)
   values ('RiceMinuta_BACend_OMAP_nucleotide_match','blat','1','RiceMinuta_BACend_OMAP');
insert into analysis (name,program,programversion,sourcename)
   values ('RiceGranulata_BACend_OMAP_nucleotide_match','blat','1','RiceGranulata_BACend_OMAP');
insert into analysis (name,program,programversion,sourcename)
   values ('RiceRidleyi_BACend_OMAP_nucleotide_match','blat','1','RiceRidleyi_BACend_OMAP');
insert into analysis (name,program,programversion,sourcename)
   values ('RiceOfficinalis_BACend_OMAP_nucleotide_match','blat','1','RiceOfficinalis_BACend_OMAP');


insert into db (name) values ('DB:TIGR_EGAD');
insert into db (name) values ('DB:Maize_Seq_Panzea');
insert into db (name) values ('DB:RiceRufipogon_BACend_OMAP');
insert into db (name) values ('DB:Maize_ESTCluster_PlantGDB');
insert into db (name) values ('DB:Sorghum_ESTCluster_PlantGDB');
insert into db (name) values ('DB:Sorghum_ESTCluster3P_LGBPratt');
insert into db (name) values ('DB:Wheat_ESTCluster_PlantGDB');
insert into db (name) values ('DB:Barley_ESTCluster_PlantGDB');
insert into db (name) values ('DB:Rice_ESTCluster_PlantGDB');
insert into db (name) values ('DB:RiceNivara_BACend_OMAP');
insert into db (name) values ('DB:RiceGlaberrima_BACend_OMAP');
insert into db (name) values ('DB:RicePunctata_BACend_OMAP');
insert into db (name) values ('DB:RiceCoarctata_BACend_OMAP');
insert into db (name) values ('DB:RiceJaponica_BACend_OMAP');
insert into db (name) values ('DB:RiceBrachyantha_BACend_OMAP');
insert into db (name) values ('DB:RiceAlta_BACend_OMAP');
insert into db (name) values ('DB:Sorghum_gss-read_Klein');
insert into db (name) values ('DB:Rice_CDS');
insert into db (name) values ('DB:Sorghum_Markers');
insert into db (name) values ('DB:Rice_BAC');
insert into db (name) values ('DB:Wheat_Markers');
insert into db (name) values ('DB:Maize_cornsensus');
insert into db (name) values ('DB:Sorghum_tug');
insert into db (name) values ('DB:Maize_tug');
insert into db (name) values ('DB:Sorghum_CDNA');
insert into db (name) values ('DB:Sorghum_GI');
insert into db (name) values ('DB:Rice_ind_cluster');
insert into db (name) values ('DB:Rice_ind_est');
insert into db (name) values ('DB:Maize_markers');
insert into db (name) values ('DB:Barley_tug');
insert into db (name) values ('DB:Sugarcane_est');
insert into db (name) values ('DB:Barley_GI');
insert into db (name) values ('DB:Wheat_tug');
insert into db (name) values ('DB:Wheat_GI');
insert into db (name) values ('DB:Wheat_est');
insert into db (name) values ('DB:Maize_GI');
insert into db (name) values ('DB:Sorghum_cluster_Pratt');
insert into db (name) values ('DB:EST_match');
insert into db (name) values ('DB:Rice_tug');
insert into db (name) values ('DB:cDNA_match');
insert into db (name) values ('DB:Rice_GI');
insert into db (name) values ('DB:TIGR_Maize');
insert into db (name) values ('DB:TIGR_Osa');
insert into db (name) values ('DB:nucleotide_match');
insert into db (name) values ('DB:Sorghum_Orion');
insert into db (name) values ('DB:cross_genome_match');
insert into db (name) values ('DB:Maize_hi_cot_TIGR');
insert into db (name) values ('DB:Maize_meth_filt_TIGR');

insert into pub (uniquename, title, type_id) values ('none', 'none', (select cvterm_id from cvterm where name = 'null pub' and cv_id in (select cv_id from cv where name='pub type') ) );

--for the new triggers

create sequence uniquename_id_generator;

insert into cvtermprop (cvterm_id,type_id,value) values ((select cvterm_id from cvterm where name='prefix' and cv_id in (select cv_id from cv where name='apollo')),(select cvterm_id from cvterm where name='is_a' and cv_id in (select cv_id from cv where name='relationship')),'RICE');
insert into cvtermprop (cvterm_id,type_id,value) values ((select cvterm_id from cvterm where name='suffix' and cv_id in (select cv_id from cv where name='apollo')),(select cvterm_id from cvterm where name='is_a' and cv_id in (select cv_id from cv where name='relationship')),'');

insert into dbxref (db_id, accession) values ((select db_id from db where name='null'),'Relationship Ontology:producedby');
insert into cvterm (name, cv_id, dbxref_id, is_relationshiptype) values ('producedby',(select cv_id from cv where name='Relationship Ontology'),(select dbxref_id from dbxref where accession = 'Relationship Ontology:producedby'),1);

CREATE TABLE feature_namegenerator (
  feature_namegenerator_id serial not null,
  primary key (feature_namegenerator_id),
  name varchar(255),
  type_id bigint,
  foreign key (type_id)  references cvterm (cvterm_id) on delete cascade INITIALLY DEFERRED,
  count int
--  constraint feature_namegenerator_c1 unique (name,type_id)
);

INSERT INTO feature_namegenerator (name,type_id,count)
SELECT substr(name, 0 , (position('-' in name))),
      (select cvterm_id from cvterm where cv_id=7 and name='transcript'),
       to_number(max(substring(name from 21)), '99999')
FROM feature WHERE name like 'RICE%-transcript%' group by name;

INSERT INTO feature_namegenerator (name,type_id,count)
SELECT substr(name, 0 , (position('-' in name))),
      (select cvterm_id from cvterm where cv_id=14 and name='protein'),
       to_number(max(substring(name from 19)), '99999')
FROM feature WHERE name like 'RICE%-protein%' group by name;

delete from  feature_namegenerator where feature_namegenerator_id not in
(select feature_namegenerator_id from feature_namegenerator where count = max(count) group by name,type_id);

alter table feature_namegenerator add constraint feature_namegenerator_c1 unique (name,type_id);
