Example #1
0
void Deconvoluter::process()
{
    std::string file = FileParser::getKey("TWINNED_MTZ", std::string(""));
    wangEnvName = FileParser::getKey("WANG_ENVELOPE_OUTPUT", std::string("wang.env"));
    molUnitName = FileParser::getKey("MOL_UNIT_ENVELOPE_OUTPUT", std::string("mol_unit.env"));
    tempFftMapName = FileParser::getKey("TEMP_FFT_MAP", std::string("temp_fft.map"));
    tempAveMapName = FileParser::getKey("TEMP_AVERAGE_MAP", std::string("temp_ave.map"));
    tempMtzName = FileParser::getKey("TEMP_AVERAGE_MTZ", std::string("temp_ave.mtz"));
    tempSortedMtzName = FileParser::getKey("TEMP_AVERAGE_SORTED_MTZ", std::string("temp_sorted_ave.mtz"));
    cadMtzName = FileParser::getKey("TEMP_CAD_MTZ", std::string("temp_cad.mtz"));
    fsmeltMtzName = FileParser::getKey("TEMP_FSMELT_MTZ", std::string("temp_fsmelt.mtz"));
    std::string fObsLab = FileParser::getKey("LABIN_AMPLITUDES", std::string("F"));
    std::string fCalcLab = FileParser::getKey("LABOUT_CALC_AMPLITUDES", std::string("FC"));
    std::string ncsDefs = FileParser::getKey("NCS_DEFINITIONS_FILE", std::string(""));

    std::vector<double> ccAll, ccSinglets, rAll, rSinglets;

    if (ncsDefs.length() == 0)
    {
        std::cout << "Warning! NCS definitions file missing. Please specify file in GAP format using keyword NCS_DEFINITIONS_FILE" << std::endl;
        exit(1);
    }

    int bins = 20;
    int maxCycles = FileParser::getKey("MAXIMUM_CYCLES", 10);

    if (file == "")
    {
        std::cout << "Twinned MTZ has not been provided, please provide file path under keyword TWINNED_MTZ" << std::endl;
        exit(1);
    }

    // loading original, twinned MTZ into memory.
    originalMtz = MtzPtr(new MtzManager(file));
    originalMtz->loadReflections(false);
    MtzManager::setReference(&*originalMtz);
    spaceGroup = FileParser::getKey("SPACE_GROUP", originalMtz->getLowGroup()->spg_num);



    std::cout << "Loaded original MTZ file " << file << std::endl;
    bool skipFirst = FileParser::getKey("SKIP_FIRST_CYCLE", false);

    int beginning = FileParser::getKey("RESUME_FROM_CYCLE", 0);
    if (beginning > 0) skipFirst = true;

    for (int i = beginning; i < maxCycles; i++)
    {
        if (i > beginning || (i == beginning && !skipFirst))
        {
            fastFourierTransform(file, tempFftMapName, (i > 0));
            gapEnvelope(tempFftMapName);
            gapAverage(tempFftMapName, wangEnvName, molUnitName);
            map_to_sf(tempAveMapName, tempMtzName);
            sort_mtz(tempMtzName, tempSortedMtzName);
            cad(tempSortedMtzName, cadMtzName);
            fsmelt(file, cadMtzName, fsmeltMtzName);
        }

        MtzPtr nextMtz = MtzPtr(new MtzManager(fsmeltMtzName));
        nextMtz->loadReflections(true); // fc in "intensity" and f in "fc"...

        nextMtz->applyScaleFactorsForBins(bins);

        nextMtz->individualDetwinningScales((i == maxCycles - 1));
        nextMtz->copyOtherAmplitudesFromReference();
        file = "detwinned_cycle_" + i_to_str(i) + ".mtz";
        nextMtz->writeToFile(file);

        std::cout << "******************************" << std::endl;
        std::cout << "**    CORRELATION (ALL)     **" << std::endl;
        std::cout << "******************************" << std::endl;
        std::cout << std::endl << "Correlation between all scaled data and original twinned data" << std::endl;
        std::cout << std::endl << std::setw(15) << "Low res " << std::setw(15) << "High res " << std::setw(15) << "Correl" << std::setw(15) << "Num refl" << std::endl;

        ccAll.push_back(nextMtz->correlationWithManager(&*originalMtz, false, false, 0, 0, bins, NULL, false));

        std::cout << "*******************************" << std::endl;
        std::cout << "**  CORRELATION (SINGLETS)   **" << std::endl;
        std::cout << "*******************************" << std::endl;
        std::cout << std::endl << "Correlation between singlet scaled data and original twinned data" << std::endl;
        std::cout << std::endl << std::setw(15) << "Low res " << std::setw(15) << "High res " << std::setw(15) << "Correl" << std::setw(15) << "Num refl" << std::endl;

        ccSinglets.push_back(nextMtz->correlationWithManager(&*originalMtz, false, false, 0, 0, bins, NULL, true));

        std::cout << "******************************" << std::endl;
        std::cout << "**      R FACTOR (ALL)      **" << std::endl;
        std::cout << "******************************" << std::endl;
        std::cout << std::endl << "R factor between all scaled data and original twinned data" << std::endl;
        std::cout << std::endl << std::setw(15) << "Low res " << std::setw(15) << "High res " << std::setw(15) << "Correl" << std::setw(15) << "Num refl" << std::endl;

        rAll.push_back(nextMtz->rSplitWithManager(&*originalMtz, false, false, 0, 0, bins, NULL, false));

        std::cout << "*******************************" << std::endl;
        std::cout << "**   R FACTOR (SINGLETS)     **" << std::endl;
        std::cout << "*******************************" << std::endl;
        std::cout << std::endl << "R factor between singlet scaled data and original twinned data" << std::endl;
        std::cout << std::endl << std::setw(15) << "Low res " << std::setw(15) << "High res " << std::setw(15) << "Correl" << std::setw(15) << "Num refl" << std::endl;

        rSinglets.push_back(nextMtz->rSplitWithManager(&*originalMtz, false, false, 0, 0, bins, NULL, true));
    }

    std::cout << "*******************************" << std::endl;
    std::cout << "**   END OF DECONVOLUTION    **" << std::endl;
    std::cout << "*******************************" << std::endl;
    std::cout << std::endl << "Summary of deconvolution:" << std::endl << std::endl;
    std::cout << "Cycle\tCCall\tCCsinglets\tRall\tRsinglets" << std::endl;

    for (int i = 0; i < ccAll.size(); i++)
    {
        std::cout << i << "\t" << ccAll[i] << "\t" << ccSinglets[i] << "\t" << rAll[i] << "\t" << rSinglets[i] << std::endl;
    }
}
Example #2
0
void new_main(int argc, char *argv[])
{
    time_t startcputime;
    time(&startcputime);
    
	if (argc == 1)
	{
        std::cout << "Welcome to cppxfel version 1.1.1!" << std::endl;
        std::cout << "Please refer to & cite paper in Journal of Applied Crystallography (unpublished)" << std::endl << std::endl;
        std::cout << "Command order for regular structure solution:" << std::endl;
        std::cout << "\tcppxfel.run_dials shot*.pickle" << std::endl;
        std::cout << "\tcppxfel.input_gen" << std::endl;
        std::cout << "\tcppxfel.run -i integrate.txt" << std::endl;
        std::cout << "\tcppxfel.run -i refine.txt" << std::endl;
        std::cout << "\tcppxfel.run -i merge.txt" << std::endl << std::endl;;
        std::cout << "Other functions for assessing data quality:" << std::endl << std::endl;
        
        std::cout << "Correlation between two MTZ files:" << std::endl << std::endl;
        std::cout << "\tcppxfel.run -cc firstFile.mtz secondFile.mtz [ambiguity] [lowRes] [highRes] [bins]" << std::endl << std::endl;
        std::cout << "ambiguity: 0, 1, 2 or 3 - will compare different indexing solutions where the Bravais lattice symmetry is higher than that of the point group for certain space groups. Default 0" << std::endl;
        std::cout << "lowRes and highRes: set to resolution in Angstroms to bound the results, or set to 0 to take lowest/highest resolution data. Default 0, 0" << std::endl;
        std::cout << "bins: number of bins to report correlation statistics. Default 20." << std::endl << std::endl;
        
        std::cout << "Partiality CSV files:" << std::endl << std::endl;
        std::cout << "\tcppxfel.run -partiality reference.mtz ref-img-shot-number.mtz [highRes]" << std::endl << std::endl;
        std::cout << "highRes: 0, 1, 2 or 3 - highest resolution reflection to report results on. Default 1.4" << std::endl;
        std::cout << "This outputs partiality_[m].csv where m is bin number, which can be imported into other graphing softwares such as R." << std::endl << std::endl;;

        
        std::cout << "Merging statistics:" << std::endl << std::endl;
        std::cout << "\tcppxfel.run -rpim unmerged_file.mtz [lowRes] [highRes] [bins]" << std::endl;
        std::cout << "\tcppxfel.run -rmeas unmerged_file.mtz [lowRes] [highRes] [bins]" << std::endl;
        std::cout << "\tcppxfel.run -rmerge unmerged_file.mtz [lowRes] [highRes] [bins]" << std::endl << std::endl;
        std::cout << "lowRes and highRes: set to resolution in Angstroms to bound the results, or set to 0 to take lowest/highest resolution data. Default 0, 0" << std::endl;
        std::cout << "bins: number of bins to report correlation statistics. Default 20." << std::endl << std::endl;
        
        exit(1);
	}

	if (strcmp(argv[1], "-wiki") == 0)
	{
		if (argc <= 2)
		{
			std::cout << "arguments: -wiki <logfile>" << std::endl;
			exit(1);
		}

        Wiki wiki = Wiki(std::string(argv[2]));
		wiki.process();

		exit(1);
	}
    
	std::cout << "Welcome to cppxfel!" << std::endl;
    
	if (strcmp(argv[1], "-i") == 0 || strcmp(argv[1], "-dry") == 0)
	{
        bool dry = (strcmp(argv[1], "-dry") == 0);
        
		if (argc < 3)
		{
			std::cout << "arguments: -i <input_script>" << std::endl;
			exit(1);
		}

        std::vector<std::string> extras;
        
        for (int i = 3; i < argc; i++)
        {
            extras.push_back(std::string(argv[i]));
        }
        
        InputFileParser *parser = new InputFileParser(std::string(argv[2]), extras);
        parser->setDry(dry);
		parser->parse(false);
        
        delete parser;
	}
    else
    {
        Logger::mainLogger = LoggerPtr(new Logger());
        boost::thread thr = boost::thread(Logger::awaitPrintingWrapper, Logger::mainLogger);
    }
    
    if (strcmp(argv[1], "-b") == 0)
    {
        float bFactor = atof(argv[2]);
        
        MtzManager *mtz1 = new MtzManager();
        mtz1->setFilename(std::string(argv[3]));
        mtz1->loadReflections(0);
        
        mtz1->applyBFactor(bFactor);
        
        mtz1->writeToFile("b-" + std::string(argv[3]));

    }
    
	if (strcmp(argv[1], "-rmerge") == 0 || strcmp(argv[1], "-rpim") == 0
			|| strcmp(argv[1], "-rmeas") == 0)
	{
		if (argc < 3)
		{
			std::cout << "arguments: -r{merge} <file1> [lowRes] [highRes] [bins]."
					<< std::endl;
			exit(1);
		}

		RFactorType rFactor = RFactorTypeMerge;

		if (strcmp(argv[1], "-rpim") == 0)
		{
			rFactor = RFactorTypePim;
		}
		else if (strcmp(argv[1], "-rmeas") == 0)
		{
			rFactor = RFactorTypeMeas;
		}

		double highRes = 0;
		double lowRes = 0;
		int bins = 20;

		if (argc > 5)
		{
			lowRes = atof(argv[4]);
			highRes = atof(argv[5]);
		}
		if (argc > 6)
		{
			bins = atoi(argv[6]);
		}

        MtzManager *mtz = new MtzManager();
		mtz->setFilename(std::string(argv[2]));
		mtz->loadReflections(1);

		mtz->rFactorWithManager(rFactor, false, false, lowRes, highRes, bins);
	}

	if (strcmp(argv[1], "-cc") == 0 || strcmp(argv[1], "-rsplit") == 0)
	{
		if (argc < 4)
		{
			std::cout
					<< "arguments: -cc <file1> <file2> (0 / 1) [lowRes] [highRes] [bins]."
					<< std::endl;
			exit(1);
		}

		bool rsplit = (strcmp(argv[1], "-rsplit") == 0);

		int inverted = 0;
		double highRes = 0;
		double lowRes = 0;
		int bins = 20;

		if (argc > 4)
		{
			inverted = atoi(argv[4]);
		}
		if (argc > 6)
		{
			lowRes = atof(argv[5]);
			highRes = atof(argv[6]);
		}
		if (argc > 7)
		{
			bins = atoi(argv[7]);
		}

		MtzManager *mtz1 = new MtzManager();
		mtz1->setFilename(std::string(argv[2]));
		mtz1->loadReflections(1);

		MtzManager *mtz2 = new MtzManager();
		mtz2->setFilename(std::string(argv[3]));
		mtz2->loadReflections(PartialityModelScaled, true);

		if (inverted)
            mtz1->setActiveAmbiguity(1);

		if (rsplit)
		{
			mtz1->rSplitWithManager(mtz2, 1, 0, lowRes, highRes, bins);
		}
		else
		{
			mtz1->correlationWithManager(mtz2, 1, 0, lowRes, highRes, bins, NULL, true);

		}

		delete mtz1;
		delete mtz2;

		exit(1);
	}

	if (strcmp(argv[1], "-gradscaling") == 0)
	{
		if (argc <= 3)
		{
			std::cout << "arguments: -gradscaling <ref> <file2> ... <filen>."
					<< std::endl;
			exit(1);
		}

		MtzManager *reference = new MtzManager();
		reference->setFilename(argv[2]);
		reference->loadReflections(1);

		for (int i = 3; i < argc; i++)
		{
			MtzManager *image = new MtzManager();
			image->setFilename(argv[i]);
			image->loadReflections(1);

			double gradientOld = image->gradientAgainstManager(reference);
			double gradientBefore = image->minimizeRFactor(reference);

			std::cout << image->getFilename() << "\t" << gradientOld << "\t"
					<< gradientBefore << std::endl;
		}
	}

	if (strcmp(argv[1], "-inv") == 0)
	{
		if (argc <= 2)
		{
			std::cout << "arguments: -inv <file>." << std::endl;
			exit(1);
		}
        
        MtzManager *mtz = new MtzManager();
		mtz->setFilename(std::string(argv[2]));
		mtz->loadReflections(1);
        mtz->setActiveAmbiguity(1);

		mtz->writeToFile(std::string("inv-") + argv[2], false, false, true);
	}

	if (strcmp(argv[1], "-stats") == 0)
	{
		if (argc < 3)
		{
			std::cout << "arguments: -stats <filein> <threshold>." << std::endl;
			exit(1);
		}

		if (argc >= 3)
		{
			StatisticsManager stats;
			stats.loadFiles(&argv[2], 1, 0);
            
            double threshold = -100;
            int h = 0; int k = 0; int l = 0;
            
            if (argc >= 3)
            {
                threshold = atof(argv[3]);
                std::cout << "Threshold set to " << threshold << std::endl;
            }
            
            if (argc >= 6)
            {
                h = atoi(argv[4]);
                k = atoi(argv[5]);
                l = atoi(argv[6]);

                std::cout << "Searching for " << h << " " << k << " " << l << std::endl;
            }

            GraphDrawer drawer = GraphDrawer(&*stats.mtzs[0]);
            drawer.plotPartialityStats(h, k, l);
		}
	}
    
    if (strcmp(argv[1], "-intensities") == 0)
    {
        if (argc <= 5)
        {
            std::cout << "arguments: -intensities h k l <file1> {<file2> ...}." << std::endl;
            exit(1);
        }
        
        int h = atoi(argv[2]);
        int k = atoi(argv[3]);
        int l = atoi(argv[4]);
        
        if (h == 0 && k == 0 && l == 0)
        {
            h = (double)(rand() / RAND_MAX) * 40;
            k = (double)rand() / RAND_MAX * (40 - h) + h;
            l = (double)rand() / RAND_MAX * (40 - k) + k;
        }
        
        std::cout << "Plotting intensities for (" << h << ", " << k << ", " << l << ")" << std::endl;
        
        
        std::vector<MtzPtr> mtzs;
        
        for (int i = 5; i < argc; i++)
        {
            MtzPtr mtz = MtzPtr(new MtzManager());
            mtz->setFilename(argv[i]);
            mtz->loadReflections(1);
            
            mtzs.push_back(mtz);
        }
        
        GraphDrawer drawer = GraphDrawer(&*mtzs[0]);
        drawer.plotReflectionFromMtzs(mtzs, h, k, l);
    }

#ifdef MAC
    if (strcmp(argv[1], "-partiality") == 0)
	{
		if (argc <= 3)
		{
			std::cout << "arguments: -partiality <ref> <filein> {<maxres>}." << std::endl;
			exit(1);
		}

		MtzManager *reference = new MtzManager();
		reference->setFilename(argv[2]);
		reference->loadReflections(1);
		MtzManager::setReference(reference);
        
        double maxRes = 1.6;
        
        if (argc > 4)
        {
            maxRes = atof(argv[4]);
        }
        
        
        MtzManager *mtz = new MtzManager();
        mtz->setFilename(argv[3]);
        std::cout << "Partiality plot for " << argv[3] << std::endl;
        mtz->loadReflections(PartialityModelNone, true);
        
        vector<Reflection *>refReflections, imageReflections;
        
        GraphDrawer graph = GraphDrawer(mtz);
        
        graph.partialityPlot("partiality", GraphMap(), maxRes);
        
        delete mtz;

		delete reference;
	}
    
    if (strcmp(argv[1], "-scale") == 0)
    {
        if (argc <= 3)
        {
            std::cout << "arguments: -scale <ref> <file1>." << std::endl;
            exit(1);
        }
        
        MtzManager *reference = new MtzManager();
        reference->setFilename(argv[2]);
        reference->loadReflections(1);
        MtzManager::setReference(reference);
        
        MtzManager *mtz = new MtzManager();
        mtz->setFilename(argv[3]);
        mtz->loadReflections(1);
        
        mtz->applyScaleFactorsForBins(50);
        mtz->writeToFile("scaled-" + std::string(argv[3]));
    }
    
    if (strcmp(argv[1], "-bfac") == 0)
    {
        MtzManager *reference = new MtzManager();
        reference->setFilename(argv[2]);
        reference->loadReflections(1);
        
        double bFactor = atof(argv[3]);
        
        reference->applyBFactor(bFactor);
        reference->writeToFile("bfac-" + reference->getFilename(), true);
        
    }
    
	if (strcmp(argv[1], "-bfactor") == 0)
	{
		if (argc <= 2)
		{
			std::cout << "arguments: -bfactor <ref> <file1> {<file2> ...}." << std::endl;
			exit(1);
		}

		vector<MtzManager *> managers = vector<MtzManager *>();

		MtzManager *reference = new MtzManager();
		reference->setFilename(argv[2]);
		reference->loadReflections(1);
		MtzManager::setReference(reference);
        FileParser::setKey("REFINE_B_FACTOR", true);
        
		for (int i = 3; i < argc; i++)
		{
			MtzManager *mtz = new MtzManager();
			mtz->setFilename(argv[i]);
			mtz->loadReflections(1);
            managers.push_back(mtz);
		}

		GraphDrawer graph = GraphDrawer(reference);

		graph.resolutionStatsPlot(managers, "intensity_bins", GraphMap(), true,
				false);
		graph.resolutionStatsPlot(managers, "intensity_bins_2", GraphMap(),
				true, true);
		graph.resolutionStatsPlot(managers);
        graph.bFactorPlot(managers);
        
		for (int i = 0; i < managers.size(); i++)
			delete managers[i];

	}

	if (strcmp(argv[1], "-ccplot") == 0)
	{
		MtzManager *reference = new MtzManager();
		reference->setFilename(argv[2]);
		reference->loadReflections(1);
		MtzManager::setReference(reference);

		for (int i = 3; i < argc; i++)
		{
			MtzManager *image = new MtzManager();
			image->setFilename(argv[i]);
			image->loadReflections(1);

			GraphDrawer graph = GraphDrawer(image);
			graph.correlationPlot("correl");

			delete image;
		}

		delete reference;
	}
#endif
    
    time_t endcputime;
    time(&endcputime);
    
    clock_t difference = endcputime - startcputime;
    double seconds = difference;
    
    int finalSeconds = (int) seconds % 60;
    int minutes = seconds / 60;
    
    std::ostringstream logged;
    logged << "N: Total time: " << minutes << " minutes, "
    << finalSeconds << " seconds (" << seconds << " seconds)." << std::endl;

	logged << "Done" << std::endl;
    Logger::mainLogger->addStream(&logged);
    
    if (strcmp(argv[1], "-i") == 0)
        finishJobNotification(argc, argv, minutes);
    
    sleep(2);
}