Você está na página 1de 26

conectarPostgres<-function(){

library("RPostgreSQL")
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, dbname="tm", user="postgres",
password="postgres", host="localhost")
return(con)
}
executaConsulta<-function(strSql){
con <- conectarPostgres();
print(strSql)
rs<-dbGetQuery(con,strSql)
dbDisconnect(con)
return(rs)
}
#############################################
# ATUALIZACOES DE BASE
#############################################
armazenarProcesso <- function(chave, requisitor){

con <-conectarPostgres();
dados <- c(chave, requisitor);
salvarProcesso(con,dados);
dbDisconnect(con)
return (buscarProcessoCriado());

}
salvarProcesso<-function(conn,dados){

colunas <- "chavebusca, requisitor"

ins <- "insert into tm.processo "
ins <- paste(ins,"(",colunas,")",sep="")
ins <- paste(ins," values ",sep="")
ins <- paste(ins,"(","$SomeTag$",dados[1],"$SomeTag$",",","$SomeTag$",dados[2]
,"$SomeTag$",")",sep="")
#print(ins)
dbSendQuery(conn,ins)
}
salvarNomeImagem<-function(dados){

con <- conectarPostgres();

colunas <- c("nomeimagem","idprocesso")

ins <- "update tm.processo set "
ins <- paste(ins,colunas[1]," = ", "$SomeTag$", dados[1],"$SomeTag$", sep="")
ins <- paste(ins," where ",sep="")
ins <- paste(ins,colunas[2]," = ", dados[2],sep="")
#print(ins)
dbSendQuery(con,ins)

dbDisconnect(con)
}
salvarTermo<-function(conn,dados){

rs <- buscarTermo(dados[1]);

if (length(rs) <= 0){
colunas <- "descricao, xmluniprot"

ins <- "insert into tm.termo "
ins <- paste(ins,"(",colunas,")",sep="")
ins <- paste(ins," values ",sep="")
ins <- paste(ins,"(","$SomeTag$",dados[1],"$SomeTag$",",","$SomeTag$",dados[
2],"$SomeTag$",")",sep="")
#print(ins)
dbSendQuery(conn,ins)
return(buscarIdTermoPorDescricao(conn,dados[1]))
}else{
return(rs$idtermo[1])
}


}
salvarArtigo<-function(conn,dados){

rs <- buscarArtigoPorPmid(conn,dados[1]);

print(length(rs))

if (length(rs) > 0){
alterarArtigo(conn,dados);
}else{
colunas <- "pmid, nomeartigo, corpus, idprocesso"
ins <- "insert into tm.artigo "
print(ins)
ins <- paste(ins,"(",colunas,")",sep="")
ins <- paste(ins," values ",sep="")
ins <- paste(ins,"(",dados[1],",","$SomeTag$",dados[2],"$SomeTag$",",","$Som
eTag$",dados[3],"$SomeTag$",",",dados[4],")",sep="")
#print(ins)
dbSendQuery(conn,ins)
}

}
alterarArtigo<-function(conn,dados){
alt <- "update tm.artigo set "
alt <- paste(alt,"nomeartigo = ","$SomeTag$",dados[2],"$SomeTag$",",",sep="")
alt <- paste(alt,"corpus = ","$SomeTag$",dados[3],"$SomeTag$",",",sep="")
alt <- paste(alt,"idprocesso = ",dados[4],sep="")
alt <- paste(alt," where pmid = ",dados[1],sep="")
#print(alt)
dbSendQuery(conn,alt)
}
salvarTermoArtigo<-function(conn,idtermo,idartigo){
colunas<-"idtermo, idartigo"
ins <- "insert into tm.artigo_termo "
print(ins)
ins <- paste(ins,"(",colunas,")",sep="")
ins <- paste(ins," values ",sep="")
ins <- paste(ins,"(",idtermo,",",idartigo,")",sep="")

dbSendQuery(conn,ins)
}
salvarTermoListaArtigo<-function(conn,idTermo,idsArtigo){

pmids <- unlist(strsplit(idsArtigo, " "))
len<-length(pmids)
for(i in 1:len){

pmid<-pmids[i]

if (pmid != ""){
idArtigo <- ""
#busca o id do artigo de acordo com o pmid
rs <- buscarArtigoPorPmid(conn,pmid);
if (length(rs) > 0){
idArtigo <-rs[1]
}

if (idTermo != "" || idArtigo != ""){
busca<-buscarArtigoTermo(conn,idTermo,idArtigo);
#faz a insercao na tabela de artigo_termo
if (busca == 0){
salvarTermoArtigo(conn,idTermo,idArtigo);
}
}
}
}
}
salvarProteina <- function(dados){
conn <- conectarPostgres();
rs <- buscarProteinaPorAccession(conn,dados[2]);

if (length(rs) == 0){
colunas <- "nomeuniprot,iduniprot,nomecompleto"
ins <- "insert into tm.proteina "
ins <- paste(ins,"(",colunas,")",sep="")
ins <- paste(ins," values ",sep="")
ins <- paste(ins,"(","$SomeTag$",dados[1],"$SomeTag$",",","'",dados[2],"'","
,","$SomeTag$",dados[3],"$SomeTag$",")",sep="")
#print(ins)
dbSendQuery(conn,ins)
rs <- buscarProteinaPorAccession(conn,dados[2])
#print(rs)
dbDisconnect(conn);
if (length(rs) > 0){
return(rs$idproteina[1]);
}
}else{
dbDisconnect(conn);
return(rs$idproteina[1])
}
}
salvarTermoProteina <- function(dados){
conn <- conectarPostgres();

rs <- buscarTermoProteina(conn,dados[1], dados[2])
#print(rs)

if (length(rs) <= 0){
colunas <- "idproteina,idtermo"
ins <- "insert into tm.termo_proteina "
ins <- paste(ins,"(",colunas,")",sep="")
ins <- paste(ins," values ",sep="")
ins <- paste(ins,"(",dados[1],",",dados[2],")",sep="")
#print(ins)
dbSendQuery(conn,ins)
}
dbDisconnect(conn)
}
salvarPDB<-function(dados){

conn <- conectarPostgres();

rs <- buscarPDBPorId(conn,dados[1], dados[3]);
#print(rs)

if (length(rs) <= 0){

colunas <- "idpdb,descricao,idproteina"

ins <- "insert into tm.pdb "
ins <- paste(ins,"(",colunas,")",sep="")
ins <- paste(ins," values ",sep="")
ins <- paste(ins,"(","$SomeTag$",dados[1],"$SomeTag$",",","$SomeTag$",dados[
2],"$SomeTag$",",",dados[3],")",sep="")
print(ins);
dbSendQuery(conn,ins);
}
dbDisconnect(conn);

}
salvarLigante<-function(hetname,formula,chemicalid){

conn <- conectarPostgres();

rs <- buscarLigantePorHetname(conn,hetname);
#print(rs)

if (length(rs) <= 0){

colunas <- "hetname,formula,chemicalid"

ins <- "insert into tm.ligante "
ins <- paste(ins,"(",colunas,")",sep="")
ins <- paste(ins," values ",sep="")
ins <- paste(ins,"(","$SomeTag$",hetname,"$SomeTag$",",",
"$SomeTag$",formula,"$SomeTag$",",","$SomeTag$",chemicalid,"$So
meTag$",")",sep="")
#print(ins);
dbSendQuery(conn,ins);

rs <- buscarLigantePorHetname(conn,hetname);

dbDisconnect(conn);

if (length(rs) > 0){
return(rs$idligante[1]);
}

}else{
dbDisconnect(conn);
return(rs$idligante[1]);
}
}
salvarPdbLigante <-function(idpdb,idligante,idproteina){

conn <- conectarPostgres();

rs <- buscarPdbLigante(conn,idpdb,idligante,idproteina);
#print(rs)

if (length(rs) <= 0){

colunas <- "idpdb,idligante,idproteina"

ins <- "insert into tm.pdb_ligante "
ins <- paste(ins,"(",colunas,")",sep="")
ins <- paste(ins," values ",sep="")
ins <- paste(ins,"(","$SomeTag$",idpdb,"$SomeTag$",",",idligante,",",idprote
ina,")",sep="")
#print(ins);
dbSendQuery(conn,ins);
}
dbDisconnect(conn);
}
salvarDrugBank<-function(dados){

conn <- conectarPostgres();

rs <- buscarDrugBankPorId(conn,dados[1]);
print(rs)

if (length(rs) <= 0){
colunas <- "dbid, nome, hetid"
ins <- "insert into tm.drug_bank "
ins <- paste(ins,"(",colunas,")",sep="")
ins <- paste(ins," values ",sep="")
ins <- paste(ins,"(","$SomeTag$",dados[1],"$SomeTag$",",",
"$SomeTag$",dados[2],"$SomeTag$",",",
"$SomeTag$",dados[3],"$SomeTag$",")",sep="")

#print(ins);
dbSendQuery(conn,ins);
}
dbDisconnect(conn);
}
salvarLiganteDrugBank <-function(dbid,idligante){

conn <- conectarPostgres();

rs <- buscarLiganteDrugBank(conn,dbid,idligante);
#print(rs)

if (length(rs) <= 0){

colunas <- "dbid,idligante"

ins <- "insert into tm.ligante_drug "
ins <- paste(ins,"(",colunas,")",sep="")
ins <- paste(ins," values ",sep="")
ins <- paste(ins,"(","$SomeTag$",dbid,"$SomeTag$",",",idligante,")",sep="")
#print(ins);
dbSendQuery(conn,ins);
}
dbDisconnect(conn);
}
salvarFamiliaProteina <-function(idproteina,familia){

conn <- conectarPostgres();

ins <- "update tm.proteina set "
ins <- paste(ins,"familia = ","$SomeTag$",familia,"$SomeTag$",sep="")
ins <- paste(ins," where idproteina = ",idproteina, sep="")
print("atualizado!");
dbSendQuery(conn,ins);
dbDisconnect(conn);
}
salvarSimilaresTaxon <-function(idpdb, similares,taxsimilares){

conn <- conectarPostgres();

ins <- "update tm.pdb set "
ins <- paste(ins,"similares = ","$SomeTag$",similares,"$SomeTag$",sep="")
ins <- paste(ins,", taxsimilares = ","$SomeTag$",taxsimilares,"$SomeTag$",sep=
"")
ins <- paste(ins," where idpdb = ","$SomeTag$",idpdb,"$SomeTag$", sep="")
#print(ins);
dbSendQuery(conn,ins);
dbDisconnect(conn);
}
salvarOrganismoProteina <-function(idproteina,organismo){

conn <- conectarPostgres();

ins <- "update tm.proteina set "
ins <- paste(ins,"organismo = ","$SomeTag$",organismo,"$SomeTag$",sep="")
ins <- paste(ins," where idproteina = ",idproteina, sep="")
print("atualizado!");
dbSendQuery(conn,ins);
dbDisconnect(conn);
}
#############################################
# CONSULTAS
#############################################
buscarPDBPorProcesso<-function(idprocesso){

conn <- conectarPostgres();

strSql<- paste("select distinct pdb.idpdb as idpdb
from tm.artigo as a, tm.artigo_termo as at,
tm.termo_proteina as tp, tm.pdb as pdb
where a.idprocesso = ",idprocesso,
" and a.idartigo = at.idartigo
and at.idtermo = tp.idtermo
and tp.idproteina = pdb.idproteina",sep="")
print(strSql)

rs<-dbGetQuery(conn,strSql)
dbDisconnect(conn);

return(rs)
}
buscarLiganteDrugBank<-function(con,dbid,idligante){

strSql <- paste("select dbid from tm.ligante_drug where dbid = ",
"$SomeTag$", dbid, "$SomeTag$"," and ",
"idligante = ", idligante, sep="")
print(strSql)
rs<-dbGetQuery(con,strSql)
return(rs)

}
buscarDrugBankPorId<-function(con,dbid){

strSql <- paste("select dbid from tm.drug_bank where dbid = ", "$SomeTag$", db
id, "$SomeTag$", sep="")
print(strSql)
rs<-dbGetQuery(con,strSql)
return(rs)

}
buscarPdbLigante<-function(con,idpdb,idligante,idproteina){

strSql <- paste("select idligante from tm.pdb_ligante where idpdb = ", "$SomeT
ag$", idpdb, "$SomeTag$",
" and idligante = ", idligante,
" and idproteina = ", idproteina, sep="")
print(strSql)
rs<-dbGetQuery(con,strSql)
return(rs)
}
buscarLigantePorHetname<-function(con,hetname){
strSql <- paste("select * from tm.ligante where hetname = ", "$SomeTag$", hetn
ame, "$SomeTag$", sep="")
#print(strSql)
rs<-dbGetQuery(con,strSql)
return(rs)
}
buscarLigantes<-function(){

conn <- conectarPostgres();

strSql <- paste("select * from tm.ligante", sep="")
print(strSql)

rs<-dbGetQuery(conn,strSql)

dbDisconnect(conn);

return(rs)
}
buscarTermoProteina<-function(con,proteina,termo){
strSql <- paste("select * from tm.termo_proteina where idtermo = ",termo," and
idproteina = ",proteina,sep="")
#print(strSql)
rs<-dbGetQuery(con,strSql)
return(rs)
}
buscarProteinaPorAccession<-function(con,accession){
strSql <- paste("select * from tm.proteina where iduniprot = ","$SomeTag$",acc
ession,"$SomeTag$",sep="")
#print(strSql)
rs<-dbGetQuery(con,strSql)
return(rs)
}
buscarArtigoPorPmid <-function(con,pmid){
strSql <- paste("select * from tm.artigo where pmid = ",pmid,sep="")
#print(strSql)
rs<-dbGetQuery(con,strSql)

#rs[[1]] - id
#rs[[2]] - nomeartigo
#rs[[3]] - pmid
#rs[[4]] - corpus

return(rs)
}
buscarIdTermoPorDescricao <-function(conn,descricao){

strSql <- paste("select idtermo from tm.termo where descricao=","$SomeTag$",de
scricao,"$SomeTag$",sep="")
#print(strSql)
rs<-dbGetQuery(conn,strSql)
#print(length(rs))
if (length(rs)>0){
return(rs[1])
}else{
return("")
}

#rs[[1]] - idtermo

}
buscarProteinaPorNome <-function(nomeProteina){
con <- conectarPostgres();
strSql <- paste("select * from tm.proteina where nome_proteina=","$SomeTag$",s
ep="")
strSql <- paste(strSql,nomeProteina,sep="")
strSql <- paste(strSql,"$SomeTag$",sep="")
#print(strSql)
rs<-dbGetQuery(con,strSql)

#rs[[1]] - so os ids
#rs[[2]] - so os termos identificados no uniprot
#rs[[3]] - so os xmls retornados no uniprot

dbDisconnect(con)

return(rs)
}
buscarTodasProteinasPorIdProcesso <-function (idprocesso){

con <- conectarPostgres();
strSql <- paste("select distinct p.idproteina as idproteina, p.iduniprot as id
uniprot",
" from tm.artigo as a",
" , tm.artigo_termo as at ,tm.termo_proteina as tp",
" ,tm.proteina as p where
a.idartigo = at.idartigo",
" and at.idtermo = tp.idtermo",
" and p.organismo = ''",
" and tp.idproteina = p.idproteina",sep="")

if (idprocesso > 0){
strSql <- paste(strSql," and a.idprocesso = ",idprocesso,sep="")
}

print(strSql)
rs<-dbGetQuery(con,strSql)

#rs[[1]] - so os ids
#rs[[2]] - nomeuniprot so os termos identificados no uniprot
#rs[[3]] - nomecompleto
#rs[[5]] - iduniprot

dbDisconnect(con)
return(rs)
}
buscarTodasProteinas <-function(){
con <- conectarPostgres();
strSql <- "select * from tm.proteina"
#print(strSql)
rs<-dbGetQuery(con,strSql)

#rs[[1]] - so os ids
#rs[[2]] - nomeuniprot so os termos identificados no uniprot
#rs[[3]] - idpdb
#rs[[4]] - nomecompleto
#rs[[5]] - iduniprot

dbDisconnect(con)

return(rs)
}
buscarValoresTabelaTermos <-function(){
con <- conectarPostgres();
strSql <- "select * from tm.termo"
#print(strSql)
rs<-dbGetQuery(con,strSql)

#rs[[1]] - so os ids
#rs[[2]] - so os termos identificados no uniprot
#rs[[3]] - so os xmls retornados no uniprot

dbDisconnect(con)

return(rs)
}
buscarProcessoCriado <-function(){
con <- conectarPostgres();
strSql <- "select max(idprocesso) from tm.processo"
#print(strSql)
rs<-dbGetQuery(con,strSql)

dbDisconnect(con)
if (length(rs)>0){
return (rs[[1]])
}else
{
return("")
}
}
buscarArtigoTermo <-function(con,idtermo,idartigo){
strSql <- "select * from tm.artigo_termo where";
strSql <- paste(strSql," idtermo = ",idtermo,sep="");
strSql <- paste(strSql," and idartigo = ",idartigo,sep="")
#print(strSql)
rs<-dbGetQuery(con,strSql)
#print("antes if")
return(length(rs))
}
buscarPDBPorId<-function(con,idpdb,idproteina){
strSql <- paste("select idproteina from
tm.pdb where idproteina = ",idproteina,
" and idpdb = ","$SomeTag$",idpdb,"$SomeTag$",sep="")
#print(strSql)
rs<-dbGetQuery(con,strSql)
return(rs)
}
buscarLigantesPorProcesso<-function(idprocesso){

con <- conectarPostgres();

strSql <- paste("select distinct li.chemicalid as chemicalid, li.hetname as he
tname, li.idligante as idligante
from tm.artigo as a, tm.artigo_termo as at,
tm.termo_proteina as tp,tm.proteina as p,
tm.pdb_ligante as pdb, tm.ligante as li
where a.idprocesso = ",idprocesso,
" and a.idartigo = at.idartigo
and at.idtermo = tp.idtermo
and tp.idproteina = p.idproteina
and p.idproteina = pdb.idproteina
and pdb.idligante = li.idligante",sep="")

print(strSql)
rs<-dbGetQuery(con,strSql)

#rs[[1]] - li.chemicalid
#rs[[2]] - li.hetname

dbDisconnect(con)
return(rs)

}
buscarTermosFrequencia<-function(frequencia){

con <- conectarPostgres();

if (frequencia > 0){
frequencia <- paste(" and maisfrequentes.total >", frequencia," ",sep="")
}else{
frequencia <- " "
}

strSql <- paste("select distinct maisfrequentes.idtermo as idtermo, maisfreque
ntes.descricao, maisfrequentes.total
from tm.artigo_termo ati, (select a.idtermo as idtermo, b.desc
ricao as descricao, count(c.idtermo) as total
from tm.artigo_termo a, tm.termo b, tm.termo_proteina c, tm.pd
b d
where a.idtermo = b.idtermo
and b.idtermo = c.idtermo
and c.idproteina = d.idproteina
group by a.idtermo, b.descricao) maisfrequentes
where ati.idtermo = maisfrequentes.idtermo", frequencia,
"group by maisfrequentes.idtermo, maisfrequentes.descricao,mai
sfrequentes.total
order by maisfrequentes.total desc",sep="")

print(strSql)
rs<-dbGetQuery(con,strSql)

dbDisconnect(con)
return(rs)

}
buscarTermosFrequentesPorDoenca<-function(){

con <- conectarPostgres();

strSql <- paste("select b.descricao as descricao, pro.doenca, count(c.idtermo
) as total
from tm.artigo_termo a, tm.termo b, tm.termo_proteina c, tm.pd
b d, tm.processo pro, tm.artigo arg
where a.idtermo = b.idtermo
and b.idtermo = c.idtermo
and c.idproteina = d.idproteina
and a.idartigo = arg.idartigo
and arg.idprocesso = pro.idprocesso
and b.idtermo not in (select idtermo from tm.termosespurios)
group by a.idtermo, b.descricao, pro.doenca
order by count(c.idtermo) desc, pro.doenca",sep="")

print(strSql)
rs<-dbGetQuery(con,strSql)

dbDisconnect(con)
return(rs)

}
buscarTermosItensPDB<-function(){
con <- conectarPostgres();

strSql <- paste("select t.idtermo, e.descricao, count(distinct l.idligante) as
total
from tm.termo e, tm.termo_proteina t, tm.pdb p, tm.pdb_ligante
l
where e.idtermo = t.idtermo
and t.idproteina = p.idproteina
and p.idproteina = l.idproteina
group by t.idtermo, e.descricao
order by count(distinct l.idligante) desc",sep="")

print(strSql)
rs<-dbGetQuery(con,strSql)

dbDisconnect(con)
return(rs)
}
buscarTermosComLigante<-function(){
con <- conectarPostgres();

strSql <- paste("select distinct e.idtermo as idtermo, e.descricao as descrica
o, freq.frequencia as total
from tm.termo e,
(select t.idtermo, count(distinct dbid) as frequencia
from tm.termo_proteina t, tm.pdb p, tm.pdb_ligante l, tm.ligan
te_drug g
where t.idproteina = p.idproteina
and p.idproteina = l.idproteina
and l.idligante = g.idligante
group by t.idtermo
order by count(distinct dbid) desc) freq
where e.idtermo = freq.idtermo
order by freq.frequencia desc",sep="")

print(strSql)
rs<-dbGetQuery(con,strSql)

dbDisconnect(con)
return(rs)
}
buscarProteinasFrequentesOrganismo<-function(){
con <- conectarPostgres();

##As 100 mais frequentes proteinas

strSql <- paste("select distinct pa.idproteina, pa.iduniprot
from tm.drug_bank db,
tm.ligante_drug ld,
tm.pdb_ligante pl,
tm.pdb p,
tm.termo_proteina tp,
tm.proteina pa
where db.dbid = ld.dbid
and ld.idligante = pl.idligante
and pl.idpdb = p.idpdb
and p.idproteina = tp.idproteina
and tp.idproteina = pa.idproteina",sep="")
#and tp.idtermo not in
#(select idtermo from tm.termosespurios)
#and tp.idtermo in
#(select idtermo
#from tm.contagemfrequenciatermos where total > 224)",sep="")

print(strSql)
rs<-dbGetQuery(con,strSql)

dbDisconnect(con)
return(rs)
}
buscarPDBsSimilares<-function(min,max,limite){

con <- conectarPostgres();

strSql <- paste("select distinct tp.idproteina, p.idpdb, p.descricao,
p.similares, p.taxsimilares
from tm.drug_bank db,
tm.ligante_drug ld,
tm.pdb_ligante pl,
tm.pdb p,
tm.termo_proteina tp,
tm.proteina pa
where db.dbid = ld.dbid
and ld.idligante = pl.idligante
and pl.idpdb = p.idpdb
and p.idproteina = tp.idproteina
and tp.idproteina = pa.idproteina
and p.similares <> ''",sep="")

if (limite > 0){
strSql<-paste(strSql," limit ",limite,sep="")
}

print(strSql)
rs<-dbGetQuery(con,strSql)

dbDisconnect(con)
return(rs)
}
buscarProteinasFrequentesUniprot<-function(){
con <- conectarPostgres();

##As 100 mais frequentes proteinas

strSql <- paste("select distinct pa.idproteina, pa.iduniprot
from tm.drug_bank db,
tm.ligante_drug ld,
tm.pdb_ligante pl,
tm.pdb p,
tm.termo_proteina tp,
tm.proteina pa
where db.dbid = ld.dbid
and ld.idligante = pl.idligante
and pl.idpdb = p.idpdb
and p.idproteina = tp.idproteina
and tp.idproteina = pa.idproteina",sep="")

print(strSql)
rs<-dbGetQuery(con,strSql)

dbDisconnect(con)
return(rs)
}
buscarTermosComPDB<-function(){
con <- conectarPostgres();
strSql <- paste("select b.descricao as descricao
from tm.artigo_termo a, tm.termo b, tm.termo_proteina c, tm.pd
b d
where a.idtermo = b.idtermo
and b.idtermo = c.idtermo
and c.idproteina = d.idproteina
and b.idtermo not in (select idtermo from tm.termosespurios)",
sep="")
#print(strSql)
rs<-dbGetQuery(con,strSql)
dbDisconnect(con)
return(rs)
}
buscarTermo<-function(termo){
con <- conectarPostgres();
strSql <- paste("select idtermo from
tm.termo where upper(descricao) = upper(","$SomeTag$",termo,"$
SomeTag$",")",sep="")
#print(strSql)
rs<-dbGetQuery(con,strSql)
dbDisconnect(con)
return(rs)
}
termosMaisFrequentes<-function(valor){
con <- conectarPostgres();

strSql <- paste(" select distinct maisfrequentes.idtermo, maisfrequentes.descr
icao as termo, maisfrequentes.total
from tm.artigo_termo ati,
(select a.idtermo as idtermo, b.descricao as descricao, count(
c.idtermo) as total
from tm.artigo_termo a, tm.termo b, tm.termo_proteina c, tm.pd
b d
where a.idtermo = b.idtermo and
b.idtermo = c.idtermo and
c.idproteina = d.idproteina and
a.idtermo not in (select idtermo from tm.termosespurios)
group by a.idtermo, b.descricao) maisfrequentes
where ati.idtermo = maisfrequentes.idtermo
and maisfrequentes.total >=",valor,
"group by maisfrequentes.idtermo, maisfrequentes.descricao,mai
sfrequentes.total
order by maisfrequentes.total desc",sep="")

print(strSql)
rs<-dbGetQuery(con,strSql)

dbDisconnect(con)
return(rs)
}
termosMaisFrequentesSemCount<-function(valor){
con <- conectarPostgres();

strSql <- paste("select distinct a.idtermo as idtermo, b.descricao as descrica
o
from tm.artigo_termo a, tm.termo b, tm.termo_proteina c, tm.pd
b d
where a.idtermo = b.idtermo and
b.idtermo = c.idtermo and
c.idproteina = d.idproteina and
a.idtermo not in (select idtermo from tm.termosespurios) and
a.idtermo in
( select distinct maisfrequentes.idtermo
from tm.artigo_termo ati,
(select a.idtermo as idtermo, b.descricao as descricao, count(
c.idtermo) as total
from tm.artigo_termo a, tm.termo b, tm.termo_proteina c, tm.pd
b d
where a.idtermo = b.idtermo and
b.idtermo = c.idtermo and
c.idproteina = d.idproteina and
a.idtermo not in (select idtermo from tm.termosespurios)
group by a.idtermo, b.descricao) maisfrequentes
where ati.idtermo = maisfrequentes.idtermo
and maisfrequentes.total >=", valor,")",sep="")

print(strSql)
rs<-dbGetQuery(con,strSql)

dbDisconnect(con)
return(rs)
}
artigosTermos<-function(){
con <- conectarPostgres();

##DB70 - funcao get2
#'IgG',
#'HLA','ATP','NS4B','falciparum','DENV','GFP','RNase','cDNA','NS3p','NS2B','TN
F','mRNA',
#'CDC','IgG2a','anhydrase','dhfr','AChE','CSF','DSS','NS4b','VEGFR2','LDH','MA
PK','CRP',
#'RNAse','HPLC','NOS','oxygenase-1','GPI','DHFR','kdr','caspase-3','MIF','sial
idase','DRB1',
#'microglobulin','beta2-microglobulin','AMA1','das','cyclin','RNAs','RPMI','Az
urin','HEK','PLC',
#'HLA-DR','RNS','HRP2','DCs','IgM','GST','DEN-1','HbA','JNK','MDA','CCP','MAPK
14','beta-galactosidase','VEGF',
#'AMA-1','HSP','APP','CYP','Arg','ICAM-1','RBCs','cGMP','MSP1','NS2A'

strSql <- paste("SELECT a.idartigo, get3(a.idartigo) as termos
FROM tm.artigo_termo a, tm.termo b
WHERE a.idtermo = b.idtermo and
b.descricao in ('NS4b','NS4B','ITS-1','ITS-2','CDC','CSF','GFP
','HEK','IgH','mth','Rab','CAM','thymidylate','5-phosphate','gfp','Env'
'HLA','IgG','RNase','TAT','anhydrase','ribosyl','FKBP','RNAse'
,'SRP','SRP54','acetylase','ATP','EPs','GAG','gamma-chain','GPx'
'GPX','GST','kalata','mevalonate','NOS','NS2B','NS3p','EPS',
'MLN','Pak','PIK','topoisomerase','alpha-amylase','FabI','GDH'
,'LTF','PAK','PyL','CSE','dUTP','Epstein','G-CSF',
'HPD','jacalin','LDH','MEK','microglobulin','MIF','MMP','mRNA'
,'MTase','PLC','RNS','TPI','Xaa','CMR','ICAM',
'falciparum','DENV','IgG2a')",sep="")

print(strSql)
rs<-dbGetQuery(con,strSql)

dbDisconnect(con)
return(rs)
}
removeTermoProteina<-function(idtermo){

con <- conectarPostgres();

colunas <- c("nomeimagem","idprocesso")

del <- paste("delete tm.termo_proteina where idtermo = ", idtermo,sep="")
print(del)
dbSendQuery(con,del)

dbDisconnect(con)
}
removeProteina<-function(idtermo){

con <- conectarPostgres();

del <- paste("delete tm.termo where idtermo = ", idtermo,sep="")
print(del)
dbSendQuery(con,del)

dbDisconnect(con)
}
normalizarTermo <-function(idtermo,descricao){

conn <- conectarPostgres();

ins <- "update tm.termo set "
ins <- paste(ins,"descricao = ","$SomeTag$",descricao,"$SomeTag$",sep="")
ins <- paste(ins," where idtermo = ",idtermo, sep="")
print("atualizado!");
dbSendQuery(conn,ins);
dbDisconnect(conn);
}
##############################################################
#
# CONSULTAS PARA MONTAGEM DOS GRAFOS
#
##############################################################
dadosGrafoTermoxPDBPorFrequenciaMinMax<-function(min,max){
con <- conectarPostgres();

strSql <- paste("select distinct upper(b.descricao) as nometermo, d.descricao
as nomepdb, d.idpdb
from tm.artigo_termo a, tm.termo b, tm.termo_proteina c, tm.pd
b d
where a.idtermo = b.idtermo and
b.idtermo = c.idtermo and
c.idproteina = d.idproteina and
a.idtermo in
(select idtermo
from tm.contagemfrequenciatermos
where (total <= ",min," and total >= ",max,"))
order by nometermo",sep="")

print(strSql)
rs<-dbGetQuery(con,strSql)

dbDisconnect(con)
return(rs)
}
artigosTermosChagas<-function(){
con <- conectarPostgres();

strSql <- paste("SELECT a.idartigo, get3Chagas(a.idartigo) as termos
FROM tm.artigo_termo a,
tm.termo b,
tm.artigo e,
tm.processo f
WHERE a.idtermo = b.idtermo and
b.descricao in ('Chagas','cruzi','benznidazol','PCR','benznida
zole','IgG','follow-up','ECG','III',
'IgM','trypomastigote','non','amastigote','Benznidazol','chaga
s','Benznidazole',
'TNF','kDa','parasitaemia','das','min','IFA','HIV','chagasi',
'HLA','IHA','Andean','host-parasite','EKG','Chaco','C57BL/6','
ChD',
'PBM','hemoculture','vector-borne','PBMC','rangeli','BALB','ch
agasics','IgG2',
'cDNA','mRNA','IFAT','synthase','spp','CARD','APA','IgA',
'amiodarone','BALB/c','IgG1','kDNA','sialidase','IIF','ICD','N
YHA',
'zymodeme','TcI','brucei','cruzipain','QRS','BNP','IFN',
'pre','ESA','trans-sialidase','CNS',
'TcII','CHF','cyto','tid',
'situ','large-scale','CHD','microhematocrit',
'IId','hemiblock','periurban',
'healthcare','HLA-DR','mmHg') and
a.idartigo = e.idartigo
and e.idprocesso = f.idprocesso
and f.idprocesso = 4
and b.idtermo not in (select idtermo from tm.termosespurios)",
sep="")

print(strSql)
rs<-dbGetQuery(con,strSql)

dbDisconnect(con)
return(rs)
}
artigosTermosDengue<-function(){
con <- conectarPostgres();

strSql <- paste("SELECT a.idartigo, get3Dengue(a.idartigo) as termos
FROM tm.artigo_termo a,
tm.termo b,
tm.artigo e,
tm.processo f
WHERE a.idtermo = b.idtermo and
b.descricao in ('DENV','DHF','aegypt','IgM','PCR','vitro','DSS
','DENV-2','IgG','DEN-2','RT-PCR','mosquito-born','mosquito-borne','DENV-1','DEN
-1','DENV-3','DHF/DSS','cDNA','prM','DEN-4','DENV-4','IFN','DEN-3','NS2B','arthr
opod-born','arthropod-borne','vector-borne','DEN2',
'Escherichia','HLA','BHK','RNAs','DENV2','long-term','nia','TN
F','non','DEN4','MAb','ADE','mRNA','Lanka','Abs','BALB/c',
'HepG2','WNV','MAbs','acute-phase','UTR','PFU','min','NS4B','P
BMC','NS2A','PRNT','kDa','ovitrap','follow-up','type-2','DENV1',
'DENVs','LLC-MK2','PDK','BHK-21','NS4A','helicase','sero','DEN
1','DCs','CTL','NS2B-NS3','DF/DHF','ovitraps','large-scale',
'NGC','mAb','AST','pre-membrane','high-throughput','tick-borne
','ATP','ISA','factor-alpha',
'CHIK','CDC','DC-SIGN','JEV') and
a.idartigo = e.idartigo
and e.idprocesso = f.idprocesso
and f.idprocesso = 5",sep="")

print(strSql)
rs<-dbGetQuery(con,strSql)

dbDisconnect(con)
return(rs)
}
artigosTermosMalaria<-function(){
con <- conectarPostgres();

strSql <- paste("SELECT a.idartigo, get3Malaria(a.idartigo) as termos
FROM tm.artigo_termo a,
tm.termo b,
tm.artigo e,
tm.processo f
WHERE a.idtermo = b.idtermo and
b.descricao in ('falciparum','vivax','vitro','parasitaemia','b
erghei','PCR','dox','yoeli',
'yoelii','IgG','sulfadoxin','chabaudi','follow-up','MSP','sulf
adoxine-pyrimethamine','HIV',
'RBC','DDT','cyt','ovale','long-term','TNF','ANKA','stephensi'
,
'ITN','che','IPT','tri','Tha','C57BL','mRNA',
'knowlesi','spp','Thai','artemether-lumefantrine','C57BL/6','A
CT','ITNs','Faso',
'IgM','tid','synthase','kDa','RDT','MSP1','RII','BALB/c',
'HRP','MSP-1','CSP','RBCs','multi','anka','cDNA',
'sulphadoxine-pyrimethamine','IFA','Escherichia','Falciparum',
'RTS,S','large-scale','Kenyan','half-life','histidine-rich','I
gG1','IFN','glycaemia',
'HLA','host-parasite','G6PD','hemozoin','parasitaemias','vecto
r-born','EMP1','CAM-1',
'PfEMP1','vector-borne','hypoglycaemia','RIII','ICAM-1','IgG3'
,
'pre','IPTp','CDC','anc','MHC','SNP','high-risk','rRNA',
'crt','factor-a','short-term','LDH','chloroquin','genome-wide'
,'aegypti','factor-alpha',
'PbA','IgG2','CBA','NPs','CSF','healthcare','ICT') and
a.idartigo = e.idartigo
and e.idprocesso = f.idprocesso
and f.idprocesso = 7",sep="")

print(strSql)
rs<-dbGetQuery(con,strSql)

dbDisconnect(con)
return(rs)
}
dadosGrafoTermoxPDBPorTermos<-function(termos){
con <- conectarPostgres();

strSql <- paste("select distinct a.idtermo as idtermo, upper(b.descricao) as n
ometermo, d.idpdb, d.idpdb || ':' || d.descricao as nomepdb,
'http://www.pdb.org/pdb/explore/explore.do?structureId=' || d.
idpdb as url
from tm.artigo_termo a, tm.termo b, tm.termo_proteina c, tm.pd
b d
where a.idtermo = b.idtermo and
b.idtermo = c.idtermo and
c.idproteina = d.idproteina and
upper(b.descricao) in upper((", termos , "))
order by a.idtermo",sep="")

print(strSql)
rs<-dbGetQuery(con,strSql)

dbDisconnect(con)
return(rs)
}
dadosGrafoFarmacoArtigo<-function(limite){

strSql <- paste("select distinct db.dbid as dbid, db.nome as nomeFarmaco, 'PMI
D:' || a.pmid as pmid
from tm.drug_bank db,
tm.ligante_drug ld,
tm.pdb_ligante pl,
tm.pdb p,
tm.termo_proteina tp,
tm.artigo_termo ar,
tm.artigo a
where db.dbid = ld.dbid
and ld.idligante = pl.idligante
and pl.idpdb = p.idpdb
and p.idproteina = tp.idproteina
and tp.idtermo = ar.idtermo
and ar.idartigo = a.idartigo
and a.pmid = '11369871'
and tp.idtermo not in
(select idtermo
from tm.termosespurios
)
and tp.idtermo in
(select idtermo
from tm.contagemfrequenciatermos
where total>=11
)order by pmid",sep="")
if (limite > 0){
strSql<-paste(strSql," limit ",limite,sep="")
}
return(executaConsulta(strSql))
}
dadosGrafoArtigosLigadosPeloTermo<-function(minTermos,idartigo){

strSql <- paste("select distinct 'PMID:' || listaartigo1.pmid1 as artigo1, 'ht
tp://www.ncbi.nlm.nih.gov/pubmed/?term=' || listaartigo1.pmid1 as url1,
listaartigo1.termo1, 'PMID:' || listaartigo2.pmid2 as artigo2,
'http://www.ncbi.nlm.nih.gov/pubmed/?term=' || listaartigo2.pmid2 as url2
from
(select a.idartigo as idartigo1, t.descricao as termo1, a.pmid
as pmid1
from
tm.artigo_termo ar,
tm.artigo a,
tm.termo t
where ar.idartigo = a.idartigo
and ar.idtermo = t.idtermo
and ar.idtermo in
(select idtermo
from tm.contagemfrequenciatermos
where total >=",minTermos,")) as listaartigo1,
(select a.idartigo as idartigo2, t,descricao as termo2, a.pmid
as pmid2
from
tm.artigo_termo ar,
tm.artigo a,
tm.termo t
where ar.idartigo = a.idartigo
and ar.idtermo = t.idtermo
and ar.idtermo in
(select idtermo
from tm.contagemfrequenciatermos
where total >=",minTermos,")) as listaartigo2
where listaartigo1.idartigo1 <> listaartigo2.idartigo2
and listaartigo1.termo1 = listaartigo2.termo2",sep="")

if (idartigo > 0){
strSql<-paste(strSql," and listaartigo1.idartigo1 = ", idartigo, sep="")
}

return(executaConsulta(strSql))
}
dadosGrafoTermoFrequenteDoenca<-function(minTermos,maxTermos){

strSql <- paste("select distinct b.idtermo, b.descricao as termo, pro.doenca a
s doenca
from tm.artigo_termo a,
tm.termo b,
tm.artigo art,
tm.processo pro
where a.idtermo = b.idtermo
and a.idartigo = art.idartigo
and art.idprocesso = pro.idprocesso
and a.idtermo in
(select idtermo
from tm.contagemfrequenciatermos
where (total <=",maxTermos," and total >=",minTermos,"))
order by termo",sep="")

return(executaConsulta(strSql))
}
dadosGrafoProteinaFrequenteDoenca<-function(minTermos){
strSql <- paste("select distinct p.nomeuniprot as proteina, pro.doenca as doe
nca
from tm.frequenciaproteinas freq,
tm.proteina p,
tm.termo_proteina tp,
tm.artigo_termo atm,
tm.artigo art,
tm.processo pro
where freq.idproteina = p.idproteina
and p.idproteina = tp.idproteina
and tp.idtermo = atm.idtermo
and atm.idartigo = art.idartigo
and art.idprocesso = pro.idprocesso
and freq.count >=",minTermos,sep="")

return(executaConsulta(strSql))
}
dadosGrafoLiganteDoenca<-function(val,min,max,limite){
strSql<-paste("select distinct tl.chemicalid, tl.hetname, pro.doenca as doenca
, freq.count
from tm.ligante tl,
tm.pdb_ligante pl,
tm.pdb p,
tm.termo_proteina tp,
tm.artigo_termo ar,
tm.artigo a,
tm.processo pro,
tm.frequencialigantes freq
where tl.idligante = pl.idligante
and pl.idpdb = p.idpdb
and p.idproteina = tp.idproteina
and tp.idtermo = ar.idtermo
and ar.idartigo = a.idartigo
and a.idprocesso = pro.idprocesso
and tl.idligante = freq.idligante
and (freq.count >=",val," or (freq.count >=",min," and freq.coun
t <= ",max,") )
order by freq.count desc",sep="")
if (limite > 0){
strSql<-paste(strSql," limit ",limite,sep="")
}

return(executaConsulta(strSql))
}
dadosGrafoFarmacoDoenca<-function(min,max,limite){
strSql<-paste("select distinct db.dbid, db.nome as nomeFarmaco, pro.doenca as
doenca
from tm.drug_bank db,
tm.ligante_drug ld,
tm.pdb_ligante pl,
tm.pdb p,
tm.termo_proteina tp,
tm.artigo_termo ar,
tm.artigo a,
tm.processo pro
where db.dbid = ld.dbid
and ld.idligante = pl.idligante
and pl.idpdb = p.idpdb
and p.idproteina = tp.idproteina
and tp.idtermo = ar.idtermo
and ar.idartigo = a.idartigo
and a.idprocesso = pro.idprocesso
and tp.idtermo in
(select idtermo
from tm.contagemfrequenciatermos where total >=",min," and total
<=",max,")",sep="")
if (limite > 0){
strSql<-paste(strSql," limit ",limite,sep="")
}
return(executaConsulta(strSql))
}
dadosGrafoProteinaFamilia<-function(valorFamilia, limite){

strSql<-paste("select distinct pa.nomeuniprot as proteina, substr(pa.familia,3
3,length(pa.familia)) as familia, frequenciafamilias.total
from
tm.termo_proteina tp,
tm.proteina pa,
(select distinct pa.familia as familia, count(*) as total
from tm.proteina pa, tm.termo_proteina tp
where pa.idproteina = tp.idproteina
and tp.idtermo in
(select idtermo
from tm.contagemfrequenciatermos
)
group by pa.familia
order by count(*) desc) as frequenciafamilias
where tp.idproteina = pa.idproteina
and pa.familia is not null
and pa.familia = frequenciafamilias.familia
and frequenciafamilias.total >=",valorFamilia,"order by frequen
ciafamilias.total desc",sep="")

if (limite > 0){
strSql<-paste(strSql," limit ",limite,sep="")
}

return(executaConsulta(strSql))
}
dadosGrafoProteinaOrganismo<-function(valorOrganismo, limite){

strSql<-paste("select distinct pa.nomeuniprot as proteina, pa.organismo as org
anismo, frequenciaorganismo.total
from
tm.termo_proteina tp,
tm.proteina pa,
(select distinct pa.organismo as organismo, count(*) as total
from tm.proteina pa, tm.termo_proteina tp
where pa.idproteina = tp.idproteina
and tp.idtermo in
(select idtermo
from tm.contagemfrequenciatermos)
group by pa.organismo
order by count(*) desc) as frequenciaorganismo
where tp.idproteina = pa.idproteina
and pa.organismo is not null
and pa.organismo <> ''
and pa.organismo like '%SAR%'
and pa.organismo = frequenciaorganismo.organismo
and frequenciaorganismo.total =",valorOrganismo," order by freq
uenciaorganismo.total desc",sep="")

if (limite > 0){
strSql<-paste(strSql," limit ",limite,sep="")
}

return(executaConsulta(strSql))
}
termosChagas<-function(){

strSql <- "SELECT upper(b.descricao) as termos
FROM tm.artigo_termo a, tm.termo b, tm.artigo c, tm.processo d
WHERE a.idtermo = b.idtermo and
a.idartigo = c.idartigo and
c.idprocesso = d.idprocesso and
d.doenca = 'Chagas' and
a.idtermo not in (select idtermo from tm.termosespurios)"

return(executaConsulta(strSql))
}
termosMalaria<-function(){

strSql <- "SELECT upper(b.descricao) as termos
FROM tm.artigo_termo a, tm.termo b, tm.artigo c, tm.processo d
WHERE a.idtermo = b.idtermo and
a.idartigo = c.idartigo and
c.idprocesso = d.idprocesso and
d.chavebusca = 'malaria[ti] 1960:2014[dp]' and
a.idtermo not in (select idtermo from tm.termosespurios)"

return(executaConsulta(strSql))
}
termosDengue<-function(){

strSql <- "SELECT upper(b.descricao) as termos
FROM tm.artigo_termo a, tm.termo b, tm.artigo c, tm.processo d
WHERE a.idtermo = b.idtermo and
a.idartigo = c.idartigo and
c.idprocesso = d.idprocesso and
d.doenca = 'Dengue' and
a.idtermo not in (select idtermo from tm.termosespurios)"

return(executaConsulta(strSql))
}
buscarTodosTermos <-function(){

strSql <- paste("select distinct c.idtermo, upper(c.descricao) as termo,f.doen
ca
from tm.termo c,
tm.artigo_termo d, tm.artigo e, tm.processo f
where c.idtermo = d.idtermo
and d.idartigo = e.idartigo
and e.idprocesso = f.idprocesso",sep="")

return(executaConsulta(strSql))

}
buscarPtnsTermosNormalizados <-function(){

strSql <- paste("select distinct p.idproteina as idproteina, p.iduniprot as id
uniprot
from tm.termo_proteina a, tm.proteina p
where a.idproteina = p.idproteina
and a.idtermo in
(select idtermo from tm.termosselecionadosnormalizacao)",sep="")

return(executaConsulta(strSql))

}
buscarLigantesNormalizados<-function(){

con <- conectarPostgres();

strSql <- paste("select distinct li.chemicalid as chemicalid, li.hetname as he
tname, li.idligante as idligante
from tm.termo_proteina as tp,
tm.pdb_ligante as pdb, tm.ligante as li
where tp.idproteina = pdb.idproteina
and pdb.idligante = li.idligante
and tp.idtermo in(
select idtermo
from tm.termosselecionadosnormalizacao
)
",sep="")

print(strSql)
rs<-dbGetQuery(con,strSql)

dbDisconnect(con)
return(rs)

}
buscaProteinasNormalizados<-function(){
strSql = "select distinct p.idproteina as idproteina, p.iduniprot as iduniprot
from tm.processo as pro JOIN tm.artigo as a USING (idprocesso)
JOIN tm.artigo_termo at USING (idartigo)
JOIN tm.termo as t USING (idtermo)
JOIN tm.termo_proteina as tp USING (idtermo)
JOIN tm.proteina as p USING (idproteina)
JOIN tm.pdb USING (idproteina)
JOIN tm.pdb_ligante pl USING (idpdb, idproteina)
JOIN tm.ligante as l USING (idligante)
JOIN tm.ligante_drug as ld USING (idligante)
JOIN tm.drug_bank as db USING (dbid)
where
p.organismo is null
and t.idtermo in
(select idtermo
from tm.termosselecionadosnormalizacao where idtermo not in (select
idtermo from tm.termosespurios)
)"

return(executaConsulta(strSql))
}
buscaProteinaParaFamilia<-function(){

strSql<-"select distinct p.idproteina as idproteina, p.iduniprot as iduniprot
from tm.processo as pro JOIN tm.artigo as a USING (idprocesso)
JOIN tm.artigo_termo at USING (idartigo)
JOIN tm.termo as t USING (idtermo)
JOIN tm.termo_proteina as tp USING (idtermo)
JOIN tm.proteina as p USING (idproteina)
JOIN tm.pdb USING (idproteina)
JOIN tm.pdb_ligante pl USING (idpdb, idproteina)
JOIN tm.ligante as l USING (idligante)
JOIN tm.ligante_drug as ld USING (idligante)
JOIN tm.drug_bank as db USING (dbid)
where
t.idtermo in
(select idtermo
from tm.termosselecionadosnormalizacao where idtermo not in (select idtermo
from tm.termosespurios)
)"

return(executaConsulta(strSql))
}

Você também pode gostar