Esempio n. 1
0
void crypto::columnar_decryption(std::string cipher)
{
	static int count = 0; //static count for printing, not really useful in general
	std::ofstream file("perms.txt", std::ofstream::app);	//open the file to append to
	scores ord, writer;								// 2 vectors of scores writer is the master list, ord is the temp list
	// attack all key lengths up to 10 (should probably avoid a magic number and make max_key_len a parameter)
	for (int columns = 1; columns <= 10; columns++)
	{
		//create a vector of the indexes based on the key length
		std::vector<size_t> indexes;
		for (int i = 0; i < columns; ++i)
			indexes.push_back(i);
		
		//number of columns for all rows(integer division is ok here)
		int rows = cipher.size() / columns;
		
		//number of columns that will be in the final row (if it exists)
		int extra = cipher.size() % columns;

		// the columns of the message
		std::vector<std::string> pqr(indexes.size());
		//decrypt the columnar transposition for each permutation of column orderings
		while (std::next_permutation(indexes.begin(), indexes.end()))
		{
			int start = 0;
			int end;
			for (int i = 0; i < columns; ++i)
			{
				end = rows + (extra > indexes[i]);
				pqr[indexes[i]] = cipher.substr(start, end);
				start += end;
			}
			std::string word;
			for (int i = 0; i < rows + (extra > 0); ++i)
			{
				auto offset = i*columns;
				for (int j = 0; j < columns; ++j)
				{
					auto index = j + offset;
					if (index < cipher.size())
						word += pqr[j][i];
				}
			}
			auto score = get_scores(word);// get the score for the decrypted message
			if (ord.find(word) != ord.end())
				ord[word] = std::max(ord[word], score);//only keep the max scores
			else
				ord[word] = score;//add a new score if we don't have it yet
		} // end while			
		writer = top(ord, writer, 1000);//only keep the top 1000 scores for the future		
	}
	// order the list by score instead of string so we need to use a multimap
	auto ret = flip_map(writer);
	//write the scores to a file
	for (auto it = ret.begin(); it != ret.end(); ++it)
		file << it->first << " " << it->second << std::endl;
	file.close();
	++count;
	printf("Finished %d \n", count);
}
Esempio n. 2
0
/* This needs multi threading !!!!!*/
int crypto::get_scores(const std::string& str, std::map<std::string, size_t> &memo, size_t pos)
{
	//recursive base case check
	if (pos >= str.size())
		return 0;
	int ret= 0;                             // return value
    int temp = 0;                           // temp value for calc
    int m = 1; 	                            // window size

    std::string window = str.substr(pos, m);
	// look for the current string in the memo, if its there just use its value, if not calculate it
	//lock hash
	auto it = memo.find(str.substr(pos, str.size())); //<-- not thread safe
	bool in_hash = (it != memo.end()); //<-- not thread safe	
	if (!in_hash)  //<-- not thread safe
	{
		//unlock memo
		while (m <= (str.size() - pos))
		{	
			//look up the substring in the dictionary
			if (dict.find(window) != dict.end())
			{
				//if we find them, give them a score and look at the rest of the string for more words
				temp = (m*m + get_scores(str, memo, pos + m));
				if (ret < temp)
					ret = temp;
			}
			++m;//increase the window size
			window = str.substr(pos, m);//update window to be the proper substring									
		}
		
		//insert the new score and substring pair into the memo if its not there or update it with the high score
		//lock memo
		it = memo.find(str.substr(pos, str.size())); //<-- not thread safe
		if (it == memo.end() || (it->second < ret))
			memo.insert(std::make_pair(str.substr(pos, str.size()), ret));  //<-- not thread safe. need a mutex
		//unlock memo
	}
	else
	{
		ret = it->second;
		//unlock memo
	}	

	return std::max(ret, get_scores(str, memo, pos+1));
}
Esempio n. 3
0
void get_scores(const char* filename, vector<float>& sc, vector<string>& nameset){
	ifstream scfile(filename);
	if(! scfile){
	  cerr << "No such file '" << filename << "'\n";
	  exit(0);
	}
	return get_scores(scfile, sc, nameset);
}
Esempio n. 4
0
int crypto::get_scores(const std::string& str)
{
	std::map<std::string, size_t> memo;
	return get_scores(str, memo, 0); 
}
int main(int argc, char *argv[])
{
	char arg_string[2000];
	int nsub,n_new_sub,real_nsub;
	float *score,p;
	int max_cc[2],s,n_non_mendelian;
	non_mendelian *non_mendelians;
	char *non_mendelian_report;
	par_info pi;
	sa_par_info spi;
	subject **sub,**new_sub,**real_sub;
	pi.use_cc=1;
	printf("%s v%s\n",PROGRAM,SAVERSION);
	printf("MAX_LOCI=%d\nMAX_SUB=%d\n",MAX_LOCI,MAX_SUB);

	assert(sub=(subject **)calloc(MAX_SUB,sizeof(subject*)));
	for (s=0;s<MAX_SUB;++s)
		assert(sub[s]=(subject *)calloc(1,sizeof(subject)));
	assert(score=(float *)calloc(MAX_SUB,sizeof(float)));
	max_cc[0]=max_cc[1]=0;
	read_all_args(argv,argc, &pi, &spi);
	// make_arg_string(arg_string,argc,argv);
	// parse_arg_string(arg_string,&pi,&spi,&pspi);
	process_options(&pi,&spi);
	if (spi.df[FILTERFILE].fp)
		initExclusions(spi.df[FILTERFILE].fp);
	read_all_data(&pi,&spi,sub,&nsub,names,comments,func_weight);
if (spi.use_trios)
{
	if (atoi(comments[0])>22 || toupper(comments[0][0]) == 'X' || toupper(comments[0][0]) == 'Y' ||
		toupper(comments[0][0]) == 'C'&&toupper(comments[0][1]) == 'H'&&toupper(comments[0][2]) == 'R' && (atoi(comments[0] + 3) > 22 || toupper(comments[0][3]) == 'X' || toupper(comments[0][3]) == 'Y'))
	{
		error("Cannot at present use trios for genes on X or Y chromosome", "");
		return 1;
	}
}

if (spi.use_trios)
	{
		assert(new_sub=(subject **)calloc(nsub,sizeof(subject*)));
		for (s=0;s<nsub;++s)
			assert(new_sub[s]=(subject *)calloc(1,sizeof(subject)));
		assert(non_mendelians=(non_mendelian *)calloc(MAX_SUB,sizeof(non_mendelian)));
		if ((n_new_sub=sort_trios(sub,nsub,&pi,new_sub,non_mendelians,&n_non_mendelian,long_line))==0)
			exit(1);
		real_sub=sub;
		sub=new_sub;
		real_nsub=nsub;
		nsub=n_new_sub;
		assert(non_mendelian_report=(char*)malloc(strlen(long_line)+1));
		strcpy(non_mendelian_report,long_line);
	}
	fprintf(spi.df[OUTFILE].fp,"pscoreassoc output\n"
"Locus                   controls     frequency        cases          frequency   frequency allele  weight\n"
"                     AA  :   AB  :  BB                  AA  :   AB  :  BB                     \n");
get_freqs(sub,nsub,&pi,&spi,cc_freq,cc_count,cc_genocount);
applyExclusions(&pi);
set_weights(spi.df[OUTFILE].fp,weight,missing_score,rarer,sub,nsub,&pi,&spi,func_weight,cc_freq,cc_count,max_cc,names,comments);
get_scores(score,weight,missing_score,rarer,sub,nsub,&pi,&spi);
p=do_score_onetailed_ttest(spi.df[OUTFILE].fp,score,sub,nsub,&pi,&spi,cc_freq,cc_count,max_cc,weight,missing_score,rarer);
if (spi.df[SCOREFILE].fp)
	write_scores(spi.df[SCOREFILE].fp,sub,nsub,score);
if (spi.do_recessive_test)
{
if (atoi(comments[0]+3)>22 || toupper(comments[0][4])=='X' || toupper(comments[0][4])=='Y')
// simple trick for now to avoid X and Y genes
	fprintf(spi.df[OUTFILE].fp,"\nCannot do recessive test for genes on X or Y chromosome.\n");
else if (spi.use_haplotypes)
	do_recessive_HWE_test_with_haplotypes(spi.df[OUTFILE].fp,score,sub,nsub,&pi,&spi,cc_freq,cc_count,max_cc,weight,missing_score,rarer,names);
else 
	do_recessive_HWE_test(spi.df[OUTFILE].fp,score,sub,nsub,&pi,&spi,cc_freq,cc_count,max_cc,weight,missing_score,rarer,names);
}

stateExclusions(spi.df[OUTFILE].fp);
printf("\nProgram run completed OK\n");
return 0;

}
Esempio n. 6
0
int listen_for_connections(void) {
    struct sockaddr_in serveraddr, clientaddr;
    int request_sd;
    int numsocks = 0;
    int maxsocks = 10;
    int sock[maxsocks];
    int receive;
    int clientaddrlen = sizeof(struct sockaddr);
    fd_set readfds, fds;
    struct timeval time;
    time.tv_sec = 20;
    time.tv_usec = 0;

    //request-socket, lytter på innkommende forbindelser
    request_sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

    //adresse struct
    memset((void*)  &serveraddr, 0, sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_addr.s_addr = INADDR_ANY;

    if(listening_port != 0) {
        serveraddr.sin_port = htons(listening_port);
        printf("Listening for connections on port '%d'\n", listening_port);
    } else {
        serveraddr.sin_port = htons(9090); //default if no port is provided
        printf("Listening for connections on default port '9090'\n");
    }
    //bind adressen til socketen
    bind(request_sd, (struct sockaddr *) &serveraddr, sizeof(serveraddr));

    //start lytting
    listen(request_sd, SOMAXCONN);

    FD_ZERO(&readfds);
    FD_ZERO(&fds);
    FD_SET(request_sd, &fds);
    FD_SET(0, &fds);
    numsocks++;

    for(;;) {
        readfds = fds;
        receive = select(maxsocks + 1, &readfds, NULL, NULL, &time);

        if(receive < 0) {
            perror("select encountered an error.\n");
            return -1;
        } else if(receive == 0) {
            //timeout, do nothing
        } else {
            int i;
            for(i = 0; i < FD_SETSIZE; i++) {
                if(FD_ISSET(i, &readfds)) {
                    if(i == 0) { //stdin look for ctrl-d
                        if(read(i, NULL, 10) == 0) {
                            for(i = 0; i < numsocks; i++) {
                                close(sock[i]);
                                FD_CLR(sock[i], &fds);
                            }
                            exit_server();
                        }
                    }
                    if(i == request_sd) {
                        if(numsocks < maxsocks) {
                            sock[numsocks] = accept(request_sd, (struct sockaddr *) &clientaddr,
                                                    (socklen_t *) &clientaddrlen);
                            FD_SET(sock[numsocks], &fds);
                            numsocks++;
                        } else {
                            //No more space for sockets
                            perror("Ran out of socket space.\n");
                            for(i = 0; i < numsocks; i++) {
                                close(sock[i]);
                                FD_CLR(sock[i], &fds);
                            }
                            exit_server();
                        }
                    } else {
                        //read first 4 bytes, and determine what kind of package is coming
                        char buf[4];
                        memset(buf, 0, 4);
                        if(read(i, buf, 4) == -1) {
                            perror("Error reading from client.\n");
                            return -1;
                        }
                        int size = bytes_to_int(buf);
                        byte msg_type = buf[2];

                        if(msg_type == SCORE_LIST) {
                            int score_packages = btotci(buf[3]);
                            if(score_packages > 0) {
                                score *scores[score_packages];
                                int count;
                                for(count = 0; count < score_packages; count++) {
                                    scores[count] = init_score();
                                }
                                int result = receive_score(i, score_packages, scores);
                                if(result != -1) {
                                    for(count = 0; count < score_packages; count++) {
                                        add_score(scores[count]);
                                    }
                                }
                                for(count = 0; count < score_packages; count++) {
                                    free(scores[count]);
                                }
                            } else if(score_packages < 0) {
                                //reply with abs(score_packages);
                                int num = abs(score_packages);
                                score *s[num];
                                int count;

                                count = get_scores(num, s);
                                if(send_score_list(i, s, count) < 0) {
                                    perror("Closing connection.\n");
                                    close(i);
                                    FD_CLR(i, &fds);
                                }
                            }
                        } else if(msg_type == SHUTDOWN_NOTIFICATION) {
                            //receive shutdown msg
                            char buf[size - 4]; //First 4 bytes allready read.
                            int result = receive_shutdown_notification(i, buf, size - 4);
                            if(result == 0) {
                                printf("Received shutdown notification: %s\n", buf);
                                close(i);
                                FD_CLR(i, &fds);
                            }
                        } else {
                            //error, unknown type, reply with shutdown notification
                            perror("Sending shutdown_notification.\n");
                            send_shutdown_notification(i, "Unknown msg_type, got: " + msg_type);
                            //close and remove socket criptor
                            close(i);
                            FD_CLR(i, &fds);
                        }
                    }
                }
            }
        }
    }
    int i;
    for(i = 0; i < maxsocks; i++) {
        close(sock[i]);
    }
    close(request_sd);
    return 0;
}
Esempio n. 7
0
int main(int argc, char *argv[]) {
	set_new_handler(alloc_error);

	if(argc < 6) {
		print_usage(cout);
		exit(0);
	}
	
	string seqfile;                       // file with sequences
	string exprfile;                      // file with expression data
	string subsetfile;                    // file with subset of sequence names to search
	string scorefile;                     // file with scores

	if(! GetArg2(argc, argv, "-s", seqfile)) {
		cerr << "Please specify sequence file\n\n";
		print_usage(cout);
		exit(0);
	}
	if(! GetArg2(argc, argv, "-o", outfile)) {
		cerr << "Please specify output file\n\n";
		print_usage(cout);
		exit(0);
	}
	
	int search_type = UNDEFINED;
	if(GetArg2(argc, argv, "-ex", exprfile)) {
		search_type = EXPRESSION;
	}
	if(GetArg2(argc, argv, "-su", subsetfile)) {
		search_type = SUBSET;
	}
	if(GetArg2(argc, argv, "-sc", scorefile)) {
		search_type = SCORE;
	}
	if(search_type == UNDEFINED) {
		cerr << "Please specify either an expression data file, a file with a subset of sequence names, or a file with sequence scores.\n\n";
		print_usage(cout);
		exit(0);
	}

	// Decide mode of running
	archive = false;
	if(! GetArg2(argc, argv, "-worker", worker)) {
		worker = -1;
		archive = true;
	}
	
	// Read parameters
	vector<string> seqs;
	cerr << "Reading sequence data from '" << seqfile << "'... ";
	get_fasta_fast(seqfile.c_str(), seqs, seq_nameset);
	cerr << "done.\n";
	ngenes = seq_nameset.size();
	
	npoints = 0;
	if(search_type == EXPRESSION) {
		cerr << "Reading expression data from '" << exprfile << "'... ";
		get_expr(exprfile.c_str(), expr, data_nameset);
		cerr << "done.\n";
		npoints = expr[0].size();
		nsubset = 0;
	} else if(search_type == SUBSET) {
		cerr << "Reading subset of sequences to search from '" << subsetfile << "'... ";
		get_list(subsetfile.c_str(), subset);
		cerr << "done.\n";
		npoints = 0;
		for(int i = 0; i < ngenes; i++) {
			vector<float> row(0);
			expr.push_back(row);
		}
		nsubset = subset.size();
		sort(subset.begin(), subset.end());
	} else if(search_type == SCORE) {
		cerr << "Reading sequence scores from '" << scorefile << "'... ";
		get_scores(scorefile.c_str(), scores, data_nameset);
		cerr << "done.\n";
		npoints = 1;
		nsubset = 0;
	}

	vector<vector <float> > newexpr;
	vector<float> newscores;
	if(search_type == EXPRESSION) {
		order_data_expr(newexpr);
	}
	if(search_type == SCORE) {
		order_data_scores(newscores);
	}

	if(search_type == EXPRESSION) {
		cerr << "Successfully read input files -- dataset size is " << ngenes << " sequences X " << npoints << " timepoints\n";
	} else if(search_type == SUBSET) {
		cerr << "Successfully read input files -- dataset size is " << ngenes << " sequences, with " << nsubset << " to be searched\n";
	} else if(search_type == SCORE) {
		cerr << "Successfully read input files -- dataset size is " << ngenes << " scored sequences\n";
	}

	cerr << "Setting up MotifSearch... ";
	if(! GetArg2(argc, argv, "-numcols", ncol)) ncol = 10;
	if(! GetArg2(argc, argv, "-order", order)) order = 0;
	if(! GetArg2(argc, argv, "-simcut", simcut)) simcut = 0.8;
	if(! GetArg2(argc, argv, "-maxm", maxm)) maxm = 20;
	MotifSearch* ms;
	if(search_type == EXPRESSION) {
		ms = new MotifSearchExpr(seq_nameset, seqs, ncol, order, simcut, maxm, newexpr, npoints);
	} else if(search_type == SCORE) {
		ms = new MotifSearchScore(seq_nameset, seqs, ncol, order, simcut, maxm, newscores);
	} else {
		ms = new MotifSearchSubset(seq_nameset, seqs, ncol, order, simcut, maxm, subset);
	}
	ms->modify_params(argc, argv);
	ms->set_final_params();
	ms->ace_initialize();
	cerr << "done.\n";
	cerr << "Random seed: " << ms->get_params().seed << '\n';

	if(archive) {
		cerr << "Running in archive mode...\n";
		string archinstr(outfile);
		archinstr.append(".ms");
		ifstream archin(archinstr.c_str());
		if(archin) {
			cerr << "Refreshing from existing archive file " << archinstr << "... ";
			ms->get_archive().read(archin);
			cerr << "done.\n";
		}
		while(true) {
			int found = read_motifs(ms);
			if(found > 0) output(ms);
			if(found < 50) sleep(10);
		}
	} else {
		cerr << "Running as worker " << worker << "...\n";
		int nruns = ms->positions_in_search_space()/(ms->get_params().expect * ncol);
		nruns *= ms->get_params().oversample;
		nruns /= ms->get_params().undersample;
		cerr << "Restarts planned: " << nruns << '\n';
		string archinstr(outfile);
		archinstr.append(".ms");
		string lockstr(outfile);
		lockstr.append(".lock");
		for(int j = 1; j <= nruns; j++) {
			if(j == 1 || j % 50 == 0 || search_type == SUBSET) {
				struct flock fl;
				int fd;
				fl.l_type   = F_RDLCK;
				fl.l_whence = SEEK_SET;
				fl.l_start  = 0;
				fl.l_len    = 0;
				fl.l_pid    = getpid();
				fd = open(lockstr.c_str(), O_RDONLY);
				if(fd == -1) {
					if(errno != ENOENT)
						cerr << "\t\tUnable to read lock file, error was " << strerror(errno) << "\n";
				} else {
					while(fcntl(fd, F_SETLK, &fl) == -1) {
						cerr << "\t\tWaiting for lock release on archive file... \n";
						sleep(10);
					}
					ifstream archin(archinstr.c_str());
					if(archin) {
						cerr << "\t\tRefreshing archive from " << archinstr << "...";
						ms->get_archive().clear();
						ms->get_archive().read(archin);
						archin.close();
						cerr << "done.\n";
					}
					fl.l_type = F_UNLCK;
					fcntl(fd, F_SETLK, &fl);
					close(fd);
					cerr << "\t\tArchive now has " << ms->get_archive().nmots() << " motifs\n";
				}
			}
			cerr << "\t\tSearch restart #" << j << "/" << nruns << "\n";
			ms->search_for_motif(worker, j, outfile);
		}
	}
	delete ms;
	return 0;
}