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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
/**
*
* Copyright (c) 2007 Des Higgins, Julie Thompson and Toby Gibson.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <string>
#include <stdio.h>
#include "../alignment/Alignment.h"
#ifdef HAVE_MHASH_H
#include "mhash.h"
#endif
#include "Stats.h"
using namespace std;
namespace clustalw
{
Stats::Stats()
{
enabled = false;
}
Stats::~Stats()
{
}
/* adopted from Sean Eddy'ssquid:aligneval.c */
/* Function: PairwiseIdentity()
*
* Purpose: Calculate the pairwise fractional identity between
* two aligned sequences s1 and s2. This is simply
* (idents / MIN(len1, len2)).
*
* Note how many ways there are to calculate pairwise identity,
* because of the variety of choices for the denominator:
* idents/(idents+mismat) has the disadvantage that artifactual
* gappy alignments would have high "identities".
* idents/(AVG|MAX)(len1,len2) both have the disadvantage that
* alignments of fragments to longer sequences would have
* artifactually low "identities".
*
* Case sensitive; also, watch out in nucleic acid alignments;
* U/T RNA/DNA alignments will be counted as mismatches!
*/
/* float
PairwiseIdentity(char @s1, char @s2)
{
int idents; /@ total identical positions @/
int len1, len2; /@ lengths of seqs @/
int x; /@ position in aligned seqs @/
idents = len1 = len2 = 0;
for (x = 0; s1[x] != '\0' && s2[x] != '\0'; x++)
{
if (!isgap(s1[x])) {
len1++;
if (s1[x] == s2[x]) idents++;
}
if (!isgap(s2[x])) len2++;
}
if (len2 < len1) len1 = len2;
return (len1 == 0 ? 0.0 : (float) idents / (float) len1);
}
*/
/* s1/s2 are unit-offset as usual
*
*/
float
Stats::pairwiseIdentity(Alignment *alnObj, int s1, int s2)
{
int idents; /* total identical positions */
int len1, len2; /* real lengths of seqs */
int x; /* position in aligned seqs */
const vector<int>* seq1 = alnObj->getSequence(s1);
const vector<int>* seq2 = alnObj->getSequence(s2);
idents = len1 = len2 = 0;
// cerr << "comparing " << alnObj->getName(s1).c_str() << ":" << alnObj->getName(s2).c_str() << " " << s1 << ":" << s2 << "\n";
// sequence length should be identical, but be paranoid
for (x = 1; x<=alnObj->getSeqLength(s1) && x<=alnObj->getSeqLength(s2); x++)
{
if (! alnObj->isGap(s1, x)) {
len1++;
//cerr << " pos " << x << ": " << (*seq1)[x] << ":" << (*seq2)[x] << "\n";
if ((*seq1)[x] == (*seq2)[x])
idents++;
}
//DEBUG
//else {
//cerr << " gap at pos " << x << " (" << s1 << ")\n";
//}
if (! alnObj->isGap(s2, x))
len2++;
//DEBUG
//else
// cerr << " gap at pos " << x << " (" << s2 << ")\n";
}
if (len2 < len1)
len1 = len2;
return (len1 == 0 ? 0.0 : (float) idents / (float) len1);
}
#ifdef HAVE_MHASH_H
string
Stats::ConcatInputHash(Alignment *alnObj)
{
vector<string> rawSeqArray;
string ret;
char *hash;
// collect all sequences and sort
const clustalw::SeqArray* seqArray = alnObj->getSeqArray();
for(int s = 1; s <= alnObj->getNumSeqs(); s++)
{
string seq;
for(int r = 1; r <= alnObj->getSeqLength(s); r++)
{
int val = (*seqArray)[s][r];
seq.append(1, clustalw::userParameters->getAminoAcidCode(val));
}
rawSeqArray.push_back(seq);
}
std::sort(rawSeqArray.begin(), rawSeqArray.end());
// concatenate sorted seqs
string concatSeq;
std::vector<string>::iterator iter;
for(iter=rawSeqArray.begin(); iter != rawSeqArray.end(); ++iter)
concatSeq.append(*iter);
// build hash and return
hash = Md5Hash(concatSeq.c_str());
if (hash==NULL)
{
ret="HASHING_FAILURE";
} else {
for (int i=0; i<strlen(hash); i++)
ret.append(1, hash[i]);
free(hash);
}
return ret;
}
string
Stats::Md5ForSeq(Alignment *alnObj, int s)
{
string seq;
const clustalw::SeqArray* seqArray = alnObj->getSeqArray();
char *hash;
string ret;
for(int l = 1; l <= alnObj->getSeqLength(s); l++)
{
int val = (*seqArray)[s][l];
// continue if gap
if((val < 0) || (val > userParameters->getMaxAA()))
continue;
seq.append(1, clustalw::userParameters->getAminoAcidCode(val));
}
std::transform(seq.begin(), seq.end(), seq.begin(), ::toupper);
hash = Md5Hash(seq.c_str());
if (hash==NULL)
{
ret = "HASHING_FAILURE";
} else {
ret = hash;
}
return ret;
}
/* create md5 hash from input string
* returns NULL on failure
* user must free returned string
*/
char *
Stats::Md5Hash(const char *thread)
{
MHASH td;
char *tmpcstr;
int i;
unsigned char *hash;
char *rethash;
td = mhash_init(MHASH_MD5);
if (td == MHASH_FAILED)
return NULL;
if (thread==NULL)
return NULL;
mhash(td, thread, strlen(thread));
//mhash_deinit(td, hash);
hash = (unsigned char*) mhash_end(td);
rethash = (char*) calloc(mhash_get_block_size(MHASH_MD5)*2+1, sizeof(char));
for (i = 0; i < mhash_get_block_size(MHASH_MD5); i++) {
sprintf(&rethash[i*2], "%.2x", hash[i]);
}
return rethash;
}
#endif
// HAVE_MHASH_H
void
Stats::logCmdLine(int argc, char **argv)
{
FILE *fp = fopen(logfilename.c_str(), "a");
if (fp == NULL)
{
cerr << "couldn't open file " << logfilename << " for logging of stats\n";
return;
}
if (argc > 1)
{
for (int i=1; i<argc; i++)
{
// remove non-interesting stuff
if (strstr(argv[i], "-infile=")==NULL &&
strstr(argv[i], "-outfile=")==NULL &&
strstr(argv[i], "-stats=")==NULL &&
strstr(argv[i], "-align")==NULL)
{
fprintf(fp, "cmdline non-default arg: %s\n", argv[i]);
}
}
}
}
/* log some statistics for input sequences, i.e. alnObj should hold
* the unaligned sequences
*
*/
void
Stats::logInputSeqStats(Alignment *alnObj)
{
int i;
std::vector<double> lengths;
time_t t = time(NULL);
tm s = *localtime(&t);
int shortest;
string hash;
FILE *fp = fopen(logfilename.c_str(), "a");
if (fp == NULL)
{
cerr << "couldn't open file " << logfilename << " for logging of stats\n";
return;
}
fprintf(fp, "logging job: %s on %s", userParameters->getSeqName().c_str(), asctime(&s));
fprintf(fp, "clustal version: %s\n", userParameters->getRevisionLevel().c_str());
fprintf(fp, "seq type: ");
if (userParameters->getDNAFlag())
fprintf(fp, "DNA");
else
fprintf(fp, "protein");
fprintf(fp, "\n");
fprintf(fp, "numseqs: %d\n", alnObj->getNumSeqs());
// create a vector of seq lengths for later
// and get shortest seq at the same time
shortest=alnObj->getLengthLongestSequence();
for (i = 1; i <= alnObj->getNumSeqs(); i++) {
int l = alnObj->getSeqLength(i);
lengths.push_back(l);
if (l<shortest)
shortest=l;
}
fprintf(fp, "seqlen longest: %d\n", alnObj->getLengthLongestSequence());
fprintf(fp, "seqlen shortest: %d\n", shortest);
fprintf(fp, "seqlen avg: %.2f\n", utilityObject->average(lengths));
fprintf(fp, "seqlen std-dev: %.2f\n", utilityObject->stdDev(lengths));
fprintf(fp, "seqlen median: %.2f\n", utilityObject->median(lengths));
#ifdef HAVE_MHASH_H
//hash = concatInputHash(alnObj);
//fprintf(fp, "seq hash: %s\n", hash.c_str());
for (int s = 1; s <= alnObj->getNumSeqs(); s++) {
string md5 = Md5ForSeq(alnObj, s);
fprintf(fp, "md5 for seq %d: %s\n", s, md5.c_str());
}
#else
fprintf(fp, "md5: disabled\n");
#endif
fclose(fp);
}
/* log some statistics for aligned sequences, i.e. alnObj should hold
* the already aligned sequences
*
*/
void
Stats::logAlignedSeqStats(Alignment *alnObj)
{
FILE *fp = fopen(logfilename.c_str(), "a");
if (fp == NULL)
{
cerr << "couldn't open file " << logfilename << " for logging of stats\n";
return;
}
// alignment length is the length of any sequence
fprintf(fp, "aln len: %d\n", alnObj->getSeqLength(1));
std::vector<double> pwIdents;
double lowestPwId = 1.0;
double hightestPwId = 0.0;
// create vector of pairwise identities
for(int s1 = 1; s1 <= alnObj->getNumSeqs(); s1++)
{
for(int s2 = s1+1; s2 <= alnObj->getNumSeqs(); s2++)
{
double thisPwId = pairwiseIdentity(alnObj, s1, s2);
pwIdents.push_back(thisPwId);
if (thisPwId>hightestPwId)
hightestPwId=thisPwId;
if (thisPwId<lowestPwId)
lowestPwId=thisPwId;
//fprintf(fp, "ident %s:%s %d:%d=%f\n", alnObj->getName(s1).c_str(), alnObj->getName(s2).c_str(), s1, s2, PairwiseIdentity(alnObj, s1, s2));
}
}
fprintf(fp, "aln pw-id highest: %.2f\n", hightestPwId);
fprintf(fp, "aln pw-id lowest: %.2f\n", lowestPwId);
fprintf(fp, "aln pw-id avg: %.2f\n", utilityObject->average(pwIdents));
fprintf(fp, "aln pw-id std-dev: %.2f\n", utilityObject->stdDev(pwIdents));
fprintf(fp, "aln pw-id median: %.2f\n", utilityObject->median(pwIdents));
fclose(fp);
}
}
|