Beispiel #1
0
/* Generates a public/private key-pair. The keys are retured in a 
 * KeyPair. The generated keys are 
 * 2 * "digitCount" or 2 * "digitCount - 1 digits long, 
 * and have the probability of at least 1 - 4^(-k) of being prime. 
 * For k = 3, that probability is 98.4375%, 
 * and for k = 4 it is 99.609375%. 
 * 
 * k = 3 is recommended by Introduction to Algorithms, Second Edition;
 * by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest and 
 * Clifford Stein for prime number generation. 
 * */
KeyPair RSA::GenerateKeyPair(	unsigned long int digitCount, 
								unsigned long int k)
{
	//generate two random numbers p and q, each "digitCount" digits long
	BigInt p(PrimeGenerator::Generate(digitCount, k));
	BigInt q(PrimeGenerator::Generate(digitCount, k));
	
	//make sure they are different
	while (p == q)
	{
		p = PrimeGenerator::Generate(digitCount, k);
	}
	
	//calculate the modulus of both the public and private keys, n
	BigInt n(p * q);
	
	//calculate the totient phi
	BigInt phi((p - 1) * (q - 1));
	
	//we don't want the keys to be less than 20 bits long
	if (phi < "1048576")
		throw "Insufficient key strength!";
	
	//select a small odd integer e that is coprime with phi and e < phi
	//usually 65537 is used, and we will use it too if it fits
	//it is recommended that this be the least possible value for e
	BigInt e("65537");
	
	//make sure the requirements are met
	while (RSA::GCD(phi, e) != BigIntOne || e < "65537")
	{
		PrimeGenerator::makeRandom(e, 5);
	}
	
	//now we have enough information to create the public key
	//e is the public key exponent, n is the modulus
	Key publicKey(n, e);
	
	//calculate d, de = 1 (mod phi)
	BigInt d(RSA::solveModularLinearEquation(e, BigIntOne, phi));
	
	//we can create the private key
	//d is the private key exponent, n is the modulus
	Key privateKey(n, d);
	
	//finally, the keypair is created and returned
	KeyPair newKeyPair(privateKey, publicKey);
	return newKeyPair;
}
Beispiel #2
0
int Caller::loadEntries( const std::string path)
{
	std::string nextLine;
	std::string key;
	std::string chr;
	std::string refBase;
	int readDepth;
	int pos;

	// Open the sample file
	std::ifstream inputFile( path.c_str());
	if( !inputFile.is_open())
	{
		perror( "Error opening input readcount file");
		exit( 1);
	}

	// For each line in the sample file (which will correspond to a genomic location)
	while( std::getline( inputFile, nextLine))
	{
		// Split the line into tokens separated by whitespace (for columns, since this is a tab delimited file)
		std::istringstream strStream( nextLine);
		std::istream_iterator<std::string> begin( strStream), end;
		std::vector<std::string> stringTokens( begin, end);

		// Get all main fields
		chr = stringTokens[0];
		pos = atoi( stringTokens[1].c_str());
		refBase = stringTokens[2];
		refBase[0] = toupper( refBase[0]);
		readDepth = atoi( stringTokens[3].c_str());

		// Generate the key, (chr:pos)
		key = stringTokens[0] + ":" + stringTokens[1];
		if( key == "")
		{
			std::cout << "Empty key" << std::endl;
		}

		// Create the base ReadcountEntry object
		ReadcountEntry nextReadcountEntry( refBase, readDepth);

		// Get all subfields for each allele, the 5th column (stringTokens[4]) is garbage due to a bug with the bam-readcount program, ignore it
		for( int i = 5; i < stringTokens.size(); i++)
		{
			std::vector<std::string> nextSubTokens = Common::split( stringTokens[i], ":", true);

			// Create the Allele objects and add them to the current ReadcountEntry object
			std::string base = nextSubTokens[0];
			int count = atoi( nextSubTokens[1].c_str());
			double avgMappingQuality = atof( nextSubTokens[2].c_str());
			double avgBaseQuality = atof( nextSubTokens[3].c_str());
			double avgSEMappingQuality = atof( nextSubTokens[4].c_str());
			int numPlusStrand = atoi( nextSubTokens[5].c_str());
			int numMinusStrand = atoi( nextSubTokens[6].c_str());
			double avgPosAsFraction = atof( nextSubTokens[7].c_str());
			double avgNumMismatchesAsFraction = atof( nextSubTokens[8].c_str());
			double avgSumMismatchQualities = atof( nextSubTokens[9].c_str());
			int numQ2ContainingReads = atoi( nextSubTokens[10].c_str());
			double avgDistanceToQ2StartInQ2Reads = atof( nextSubTokens[11].c_str());
			double avgClippedLength = atof( nextSubTokens[12].c_str());
			double avgDistanceToEffective3pEnd = atof( nextSubTokens[13].c_str());

			bool variant = false;
			if( base != refBase)
			{
				variant = true;
			}

			double percentage;
			if( readDepth != 0)
			{
				percentage = ( double) count / ( double) readDepth * 100;
			}
			else
			{
				percentage = 0;
			}

			Allele nextAllele( base, count, avgMappingQuality, avgBaseQuality, avgSEMappingQuality, numPlusStrand, numMinusStrand,
							   avgPosAsFraction, avgNumMismatchesAsFraction, avgSumMismatchQualities, numQ2ContainingReads,
							   avgDistanceToQ2StartInQ2Reads, avgClippedLength, avgDistanceToEffective3pEnd, percentage, variant);

			nextReadcountEntry.addAllele( nextAllele);
		}

		// Now, the ReadcountEntry object is filled, so we can create the Sample object
		nextReadcountEntry.setMostFreqVariantAllele();
		Sample nextSample( path, nextReadcountEntry);

		// Finally, add the Sample object to the Location object,
		// Check if the Location object with the current key exists in the hash table
		std::unordered_map<std::string, Location>::iterator iter = locationTable.find( key);
		if( iter == locationTable.end())
		{
			// If it does not exist, create the Location object
			Location newLocation( chr, pos);

			// Add the new Sample to the Location object
			newLocation.addSample( nextSample);

			// Insert the new key-Location pair to the hash table
			std::pair<std::string, Location> newKeyPair( key, newLocation);
			locationTable.insert( newKeyPair);
		}
		else
		{
			bool sampleExists = false;
			std::vector<Sample> samples = ( iter->second).getSamples();
			for( int j = 0; j < samples.size(); j++)
			{
				if( samples[j].getSampleName() == nextSample.getSampleName())
				{
					sampleExists = true;
				}
			}

			if( !sampleExists)
			{
				( iter->second).addSample( nextSample);
			}
		}
	}

	// Check if the file was read correctly
	if( inputFile.bad())
	{
		perror( "Error reading input readcount file");
	}

	// Close the input sample file
	inputFile.close();
}