int main(int argc, char **argv) { ajint i; ajint numseq; ajint j = 0; ajint numres; ajint count; ajint k; ajint kmax; float defheight; float currentscale; AjPStr shade = NULL; AjPFloat pair = NULL; AjPGraph graph = NULL; AjPMatrix cmpmatrix = NULL; AjPSeqCvt cvt = NULL; AjPStr matcodes = NULL; AjBool consensus; AjBool colourbyconsensus; AjBool colourbyresidues; AjBool colourbyshade = AJFALSE; AjBool boxit; AjBool boxcol; AjBool portrait; AjBool collision; ajint identity; AjBool listoptions; ajint alternative; AjPStr altstr = NULL; AjPStr sidentity = NULL; AjPStr ssimilarity = NULL; AjPStr sother = NULL; AjPStr sboxcolval = NULL; AjPStr options = NULL; /* ajint showscore = 0; */ ajint iboxcolval = 0; ajint cidentity = RED; ajint csimilarity = GREEN; ajint cother = BLACK; float fxp; float fyp; float yincr; float y; ajint ixlen; ajint iylen; ajint ixoff; ajint iyoff; char res[2] = " "; float *score = 0; float scoremax = 0; float *identical = NULL; ajint identicalmaxindex; float *matching = NULL; ajint matchingmaxindex; float *colcheck = NULL; ajint **matrix; ajint m1 = 0; ajint m2 = 0; ajint ms = 0; ajint highindex = 0; ajint myindex; ajint *previous = 0; AjBool iscons = ajFalse; ajint currentstate = 0; ajint oldfg = 0; float fold = 0.0; ajint *colmat = 0; ajint *shadecolour = 0; /* float identthresh = 1.5; */ /* float simthresh = 1.0; */ /* float relthresh = 0.5; */ float part = 0.0; const char *cptr; ajint resbreak; float fplural; float ystart; float xmin; float xmax; float xmid; AjPTime ajtime; ajint gapcount = 0; ajint countforgap = 0; ajint boxindex; float max; ajint matsize; ajint seqperpage = 0; ajint startseq; ajint endseq; ajint newILend = 0; ajint newILstart; void *freeptr; ajint itmp; embInit("prettyplot", argc, argv); seqset = ajAcdGetSeqset("sequences"); numres = ajAcdGetInt("residuesperline"); resbreak = ajAcdGetInt("resbreak"); ajSeqsetFill(seqset); /* Pads sequence set with gap characters */ numseq = ajSeqsetGetSize(seqset); graph = ajAcdGetGraph("graph"); colourbyconsensus = ajAcdGetBoolean("ccolours"); colourbyresidues = ajAcdGetBoolean("docolour"); shade = ajAcdGetString("shade"); pair = ajAcdGetArray("pair"); identity = ajAcdGetInt("identity"); boxit = ajAcdGetBoolean("box"); ajtime = ajTimeNewTodayFmt("daytime"); ajSeqsetTrim(seqset); /* offset = ajSeqsetGetOffset(seqset); Unused */ ajGraphAppendTitleS(graph, ajSeqsetGetUsa(seqset)); if(boxit) { AJCNEW(seqboxptr, numseq); for(i=0;i<numseq;i++) AJCNEW(seqboxptr[i], ajSeqsetGetLen(seqset)); } boxcol = ajAcdGetBoolean("boxcol"); sboxcolval = ajAcdGetString("boxuse"); if(boxcol) { iboxcolval = ajGraphicsCheckColourS(sboxcolval); if(iboxcolval == -1) iboxcolval = GREY; } consensus = ajAcdGetBoolean("consensus"); if(consensus) { AJCNEW(constr, ajSeqsetGetLen(seqset)+1); constr[0] = '\0'; } shownames = ajAcdGetBoolean("name"); shownumbers = ajAcdGetBoolean("number"); charlen = ajAcdGetInt("maxnamelen"); fplural = ajAcdGetFloat("plurality"); portrait = ajAcdGetBoolean("portrait"); collision = ajAcdGetBoolean("collision"); listoptions = ajAcdGetBoolean("listoptions"); altstr = ajAcdGetListSingle("alternative"); cmpmatrix = ajAcdGetMatrix("matrixfile"); ajStrToInt(altstr, &alternative); matrix = ajMatrixGetMatrix(cmpmatrix); cvt = ajMatrixGetCvt(cmpmatrix); matsize = ajMatrixGetSize(cmpmatrix); AJCNEW(identical,matsize); AJCNEW(matching,matsize); AJCNEW(colcheck,matsize); numgaps = numres/resbreak; numgaps--; if(portrait) { ajGraphicsSetPortrait(1); ystart = (float) 75.0; } else ystart = (float) 75.0; /* pair is an array of three non-negative floats */ /* identthresh = ajFloatGet(pair,0); Unused */ /* simthresh = ajFloatGet(pair,1); Unused */ /* relthresh = ajFloatGet(pair,2); Unused */ /* ** shade is a formatted 4-character string. Characters BLPW only. ** controlled by a pattern in ACD. */ if(ajStrGetLen(shade)) { AJCNEW(shadecolour,4); cptr = ajStrGetPtr(shade); for(i=0;i<4;i++){ if(cptr[i]== 'B' || cptr[i]== 'b') shadecolour[i] = BLACK; else if(cptr[i]== 'L' || cptr[i]== 'l') shadecolour[i] = BROWN; else if(cptr[i]== 'P' || cptr[i]== 'p') shadecolour[i] = WHEAT; else if(cptr[i]== 'W' || cptr[i]== 'w') shadecolour[i] = WHITE; } colourbyconsensus = colourbyresidues = ajFalse; colourbyshade = ajTrue; } /* ** we can colour by consensus or residue but not both ** if we have to choose, use the consensus */ if(colourbyconsensus && colourbyresidues) colourbyconsensus = AJFALSE; sidentity = ajAcdGetString("cidentity"); ssimilarity = ajAcdGetString("csimilarity"); sother = ajAcdGetString("cother"); if(colourbyconsensus) { cidentity = ajGraphicsCheckColourS(sidentity); if(cidentity == -1) cidentity = RED; csimilarity = ajGraphicsCheckColourS(ssimilarity); if(csimilarity == -1) csimilarity = GREEN; cother = ajGraphicsCheckColourS(sother); if(cother == -1) cother = BLACK; } else if(colourbyresidues) { matcodes = ajMatrixGetCodes(cmpmatrix); if(ajSeqsetIsProt(seqset)) colmat = ajGraphicsBasecolourNewProt(matcodes); else colmat = ajGraphicsBasecolourNewNuc(matcodes); } /* output the options used as the subtitle for the bottom of the graph */ if(listoptions) { ajStrAssignC(&options,""); ajFmtPrintAppS(&options,"-plurality %.1f",fplural); if(collision) ajStrAppendC(&options," -collision"); else ajStrAppendC(&options," -nocollision"); if(boxit) ajStrAppendC(&options," -box"); else ajStrAppendC(&options," -nobox"); if(boxcol) ajStrAppendC(&options," -boxcol"); else ajStrAppendC(&options," -noboxcol"); if(colourbyconsensus) ajStrAppendC(&options," -colbyconsensus"); else if(colourbyresidues) ajStrAppendC(&options," -colbyresidues"); else if(colourbyshade) ajStrAppendC(&options," -colbyshade"); else ajStrAppendC(&options," -nocolour"); if(alternative==2) ajStrAppendC(&options," -alt 2"); else if(alternative==1) ajStrAppendC(&options," -alt 1"); else if(alternative==3) ajStrAppendC(&options," -alt 3"); } AJCNEW(seqcolptr, numseq); for(i=0;i<numseq;i++) AJCNEW(seqcolptr[i], ajSeqsetGetLen(seqset)); AJCNEW(seqcharptr, numseq); AJCNEW(seqnames, numseq); AJCNEW(score, numseq); AJCNEW(previous, numseq); AJCNEW(seqcount, numseq); for(i=0;i<numseq;i++) { ajSeqsetFmtUpper(seqset); seqcharptr[i] = ajSeqsetGetseqSeqC(seqset, i); seqnames[i] = 0; ajStrAppendS(&seqnames[i],ajSeqsetGetseqNameS(seqset, i)); ajStrTruncateLen(&seqnames[i],charlen); previous[i] = 0; seqcount[i] = 0; } /* ** user will pass the number of residues to fit a page ** therefore we now need to calculate the size of the chars ** based on this and get the new char width. ** 'charlen' maximum characters for the name (truncated above) */ ajGraphicsGetCharsize(&defheight,¤tscale); xmin = -charlen - (float)2.0; xmax = (float)numres+(float)11.0+(float)(numres/resbreak); xmid = (xmax + xmin)/(float)2.0; ajGraphOpenWin(graph, xmin, xmax, (float)0.0, ystart+(float)1.0); ajGraphGetParamsPage(graph, &fxp,&fyp,&ixlen,&iylen,&ixoff,&iyoff); if(portrait) { itmp = ixlen; ixlen = iylen; iylen = itmp; } ajGraphicsGetCharsize(&defheight,¤tscale); ajGraphicsSetCharscale(((float)ixlen/((float)(numres+charlen+1)* (currentscale * (float) 1.5)))/ currentscale); /* ajGraphicsSetCharscale(((float)ixlen/((float)(numres+charlen)* (currentscale+(float)1.0)))/ currentscale); */ ajGraphicsGetCharsize(&defheight,¤tscale); yincr = (currentscale + (float)3.0)*(float)0.3; /* ** If we have titles (now the standard graph title and subtitle and footer) ** leave 7 rows of space for them */ y=ystart-(float)7.0; if(ajStrGetLen(options)) { fold = ajGraphicsSetCharscale(1.0); ajGraphicsDrawposTextAtmid(xmid,2.0, ajStrGetPtr(options)); ajGraphicsSetCharscale(fold); } /* if sequences per page not set then calculate it */ if(!seqperpage) { seqperpage = prettyplot_calcseqperpage(yincr,y,consensus); if(seqperpage>numseq) seqperpage=numseq; } count = 0; /* ** for boxes we need to set a foreground colour for the box lines ** and save the current foreground colour */ if(boxit && boxcol) oldfg = ajGraphicsSetFgcolour(iboxcolval); /* ** step through each residue position */ kmax = ajSeqsetGetLen(seqset) - 1; for(k=0; k<= kmax; k++) { /* reset column score array */ for(i=0;i<numseq;i++) score[i] = 0.0; /* reset matrix character testing arrays */ for(i=0;i<matsize;i++) { identical[i] = 0.0; matching[i] = 0.0; colcheck[i] = 0.0; } /* generate a score for this residue in each sequence */ for(i=0;i<numseq;i++) { m1 = ajSeqcvtGetCodeK(cvt, seqcharptr[i][k]); for(j=0;j<numseq;j++) { m2 = ajSeqcvtGetCodeK(cvt, seqcharptr[j][k]); if(m1 && m2) score[i] += (float)matrix[m1][m2]* ajSeqsetGetseqWeight(seqset, j); } if(m1) identical[m1] += ajSeqsetGetseqWeight(seqset, i); } /* find the highest score */ highindex = -1; scoremax = INT_MIN; /*ajDebug("Scores at position %d:\n", k);*/ for(i=0;i<numseq;i++) { /*ajDebug(" seq %d: '%c' %f\n",i,seqcharptr[i][k],score[i]);*/ if(score[i] > scoremax) { scoremax = score[i]; highindex = i; } } for(i=0;i<numseq;i++) { m1 = ajSeqcvtGetCodeK(cvt, seqcharptr[i][k]); if(!matching[m1]) { for(j=0;j<numseq;j++) { m2 = ajSeqcvtGetCodeK(cvt, seqcharptr[j][k]); if(m1 && m2 && matrix[m1][m2] > 0) matching[m1] += ajSeqsetGetseqWeight(seqset, j); } } } /* find highs for matching and identical */ matchingmaxindex = 0; identicalmaxindex = 0; for(i=0;i<numseq;i++) { m1 = ajSeqcvtGetCodeK(cvt, seqcharptr[i][k]); if(identical[m1] > identical[identicalmaxindex]) identicalmaxindex = m1; } for(i=0;i<numseq;i++) { m1 = ajSeqcvtGetCodeK(cvt, seqcharptr[i][k]); if(matching[m1] > matching[matchingmaxindex]) matchingmaxindex = m1; else if(matching[m1] == matching[matchingmaxindex]) { if(identical[m1] > identical[matchingmaxindex]) matchingmaxindex= m1; } } iscons = ajFalse; boxindex = -1; max = -3; ajDebug("k:%2d highindex:%2d matching:%4.2f\n", k, highindex, matching[ajSeqcvtGetCodeK(cvt, seqcharptr[highindex][k])]); if(highindex != -1 && matching[ajSeqcvtGetCodeK(cvt, seqcharptr[highindex][k])] >= fplural) { iscons = ajTrue; boxindex = highindex; } else { for(i=0;i<numseq;i++) { m1 = ajSeqcvtGetCodeK(cvt, seqcharptr[i][k]); if(matching[m1] > max) { max = matching[m1]; highindex = i; } else if(matching[m1] == max) { if(identical[m1] > identical[ajSeqcvtGetCodeK(cvt, seqcharptr[highindex][k])] ) { max = matching[m1]; highindex = i; } } } if(matching[ajSeqcvtGetCodeK(cvt, seqcharptr[highindex][k])] >= fplural) { iscons = ajTrue; boxindex = highindex; } } if(iscons) { if(!collision) { /* check for collisions */ if(alternative == 1) { /* check to see if this is unique for collisions */ for(i=0;i<numseq;i++) { m1 = ajSeqcvtGetCodeK(cvt, seqcharptr[i][k]); if(identical[m1] >= identical[identicalmaxindex] && m1 != identicalmaxindex) iscons = ajFalse; } /*ajDebug("after (alt=1) iscons: %B",iscons);*/ } else if(alternative == 2) { for(i=0;i<numseq;i++) { m1 = ajSeqcvtGetCodeK(cvt, seqcharptr[i][k]); if((matching[m1] >= matching[matchingmaxindex] && m1 != matchingmaxindex && matrix[m1][matchingmaxindex] < 0.1)|| (identical[m1] >= identical[matchingmaxindex] && m1 != matchingmaxindex)) iscons = ajFalse; } } else if(alternative == 3) { /* ** to do this check one is NOT in consensus to see if ** another score of fplural has been found */ ms = ajSeqcvtGetCodeK(cvt, seqcharptr[highindex][k]); for(i=0;i<numseq;i++) { m1 = ajSeqcvtGetCodeK(cvt, seqcharptr[i][k]); if(ms != m1 && colcheck[m1] == 0.0) /* NOT in the current consensus */ for(j=0;j<numseq;j++) { m2 = ajSeqcvtGetCodeK(cvt, seqcharptr[j][k]); if( matrix[ms][m2] < 0.1) { /* NOT in the current consensus */ if( matrix[m1][m2] > 0.1) colcheck[m1] += ajSeqsetGetseqWeight(seqset, j); } } } for(i=0;i<numseq;i++) { m1 = ajSeqcvtGetCodeK(cvt, seqcharptr[i][k]); /* if any other matches then we have a collision */ if(colcheck[m1] >= fplural) iscons = ajFalse; } /*ajDebug("after alt=2 iscons: %B", iscons);*/ } else { for(i=0;i<numseq;i++) { m1 = ajSeqcvtGetCodeK(cvt, seqcharptr[i][k]); if((matching[m1] >= matching[matchingmaxindex] && m1 != matchingmaxindex && matrix[m1][matchingmaxindex] < 0.1)) iscons = ajFalse; if(identical[m1] >= identical[matchingmaxindex] && m1 != matchingmaxindex && matrix[m1][matchingmaxindex] > 0.1) iscons = ajFalse; } if(!iscons) { /* matches failed try identicals */ if(identical[identicalmaxindex] >= fplural) { iscons = ajTrue; /* ** if nothing has an equal or higher match that ** does not match highest then false */ for(i=0;i<numseq;i++) { m1 = ajSeqcvtGetCodeK(cvt, seqcharptr[i][k]); if(identical[m1] >= identical[identicalmaxindex] && m1 != identicalmaxindex) iscons = ajFalse; else if(matching[m1] >= matching[identicalmaxindex] && matrix[m1][matchingmaxindex] <= 0.0) iscons = ajFalse; else if(m1 == identicalmaxindex) j = i; } if(iscons) highindex = j; } } } } if(identity) { j = 0; for(i=0;i<numseq;i++) if(seqcharptr[highindex][k] == seqcharptr[i][k]) j++; if(j<identity) iscons = ajFalse; } } /* ** Done a full line of residues ** Boxes have been defined up to this point */ if(count >= numres ) { /* check y position for next set */ y=y-(yincr*((float)numseq+(float)2.0+((float)consensus*(float)2))); if(y<yincr*((float)numseq+(float)2.0+((float)consensus*(float)2))) { /* full page - print it */ y=ystart-(float)6.0; startseq = 0; endseq = seqperpage; newILstart = newILend; newILend = k; while(startseq < numseq) { /* AJB */ /*if(startseq != 0) ajGraphNewpage(graph, AJFALSE);*/ /*ajDebug("Inner loop: startseq: %d numseq: %d endseq: %d\n", startseq, numseq, endseq);*/ if(endseq>numseq) endseq=numseq; prettyplot_fillinboxes(numseq,ajSeqsetGetLen(seqset), startseq,endseq, newILstart,newILend, numres,resbreak, boxit,boxcol,consensus, ystart,yincr,cvt); startseq = endseq; endseq += seqperpage; ajGraphNewpage(graph, AJFALSE); } } count = 0; gapcount = 0; } count++; countforgap++; for(j=0;j<numseq;j++) { /* START OF BOXES */ if(boxit) { seqboxptr[j][k] = 0; if(boxindex!=-1) { myindex = boxindex; if(matrix[ajSeqcvtGetCodeK(cvt, seqcharptr[j][k])] [ajSeqcvtGetCodeK(cvt, seqcharptr[myindex][k])] > 0) part = 1.0; else { if(identical[ajSeqcvtGetCodeK(cvt, seqcharptr[j][k])] >= fplural) part = 1.0; else part = 0.0; } if(previous[j] != part) /* draw vertical line */ seqboxptr[j][k] |= BOXLEF; if(j==0) { /* special case for horizontal line */ if(part) { currentstate = 1; /* draw hori line */ seqboxptr[j][k] |= BOXTOP; } else currentstate = 0; } else { /* j != 0 Normal case for horizontal line */ if(part != currentstate) { /*draw hori line */ seqboxptr[j][k] |= BOXTOP; currentstate = (ajint) part; } } if(j== numseq-1 && currentstate) /* draw horiline at bottom */ seqboxptr[j][k] |= BOXBOT; previous[j] = (ajint) part; } else { part = 0; if(previous[j]) { /* draw vertical line */ seqboxptr[j][k] |= BOXLEF; } previous[j] = 0; } if(count == numres || k == kmax || countforgap >= resbreak ) { /* last one on the row or a break*/ if(previous[j]) { /* draw vertical line */ seqboxptr[j][k] |= BOXRIG; } previous[j] = 0; } } /* end box */ if(boxit && boxcol) if(boxindex != -1) { myindex = boxindex; if(matrix[ajSeqcvtGetCodeK(cvt, seqcharptr[j][k])] [ajSeqcvtGetCodeK(cvt, seqcharptr[myindex][k])] > 0 || identical[ajSeqcvtGetCodeK(cvt, seqcharptr[j][k])] >= fplural ) seqboxptr[j][k] |= BOXCOLOURED; } /* END OF BOXES */ if(ajSeqcvtGetCodeK(cvt, seqcharptr[j][k])) res[0] = seqcharptr[j][k]; else res[0] = '-'; if(colourbyconsensus) { part = (float) matrix[ajSeqcvtGetCodeK(cvt, seqcharptr[j][k])] [ajSeqcvtGetCodeK(cvt, seqcharptr[highindex][k])]; if(iscons && seqcharptr[highindex][k] == seqcharptr[j][k]) seqcolptr[j][k] = cidentity; else if(part > 0.0) seqcolptr[j][k] = csimilarity; else seqcolptr[j][k] = cother; } else if(colourbyresidues) seqcolptr[j][k] = colmat[ajSeqcvtGetCodeK(cvt, seqcharptr[j][k])]; else if(iscons && colourbyshade) { part = (float) matrix[ajSeqcvtGetCodeK(cvt, seqcharptr[j][k])] [ajSeqcvtGetCodeK(cvt, seqcharptr[highindex][k])]; if(part >= 1.5) seqcolptr[j][k] = shadecolour[0]; else if(part >= 1.0) seqcolptr[j][k] = shadecolour[1]; else if(part >= 0.5) seqcolptr[j][k] = shadecolour[2]; else seqcolptr[j][k] = shadecolour[3]; } else if(colourbyshade) seqcolptr[j][k] = shadecolour[3]; else seqcolptr[j][k] = BLACK; } if(consensus) { if(iscons) res[0] = seqcharptr[highindex][k]; else res[0] = '-'; strcat(constr,res); } if(countforgap >= resbreak) { gapcount++; countforgap=0; } } startseq = 0; endseq=seqperpage; newILstart = newILend; newILend = k; while(startseq < numseq) { if(startseq) ajGraphNewpage(graph, AJFALSE); /*ajDebug("Final loop: startseq: %d numseq: %d endseq: %d\n", startseq, numseq, endseq);*/ if(endseq>numseq) endseq = numseq; prettyplot_fillinboxes(numseq,ajSeqsetGetLen(seqset), startseq,endseq, newILstart,newILend, numres,resbreak, boxit,boxcol,consensus, ystart,yincr,cvt); startseq = endseq; endseq += seqperpage; } ajGraphicsGetCharsize(&defheight,¤tscale); if(boxit && boxcol) oldfg = ajGraphicsSetFgcolour(oldfg); ajGraphicsCloseWin(); ajGraphxyDel(&graph); ajStrDel(&sidentity); ajStrDel(&ssimilarity); ajStrDel(&sother); ajStrDel(&options); ajStrDel(&altstr); ajStrDel(&matcodes); for(i=0;i<numseq;i++) { ajStrDel(&seqnames[i]); AJFREE(seqcolptr[i]); if(seqboxptr) AJFREE(seqboxptr[i]); } AJFREE(seqcolptr); AJFREE(seqboxptr); AJFREE(seqnames); AJFREE(score); AJFREE(previous); AJFREE(seqcount); AJFREE(colmat); AJFREE(shadecolour); freeptr = (void *) seqcharptr; AJFREE(freeptr); AJFREE(identical); AJFREE(matching); AJFREE(colcheck); ajSeqsetDel(&seqset); ajMatrixDel(&cmpmatrix); ajStrDel(&shade); ajStrDel(&sboxcolval); ajStrDel(&sidentity); ajStrDel(&ssimilarity); ajStrDel(&sother); ajFloatDel(&pair); ajTimeDel(&ajtime); AJFREE(constr); embExit(); return 0; }
int main(int argc, char **argv) { /* ACD data item variables */ AjPSeqset dataset = NULL; AjPFile bfile = NULL; AjPFile plib = NULL; AjPStr mod = NULL; ajint nmotifs = 0; AjBool text = ajFalse; AjPStr prior = NULL; float evt = 0.0; ajint nsites = 0; ajint minsites = 0; ajint maxsites = 0; float wnsites = 0.0; ajint w = 0; ajint minw = 0; ajint maxw = 0; AjBool nomatrim = ajFalse; ajint wg = 0; ajint ws = 0; AjBool noendgaps = ajFalse; AjBool revcomp = ajFalse; AjBool pal = ajFalse; AjBool nostatus = ajFalse; ajint maxiter = 0; float distance = 0.0; float b = 0.0; float spfuzz = 0.0; AjPStr spmap = NULL; AjPStr cons = NULL; ajint maxsize = 0; ajint p = 0; ajint time = 0; AjPStr sf = NULL; ajint heapsize = 64; AjBool xbranch = ajFalse; AjBool wbranch = ajFalse; ajint bfactor = 0; AjPFile outtext = NULL; /* Housekeeping variables */ AjPStr cmd = NULL; AjPStr ssname = NULL; AjPSeqout outseq = NULL; AjPStr tmp = NULL; char option; /* ACD file processing */ embInitPV("ememetext",argc,argv,"MEME",VERSION); dataset = ajAcdGetSeqset("dataset"); bfile = ajAcdGetInfile("bfile"); plib = ajAcdGetInfile("plibfile"); mod = ajAcdGetSelectSingle("mod"); nmotifs = ajAcdGetInt("nmotifs"); text = ajAcdGetBoolean("text"); prior = ajAcdGetSelectSingle("prior"); evt = ajAcdGetFloat("evt"); nsites = ajAcdGetInt("nsites"); minsites = ajAcdGetInt("minsites"); maxsites = ajAcdGetInt("maxsites"); wnsites = ajAcdGetFloat("wnsites"); w = ajAcdGetInt("w"); minw = ajAcdGetInt("minw"); maxw = ajAcdGetInt("maxw"); nomatrim = ajAcdGetBoolean("nomatrim"); wg = ajAcdGetInt("wg"); ws = ajAcdGetInt("ws"); noendgaps = ajAcdGetBoolean("noendgaps"); revcomp = ajAcdGetBoolean("revcomp"); pal = ajAcdGetBoolean("pal"); nostatus = ajAcdGetBoolean("nostatus"); maxiter = ajAcdGetInt("maxiter"); distance = ajAcdGetFloat("distance"); b = ajAcdGetFloat("b"); spfuzz = ajAcdGetFloat("spfuzz"); spmap = ajAcdGetSelectSingle("spmap"); cons = ajAcdGetString("cons"); maxsize = ajAcdGetInt("maxsize"); p = ajAcdGetInt("p"); time = ajAcdGetInt("time"); sf = ajAcdGetString("sf"); heapsize = ajAcdGetInt("heapsize"); xbranch = ajAcdGetBoolean("xbranch"); wbranch = ajAcdGetBoolean("wbranch"); bfactor = ajAcdGetInt("bfactor"); outtext = ajAcdGetOutfile("outtext"); outseq = ajAcdGetSeqoutset("outseq"); /* MAIN APPLICATION CODE */ /* 1. Housekeeping */ cmd = ajStrNew(); tmp = ajStrNew(); /* 2. Re-write dataset to a temporary file in a format (fasta) MEME ** can understand. ** Can't just pass the name of dataset to MEME as the name provided ** might be a USA which MEME would not understand. */ ssname = ajStrNewS(ajFileGetNameS(outseq->File)); ajSeqoutSetFormatC(outseq, "fasta"); ajSeqoutWriteSet(outseq, dataset); ajSeqoutClose(outseq); ajSeqoutDel(&outseq); /* 3. Build ememe command line */ /* Command line is built in this order: i. Application name. ii. Original MEME options (in order they appear in ACD file) iii.Original MEME options (that don't appear in ACD file) iv. EMBASSY MEME new qualifiers and parameters. */ ajStrAssignS(&cmd, ajAcdGetpathC("meme")); ajFmtPrintAppS(&cmd, " %S", ssname); if(bfile) ajFmtPrintAppS(&cmd, " -bfile %s ", ajFileGetNameC(bfile)); if(plib) ajFmtPrintAppS(&cmd, " -plib %s ", ajFileGetNameC(plib)); option = ajStrGetCharFirst(mod); if(option == 'o') ajStrAppendC(&cmd, " -mod oops "); else if(option == 'z') ajStrAppendC(&cmd, " -mod zoops "); else if(option == 'a') ajStrAppendC(&cmd, " -mod anr "); if(nmotifs != 1) ajFmtPrintAppS(&cmd, " -nmotifs %d ", nmotifs); if(text) ajFmtPrintAppS(&cmd, " -text "); ajFmtPrintAppS(&cmd, " -prior %S ", prior); if(evt != -1) ajFmtPrintAppS(&cmd, " -evt %f ", evt); if(nsites != -1) ajFmtPrintAppS(&cmd, " -nsites %d ", nsites); else { if(minsites != -1) ajFmtPrintAppS(&cmd, " -minsites %d ", minsites); if(maxsites != -1) ajFmtPrintAppS(&cmd, " -maxsites %d ", maxsites); } if(wnsites < 0.7999 || wnsites > .8001) ajFmtPrintAppS(&cmd, " -wnsites %f ", wnsites); if(w != -1) ajFmtPrintAppS(&cmd, " -w %d ", w); if(minw != 8) ajFmtPrintAppS(&cmd, " -minw %d ", minw); if(maxw != 50) ajFmtPrintAppS(&cmd, " -maxw %d ", maxw); if(nomatrim) ajFmtPrintAppS(&cmd, " -nomatrim "); if(wg != 11) ajFmtPrintAppS(&cmd, " -wg %d ", wg); if(ws != 1) ajFmtPrintAppS(&cmd, " -ws %d ", ws); if(noendgaps) ajFmtPrintAppS(&cmd, " -noendgaps "); if(revcomp) ajFmtPrintAppS(&cmd, " -revcomp "); if(pal && ajSeqsetIsNuc(dataset)) ajFmtPrintAppS(&cmd, " -pal "); if(nostatus) ajFmtPrintAppS(&cmd, " -nostatus "); if(maxiter != 50) ajFmtPrintAppS(&cmd, " -maxiter %d ", maxiter); if(distance < 0.00099 || distance > 0.00101) ajFmtPrintAppS(&cmd, " -distance %f ", distance); if(b != -1) ajFmtPrintAppS(&cmd, " -b %f ", b); if(spfuzz != -1) ajFmtPrintAppS(&cmd, " -spfuzz %f ", spfuzz); if(!ajStrMatchC(spmap,"default")) ajFmtPrintAppS(&cmd, " -spmap %S ", spmap); if(MAJSTRGETLEN(cons)) ajFmtPrintAppS(&cmd, "-cons %S", cons); if(maxsize != -1) ajFmtPrintAppS(&cmd, " -maxsize %d ", maxsize); if(p > 0) ajFmtPrintAppS(&cmd, " -p %d ", p); if(time > 0) ajFmtPrintAppS(&cmd, " -time %d ", time); if(MAJSTRGETLEN(sf)) ajFmtPrintAppS(&cmd, " -sf %S", sf); if(heapsize != 64) ajFmtPrintAppS(&cmd, " -heapsize %d ", heapsize); if(xbranch) ajFmtPrintAppS(&cmd, " -x_branch"); if(wbranch) ajFmtPrintAppS(&cmd, " -w_branch"); if(bfactor != 3) ajFmtPrintAppS(&cmd, " -bfactor %d ", bfactor); if(ajSeqsetIsProt(dataset)) ajFmtPrintAppS(&cmd, "-protein "); else ajFmtPrintAppS(&cmd, "-dna "); ajFmtPrintAppS(&cmd, " -text"); ajFmtPrintAppS(&cmd, " > %S ", ajFileGetNameS(outtext)); /* 4. Close files from ACD before calling meme */ ajFileClose(&bfile); ajFileClose(&plib); /* 5. Call meme */ /* ajFmtPrint("\n%S\n", cmd); */ system(ajStrGetPtr(cmd)); /* 6. Exit cleanly */ ajSeqsetDel(&dataset); ajStrDel(&cons); ajStrDel(&sf); ajStrDel(&mod); ajStrDel(&prior); ajStrDel(&spmap); ajStrDel(&cmd); ajStrDel(&ssname); ajStrDel(&tmp); ajFileClose(&bfile); ajFileClose(&plib); ajFileClose(&outtext); ajSeqoutDel(&outseq); embExit(); return 0; }
void emboss_copy(AjPSeqset seqset, char ***retseqs, AINFO *info) { ajint n; ajint maxlen; ajint len; char **seqs; const AjPSeq seq = NULL; ajint i=0; const AjPStr fmt=NULL; const char *p=NULL; char c='\0'; /* char *q=NULL; AjPSelexseq sqdata=NULL; AjPSelexdata sdata=NULL; */ ajint cnt=0; info->name = NULL; info->rf=NULL; info->cs=NULL; info->desc=NULL; info->acc=NULL; info->au=NULL; info->flags=0; AjPStr tmpstr = NULL; ajSeqsetFill(seqset); fmt = ajSeqsetGetFormat(seqset); n = ajSeqsetGetSize(seqset); ajSeqsetFmtUpper(seqset); maxlen = ajSeqsetGetLen(seqset); /* First allocate and copy sequences */ AJCNEW0(seqs,n); for(i=0; i<n; ++i) { seqs[i] = ajCharNewRes(maxlen+1); strcpy(seqs[i],ajSeqGetSeqC(ajSeqsetGetseqSeq(seqset,i))); } info->sqinfo = (SQINFO *) calloc (sizeof(SQINFO), n); for(i=0; i<n; ++i) { info->sqinfo[i].flags = 0; strcpy(info->sqinfo[i].name,""); strcpy(info->sqinfo[i].id,""); strcpy(info->sqinfo[i].acc,""); strcpy(info->sqinfo[i].desc,""); info->sqinfo[i].len = 0; info->sqinfo[i].start = 0; info->sqinfo[i].stop = 0; info->sqinfo[i].olen = 0; info->sqinfo[i].type = 0; info->sqinfo[i].ss = NULL; info->sqinfo[i].sa =NULL; } AJCNEW0(info->wgt,n); for(i=0; i<n; ++i) { info->sqinfo[i].flags = 0; info->wgt[i] = ajSeqsetGetseqWeight(seqset,i); } info->nseq = n; info->alen = maxlen; for(i=0; i<n; ++i) { seq = ajSeqsetGetseqSeq(seqset,i); if((len=ajStrGetLen(ajSeqGetNameS(seq)))) { if(len>= SQINFO_NAMELEN) len = SQINFO_NAMELEN - 1; ajStrAssignSubS(&tmpstr, ajSeqGetNameS(seq), 0, len); strcpy(info->sqinfo[i].id,ajStrGetPtr(tmpstr)); info->sqinfo[i].flags |= SQINFO_ID; strcpy(info->sqinfo[i].name,ajStrGetPtr(tmpstr)); info->sqinfo[i].flags |= SQINFO_NAME; } if((len=ajStrGetLen(ajSeqGetAccS(seq)))) { if(len>= SQINFO_NAMELEN) len = SQINFO_NAMELEN - 1; ajStrAssignSubS(&tmpstr, ajSeqGetAccS(seq), 0, len); strcpy(info->sqinfo[i].acc,ajStrGetPtr(tmpstr)); info->sqinfo[i].flags |= SQINFO_ACC; } } seq = ajSeqsetGetseqSeq(seqset,0); info->cs = ajCharNewS(ajSeqGetSeqS(seq)); info->name = ajCharNewS(ajSeqGetNameS(seq)); info->acc = ajCharNewS(ajSeqGetAccS(seq)); info->desc = ajCharNewS(ajSeqGetDescS(seq)); info->rf = ajCharNewS(ajSeqGetSeqS(seq)); /* info->rf = ajCharNewS(seq); len = ajStrGetLen(seq->Selexdata->name); info->name = ajCharNewRes(len+1); strcpy(info->name,ajStrGetPtr(seq->Selexdata->name)); len = ajStrGetLen(seq->Selexdata->de); info->desc = ajCharNewRes(len+1); sdata = seq->Selexdata; strcpy(info->desc,ajStrGetPtr(sdata->de)); len = ajStrGetLen(sdata->ac); info->acc = ajCharNewRes(len+1); strcpy(info->acc,ajStrGetPtr(sdata->ac)); len = ajStrGetLen(sdata->au); info->au = ajCharNewRes(len+1); strcpy(info->au,ajStrGetPtr(sdata->au)); if(sdata->tc[0] || sdata->tc[1]) { info->flags |= AINFO_TC; info->tc1 = sdata->tc[0]; info->tc2 = sdata->tc[1]; } if(sdata->nc[0] || sdata->nc[1]) { info->flags |= AINFO_NC; info->nc1 = sdata->nc[0]; info->nc2 = sdata->nc[1]; } if(sdata->ga[0] || sdata->ga[1]) { info->flags |= AINFO_GA; info->ga1 = sdata->ga[0]; info->ga2 = sdata->ga[1]; } for(i=0;i<n;++i) { seq = ajSeqsetGetseqSeq(seqset,i); sqdata = seq->Selexdata->sq; if((len=ajStrGetLen(sqdata->name))) { if(len<64) strcpy(info->sqinfo[i].name,ajStrGetPtr(sqdata->name)); else strncpy(info->sqinfo[i].name,ajStrGetPtr(sqdata->name),63); info->sqinfo[i].name[63]='\0'; info->sqinfo[i].flags |= SQINFO_NAME; } / * if((len=ajStrGetLen(sqdata->id))) { if(len<64) strcpy(info->sqinfo[i].id,ajStrGetPtr(sqdata->id)); else strncpy(info->sqinfo[i]->id,ajStrGetPtr(sqdata->id),63); info->sqinfo[i].id[63]='\0'; info->sqinfo[i].flags |= SQINFO_ID; } * / strcpy(info->sqinfo[i].id,info->sqinfo[i].name); info->sqinfo[i].flags |= SQINFO_ID; if((len=ajStrGetLen(sqdata->ac))) { if(len<64) strcpy(info->sqinfo[i].acc,ajStrGetPtr(sqdata->ac)); else strncpy(info->sqinfo[i].acc,ajStrGetPtr(sqdata->ac),63); info->sqinfo[i].acc[63]='\0'; info->sqinfo[i].flags |= SQINFO_ACC; } if((len=ajStrGetLen(sqdata->de))) { if(len<127) strcpy(info->sqinfo[i].desc,ajStrGetPtr(sqdata->de)); else strncpy(info->sqinfo[i].desc,ajStrGetPtr(sqdata->de),127); info->sqinfo[i].desc[127]='\0'; info->sqinfo[i].flags |= SQINFO_DESC; } if(sqdata->start || sqdata->stop || sqdata ->len) { info->sqinfo[i].start = sqdata->start; info->sqinfo[i].stop = sqdata->stop; info->sqinfo[i].olen = sqdata->len; info->sqinfo[i].flags |= SQINFO_START; info->sqinfo[i].flags |= SQINFO_STOP; info->sqinfo[i].flags |= SQINFO_OLEN; } if(ajStrGetLen(seq->Selexdata->ss)) { info->sqinfo[i].ss = ajCharNewRes(maxlen+1); p = ajStrGetPtr(seq->Selexdata->ss); q = info->sqinfo[i].ss; while((c==*p)) { if(c=='.' || c==' ' || c=='_' || c=='-') *q++ = c; ++p; } *q = '\0'; info->sqinfo[i].flags |= SQINFO_SS; } } } / * } */ for(i=0; i<n; ++i) { info->sqinfo[i].type = kOtherSeq; if(ajSeqsetIsDna(seqset)) info->sqinfo[i].type = kDNA; if(ajSeqsetIsRna(seqset)) info->sqinfo[i].type = kRNA; if(ajSeqsetIsProt(seqset)) info->sqinfo[i].type = kAmino; info->sqinfo[i].flags |= SQINFO_TYPE; seq = ajSeqsetGetseqSeq(seqset,i); p = ajSeqGetSeqC(seq); cnt = 0; while((c=*p)) { if(!(c=='.' || c==' ' || c=='_' || c=='-' || c=='~')) ++cnt; ++p; } info->sqinfo[i].len = cnt; info->sqinfo[i].flags |= SQINFO_LEN; } *retseqs = seqs; ajStrDel(&tmpstr); return; }
void embConsCalc(const AjPSeqset seqset,const AjPMatrix cmpmatrix, ajint nseqs, ajint mlen,float fplural,float setcase, ajint identity, AjBool gaps, AjPStr *cons) { ajint i; ajint j; ajint k; ajint **matrix; ajint m1 = 0; ajint m2 = 0; ajint highindex; ajint matsize; ajint matchingmaxindex; ajint identicalmaxindex; float max; float contri = 0; float contrj = 0; float *identical; float *matching; AjPSeqCvt cvt = 0; AjPFloat score = NULL; const char **seqcharptr; char res; char nocon = '-'; void *freeptr; matrix = ajMatrixGetMatrix(cmpmatrix); cvt = ajMatrixGetCvt(cmpmatrix); /* return conversion table */ matsize = ajMatrixGetSize(cmpmatrix); AJCNEW(seqcharptr,nseqs); AJCNEW(identical,matsize); AJCNEW(matching,matsize); score = ajFloatNew(); if(ajSeqsetIsNuc(seqset)) /* set non-consensus character */ nocon = 'N'; else if ( ajSeqsetIsProt(seqset)) nocon = 'X'; for(i=0;i<nseqs;i++) /* get sequence as string */ seqcharptr[i] = ajSeqsetGetseqSeqC(seqset, i); for(k=0; k< mlen; k++) { res = nocon; for(i=0;i<matsize;i++) /* reset id's and +ve matches */ { identical[i] = 0.0; matching[i] = 0.0; } for(i=0;i<nseqs;i++) ajFloatPut(&score,i,0.); for(i=0;i<nseqs;i++) /* generate score for columns */ { m1 = ajSeqcvtGetCodeK(cvt,seqcharptr[i][k]); if(m1 || gaps) identical[m1] += ajSeqsetGetseqWeight(seqset,i); for(j=i+1;j<nseqs;j++) { m2 = ajSeqcvtGetCodeK(cvt,seqcharptr[j][k]); if(m1 && m2) { contri = (float)matrix[m1][m2]* ajSeqsetGetseqWeight(seqset,j) +ajFloatGet(score,i); contrj = (float)matrix[m1][m2]* ajSeqsetGetseqWeight(seqset,i) +ajFloatGet(score,j); ajFloatPut(&score,i,contri); ajFloatPut(&score,j,contrj); } } } highindex = -1; max = -(float)INT_MAX; for(i=0;i<nseqs;i++) if( ajFloatGet(score,i) > max || (ajFloatGet(score,i) == max && seqcharptr[highindex][k] == '-') ) { highindex = i; max = ajFloatGet(score,i); } for(i=0;i<nseqs;i++) /* find +ve matches in the column */ { m1 = ajSeqcvtGetCodeK(cvt, seqcharptr[i][k]); if(!matching[m1]) for(j=0;j<nseqs;j++) { /* // if( i != j) // { // m2 = ajSeqcvtGetCodeK(cvt, seqcharptr[j][k]); // if(m1 && m2 && matrix[m1][m2] > 0) // matching[m1] += ajSeqsetGetseqWeight(seqset, j); // } */ m2 = ajSeqcvtGetCodeK(cvt, seqcharptr[j][k]); if(m1 && m2 && matrix[m1][m2] > 0) matching[m1] += ajSeqsetGetseqWeight(seqset, j); if(gaps && !m1 && !m2) matching[m1] += ajSeqsetGetseqWeight(seqset, j); } } matchingmaxindex = 0; /* get max matching and identical */ identicalmaxindex = 0; for(i=0;i<nseqs;i++) { m1 = ajSeqcvtGetCodeK(cvt,seqcharptr[i][k]); if(identical[m1] > identical[identicalmaxindex]) identicalmaxindex = m1; } for(i=0;i<nseqs;i++) { m1 = ajSeqcvtGetCodeK(cvt,seqcharptr[i][k]); if(matching[m1] > matching[matchingmaxindex]) matchingmaxindex = m1; else if(matching[m1] == matching[matchingmaxindex]) if(identical[m1] > identical[matchingmaxindex]) matchingmaxindex = m1; } /* plurality check */ m1 = ajSeqcvtGetCodeK(cvt,seqcharptr[highindex][k]); /* if(matching[m1] >= fplural && seqcharptr[highindex][k] != '-') res = seqcharptr[highindex][k];*/ if(matching[m1] >= fplural) res = seqcharptr[highindex][k]; if(matching[m1]<= setcase) res = tolower((int)res); if(identity) /* if just looking for id's */ { j = 0; for(i=0;i<nseqs;i++) if(matchingmaxindex == ajSeqcvtGetCodeK(cvt,seqcharptr[i][k])) j++; if(j<identity) res = nocon; } ajStrAppendK(cons,res); } freeptr = (void *) seqcharptr; AJFREE(freeptr); AJFREE(matching); AJFREE(identical); ajFloatDel(&score); return; }
int main(int argc, char **argv) { AjPStr cl = NULL; AjPSeqset seqset = NULL; AjPSeqout seqout = NULL; AjPStr stmp = NULL; AjPStr email = NULL; AjPStr fmt = NULL; AjPStr trtab = NULL; const AjPStr ofn = NULL; AjPStr *applist = NULL; AjBool crc = ajFalse; AjBool alt = ajFalse; AjBool iprlook = ajFalse; AjBool goterms = ajFalse; ajint trlen = 0; ajuint i = 0; ajuint lcnt = 0; AjPStr fn = NULL; AjPFile outf = NULL; embInitPV("eiprscan", argc, argv, "IPRSCAN", VERSION); seqset = ajAcdGetSeqset("sequence"); email = ajAcdGetString("email"); crc = ajAcdGetBoolean("crc"); applist = ajAcdGetList("appl"); fmt = ajAcdGetListSingle("format"); trtab = ajAcdGetListSingle("trtable"); trlen = ajAcdGetInt("trlen"); alt = ajAcdGetBoolean("altjobs"); iprlook = ajAcdGetBoolean("iprlookup"); goterms = ajAcdGetBoolean("goterms"); outf = ajAcdGetOutfile("outfile"); stmp = ajStrNew(); cl = ajStrNewC("iprscan -cli"); fn = ajStrNew(); ajFilenameSetTempname(&fn); seqout = ajSeqoutNew(); if(!ajSeqoutOpenFilename(seqout, fn)) ajFatal("Cannot open temporary file %S",fn); ajSeqoutSetFormatC(seqout, "fasta"); ajSeqoutWriteSet(seqout,seqset); ajSeqoutClose(seqout); ajFmtPrintS(&stmp," -i %S",fn); ajStrAppendS(&cl,stmp); if(!ajSeqsetIsProt(seqset)) ajStrAppendC(&cl," -seqtype n"); if(!crc) ajStrAppendC(&cl," -nocrc"); if(ajStrGetLen(email)) { ajFmtPrintS(&stmp," -email %S",email); ajStrAppendS(&cl,stmp); } i = 0; lcnt = 0; while(applist[i]) { ++lcnt; ++i; } i = 0; while(applist[i]) { if(ajStrMatchC(applist[i],"all")) { if(lcnt != 1) ajFatal("Cannot specify 'all' with multiple " "applications"); ++i; continue; } ajFmtPrintS(&stmp," -appl %S",applist[i]); ajStrAppendS(&cl,stmp); ++i; } ajFmtPrintS(&stmp," -format %S",fmt); ajStrAppendS(&cl,stmp); ajFmtPrintS(&stmp," -trtable %S",trtab); ajStrAppendS(&cl,stmp); ajFmtPrintS(&stmp," -trlen %d",trlen); ajStrAppendS(&cl,stmp); if(alt) ajStrAppendC(&cl," -altjobs"); if(iprlook) ajStrAppendC(&cl," -iprlookup"); if(goterms) ajStrAppendC(&cl," -goterms"); ofn = ajFileGetNameS(outf); ajFmtPrintS(&stmp," -o %S",ofn); ajFileClose(&outf); ajStrAppendS(&cl,stmp); #if 0 ajFmtPrint("%S\n",cl); #endif #if 1 system(ajStrGetPtr(cl)); #endif ajSysFileUnlink(fn); ajStrDel(&cl); ajStrDel(&fn); ajStrDel(&stmp); ajStrDel(&email); ajStrDel(&fmt); ajStrDel(&trtab); ajStrDelarray(&applist); ajSeqoutDel(&seqout); ajSeqsetDel(&seqset); embExit(); return 0; }