#!/bin/tcsh -efx

# This file describes how the proteins080707 database was
# built.  See also the sp080707 database doc, which needs
# to be built first.

hgsqladmin create proteins080707

cd /cluster/data/proteins
mkdir 080707
cd 080707
mkdir build

# Get HGNC data
~/src/hg/protein/getHgnc.pl >hgnc.tab

hgsql proteins080707 < ~/src/hg/lib/hgnc.sql
hgsql proteins080707 -e 'load data local infile "hgnc.tab" into table hgnc ignore 1 lines'
 
pbHgnc 080707   
hgsql proteins080707 < ~/src/hg/lib/hgncXref.sql
hgsql proteins080707 -e 'load data local infile "hgncXref.tab" into table hgncXref'
#note:  please notify Elspeth Bruford at hgnc that there is a new mapping
# to their names.  hgnc@genenames.org  here is the file that ann gave them
# hgsql -Ne
# "SELECT geneSymbol,hgncId,transcript
# FROM knownCanonical,kgXref,proteome.hgncXref
# WHERE kgXref.kgID=knownCanonical.transcript
# AND kgXref.geneSymbol = proteome.hgncXref.symbol"
# hg18 > hg18.hgncId
#   -- b0b 12-26-2007
#   -- ASZ updated 01-03-2008
# Actually, they have automated this task, so we no longer need to notify
# them. (ASZ: 02-04-2008). 

# make sure the pfamXref program is available:
cd /cluster/data/proteins/080707/build

# FIRST BUILD spXref2 and spXref3 tables.

mkdir /cluster/data/proteins/080707/build
cd /cluster/data/proteins/080707/build

# Create a bioentry table to use as an internal replacement for accession.
# Takes a minute
hgsql sp080707 -e "create table bioentryID (acc char(16) NOT NULL, bioentryID int not null auto_increment, primary key (bioentryID), unique (acc))"
hgsql sp080707 -e "insert into bioentryID (acc) select acc from sp080707.info order by acc"

# get records with HUGL symbol and descriptions
hgsql sp080707 -N -e 'select d.acc, d.val, taxon, id.bioentryID, 2-isCurated, des.val,hx.symbol, hx.description from displayId d, info i, accToTaxon t, bioentryID id, description des, proteins080707.hgncXref hx where  d.acc=t.acc and d.acc=i.acc and d.acc=id.acc and d.acc = id.acc and d.acc=des.acc and hx.uniProt=d.acc ' >spXref3a.tab

# get records with HUGL symbol and descriptions for variant splice proteins
hgsql sp080707 -N -e 'select v.varAcc, v.varAcc, taxon, id.bioentryID, 2-isCurated, des.val,hx.symbol, hx.description from varAcc v, displayId d, info i, accToTaxon t, bioentryID id, description des, proteins080707.hgncXref hx where v.parAcc=d.acc and d.acc=t.acc and d.acc=i.acc and d.acc=id.acc and d.acc = id.acc and d.acc=des.acc and hx.uniProt=d.acc ' >spXref3av.tab

# get all records and set HUGO symbol and description to "" 

hgsql sp080707 -N -e 'select d.acc, d.val, taxon, id.bioentryID, 2-isCurated, des.val,"", "" from displayId d, info i, accToTaxon t, bioentryID id, description des where  d.acc=t.acc and d.acc=i.acc and d.acc=id.acc and d.acc = id.acc and d.acc=des.acc ' >spXref3b.tab

# get all records and set HUGO symbol and description to "" for variant splice proteins 

hgsql sp080707 -N -e 'select v.varAcc, v.varAcc, taxon, id.bioentryID, 2-isCurated, des.val,"", "" from varAcc v, displayId d, info i, accToTaxon t, bioentryID id, description des where v.parAcc=d.acc and  d.acc=t.acc and d.acc=i.acc and d.acc=id.acc and d.acc = id.acc and d.acc=des.acc ' >spXref3bv.tab

# Clean up temp table.
hgsql sp080707 -e "drop table bioentryID"

# collect all data from the resulting 4 files.

cat spXref3a.tab  spXref3b.tab  spXref3av.tab spXref3bv.tab >spXref3.tab

# load into the spXref3 table
hgLoadSqlTab proteins080707 spXref3 ~/kent/src/hg/lib/spXref3.sql spXref3.tab

# load the same data into a second temp table
hgLoadSqlTab proteins080707 spXref3B ~/kent/src/hg/lib/spXref3.sql spXref3.tab

# remove records that have empty HUGO symbol, yet other records 
# of the same accession does have non-empty HUGO symbols.
hgsql proteins080707 -e 'delete spXref3 from spXref3 a, spXref3B b where a.accession=b.accession and a.hugoSymbol="" and b.hugoSymbol!=""'

# Drop temp table
hgsql proteins080707 -e "drop table spXref3B"

######################################################################
# BUILD spXref2 TABLE (DONE, Fan, 07/29/08)

/cluster/home/fanhsu/bin/x86_64/spToSpXref2 080707 >spXref2.tab

hgsql proteins080707 < ~/src/hg/lib/spXref2.sql

# load the big table, ~63 million records
hgsql proteins080707 -e 'load data local infile "spXref2.tab" into table spXref2'

# create the indices, took about 40 minutes for each index. 

hgsql proteins080707 -e 'create index displayID on spXref2(displayID)'
hgsql proteins080707 -e 'create index extAC on spXref2(extAC)'
hgsql proteins080707 -e 'create index accession on spXref2(accession)'

########################################################################
# Build spOrganism table
hgsql sp080707 -N -e 'select d.val, taxon from displayId d, accToTaxon t where d.acc=t.acc ' >spOrganism.tab

hgLoadSqlTab proteins080707 spOrganism ~/kent/src/hg/lib/spOrganism.sql ./spOrganism.tab

#	Build spSecondaryID table
cd /cluster/data/proteins/080707
hgsql -e "select displayId.val, displayId.acc, otherAcc.val from displayId, \
        otherAcc where otherAcc.acc = displayId.acc;" sp080707 \
	| sed -e "1d" > spSecondaryID.tab

hgLoadSqlTab proteins080707 spSecondaryID ~/kent/src/hg/lib/spSecondaryID.sql ./spSecondaryID.tab

#	Build pfamXref and pfamDesc tables
mkdir /san/sanvol1/scratch/proteins/pfam/080707
cd    /san/sanvol1/scratch/proteins/pfam/080707
wget --timestamping "ftp://ftp.sanger.ac.uk/pub/databases/Pfam/current_release/Pfam-A.full.gz"
#	300 Mb compressed, over 3GB uncompressed

gunzip -c Pfam-A.full.gz | pfamXref proteins080707 stdin pfamADesc.tab pfamAXref.tab >pfamXref.log

hgLoadSqlTab proteins080707 pfamDesc ~/kent/src/hg/lib/pfamDesc.sql pfamADesc.tab
hgLoadSqlTab proteins080707 pfamXref ~/kent/src/hg/lib/pfamXref.sql pfamAXref.tab

#	Build the pdbSP table, new process using extDbRef data from spXXXXXX

cd /san/sanvol1/scratch/proteins/pfam/080707

hgsql sp080707 -N -e 'select extAcc1, d.val from extDbRef x, displayId d, extDb where x.acc=d.acc and extDb.val="PDB" and x.extDb=extDb.id'|sort -u >pdbSP.tab

hgLoadSqlTab proteins080707 pdbSP ~/kent/src/hg/lib/pdbSP.sql pdbSP.tab

#	Build the spDisease table

hgsql -N -e \
'select comment.acc, displayId.val, commentVal.val from comment, commentVal, commentType ct, displayId where comment.commentType=ct.id and ct.val="DISEASE" and commentVal.id=comment.commentVal and displayId.acc=comment.acc;' \
sp080707 > spDisease.tab

hgLoadSqlTab proteins080707 spDisease ~/kent/src/hg/lib/spDisease.sql spDisease.tab

# create swInterPro table

wget --timestamping "ftp://ftp.ebi.ac.uk/pub/databases/interpro/protein2ipr.dat.gz"
gzip -d protein2ipr.dat.gz
# rearange col positioin to match the old format
cut -f 1 protein2ipr.dat >j1
cut -f 2,3 protein2ipr.dat >j23
cut -f 4,5,6 protein2ipr.dat >j456
paste j1 j456 j23 > interProXref.tab
rm j1 j456 j23

hgLoadSqlTab proteins080707 interProXref ~/kent/src/hg/lib/interProXref.sql ./interProXref.tab

hgsql proteins080707 -N -e 'select accession, interProId from interProXref;'|sort -u >swInterPro.tab
hgLoadSqlTab proteins080707 swInterPro ~/kent/src/hg/lib/swInterPro.sql ./swInterPro.tab

# BUILD TABLES FOR pbGlobal (PB V1.1)

cd /cluster/data/proteins/080707
mkdir pbGlobal
cd pbGlobal

# Calculate Pi values for all proteins

hgsql sp080707 -e "select acc from protein" >acc080707.lis
nice pbCalPi acc080707.lis sp080707 pi080707.tab
/cluster/home/fanhsu/bin/x86_64/pbCalPi acc080707.lis sp080707 pi080707.tab 

hgLoadSqlTab proteins080707 pepPi ~/kent/src/hg/lib/pepPi.sql ./pi080707.tab

# Build pepMwAa table

hgsql sp080707 -N -e "select acc, molWeight, aaSize from info" >pepMwAa.tab
hgLoadSqlTab proteins080707 pepMwAa ~/kent/src/hg/lib/pepMwAa.sql ./pepMwAa.tab

# Calculate global protein property distributions

nice pbCalDistGlobal sp080707 proteins080707

# Load the tables

hgLoadSqlTab proteins080707 pepCCntDist ~/kent/src/hg/lib/pepCCntDist.sql pepCCntDist.tab
hgLoadSqlTab proteins080707 pepHydroDist ~/kent/src/hg/lib/pepHydroDist.sql pepHydroDist.tab
hgLoadSqlTab proteins080707 pepIPCntDist ~/kent/src/hg/lib/pepIPCntDist.sql pepIPCntDist.tab
hgLoadSqlTab proteins080707 pepMolWtDist ~/kent/src/hg/lib/pepMolWtDist.sql pepMolWtDist.tab
hgLoadSqlTab proteins080707 pepPiDist ~/kent/src/hg/lib/pepPiDist.sql pepPiDist.tab
hgLoadSqlTab proteins080707 pepResDist ~/kent/src/hg/lib/pepResDist.sql pepResDist.tab

# Calculate global AA residue distributions

pbCalResStdGlobal 080707

# Load distribution tables:

hgLoadSqlTab proteins080707 pbAnomLimit ~/kent/src/hg/lib/pbAnomLimit.sql ./pbAnomLimit.tab
hgLoadSqlTab proteins080707 pbResAvgStd  ~/kent/src/hg/lib/pbResAvgStd.sql ./pbResAvgStd.tab

# Get taxonomy names table from NCBI.

cd /cluster/data/proteins/080707
mkdir taxon
cd taxon
wget --timestamping ftp://ftp.ncbi.nih.gov/pub/taxonomy/taxdmp.zip
unzip taxdmp.zip

# Create table taxonNames in proteins080707

# Load from the file names.dmp into taxonNames table.

# hgsql proteins080707 -e 'drop table taxonNames'
hgsql proteins080707 < ~/kent/src/hg/lib/taxonNames.sql    
hgsql proteins080707 -e 'load data local infile "names.dmp" into table taxonNames fields terminated by "|" enclosed by "\t";'

# Load and edit pbStamp table

cd /cluster/data/proteins/080707
hgsql proteins060115 -N -e "select * from pbStamp" > pbStamp.tab

hgLoadSqlTab proteins080707 pbStamp ~/kent/src/hg/lib/pbStamp.sql ./pbStamp.tab

mkdir alias
cd alias

hgsql proteins080707 -N -e 'select accession, accession,  "uAcc", "2006-01-15" from spXref3' >j1.tmp
hgsql proteins080707 -N -e 'select accession, accession2, "u2Ac", "2006-01-15" from spSecondaryID' >j2.tmp
hgsql proteins080707 -N -e 'select accession, displayID,  "uDis", "2006-01-15" from spXref3' >j3.tmp

hgsql proteins040515 -N -e 'select accession, displayID,  "oDis", "2004-05-15" from spXref3' >j4.tmp
# PLEASE NOTE: proteins041115 was later deleted by Fan when hgwdev mySQL space was at 100%.
# So, use protein040515 instead.

cat j1.tmp j2.tmp j3.tmp j4.tmp >uniProtAlias.tab
rm j1.tmp j2.tmp j3.tmp j4.tmp

hgLoadSqlTab proteins080707 uniProtAlias ~/kent/src/hg/lib/uniProtAlias.sql ./uniProtAlias.tab
hgsql proteins080707 -e 'create index alias on uniProtAlias(alias)'

hgsql proteins050415 -N -e 'select * from spOldNew' >spOldNew.tab
hgLoadSqlTab proteins080707 spOldNew ~/kent/src/hg/lib/spOldNew.sql ./spOldNew.tab

hgsql proteins080707 -e 'create index oldDisplayId on spOldNew(oldDisplayId)'
hgsql proteins080707 -e 'create index newDisplayId on spOldNew(newDisplayId)'

# Build spDeleted table

# Download list of deleted accession numbers from UniProt
cd /cluster/bluearc/fan/proteins/080707/build

wget --timestamping http://ca.expasy.org/txt/delac_tr.txt
cp delac_tr.txt spDeleted.tab

# Edit spDeleted.tab to get rid of other lines except accession numbers

vi spDeleted.tab
hgLoadSqlTab proteins080707 spDeleted ~/kent/src/hg/lib/spDeleted.sql ./spDeleted.tab

# CREATE spVariant TABLE TO ENABLE UNIPROT VARIANT SPLICE ISOFORM PROCESSING 

hgsql proteins080707 -N -e 'select accession,accession from spXref3' >j1
cat j1 |grep "-" |sed -e 's/-/\t/'|cut -f 1,3 >j2
cat j1 |grep -v "-" >>j3
cat j2 j3 |sort -u >spVariant.tab
rm j1 j2 j3


hgLoadSqlTab proteins080707 spVariant ~/kent/src/hg/lib/spVariant.sql ./spVariant.tab
hgsql proteins080707 -e 'create index parent on spVariant(parent)'

# EXTEND pfamXref TO COVER VARIANT SPLICE PROTEINS

hgsql proteins080707 -N -e \
'select pfamAC, variant, variant from spVariant, pfamXref where swissAC = parent and variant like "%-%" and swissAC=parent;' \
>pfamXrefVar.tab

hgLoadSqlTab -append proteins080707 pfamXref ~/kent/src/hg/lib/pfamXref.sql ./pfamXrefVar.tab

# SWITCH SYMBOLIC PROTEIN DATABASE LINKS (TBD)

# Ask system admin to switch the following symbolic database links:

       swissProt --> sp080707
       proteins  --> proteins080707

# Run some simple test on hgTracks, hgNear, hgGene, pbTracks, and pbGlobal  
# to make sure things are running OK.

# First check to see if pbGateway and pbGlobal are working.

# Then edit pbStamp.tab to adjust maximum y values for various stamps 
# and load it to pbStamp tables until all their scales look reasonable.
# For this particular release, no adjustment seems necessary.	
    
#########################################################
# CREATE REACTOME TABLES 

# Obtain the ucsc_events24.dat.txt and ucsc_entity24.dat from Reactome.
# Contact: Gopinathrao, Gopal [gopinath@cshl.edu]

ssh hgwdev
mkdir /cluster/store10/reactome/reactome24
rm /cluster/data/reactome
ln -s /cluster/store10/reactome/reactome24 /cluster/data/reactome
cd /cluster/data/reactome

# save these two .dat files to this subdirectory.

cp ucsc_events24.dat ucsc_events24.tab
cp ucsc_entity24.dat ucsc_entity24.tab

# edit two .tab files to get rid of top head lines.

hgsql proteins080707 < ~/kent/src/hg/lib/spReactomeEvent.sql
hgsql proteins080707 -e 'load data local infile "ucsc_events24.tab" into table spReactomeEvent'

hgsql proteins080707 < ~/kent/src/hg/lib/spReactomeId.sql
hgsql proteins080707 -e 'load data local infile "ucsc_entity24.tab" into table spReactomeId'

# Generate lists for Reactome to link back to UCSC site.

hgsql hg18 -N -e 'select kgId, kgXref.spId from kgXref, proteome.spReactomeId,proteome.spVariant where kgXref.spID=proteome.spVariant.variant and spReactomeId.spId=parent' |sort -u >hg18.lis

hgsql hg17 -N -e 'select kgId, kgXref.spId from kgXref, proteome.spReactomeId,proteome.spVariant where kgXref.spID=proteome.spVariant.variant and spReactomeId.spId=parent' |sort -u >hg17.lis

hgsql mm9 -N -e 'select kgId, kgXref.spId from kgXref, proteome.spReactomeId,proteome.spVariant where kgXref.spID=proteome.spVariant.variant and spReactomeId.spId=parent' |sort -u >mm9.lis
hgsql mm8 -N -e 'select kgId, kgXref.spId from kgXref, proteome.spReactomeId,proteome.spVariant where kgXref.spID=proteome.spVariant.variant and spReactomeId.spId=parent' |sort -u >mm8.lis

hgsql rn4 -N -e 'select kgId, kgXref.spId from kgXref,proteome.spReactomeId,proteome.spVariant where kgXref.spID=proteome.spVariant.variant and spReactomeId.spId=parent' |sort -u >rn4.lis

# !!! NOTE: Email the above 5 list files to Gopinathrao, Gopal [gopinath@cshl.edu]
# AFTER THE NEW PROTEIN DBs ARE RELEASED ON RR.

####################################################################################
# UPDATE HGNC DATA (DONE, Jim Kent, 7/11/08)
#
# Go to http://www.genenames.org/cgi-bin/hgnc_downloads.cgi
# and generate the Perl program by selecting the 25 variables we use
# and replace ~/src/hg/protein/getHgnc.pl with this new program and check it
# in.

# Get HGNC data
~/src/hg/protein/getHgnc.pl >hgnc.tab

hgsql proteins080707 -e 'delete from hgnc'
hgsql proteins080707 -e 'load data local infile "hgnc.tab" into table hgnc ignore 1 lines'

pbHgnc 080707
hgsql proteins080707 -e 'delete from hgncXref'
hgsql proteins080707 -e 'load data local infile "hgncXref.tab" into table hgncXref'

