1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
/* @source wordmatch application
**
** Finds matching words in DNA sequences
**
** @author
** @@
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program 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 General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
******************************************************************************/
/* wordmatch
** Create a word table for the sequences in the sequence set.
** Then iterate over the sequences in the seqall set
** to find word matches. When word matches are found then
** it is checked whether the following characters do also match.
**
*/
#include "emboss.h"
/* @prog wordmatch ************************************************************
**
** Finds all exact matches of a given size between 2 sequences
**
******************************************************************************/
int main(int argc, char **argv)
{
AjPSeqset seqset;
AjPSeqall seqall;
AjPSeq queryseq;
const AjPSeq targetseq;
ajint wordlen;
AjPTable wordsTable = NULL;
AjPList* matchlist = NULL;
AjPFile logfile;
AjPFeattable* seqsetftables = NULL;
AjPFeattable seqallseqftable = NULL;
AjPFeattabOut ftoutforseqsetseq = NULL;
AjPFeattabOut ftoutforseqallseq = NULL;
AjPAlign align = NULL;
AjIList iter = NULL;
ajint targetstart;
ajint querystart;
ajint len;
ajuint i, j;
ajulong nAllMatches = 0;
ajulong sumAllScore = 0;
AjBool dumpAlign = ajTrue;
AjBool dumpFeature = ajTrue;
AjBool checkmode = ajFalse;
EmbPWordRK* wordsw = NULL;
ajuint npatterns = 0;
ajuint seqsetsize;
ajuint nmatches;
ajuint* nmatchesseqset;
ajuint* lastlocation; /* Cursors for Rabin-Karp search. */
/* Shows until what point the query sequence was
* scanned for a pattern sequences in the seqset.
*/
char* paddedheader = NULL;
const char* header;
AjPStr padding;
header = "Pattern %S #pat-sequences #all-matches avg-match-length\n";
padding = ajStrNew();
embInit("wordmatch", argc, argv);
wordlen = ajAcdGetInt("wordsize");
seqset = ajAcdGetSeqset("asequence");
seqall = ajAcdGetSeqall("bsequence");
logfile = ajAcdGetOutfile("logfile");
dumpAlign = ajAcdGetToggle("dumpalign");
dumpFeature = ajAcdGetToggle("dumpfeat");
if(dumpAlign)
{
align = ajAcdGetAlign("outfile");
ajAlignSetExternal(align, ajTrue);
}
seqsetsize = ajSeqsetGetSize(seqset);
ajSeqsetTrim(seqset);
AJCNEW0(matchlist, seqsetsize);
AJCNEW0(seqsetftables, seqsetsize);
AJCNEW0(nmatchesseqset, seqsetsize);
if (dumpFeature)
{
ftoutforseqsetseq = ajAcdGetFeatout("aoutfeat");
ftoutforseqallseq = ajAcdGetFeatout("boutfeat");
}
checkmode = !dumpFeature && !dumpAlign;
embWordLength(wordlen);
ajFmtPrintF(logfile, "Small sequence/file for constructing"
" target patterns: %S\n", ajSeqsetGetUsa(seqset));
ajFmtPrintF(logfile, "Large sequence/file to be scanned"
" for patterns: %S\n", ajSeqallGetUsa(seqall));
ajFmtPrintF(logfile, "Number of sequences in the patterns file: %u\n",
seqsetsize);
ajFmtPrintF(logfile, "Pattern/word length: %u\n", wordlen);
for(i=0;i<seqsetsize;i++)
{
targetseq = ajSeqsetGetseqSeq(seqset, i);
embWordGetTable(&wordsTable, targetseq);
}
AJCNEW0(lastlocation, seqsetsize);
if(ajTableGetLength(wordsTable)>0)
{
npatterns = embWordRabinKarpInit(wordsTable,
&wordsw, wordlen, seqset);
ajFmtPrintF(logfile, "Number of patterns/words found: %u\n", npatterns);
while(ajSeqallNext(seqall,&queryseq))
{
for(i=0;i<seqsetsize;i++)
{
lastlocation[i]=0;
if (!checkmode)
matchlist[i] = ajListstrNew();
}
nmatches = embWordRabinKarpSearch(
ajSeqGetSeqS(queryseq), seqset,
(EmbPWordRK const *)wordsw, wordlen, npatterns,
matchlist, lastlocation, checkmode);
nAllMatches += nmatches;
if (checkmode)
continue;
for(i=0;i<seqsetsize;i++)
{
if(ajListGetLength(matchlist[i])>0)
{
iter = ajListIterNewread(matchlist[i]) ;
while(embWordMatchIter(iter,
&targetstart, &querystart, &len,
&targetseq))
{
if(dumpAlign)
{
ajAlignDefineSS(align, targetseq, queryseq);
ajAlignSetScoreI(align, len);
/* ungapped alignment means same length
* for both sequences
*/
ajAlignSetSubRange(align, targetstart, 1, len,
ajSeqIsReversed(targetseq),
ajSeqGetLen(targetseq),
querystart, 1, len,
ajSeqIsReversed(queryseq),
ajSeqGetLen(queryseq));
}
}
if(dumpAlign)
{
ajAlignWrite(align);
ajAlignReset(align);
}
if(ajListGetLength(matchlist[i])>0 && dumpFeature)
{
embWordMatchListConvToFeat(matchlist[i],
&seqsetftables[i],
&seqallseqftable,
targetseq, queryseq);
ajFeattableWrite(ftoutforseqallseq, seqallseqftable);
ajFeattableDel(&seqallseqftable);
}
ajListIterDel(&iter);
}
embWordMatchListDelete(&matchlist[i]);
}
}
/* search completed, now report statistics */
for(i=0;i<npatterns;i++)
{
sumAllScore += wordsw[i]->lenMatches;
for(j=0;j<wordsw[i]->nseqs;j++)
nmatchesseqset[wordsw[i]->seqindxs[j]] +=
wordsw[i]->nSeqMatches[j];
}
ajFmtPrintF(logfile, "Number of sequences in the file scanned "
"for patterns: %u\n", ajSeqallGetCount(seqall));
ajFmtPrintF(logfile, "Number of all matches: %Lu"
" (wordmatch finds exact matches only)\n", nAllMatches);
if(nAllMatches>0)
{
ajFmtPrintF(logfile, "Sum of match lengths: %Lu\n", sumAllScore);
ajFmtPrintF(logfile, "Average match length: %.2f\n",
sumAllScore*1.0/nAllMatches);
ajFmtPrintF(logfile, "\nDistribution of the matches among pattern"
" sequences:\n");
ajFmtPrintF(logfile, "-----------------------------------------"
"-----------\n");
for(i=0;i<ajSeqsetGetSize(seqset);i++)
{
if (nmatchesseqset[i]>0)
ajFmtPrintF(logfile, "%-42s: %8u\n",
ajSeqGetNameC(ajSeqsetGetseqSeq(seqset, i)),
nmatchesseqset[i]);
ajFeattableWrite(ftoutforseqsetseq, seqsetftables[i]);
ajFeattableDel(&seqsetftables[i]);
}
ajFmtPrintF(logfile, "\nPattern statistics:\n");
ajFmtPrintF(logfile, "-------------------\n");
if(wordlen>7)
ajStrAppendCountK(&padding, ' ', wordlen-7);
paddedheader = ajFmtString(header,padding);
ajFmtPrintF(logfile, paddedheader);
for(i=0;i<npatterns;i++)
if (wordsw[i]->nMatches>0)
ajFmtPrintF(logfile, "%-7s: %12u %12u %17.2f\n",
wordsw[i]->word->fword, wordsw[i]->nseqs,
wordsw[i]->nMatches,
wordsw[i]->lenMatches*1.0/wordsw[i]->nMatches);
}
}
for(i=0;i<npatterns;i++)
{
for(j=0;j<wordsw[i]->nseqs;j++)
AJFREE(wordsw[i]->locs[j]);
AJFREE(wordsw[i]->locs);
AJFREE(wordsw[i]->seqindxs);
AJFREE(wordsw[i]->nnseqlocs);
AJFREE(wordsw[i]->nSeqMatches);
AJFREE(wordsw[i]);
}
embWordFreeTable(&wordsTable);
AJFREE(wordsw);
AJFREE(matchlist);
AJFREE(lastlocation);
AJFREE(nmatchesseqset);
AJFREE(seqsetftables);
if(dumpAlign)
{
ajAlignClose(align);
ajAlignDel(&align);
}
if(dumpFeature)
{
ajFeattabOutDel(&ftoutforseqsetseq);
ajFeattabOutDel(&ftoutforseqallseq);
}
ajFileClose(&logfile);
ajSeqallDel(&seqall);
ajSeqsetDel(&seqset);
ajSeqDel(&queryseq);
ajStrDel(&padding);
AJFREE(paddedheader);
embExit();
return 0;
}
|