bool CActiveStormnode::SendStormnodePing(std::string& errorMessage) { if(status != ACTIVE_STORMNODE_STARTED) { errorMessage = "Stormnode is not in a running status"; return false; } CPubKey pubKeyStormnode; CKey keyStormnode; if(!sandStormSigner.SetKey(strStormNodePrivKey, errorMessage, keyStormnode, pubKeyStormnode)) { errorMessage = strprintf("Error upon calling SetKey: %s\n", errorMessage); return false; } LogPrintf("CActiveStormnode::SendStormnodePing() - Relay Stormnode Ping vin = %s\n", vin.ToString()); CStormnodePing snp(vin); if(!snp.Sign(keyStormnode, pubKeyStormnode)) { errorMessage = "Couldn't sign Stormnode Ping"; return false; } // Update lastPing for our stormnode in Stormnode list CStormnode* psn = snodeman.Find(vin); if(psn != NULL) { if(psn->IsPingedWithin(STORMNODE_PING_SECONDS, snp.sigTime)){ errorMessage = "Too early to send Stormnode Ping"; return false; } psn->lastPing = snp; snodeman.mapSeenStormnodePing.insert(make_pair(snp.GetHash(), snp)); //snodeman.mapSeenStormnodeBroadcast.lastPing is probably outdated, so we'll update it CStormnodeBroadcast snb(*psn); uint256 hash = snb.GetHash(); if(snodeman.mapSeenStormnodeBroadcast.count(hash)) snodeman.mapSeenStormnodeBroadcast[hash].lastPing = snp; snp.Relay(); return true; } else { // Seems like we are trying to send a ping while the Stormnode is not registered in the network errorMessage = "Sandstorm Stormnode List doesn't include our Stormnode, shutting down Stormnode pinging service! " + vin.ToString(); status = ACTIVE_STORMNODE_NOT_CAPABLE; notCapableReason = errorMessage; return false; } }
bool CActiveStormnode::Register(CTxIn vin, CService service, CKey keyCollateralAddress, CPubKey pubKeyCollateralAddress, CKey keyStormnode, CPubKey pubKeyStormnode, std::string &errorMessage) { CStormnodeBroadcast snb; CStormnodePing snp(vin); if(!snp.Sign(keyStormnode, pubKeyStormnode)){ errorMessage = strprintf("Failed to sign ping, vin: %s", vin.ToString()); LogPrintf("CActiveStormnode::Register() - %s\n", errorMessage); return false; } snodeman.mapSeenStormnodePing.insert(make_pair(snp.GetHash(), snp)); LogPrintf("CActiveStormnode::Register() - Adding to Stormnode list\n service: %s\n vin: %s\n", service.ToString(), vin.ToString()); snb = CStormnodeBroadcast(service, vin, pubKeyCollateralAddress, pubKeyStormnode, PROTOCOL_VERSION); snb.lastPing = snp; if(!snb.Sign(keyCollateralAddress)){ errorMessage = strprintf("Failed to sign broadcast, vin: %s", vin.ToString()); LogPrintf("CActiveStormnode::Register() - %s\n", errorMessage); return false; } snodeman.mapSeenStormnodeBroadcast.insert(make_pair(snb.GetHash(), snb)); stormnodeSync.AddedStormnodeList(snb.GetHash()); CStormnode* psn = snodeman.Find(vin); if(psn == NULL) { CStormnode sn(snb); snodeman.Add(sn); } else { psn->UpdateFromNewBroadcast(snb); } //send to all peers LogPrintf("CActiveStormnode::Register() - RelayElectionEntry vin = %s\n", vin.ToString()); snb.Relay(); return true; }
void genotype::read_vcf (string genofile, bool isgenoytpe) { igzstream ig (genofile.c_str()); string line; vector<snp> tmp; int j = 0 ; maxgpos = unordered_map<string, double> (); mingpos = unordered_map<string, double>(); maxppos = unordered_map<string, double> (); minppos = unordered_map<string, double>(); chrs = vector<string> (); double prevgpos ; double prevppos; string prevchr=""; int size = 10000; int inc = 10000; int linenum = 0 ; bool setind = false; int numhap = 2; if (isgenotype) numhap = 1; while (std::getline (ig, line) ) { linenum ++; char c = line[0]; if (c=='#') { if (line.find ("CHROM")!=string::npos) { if (!givenind) { vector<string> toks; functions::tokenize (line.c_str(), toks, " \t"); nind = toks.size() - 9 ; nind *= numhap; for (int i = 9 ; i < toks.size(); i++){ string id = toks[i]; indiv.push_back (ind(id,'U',"G")); if (!isgenotype) indiv.push_back (ind(id,'U',"G")); } setind = true; } } } else { vector<string> toks; functions::tokenize (line.c_str(), toks, " \t"); string id = toks[2]; string chr = toks[0]; double physpos = atof(toks[1].c_str()); double genpos = physpos*1.3/1e8; if (id.compare (".")==0) { id = chr+":"+toks[1]; } string ref = toks[3]; string var = toks[4]; string pass = toks[6]; string desc = toks[7]; string format = toks[8]; if (pass.compare("PASS")!=0) continue; if (ref.compare (".")==0) continue; if (desc.compare (".") == 0 ) { cout << "Warning: No INFO field found. Assuming site is a SNP " << endl; } vector<string> tok1; functions::tokenize (desc.c_str(), tok1, ";"); if (tok1[0].compare("SNP")!=0) continue; // Fill the snps if (chr.compare(prevchr)==0) { if (physpos >= prevppos){ prevgpos = genpos; prevppos = physpos; } else { cerr << "Invalid snps in " << genofile <<" : Line " << linenum << "\t Must have non-decreasing physical positions" << endl; // exit (1); } } else { prevchr = chr; prevgpos = genpos; prevppos = physpos; if (snps.find(chr) != snps.end ()) { cerr << "Invalid snps in "<< genofile << " : Line " << linenum << "\t SNPs on a chromosome must be consecutive" << endl; exit (1); } chrs.push_back(chr); snps[chr] = vector<snp>(); // snps[chr].reserve (size); } /* if (j==size){ size += inc; snps[chr].reserve(size); } */ if ( maxppos.find(chr) == maxppos.end()) { maxppos[chr] = physpos; } else if (physpos > maxppos[chr]) maxppos[chr] = physpos; if ( minppos.find(chr) == minppos.end()) { minppos[chr] = physpos; } else if (physpos < minppos[chr]) minppos[chr] = physpos; if ( maxgpos.find(chr) == maxgpos.end()) { maxgpos[chr] = genpos; } else if (genpos > maxgpos[chr]) maxgpos[chr] = genpos; if ( mingpos.find(chr) == mingpos.end()) { mingpos[chr] = genpos; } else if (genpos < mingpos[chr]) mingpos[chr] = genpos; // Fill in the genotypes tok1.resize (0); functions::tokenize (format.c_str(), tok1,":"); int gind = -1; for (int i = 0 ; i < tok1.size(); i++){ if (tok1[i].compare ("GT")==0) { gind = i; break; } } if (gind == -1) continue; snps[chr].push_back (snp(id,chr,physpos,genpos,var,ref)); vector<int> >ype = snps[chr].back().gtype; j++; if (setind) { if (nind != numhap*(toks.size()-9)){ cerr << "Bad genotype in " << genofile << ": Line "<< linenum << endl; exit(1); } } else { nind = toks.size() - 9; nind *= numhap; setind = true; } gtype.resize(nind, 9); for (int i = 9, k = 0; i < toks.size(); i++, k++){ tok1.resize(0); functions::tokenize(toks[i], tok1,","); if (isgenotype) { int a1 = tok1[gind][0]; int a2 = tok1[gind][2]; gtype[k] = (a1-48) + (a2-48); } else { int a1 = tok1[gind][0]; int a2 = tok1[gind][2]; gtype[2*k] = a1-48; gtype[2*k+1] = a2-48; } } } if (j%10000==0) { io::println ("Read " + tostring (j) + " lines" ,0); } } nchr = chrs.size (); nsnps = j; if (io::debug >= 2 ) { cout << "genotype = " << to_string (); } ig.close (); }
void genotype::read_snps (string filename){ ifstream inp (filename.c_str()); if (!inp.is_open()){ cerr << "Error reading file "<< filename <<endl; exit(1); } string line; vector<snp> tmp; int j = 0 ; maxgpos = unordered_map<string, double> (); mingpos = unordered_map<string, double>(); maxppos = unordered_map<string, double> (); minppos = unordered_map<string, double>(); chrs = vector<string> (); double prevgpos ; double prevppos; string prevchr=""; int linenum = 0; while ( std::getline (inp, line)){ linenum ++; char c = line[0]; if (c=='#') continue; istringstream ss (line); if (line.empty()) continue; string id; string chr; double genpos; double physpos; string var; string ref; ss >> id; ss >> chr; ss >> genpos; ss >> physpos; ss >> var; ss >> ref; if (chr.compare(prevchr)==0) { if (physpos >= prevppos){ prevgpos = genpos; prevppos = physpos; } else { cerr << "Invalid snp file " << filename <<" : Line " << linenum << "\t Must have non-decreasing physical positions" << endl; // exit (1); } } else { prevchr = chr; prevgpos = genpos; prevppos = physpos; if (snps.find(chr) != snps.end ()) { cerr << "Invalid snp file "<<filename << " : Line " << linenum << "\t SNPs on a chromosome must be consecutive" << endl; exit (1); } chrs.push_back(chr); snps[chr] = vector<snp>(); } snps[chr].push_back (snp(id,chr,physpos,genpos,var,ref)); j++; if ( maxppos.find(chr) == maxppos.end()) { maxppos[chr] = physpos; } else if (physpos > maxppos[chr]) maxppos[chr] = physpos; if ( minppos.find(chr) == minppos.end()) { minppos[chr] = physpos; } else if (physpos < minppos[chr]) minppos[chr] = physpos; if ( maxgpos.find(chr) == maxgpos.end()) { maxgpos[chr] = genpos; } else if (genpos > maxgpos[chr]) maxgpos[chr] = genpos; if ( mingpos.find(chr) == mingpos.end()) { mingpos[chr] = genpos; } else if (genpos < mingpos[chr]) mingpos[chr] = genpos; } nsnps = j; nchr = chrs.size(); int cumnum = 0; cumnumsnps.resize (nchr); for (int i = 0 ; i < nchr; i++) { numsnps[chrs[i]] = snps[chrs[i]].size(); cumnumsnps[i] = cumnum; cumnum += numsnps[chrs[i]]; allsnps.insert ( snps[chrs[i]].begin(),snps[chrs[i]].end()); vector<snp> &tmpsnps = snps[chrs[i]]; for (int j = 0 ; j < tmpsnps.size(); j++){ snp &s= tmpsnps[j]; if (allsnpsmap.find(s.id) != allsnpsmap.end()){ cerr << "Duplicate SNP ids " << s.id <<endl; exit(1); } allsnpsmap[s.id] = pair<string,int>(s.chr,j); } } inp.close(); }
const real iterate( const int n, real4 pos[], real4 vel[], real4 acc[], real4 jrk[], const real eta, const real eps2, const real dt) { std::vector<real4> acc0(n), jrk0(n); const real dt2 = dt*(1.0/2.0); const real dt3 = dt*(1.0/3.0); for (int i = 0; i < n; i++) { acc0[i] = acc[i]; jrk0[i] = jrk[i]; pos[i].x += dt*(vel[i].x + dt2*(acc[i].x + dt3*jrk[i].x)); pos[i].y += dt*(vel[i].y + dt2*(acc[i].y + dt3*jrk[i].y)); pos[i].z += dt*(vel[i].z + dt2*(acc[i].z + dt3*jrk[i].z)); vel[i].x += dt*(acc[i].x + dt2*jrk[i].x); vel[i].y += dt*(acc[i].y + dt2*jrk[i].y); vel[i].z += dt*(acc[i].z + dt2*jrk[i].z); } forces(n, pos, vel, acc, jrk, eps2); if (dt > 0.0) { const real h = dt*0.5; const real hinv = 1.0/h; const real f1 = 0.5*hinv*hinv; const real f2 = 3.0*hinv*f1; const real dt2 = dt *dt * (1.0/2.0); const real dt3 = dt2*dt * (1.0/3.0); const real dt4 = dt3*dt * (1.0/4.0); const real dt5 = dt4*dt * (1.0/5.0); for (int i = 0; i < n; i++) { /* compute snp & crk */ const real4 Am( acc[i].x - acc0[i].x, acc[i].y - acc0[i].y, acc[i].z - acc0[i].z); const real4 Jm(h*(jrk[i].x - jrk0[i].x), h*(jrk[i].y - jrk0[i].y), h*(jrk[i].z - jrk0[i].z)); const real4 Jp(h*(jrk[i].x + jrk0[i].x), h*(jrk[i].y + jrk0[i].y), h*(jrk[i].z + jrk0[i].z)); real4 snp(f1* Jm.x, f1* Jm.y, f1* Jm.z ); real4 crk(f2*(Jp.x - Am.x), f2*(Jp.y - Am.y), f2*(Jp.z - Am.z)); snp.x -= h*crk.x; snp.y -= h*crk.y; snp.z -= h*crk.z; /* correct */ pos[i].x += dt4*snp.x + dt5*crk.x; pos[i].y += dt4*snp.y + dt5*crk.y; pos[i].z += dt4*snp.z + dt5*crk.z; vel[i].x += dt3*snp.x + dt4*crk.x; vel[i].y += dt3*snp.y + dt4*crk.y; vel[i].z += dt3*snp.z + dt4*crk.z; } } return timestep(n, vel, acc, eta, eps2); }