/* @source embcom ************************************************************* ** ** General routines for program Complex ** ** NB: THESE ROUTINES DO NOT CONFORM TO THE LIBRARY WRITING STANDARD AND ** THEREFORE SHOULD NOT BE USED AS A TEMPLATE FOR WRITING EMBOSS CODE ** ** @author Copyright (c) 1999 Donata Colangelo ** @version $Revision: 1.18 $ ** @modified $Date: 2011/11/08 15:12:52 $ by $Author: rice $ ** @@ ** ** This library is free software; you can redistribute it and/or ** modify it under the terms of the GNU Lesser General Public ** License as published by the Free Software Foundation; either ** version 2.1 of the License, or (at your option) any later version. ** ** This library is distributed in the hope that it will be useful, ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ** Lesser General Public License for more details. ** ** You should have received a copy of the GNU Lesser General Public ** License along with this library; if not, write to the Free Software ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, ** MA 02110-1301, USA. ** ******************************************************************************/ #include "ajlib.h" #include "embcom.h" #include "ajfile.h" #include #include #include #include #include #ifndef WIN32 #define fmodf(a,b) (float)fmod((double)a,(double)b) #endif static void comWriteTable(AjPFile fp, const char *name, const EmbPComUjwin RUj,const EmbPComUjwin MedUj, const EmbPComUjwin SDUj, const EmbPComUjwin RatUj, ajint Nwin, ajint jmin, ajint jmax); static void comCalcComplex(const EmbPComUjwin RUj,const EmbPComUjwin MedUj, EmbPComUjwin RatUj, ajint Nwin, ajint Nword, float *CompSeq); static void comCalcMedValue(const EmbPComUjsim SetUj, EmbPComUjwin MedUj,EmbPComUjwin SDUj, ajint Nsim, ajint Nwin, ajint Nword); static void comElabSetSim(const EmbPComSeqsim SetSeqSim,EmbPComUjsim SetUjSim, ajint lseq, ajint nsim, ajint jmin, ajint jmax, ajint lwin, ajint step); static void comElabSeq(const char *seq,EmbPComUjwin ujwin, ajint jmin, ajint jmax, ajint lwin, ajint lseq, ajint step); static ajint comCalcNOfWin(ajint lseq, ajint lwin, ajint step); static void comWriteSimValue(const float *ComplexOfSim, ajint nsim,AjPFile fp); static void comWriteValue(const char *name, ajint lseq, const float *ComplexOfSeq, ajint NumOfWin, ajint l, ajint step, ajint jmin, ajint jmax, ajint sim,float MedValue, AjPFile fp); static void comComplexOnSeq(const char *seqsim,const char *seq, ajint lseq, ajint lwin, ajint jmin, ajint jmax, ajuint step,float *ComplexOfSeq); static void comComplexOnSeq2(const char *seq, ajint lseq, ajint lwin, ajint jmin, ajint jmax, ajint step, float *ComplexOfSeq); static void comSimulSeq(char *seqsim, ajint lseq,const EmbPComTrace Freq, const char *ACN, ajint freq); static void comSortFreq(EmbPComTrace set); static void comCalcComplexMed(const float *ComplexOfSeq, ajint NumOfWin, float *MedValue); static void comReadWin(const char *seq, ajint bwin, ajint ewin,char *win); static void comRead_j_mer(const char *win, ajint lwin, ajint jlen,AjPStr *str); static void comWinComplex2(const char *win, ajint lwin,float *ComplexOfWin, ajint jmin, ajint jmax); static void comWinComplex(const char *win,const char *winsim, ajint lwin, float *ComplexOfWin, ajint jmin, ajint jmax); static void comCalcUj2(ajint lwin, ajint jlen,const char *win,float *Ujvalue); static void comCalcUj(ajint lwin, ajint jlen,const char *win,float *Ujvalue); static ajint comCounter(AjPStr const * str, ajint k); static void comAmbiguity(char *seq); static void comReplace(const char *vet,char *ch); static void comCalcFreqACN(const char *seq, ajint lseq,float *Freq); /* @func embComComplexity ***************************************************** ** ** Complexity calculation ** ** @param [r] seq [const char *] Sequence ** @param [r] name [const char *] Sequence name ** @param [r] len [ajint] Sequence length ** @param [r] jmin [ajint] Minimum ** @param [r] jmax [ajint] Maximum ** @param [r] l [ajint] Window length ** @param [r] step [ajint] Step size ** @param [r] sim [ajint] Simulation count ** @param [r] freq [ajint] Frequency calculation (boolean) ** @param [r] omnia [ajint] All sequences (boolean) ** @param [u] fp [AjPFile] Output file ** @param [u] pf [AjPFile] Temp file ** @param [r] print [ajint] Print (boolean) ** @param [w] MedValue [float *] Results ** @return [void] ** ** @release 1.0.0 ** @@ ******************************************************************************/ void embComComplexity(const char *seq,const char *name, ajint len, ajint jmin, ajint jmax, ajint l, ajint step, ajint sim, ajint freq, ajint omnia, AjPFile fp, AjPFile pf, ajint print, float *MedValue) { float *ComplexOfSeq = NULL; float Freq[4]; ajint NumOfWin; ajint i; ajint j; char ACN[] = "ACGT"; char *seqsim = NULL; EmbOComTrace SortedFreq[4]; EmbPComSeqsim SetSeqSim = NULL; EmbPComUjsim SetUjSim = NULL; EmbPComUjwin MedValueUj = NULL; EmbPComUjwin SDValueUj = NULL; EmbPComUjwin RealUjValue = NULL; EmbPComUjwin RatioUj = NULL; if(l == len) NumOfWin = 1; else NumOfWin = comCalcNOfWin(len,l,step); AJCNEW(ComplexOfSeq, NumOfWin); if(freq) { for(i=0;i<4;i++) Freq[i] = 0.0; comCalcFreqACN(seq,len,Freq); for(i=0;i<4;i++) { SortedFreq[i].ind = i; SortedFreq[i].pc = Freq[i]; } comSortFreq(SortedFreq); for(i=0;i<4;i++) SortedFreq[i].pc = SortedFreq[i].pc*10; } AJCNEW(RealUjValue, NumOfWin); for(i=0;i 1.0) CompSeq[i] = 1.0; return; } /* @funcstatic comCalcMedValue ************************************************ ** ** Mean value calculation for complexity ** ** @param [r] SetUj [const EmbPComUjsim] SetUj values ** @param [w] MedUj [EmbPComUjwin] MedUj values ** @param [w] SDUj [EmbPComUjwin] SDUj values ** @param [r] Nsim [ajint] Number of simulations ** @param [r] Nwin [ajint] Window number ** @param [r] Nword [ajint] Word number ** @return [void] ** ** @release 1.0.0 ** @@ ******************************************************************************/ static void comCalcMedValue(const EmbPComUjsim SetUj,EmbPComUjwin MedUj, EmbPComUjwin SDUj, ajint Nsim, ajint Nwin, ajint Nword) { ajint i; ajint j; ajint k; float *sum; float *var; AJCNEW0(sum, Nword); AJCNEW0(var, Nword); for(j=0;j 0.0 && x1 <= n1 && freq) { seqsim[k] = ACN[Freq[0].ind]; k++; } if(x1 > 0.0 && x1 <= n1 && !freq) { seqsim[k] = ACN[0]; k++; } if(x1 > n1 && x1 <= n2 && freq) { seqsim[k] = ACN[Freq[1].ind]; k++; } if(x1 > n1 && x1 <= n2 && !freq) { seqsim[k] = ACN[1]; k++; } if(x1 > n2 && x1 <= n3 && freq) { seqsim[k] = ACN[Freq[2].ind]; k++; } if(x1 > n2 && x1 <= n3 && !freq) { seqsim[k] = ACN[2]; k++; } if(x1 > n3 && freq) { seqsim[k] = ACN[Freq[3].ind]; k++; } if(x1 > n3 && !freq) { seqsim[k] = ACN[3]; k++; } } seqsim[k] = '\0'; return; } /* @funcstatic comSortFreq **************************************************** ** ** Sort frequencies for complexity calculation ** ** @param [u] set [EmbPComTrace] Frequency values ** @return [void] ** ** @release 1.0.0 ** @@ ******************************************************************************/ static void comSortFreq(EmbPComTrace set) { ajint a; ajint b; EmbOComTrace strutt; strutt.ind = 0; strutt.pc = 0.; for(a=1;a<4;++a) for(b=3;b>=a;--b) if(set[b-1].pc>set[b].pc) strutt=set[b-1]; set[b-1]=set[b]; set[b]=strutt; return; } /* @funcstatic comCalcComplexMed ********************************************** ** ** Calculate mean for complexity ** ** @param [r] ComplexOfSeq [const float*] Sequence complexity values ** @param [r] NumOfWin [ajint] Number of windows ** @param [w] MedValue [float *] Mean value result ** @return [void] ** ** @release 1.0.0 ** @@ ******************************************************************************/ static void comCalcComplexMed(const float *ComplexOfSeq, ajint NumOfWin, float *MedValue) { ajint i; float sum = 0.0; ajDebug("CalcComplexMed NumOfWin: %d\n", NumOfWin); for(i=0;i 1.0) Ujvalue = 1.0; else Ujvalue = Ujreal/Ujsim; WinValue = WinValue*Ujvalue; } *ComplexOfWin = WinValue; return; } /* @funcstatic comCalcUj2 ***************************************************** ** ** Uj calculation for complexity ** ** @param [r] lwin [ajint] Window length ** @param [r] jlen [ajint] Word length ** @param [r] win [const char *] Sequece for window ** @param [w] Ujvalue [float *] Results ** @return [void] ** ** @release 1.0.0 ** @@ ******************************************************************************/ static void comCalcUj2(ajint lwin, ajint jlen,const char *win,float *Ujvalue) { ajint unlikej_mer = 0; ajint k; ajint i; float z = 0.0; float n = 0.0; AjPStr *str; ajint njmer; njmer = (lwin-jlen+1); AJCNEW(str, njmer); comRead_j_mer(win,lwin,jlen,str); qsort(str,njmer,sizeof(AjPStr),(ajint (*)(const void *str_1, const void *str_2)) ajStrVcmp); unlikej_mer = comCounter(str,lwin-jlen+1); n=(float)pow((double)4,(double)jlen); k=(ajint)(n); if(lwin > k+jlen-1) z=((float)(unlikej_mer)/n); else z=((float)(unlikej_mer)/(float)(njmer)); *Ujvalue = z; for(i=0;i