/************************************************************************* * Ultima Modificacao: 28/04/2005 - Renatha Oliva Capua * * Last edited on 2005-10-15 10:17:13 by stolfi * * * * Universidade Federal Fluminense * * Mestrado em Computacao * * Aluna: Renatha Oliva Capua * * Orientadores: Helena Cristina da Gama Leitao * * Jorge Stolfi * * * * prob_tuplas.c - Programa que pega as frequencias extraidas das * * sequencias de treinamento e transforma em probabilidades. * *************************************************************************/ #include "prob_tuplas.h" int main(int argc, char **argv){ int tam_vfreq; unsigned long tot_tuplas; /*MUDAR PARA LONG INT*/ int e_k; tfreq *vfreq; /* vetor com as frequencias para cada k-tupla na base de treinamento */ char **evento; /* vetor para guardar os posiveis eventos */ float tempoi_CPU, tempof_CPU; /*variaveis para pegar o tempo de CPU*/ if (argc != 3) { printf ("\n use: prob_tuplas \n"); return (-1); }//end if argc /* para comecar a contar o tempo de execucao */ times(&tempoi); tempoCPU(tempoi, &tempoi_CPU); tam_vfreq = (int)pow(4,K); /* tam = 4^k */ /*procedimento para ler o arquivo de parametros*/ ler_param(argv[1], &evento, &e_k); /*para montar uma estrutura com as frequencias das triplas obtidas na fase de treinamento */ if ((alocar_vfreq(&vfreq, tam_vfreq, evento, e_k)) != 0){ exit(1); } /*carrega as frequencias do arquivo em vfreq*/ carregar_vfreq(argv[2],&vfreq,tam_vfreq, &tot_tuplas); /*transforma as frequencias em probabilidades*/ transforma_prob(&vfreq, tam_vfreq, tot_tuplas); /*grava vfreq em um arquivo*/ gravar_vfreq(argv[2],vfreq,tam_vfreq); /*libera memoria ocupada por vfreq*/ desaloca_vfreq(&vfreq,tam_vfreq); /*para finalizar o tempo de execucao*/ times(&tempof); tempoCPU(tempof, &tempof_CPU); printf("Operacao concluida com sucesso!\n"); printf("\ntempo total de CPU: %.3f\n\n", ((tempof_CPU - tempoi_CPU)/60)); return(0); } /************************************************************************* * Procedimento que retorna o rotulo de uma tupla a partir de sua posicao * * em vfreq * *************************************************************************/ char *retorna_tupla(int indice){ int cnt, resto, dividendo; char *tupla; tupla = (char*)malloc((K+1) * sizeof(char)); strcpy(tupla,"\0"); resto = 0; dividendo = 0; dividendo = indice; cnt = 0; tupla[K] = '\0'; //printf("%d",K); fflush(stdout); for(cnt = (K - 1); cnt >= 0; cnt--){ resto = dividendo % 4; switch(resto){ case 0: tupla[cnt] = 'A'; break; case 1: tupla[cnt] = 'T'; break; case 2: tupla[cnt] = 'C'; break; case 3: tupla[cnt] = 'G'; break; default: tupla[cnt] = '*'; break; }//fim do switch dividendo = dividendo / 4; } //*tupla = dividendo; return(tupla); }//fim do retorna_tupla /************************************************************************* * Procedimento que retorna o rotulo de um evento a partir de sua posicao * * em vfreq * *************************************************************************/ char *retorna_evento(int indice){ int cnt, resto, dividendo; char *evento; evento = (char*)malloc((K+1) * sizeof(char)); strcpy(evento,"\0"); resto = 0; dividendo = 0; dividendo = indice; evento[K] = '\0'; for(cnt = (K - 1); cnt >= 0; cnt--){ resto = dividendo % 4; switch(resto){ case 0: evento[cnt] = 'I'; break; case 1: evento[cnt] = 'E'; break; case 2: evento[cnt] = 'F'; break; case 3: evento[cnt] = 'G'; break; default: evento[cnt] = '*'; break; }//fim do switch dividendo = dividendo / 4; } //*evento = dividendo; return(evento); }//fim do retorna_evento /************************************************************************* * Procedimento que retorna o indice da posicao de tupla em vfreq * *************************************************************************/ int retorna_indice(char *elemento){ int indice, pos; indice = 0; pos = K - 1; while (*elemento != '\0'){ switch(*elemento){ case 'A': indice = indice + (((int)pow(4,pos)) * 0); break; case 'T': indice = indice + (((int)pow(4,pos)) * 1); break; case 'C': indice = indice + (((int)pow(4,pos)) * 2); break; case 'G': indice = indice + (((int)pow(4,pos)) * 3); break; case 'I': indice = indice + (((int)pow(4,pos)) * 0); break; case 'E': indice = indice + (((int)pow(4,pos)) * 1); break; case 'F': indice = indice + (((int)pow(4,pos)) * 2); break; default: return (-1); //case 'G': indice = indice + (((int)pow(4,pos)) * 3); break; }//fim do switch elemento++; pos--; }//fim do while return(indice); }//fim do indice /**************************************************************************** * Procedimento para ler o arquivo com os eventos considerados e carregar no * * vetor event. * ****************************************************************************/ void ler_param(char *nome_arq,char ***evento,int *e_k){ int cnt; char letra; char **evento_aux; FILE *pt_param; abrir_arq(nome_arq,&pt_param,"r"); /*para pegar o cabecalho do arquivo*/ fscanf(pt_param,"%c",&letra); consumir_cabecalho(&pt_param, &letra); ungetc(letra, pt_param); // Bota a letra de volta no arquivo. fscanf(pt_param,"%d", e_k); /*quantidade de eventos*/ evento_aux = (char**)malloc((*e_k) * sizeof(char*)); /*vetor com os eventos*/ for(cnt = 0; cnt < *e_k; cnt++){ evento_aux[cnt] = (char*)malloc((K+1) *sizeof(char)); fscanf(pt_param, "%s", evento_aux[cnt]); }//fim do for *evento = evento_aux; fclose(pt_param); }//fim do ler_param /**************************************************************************** Procedimento que coloca 0.0 no variavel valor dos eventos que sao considerados validos, ou seja, foram passados como parametro e estao no vetor evento. ****************************************************************************/ void inicializar_pos_valida(tfreq **vfreq, int tam_vfreq, char **evento, int e_k){ int cnt, cnt_freq, pos_evento; tfreq *vfreq_aux; vfreq_aux = *vfreq; for (cnt_freq = 0; cnt_freq < tam_vfreq; cnt_freq++){ for(cnt = 0; cnt < e_k; cnt++){ pos_evento = 0; pos_evento = retorna_indice(evento[cnt]); strcpy(vfreq_aux[cnt_freq].prob[pos_evento].evento,"\0"); strcpy(vfreq_aux[cnt_freq].prob[pos_evento].evento,retorna_evento(pos_evento)); vfreq_aux[cnt_freq].prob[pos_evento].valor = 0.0; }//fim do for }//fim do for *vfreq = vfreq_aux; } /**************************************************************************** * Procedimento para criar o vetor que vai guardar as frequencias * ****************************************************************************/ int alocar_vfreq(tfreq **vfreq, int tam_vfreq, char **evento, int e_k){ int cnt, cnt_prob; tfreq *vfreq_aux; vfreq_aux = *vfreq; vfreq_aux = (tfreq *) malloc(tam_vfreq * sizeof(tfreq)); if (vfreq_aux ==NULL){ printf("Erro na alocacao de memoria,vfreq nao pode ser criado!"); return(1); } for(cnt = 0; cnt < tam_vfreq; cnt++){ vfreq_aux[cnt].tupla = (char *) malloc((K+1)* sizeof(char)); /* rotulo da tupla */ strcpy(vfreq_aux[cnt].tupla, "\0"); strcpy(vfreq_aux[cnt].tupla, retorna_tupla(cnt)); vfreq_aux[cnt].prob = (tprob *)malloc(tam_vfreq * sizeof(tprob));/*rotulo de cada evento e sua probabilidade*/ for (cnt_prob=0; cnt_prob < tam_vfreq; cnt_prob++){ vfreq_aux[cnt].prob[cnt_prob].evento = (char *) malloc((K+1) * sizeof(char)); /* rotulo do evento */ //strcpy(vfreq_aux[cnt].prob[cnt_prob].evento,retorna_evento(cnt_prob)); /* limpa o valor do rotulo */ strcpy(vfreq_aux[cnt].prob[cnt_prob].evento, "*"); vfreq_aux[cnt].prob[cnt_prob].valor = -1.0; } }//fim do for i< vfreq_tam inicializar_pos_valida(&vfreq_aux, tam_vfreq, evento, e_k); *vfreq = vfreq_aux; return(0); }//fim do criar_vetor /**************************************************************************** Procedimento que carrega as frequencias do arquivo em vfreq ****************************************************************************/ void carregar_vfreq(char *nome_arq, tfreq **vfreq, int tam_vfreq, unsigned long *tot_tuplas){ int indice_tupla, indice_evento; unsigned long freq; char letra; char *tupla, *evento; tfreq *vfreq_aux; FILE *pt_freq; vfreq_aux = *vfreq; freq = 0; abrir_arq(nome_arq, &pt_freq,"r"); fscanf(pt_freq,"%c",&letra); consumir_cabecalho(&pt_freq,&letra);/*para pegar o cabecalho do arquivo*/ tupla = (char*)malloc ((K + 1) * sizeof(char)); evento = (char*)malloc ((K + 1) * sizeof(char)); fscanf(pt_freq,"%s",tupla); while(!feof(pt_freq)){ fscanf(pt_freq,"%s",evento); fscanf(pt_freq,"%lu",&freq); indice_tupla = retorna_indice(tupla); indice_evento = retorna_indice(evento); if ((indice_tupla == - 1)||(indice_tupla>=64)){ printf("Tupla:%s do arquivo de frequencia nao encontrada\n", evento); fflush(stdout); }else if ((strcmp(vfreq_aux[indice_tupla].prob[indice_evento].evento,"*"))==0){ printf("Evento:%s do arquivo de frequencia nao encontrado\n", evento); fflush(stdout); }else{ //strcpy(vfreq_aux[indice_tupla].prob[indice_evento].evento,evento); vfreq_aux[indice_tupla].prob[indice_evento].valor = (double)freq; (*tot_tuplas) = (*tot_tuplas) + freq; } strcpy(tupla,"\0"); strcpy(evento,"\0"); freq = 0; fscanf(pt_freq,"%s",tupla); }//end while fclose(pt_freq); *vfreq = vfreq_aux; }//fim do carrega_vfreq /**************************************************************************** Procedimento que transforma as frequencias do vetor vfreq em probabilidades ****************************************************************************/ void transforma_prob(tfreq **vfreq, int tam_vfreq, unsigned long tot_tuplas){ int cnt, cnt_prob; tfreq *vfreq_aux; vfreq_aux = *vfreq; for (cnt = 0; cnt < tam_vfreq; cnt++){ for(cnt_prob = 0; cnt_prob < tam_vfreq; cnt_prob++){ if(vfreq_aux[cnt].prob[cnt_prob].valor != -1.0) vfreq_aux[cnt].prob[cnt_prob].valor = (vfreq_aux[cnt].prob[cnt_prob].valor + 1.0)/ ((double)tot_tuplas + ((double)tam_vfreq)); } }//fim do for printf("total de tuplas=%lu\n",tot_tuplas); *vfreq = vfreq_aux; }//fim do prob_freq /**************************************************************************** * Procedimento para liberar o espaco de memoria ocupado por vfreq * ****************************************************************************/ void gravar_vfreq(char *nome_arq, tfreq *vfreq, int tam_vfreq){ int cnt, cnt_prob; char nome_arq_saida[40]; FILE *pt_saida; sprintf(nome_arq_saida,"prob_triplas_%s", nome_arq); abrir_arq(nome_arq_saida, &pt_saida,"w+"); fprintf(pt_saida,">arquivo com as probabilidades de ocorrencia das triplas nas sequencias de treinamento vindas do arquivo %s.\n", nome_arq); for(cnt = 0; cnt < tam_vfreq; cnt++){ for (cnt_prob = 0; cnt_prob < tam_vfreq; cnt_prob++) if (vfreq[cnt].prob[cnt_prob].valor != -1) fprintf(pt_saida,"%s %s %.10lf\n", vfreq[cnt].tupla, vfreq[cnt].prob[cnt_prob].evento, vfreq[cnt].prob[cnt_prob].valor); } fclose(pt_saida); }//fim gravar_vfreq /**************************************************************************** * Procedimento para liberar o espaco de memoria ocupado por vfreq * ****************************************************************************/ void desaloca_vfreq(tfreq **vfreq, int tam_vfreq){ int cnt, cnt_prob; tfreq *vfreq_aux; vfreq_aux = *vfreq; for (cnt=0; cnt < tam_vfreq;cnt++){ for (cnt_prob=0; cnt_prob < tam_vfreq; cnt_prob++) free(vfreq_aux[cnt].prob[cnt_prob].evento); free(vfreq_aux[cnt].prob); free(vfreq_aux[cnt].tupla); } free(vfreq_aux); }//fim do desaloca vfreq