byte* readFile(string filename) { FILE* pFile; long lSize; char* buffer; size_t result; pFile = fopen(filename.c_str(), "r"); if (pFile == NULL) { throw UnknownFile(); } // obtain file size: fseek(pFile, 0, SEEK_END); lSize = ftell(pFile); rewind(pFile); // allocate memory to contain the whole file: buffer = (char*) malloc(sizeof(char) * lSize); if (buffer == NULL) { throw MemoryError(); } // copy the file into the buffer: result = fread(buffer, 1, lSize,pFile); if ((long)result != lSize) { throw ReadingError(); } // terminate fclose (pFile); return (byte*) buffer; }
/* Function to open a joystick for use. The joystick to open is specified by the index field of the joystick. This should fill the nbuttons and naxes fields of the joystick structure. It returns 0, or -1 if there is an error. */ int STJoystick::Sys_OpenJoystick(int device_index) { int fd; SDL_logical_joydecl(int realindex); SDL_logical_joydecl(STJoystick::STJoystickData* realjoy = NULL); /* Open the joystick and set the joystick file descriptor */ #ifndef NO_LOGICAL_JOYSTICKS if (SDL_joylist[device_index].fname == NULL) { SDL_joylist_head(realindex, device_index); int opened_success = Sys_OpenJoystick(realindex); if (opened_success == -1) return(-1); realjoy = joysticks[device_index]; fd = realjoy->hwdata->fd; } else { fd = open(SDL_joylist[device_index].fname, O_RDONLY, 0); } SDL_joylist[device_index].joy = joysticks[device_index]; #else fd = open(SDL_joylist[device_index].fname, O_RDONLY, 0); #endif if ( fd < 0 ) { Error("Unable to open device in Sys_OpenJoystick()\n"); return(-1); } joysticks[device_index]->hwdata = new joystick_hwdata; if ( joysticks[device_index]->hwdata == NULL ) { MemoryError(); close(fd); return(-1); } memset(joysticks[device_index]->hwdata, 0, sizeof(*joysticks[device_index]->hwdata)); joysticks[device_index]->hwdata->fd = fd; /* Set the joystick to non-blocking read mode */ fcntl(fd, F_SETFL, O_NONBLOCK); /* Get the number of buttons and axes on the joystick */ #ifndef NO_LOGICAL_JOYSTICKS if (realjoy) ConfigLogicalJoystick(joysticks[device_index]); else #endif #if SDL_INPUT_LINUXEV if ( ! EV_ConfigJoystick(joysticks[device_index], fd) ) #endif JS_ConfigJoystick(joysticks[device_index], fd); return(0); }
bool cc2DLabel::fromFile_MeOnly(QFile& in, short dataVersion, int flags) { if (!ccHObject::fromFile_MeOnly(in, dataVersion, flags)) return false; //points count (dataVersion >= 20) uint32_t count = 0; if (in.read((char*)&count,4) < 0) return ReadError(); //points & associated cloud ID (dataVersion >= 20) assert(m_points.empty()); for (uint32_t i=0; i<count; ++i) { //point index uint32_t index = 0; if (in.read((char*)&index,4) < 0) return ReadError(); //cloud ID (will be retrieved later --> make sure that the cloud is saved alongside!) uint32_t cloudID = 0; if (in.read((char*)&cloudID,4) < 0) return ReadError(); //[DIRTY] WARNING: temporarily, we set the cloud unique ID in the 'PickedPoint::cloud' pointer!!! PickedPoint pp; pp.index = (unsigned)index; *(uint32_t*)(&pp.cloud) = cloudID; m_points.push_back(pp); if (m_points.size() != i+1) return MemoryError(); } //Relative screen position (dataVersion >= 20) if (in.read((char*)m_screenPos,sizeof(float)*2) < 0) return ReadError(); //Collapsed state (dataVersion >= 20) if (in.read((char*)&m_showFullBody,sizeof(bool)) < 0) return ReadError(); if (dataVersion > 20) { //Show in 2D boolean (dataVersion >= 21) if (in.read((char*)&m_dispIn2D,sizeof(bool)) < 0) return ReadError(); //Show in 3D boolean (dataVersion >= 21) if (in.read((char*)&m_dispIn3D,sizeof(bool)) < 0) return ReadError(); } return true; }
void* __wrap_realloc( void *ptr, size_t size ) { LockAlloc(); void *new_ptr = __real_realloc( ptr, size ); if( new_ptr == NULL && size != 0 && IsSingleLocked() && g_bMemoryAssert ) { TempLog( "realloc failed: %p %ubytes\r\n", ptr, size ); MemoryError(); } UnlockAlloc(); return new_ptr; }
void* __wrap_malloc( size_t size ) { LockAlloc(); void *ptr = __real_malloc( size ); if( ptr == NULL && size != 0 && IsSingleLocked() && g_bMemoryAssert ) { TempLog( "malloc failed: %ubytes\r\n", size ); MemoryError(); } UnlockAlloc(); return ptr; }
void* __wrap_memalign( size_t align, size_t size ) { LockAlloc(); void *ptr = __real_memalign( align, size ); if( ptr == NULL && size > 0 && IsSingleLocked() && g_bMemoryAssert ) { TempLog( "memalign failed: %ubytes(aligned %u)\r\n", size, align ); MemoryError(); } UnlockAlloc(); return ptr; }
bool ccMaterialSet::fromFile_MeOnly(QFile& in, short dataVersion, int flags) { if (!ccHObject::fromFile_MeOnly(in, dataVersion, flags)) return false; //Materials count (dataVersion>=20) uint32_t count = 0;; if (in.read((char*)&count,4)<0) return ReadError(); if (count == 0) return true; //Read each material try { resize(count); } catch (.../*const std::bad_alloc&*/) //out of memory { return MemoryError(); } for (ccMaterialSet::iterator it = begin();it!=end();++it) { //material name (dataVersion>=20) QDataStream inStream(&in); inStream >> it->name; //texture (dataVersion>=20) inStream >> it->texture; //material colors (dataVersion>=20) if (in.read((char*)it->diffuseFront,sizeof(float)*4)<0) return ReadError(); if (in.read((char*)it->diffuseBack,sizeof(float)*4)<0) return ReadError(); if (in.read((char*)it->ambient,sizeof(float)*4)<0) return ReadError(); if (in.read((char*)it->specular,sizeof(float)*4)<0) return ReadError(); if (in.read((char*)it->emission,sizeof(float)*4)<0) return ReadError(); //material shininess (dataVersion>=20) inStream >> it->shininessFront; inStream >> it->shininessBack; } return true; }
/* * Initializes a GIF extension object. */ void GIFInitExtension(struct GIFExtension *ext, struct GIFScreen *screen, unsigned int initBufferSize) { unsigned char *newBuffer; ext->Screen = screen; if (initBufferSize > 0) { newBuffer = (unsigned char *)malloc(initBufferSize); if (newBuffer == NULL) MemoryError(); ext->Buffer = newBuffer; ext->BufferSize = initBufferSize; } else { ext->Buffer = NULL; ext->BufferSize = 0; } }
bool ccIndexedTransformationBuffer::fromFile_MeOnly(QFile& in, short dataVersion, int flags) { if (!ccHObject::fromFile_MeOnly(in, dataVersion, flags)) return false; //vector size (dataVersion>=34) uint32_t count = 0; if (in.read((char*)&count,4) < 0) return ReadError(); //try to resize the vector accordingly try { resize(count); } catch (const std::bad_alloc&) { //not enough memory return MemoryError(); } //transformations (dataVersion>=34) for (ccIndexedTransformationBuffer::iterator it=begin(); it!=end(); ++it) if (!it->fromFile(in, dataVersion, flags)) return false; //display options { //Show polyline (dataVersion>=34) if (in.read((char*)&m_showAsPolyline,sizeof(bool)) < 0) return ReadError(); //Show trihedrons (dataVersion>=34) if (in.read((char*)&m_showTrihedrons,sizeof(bool)) < 0) return ReadError(); //Display scale (dataVersion>=34) if (in.read((char*)&m_trihedronsScale,sizeof(float)) < 0) return ReadError(); } return true; }
bool ccMaterialSet::fromFile_MeOnly(QFile& in, short dataVersion, int flags) { if (!ccHObject::fromFile_MeOnly(in, dataVersion, flags)) return false; //Materials count (dataVersion>=20) uint32_t count = 0;; if (in.read((char*)&count,4) < 0) return ReadError(); if (count == 0) return true; //Read each material try { resize(count); } catch (.../*const std::bad_alloc&*/) //out of memory { return MemoryError(); } QDataStream inStream(&in); for (ccMaterialSet::iterator it = begin(); it!=end(); ++it) { //material name (dataVersion>=20) inStream >> it->name; if (dataVersion < 37) { //texture (dataVersion>=20) QImage texture; inStream >> texture; it->setTexture(texture,QString(),false); } else {
/* * Reads the next GIF extension. */ static void GIFReadNextExtension(struct GIFExtension *ext, FILE *stream) { unsigned char *newBuffer; unsigned int newBufferSize; unsigned int offset, len; int count, label; GIF_FGETC(label, stream); GIF_TRACE(("Reading Extension (0x%X)\n", label)); if (ext == NULL) { GIFSkipDataBlocks(stream); return; } ext->Label = (unsigned char)label; offset = 0; len = ext->BufferSize; for ( ; ; ) { if (len < GIF_UCHAR_MAX) { newBufferSize = ext->BufferSize + 1024; newBuffer = (unsigned char *)realloc(ext->Buffer, newBufferSize); if (newBuffer == NULL) MemoryError(); ext->BufferSize = newBufferSize; ext->Buffer = newBuffer; len += 1024; } count = GIFReadDataBlock(ext->Buffer + offset, stream); if (count == 0) break; offset += count; len -= count; } }
///////////////////////////////////////////////////////////////////////////////////// // Constructor FullAlignment::FullAlignment(int maxseqdis) { qa = new HalfAlignment(maxseqdis); ta = new HalfAlignment(maxseqdis); if (!qa || !ta) MemoryError("space for formatting HMM-HMM alignment", __FILE__, __LINE__, __func__); }
NTL_START_IMPL void WordVector::DoSetLength(long n) { long m; if (n < 0) { LogicError("negative length in vector::SetLength"); } if (NTL_OVERFLOW(n, NTL_BITS_PER_LONG, 0)) ResourceError("length too big in vector::SetLength"); if (n == 0) { if (rep) rep[-1] = 0; return; } if (!rep) { m = ((n+NTL_WordVectorMinAlloc-1)/NTL_WordVectorMinAlloc) * NTL_WordVectorMinAlloc; if (NTL_OVERFLOW(m, NTL_BITS_PER_LONG, 0)) ResourceError("length too big in vector::SetLength"); _ntl_ulong *p = (_ntl_ulong *) NTL_SNS_MALLOC(m, sizeof(_ntl_ulong), 2*sizeof(_ntl_ulong)); if (!p) { MemoryError(); } rep = p+2; rep[-1] = n; rep[-2] = m << 1; return; } long max_length = (rep[-2] >> 1); if (n <= max_length) { rep[-1] = n; return; } long frozen = (rep[-2] & 1); if (frozen) LogicError("Cannot grow this WordVector"); m = max(n, long(NTL_WordVectorExpansionRatio*max_length)); m = ((m+NTL_WordVectorMinAlloc-1)/NTL_WordVectorMinAlloc)*NTL_WordVectorMinAlloc; _ntl_ulong *p = rep - 2; if (NTL_OVERFLOW(m, NTL_BITS_PER_LONG, 0)) ResourceError("length too big in vector::SetLength"); p = (_ntl_ulong *) NTL_SNS_REALLOC(p, m, sizeof(_ntl_ulong), 2*sizeof(_ntl_ulong)); if (!p) { MemoryError(); } rep = p+2; rep[-1] = n; rep[-2] = m << 1; }
bool ccColorScale::fromFile(QFile& in, short dataVersion, int flags) { if (dataVersion < 27) //structure appeared at version 27! return false; QDataStream inStream(&in); //name (dataVersion>=27) inStream >> m_name; //UUID (dataVersion>=27) inStream >> m_uuid; //relative state (dataVersion>=27) if (in.read((char*)&m_relative,sizeof(bool)) < 0) return ReadError(); //Absolute min value (dataVersion>=27) if (in.read((char*)&m_absoluteMinValue,sizeof(double)) < 0) return ReadError(); //Absolute range (dataVersion>=27) if (in.read((char*)&m_absoluteRange,sizeof(double)) < 0) return ReadError(); //locked state (dataVersion>=27) if (in.read((char*)&m_locked,sizeof(bool)) < 0) return ReadError(); //steps list (dataVersion>=27) { //steps count uint32_t stepCount = 0; if (in.read((char*)&stepCount,4) < 0) return ReadError(); //read each step m_steps.clear(); for (uint32_t i=0; i<stepCount; ++i) { double relativePos = 0.0; QColor color(Qt::white); inStream >> relativePos; inStream >> color; m_steps.push_back(ccColorScaleElement(relativePos,color)); } update(); } //custom labels (dataVersion>=40) if (dataVersion >= 40) { //custom label count uint32_t labelCount = 0; if (in.read((char*)&labelCount,4) < 0) return ReadError(); try { for (uint32_t i=0; i<labelCount; ++i) { double label = 0.0; inStream >> label; m_customLabels.insert(label); } } catch (const std::bad_alloc&) { //not enough memory return MemoryError(); } }
//TODO: inline Viterbi::BacktraceScore Viterbi::ScoreForBacktrace(HMMSimd* q_four, HMMSimd* t_four, int elem,Viterbi::BacktraceResult * backtraceResult, float alignmentScore[VECSIZE_FLOAT], int ss_hmm_mode) { // Allocate new space for alignment scores const HMM * q = (const HMM *) q_four->GetHMM(elem); const HMM * t = (const HMM *) t_four->GetHMM(elem); char * states=backtraceResult->states; int * i_steps=backtraceResult->i_steps; int * j_steps=backtraceResult->j_steps; int nsteps=backtraceResult->count; float * S=new float[nsteps+1]; float * S_ss=new float[nsteps+1]; if (!S_ss) MemoryError("space for HMM-HMM alignments", __FILE__, __LINE__, __func__); // Add contribution from secondary structure score, record score a long alignment, // and record template consensus sequence in master-slave-alignment to query sequence float score_ss=0.0f; float score=alignmentScore[elem]; float score_sort = 0.0f; float score_aass = 0.0f; float Pvalt = 1.0f; float logPvalt = 0.0f; for (int step=1; step<=nsteps; step++) { switch(states[step]) { case ViterbiMatrix::MM: S[step] = Score(q->p[i_steps[step]],t->p[j_steps[step]]); // printf("i=%d j=%d S=%d\n", i_steps[step], j_steps[step], Score(q->p[i_steps[step]],t->p[j_steps[step]])); S_ss[step] = ScoreSS(q,t,i_steps[step],j_steps[step], ssw, ss_hmm_mode, S73, S37, S33); score_ss += S_ss[step]; break; case ViterbiMatrix::MI: //if gap in template case ViterbiMatrix::DG: default: //if gap in T or Q S[step]=S_ss[step]=0.0f; break; } } // printf("###old score %f\t",score); if (ss_mode == Hit::SCORE_ALIGNMENT) score-=score_ss; // subtract SS score added during alignment!!!! // Add contribution from correlation of neighboring columns to score float Scorr=0; if (nsteps) { for (int step=2; step<=nsteps; step++) Scorr+=S[step]*S[step-1]; for (int step=3; step<=nsteps; step++) Scorr+=S[step]*S[step-2]; for (int step=4; step<=nsteps; step++) Scorr+=S[step]*S[step-3]; for (int step=5; step<=nsteps; step++) Scorr+=S[step]*S[step-4]; score+=correlation*Scorr; // printf("Scorr=%f\t",Scorr); // printf("correlation=%f\t",correlation); } // printf("new=%f\n",score); // Set score, P-value etc. score_sort = score_aass = -score; if (t->mu) { logPvalt=logPvalue(score,t->lamda,t->mu); Pvalt =Pvalue(score,t->lamda,t->mu); } else { logPvalt=0; Pvalt=1;} // printf("%-10.10s lamda=%-9f score=%-9f logPval=%-9g\n",name,t->lamda,score,logPvalt); //DEBUG: Print out Viterbi path Viterbi::BacktraceScore backtraceScore; backtraceScore.score_ss=score_ss; backtraceScore.score=score; backtraceScore.score_sort=score_sort; backtraceScore.score_aass=score_aass; backtraceScore.Pvalt=Pvalt; backtraceScore.logPvalt=logPvalt; backtraceScore.S=S; backtraceScore.S_ss=S_ss; if (Log::reporting_level() >= DEBUG1) { Viterbi::PrintDebug(q,t,&backtraceScore,backtraceResult, ss_mode); } return backtraceScore; }
void PosteriorDecoder::backtraceMAC(HMM & q, HMM & t, PosteriorMatrix & p_mm, ViterbiMatrix & backtrace_matrix, const int elem, Hit & hit, float corr) { // Trace back trough the matrix b[i][j] until STOP state is found LogLevel actual_level = Log::reporting_level(); int step; // counts steps in path through 5-layered dynamic programming matrix int i,j; // query and template match state indices initializeBacktrace(t,hit); // Make sure that backtracing stops when t:M1 or q:M1 is reached (Start state), e.g. sMM[i][1], or sIM[i][1] (M:MM, B:IM) for (i = 0; i <= q.L; ++i) backtrace_matrix.setMatMat(i, 1, elem, ViterbiMatrix::STOP); // b[i][1] = STOP; for (j = 1; j <= t.L; ++j) backtrace_matrix.setMatMat(1, j, elem, ViterbiMatrix::STOP); // b[1][j] = STOP; // Back-tracing loop // In contrast to the Viterbi-Backtracing, STOP signifies the first Match-Match state, NOT the state before the first MM state hit.matched_cols = 1; // for each MACTH (or STOP) state matched_col is incremented by 1 hit.state = ViterbiMatrix::MM; // lowest state with maximum score must be match-match state step = 0; // steps through the matrix correspond to alignment columns (from 1 to nsteps) i = hit.i2; j = hit.j2; // last aligned pair is (i2,j2) if (backtrace_matrix.getMatMat(i, j, elem) != ViterbiMatrix::MM) { // b[i][j] != MM if (Log::reporting_level() > DEBUG) fprintf(stderr,"Error: backtrace does not start in match-match state, but in state %i, (i,j)=(%i,%i)\n",backtrace_matrix.getMatMat(i, j, elem),i,j); step = 0; hit.i[step] = i; hit.j[step] = j; hit.alt_i->push_back(i); hit.alt_j->push_back(j); hit.state = ViterbiMatrix::STOP; } else { while (hit.state != ViterbiMatrix::STOP) { step++; hit.states[step] = hit.state = backtrace_matrix.getMatMat(i, j, elem); // b[i][j]; hit.i[step] = i; hit.j[step] = j; hit.alt_i->push_back(i); hit.alt_j->push_back(j); // Exclude cells in direct neighbourhood from all further alignments for (int ii = imax(i-2,1); ii <= imin(i+2, q.L); ++ii) // hit.cell_off[ii][j] = 1; backtrace_matrix.setCellOff(ii, j, elem, true); for (int jj = imax(j-2,1); jj <= imin(j+2, t.L); ++jj) backtrace_matrix.setCellOff(i, jj, elem, true); if (hit.state == ViterbiMatrix::MM) hit.matched_cols++; switch (hit.state) { case ViterbiMatrix::MM: i--; j--; break; case ViterbiMatrix::IM: j--; break; case ViterbiMatrix::MI: i--; break; case ViterbiMatrix::STOP: break; default: fprintf(stderr,"Error: unallowed state value %i occurred during backtracing at step %i, (i,j)=(%i,%i)\n", hit.state, step, i, j); hit.state = 0; actual_level = DEBUG1; break; } //end switch (state) } //end while (state) } hit.i1 = hit.i[step]; hit.j1 = hit.j[step]; hit.states[step] = ViterbiMatrix::MM; // first state (STOP state) is set to MM state hit.nsteps = step; // Allocate new space for alignment scores hit.S = new float[hit.nsteps+1]; hit.S_ss = new float[hit.nsteps+1]; hit.P_posterior = new float[hit.nsteps+1]; if (!hit.P_posterior) MemoryError("space for HMM-HMM alignments", __FILE__, __LINE__, __func__); // Add contribution from secondary structure score, record score along alignment, // and record template consensus sequence in master-slave-alignment to query sequence hit.score_ss = 0.0f; hit.sum_of_probs = 0.0; // number of identical residues in query and template sequence int ssm = hit.ssm1 + hit.ssm2; // printf("Hit=%s\n",name); ///////////////////////////////////////////////////////////// for (step = 1; step <= hit.nsteps; step++) { switch(hit.states[step]) { case ViterbiMatrix::MM: i = hit.i[step]; j = hit.j[step]; hit.S[step] = Score(q.p[i], t.p[j]); hit.S_ss[step] = Viterbi::ScoreSS(&q, &t, i, j, ssw, ssm, S73, S37, S33); hit.score_ss += hit.S_ss[step]; // hit.P_posterior[step] = powf(2, p_mm.getPosteriorValue(hit.i[step], hit.j[step], elem)); hit.P_posterior[step] = p_mm.getPosteriorValue(hit.i[step], hit.j[step]); // Add probability to sum of probs if no dssp states given or dssp states exist and state is resolved in 3D structure if (t.nss_dssp<0 || t.ss_dssp[j]>0) hit.sum_of_probs += hit.P_posterior[step]; // printf("j=%-3i P=%4.2f sum=%6.2f\n",j, hit.P_posterior[step],hit.sum_of_probs); ////////////////////////// break; case ViterbiMatrix::MI: //if gap in template case ViterbiMatrix::DG: default: //if gap in T or Q hit.S[step] = hit.S_ss[step] = hit.P_posterior[step] = 0.0; break; } } // printf("\n"); ///////////////////////////////////////////////////////////// if (hit.ssm2 >= 1) hit.score -= hit.score_ss; // subtract SS score added during alignment!!!! // Add contribution from correlation of neighboring columns to score float Scorr = 0; if (hit.nsteps) { for (step = 1; step <= hit.nsteps-1; step++) Scorr += hit.S[step] * hit.S[step+1]; for (step = 1; step <= hit.nsteps-2; step++) Scorr += hit.S[step] * hit.S[step+2]; for (step = 1; step <= hit.nsteps-3; step++) Scorr += hit.S[step] * hit.S[step+3]; for (step = 1; step <= hit.nsteps-4; step++) Scorr += hit.S[step] * hit.S[step+4]; hit.score += corr * Scorr; } // Set score, P-value etc. hit.score_sort = hit.score_aass = -hit.score; hit.logPval = 0; hit.Pval = 1; if (t.mu) { hit.logPvalt = logPvalue(hit.score, t.lamda, t.mu); hit.Pvalt = Pvalue(hit.score,t.lamda,t.mu); } else { hit.logPvalt = 0; hit.Pvalt = 1; } // printf("%-10.10s lamda=%-9f score=%-9f logPval=%-9g\n",name,t.lamda,score,logPvalt); //DEBUG: Print out MAC alignment path //TODO bad debugging code if (actual_level >= DEBUG1) { float sum_post = 0.0; printf("NAME=%7.7s score=%7.3f score_ss=%7.3f\n", hit.name, hit.score, hit.score_ss); printf("step Q T i j state score T Q cf ss-score P_post Sum_post\n"); for (step = hit.nsteps; step >= 1; step--) { switch(hit.states[step]) { case ViterbiMatrix::MM: sum_post += hit.P_posterior[step]; printf("%4i %1c %1c ",step,q.seq[q.nfirst][hit.i[step]], hit.seq[hit.nfirst][hit.j[step]]); break; case ViterbiMatrix::IM: printf("%4i - %1c ",step, hit.seq[hit.nfirst][hit.j[step]]); break; case ViterbiMatrix::MI: printf("%4i %1c - ",step,q.seq[q.nfirst][hit.i[step]]); break; } printf("%4i %4i %2i %7.1f ", hit.i[step], hit.j[step],(int)hit.states[step], hit.S[step]); printf("%c %c %1i %7.1f ", i2ss(t.ss_dssp[hit.j[step]]),i2ss(q.ss_pred[hit.i[step]]),q.ss_conf[hit.i[step]]-1, hit.S_ss[step]); printf("%7.5f %7.2f\n", hit.P_posterior[step],sum_post); } } return; }