BigInt::BigInt(char* n) { if(n == NULL) return; Init(); long long len = strlen(n); long long numBegin = 0; if(*n == '-') { if(len <= 1) return; numBegin = 1; } long long index = 0; for(long long i = len - 1; i >= numBegin && index <= MAXBIT;) { m_element[index] = 0; for(long long j = 1; j < JINZHI && i >= numBegin; j *= 10) { //if(n[i] == ' ' || n[i] == ',') // continue; if(n[i] < '0' || n[i] > '9') return; m_element[index] += (n[i] - '0') * j; i--; } index++; } if(*n == '-') { m_element[MAXBIT - 1] = JINZHI - 1; ToComplement(); } m_isRefined = false; }
// change type: binary Number to BigInt BigInt* BinNumToBigInt(Number* binNum, BigInt* a) { int i; memset(a->bit, 0, BIG_INT_BIT_LEN); // init 0 for (i = 0; i < binNum->len; i++) { a->bit[i] = binNum->value[i]; } // if BigInt is the smallest nagative number // for example, if BigInt bit len is 4, BigInt is 1000 if (binNum->len == BIG_INT_BIT_LEN) { return a; } else { a->bit[SIGN_BIT] = binNum->sign; return ToComplement(a, a); } }
int main(int argc, char **argv) { SeqPool seqpool; bool cs; if( argc < 2 ) { PrintUsage(); return 1; } cs = true; seqpool.Add(argv[1]); for( SamOutput theSam; theSam.Get(); ) { char str[81]; char seq[4096]; char csseq[4096]; int qLen, rLen; int numErr, numLErr, numRErr; int leftLen, rightLen; int i; int Pos; int identity; qLen = strlen(theSam.Qual); identity = IdentityCut; if( theSam.AS < ScoreCut ) continue; if( strncmp( theSam.CIGAR, ItoA(qLen, str, 10), strlen(ItoA(qLen, str, 10)) ) != 0 ) continue; if(cs) { if( theSam.IsPlusStrand() ) strcpy(seq, seqpool.GiveMe(theSam.rName)->c_str()); else ToComplement(seq, seqpool.GiveMe(theSam.rName)->c_str()); } else strcpy(seq, seqpool.GiveMe(theSam.rName)->c_str()); numErr=0; numLErr = 0; numRErr = 0; leftLen = 0; rightLen = 0; rLen = strlen(seq); if(cs) { ToColorSpace(csseq, seq); if( theSam.IsPlusStrand() == 0) Pos = theSam.Pos-2; else Pos = rLen-qLen-theSam.Pos; for(i=1; i<qLen; i++) { if( (i+Pos) < 0 ) continue; if( csseq[i+Pos] != theSam.CS[i+1] ) { numErr++; if( (i+Pos) <= (rLen-41) ) numLErr++; if( (i+Pos) >= 39 ) numRErr++; } if( (i+Pos) <= (rLen-41) ) leftLen++; if( (i+Pos) >= 39 ) rightLen++; } } else { // puts(seq); // puts(theSam.Seq); Pos = theSam.Pos-1; for(i=0; i<qLen; i++) { if( (i+Pos) < 0 ) continue; if( seq[i+Pos] != theSam.Seq[i] ) { numErr++; if( (i+Pos) < (rLen-40) ) numLErr++; if( (i+Pos) >= 40 ) numRErr++; } if( (i+Pos) < (rLen-40) ) leftLen++; if( (i+Pos) >= 40 ) rightLen++; } // printf("%d\t%d\t%d\t%d\n", theSam.Pos, numErr, numLErr, numRErr); } if( numErr > ( qLen*(100-identity)/75 + 1 ) ) { ErrMsg("numErr.\n"); continue; } if( leftLen < LenCut ) continue; if( rightLen < LenCut ) continue; if( numErr > 2 ) continue; if( leftLen < 20 ) if( numLErr > 1 ) continue; if( rightLen < 20 ) if( numRErr > 1 ) continue; theSam.Print(); } return 0; }
// implement of division by using binary search // result = a / b BigInt* DoDiv(BigInt* a, BigInt* b, BigInt* result, BigInt* remainder) { int low, high, mid; BigInt c, d, e, t; low = 0; // the min of left shift high = GetMaxLeftShiftLen(b); // the max of left shift memset(t.bit, 0, BIG_INT_BIT_LEN); // init 0 CopyBigInt(a, &c); // c = a // if a sign == b sign, do subtraction if (a->bit[SIGN_BIT] == b->bit[SIGN_BIT]) { t.bit[SIGN_BIT] = POSITIVE; while (1) { while (low <= high) { mid = (low + high) / 2; ShiftArithmeticLeft(b, mid, &d); DoSub(&c, &d, &e); // e = c - d // e >= 0 if (d.bit[SIGN_BIT] == e.bit[SIGN_BIT] || IsZero(&e)) low = mid + 1; else high = mid - 1; } // high == -1 means c - b < 0 if (high != -1) { t.bit[high] = 1; // here unified the operation // it can improve i think ShiftArithmeticLeft(b, high, &d); // c = c - d, let c be the next dividend DoSub(&c, &d, &c); low = 0; high--; } else { // now the dividend c is the remainder CopyBigInt(&c, remainder); break; } } } // if a sign != b sign, do addition else { t.bit[SIGN_BIT] = NEGATIVE; while (1) { while (low <= high) { mid = (low + high) / 2; ShiftArithmeticLeft(b, mid, &d); DoAdd(&c, &d, &e); // e = c + d // e >= 0 if (d.bit[SIGN_BIT] != e.bit[SIGN_BIT] || IsZero(&e)) low = mid + 1; else high = mid - 1; } // high == -1 means c - b < 0 if (high != -1) { t.bit[high] = 1; // here unified the operation // it can improve i think ShiftArithmeticLeft(b, high, &d); // c = c + d, let c be the next dividend DoAdd(&c, &d, &c); low = 0; high--; } else { // now the dividend c is the remainder CopyBigInt(&c, remainder); break; } } } return ToComplement(&t, result); }
// complement to true form BigInt* ToTrueForm(BigInt* src, BigInt* dst) { return ToComplement(src, dst); }
void BigInt::ToOposite() { m_element[MAXBIT - 1] = JINZHI - 1 - m_element[MAXBIT - 1]; ToComplement(); }