Example #1
0
///////////////////////////////////////////////////////////////////////////////
// Run calculations.
///////////////////////////////////////////////////////////////////////////////
void EnergyPlot::run() {

	/*
	 * Make sure the dot plot data can be accessed.
	 */

	// Print a message that says the dot plot data file is being checked.
	cout << "Checking dot plot input file..." << flush;

	// Initialize the dot plot handler and an error string.
	DotPlotHandler* plotHandler = 0;
	string error = "";

	// Create the RNA strand that holds the dot plot data.
	RNA* strand = new RNA( inputFile.c_str(), 4 );
	ErrorChecker<RNA>* checker = new ErrorChecker<RNA>( strand );
	error = checker->returnError();
	if( error == "" ) { cout << "done." << endl; }

	/*
	 * Prepare the dot plot data.
	 */

	if( error == "" ) {

		// Print a message saying that the dot plot data is being prepared.
		cout << "Preparing dot plot data..." << flush;

		// Build the dot plot handler using the strand's sequence length.
		int length = strand->GetSequenceLength();
		plotHandler = new DotPlotHandler( descriptionSettings.getDescription(inputFile, true), length );

		// Add all possible dots to the dot plot.
		for( int i = 1; i <= length; i++ ) {
			for( int j = i; j <= length; j++ ) {
				double energy = strand->GetPairEnergy( i, j );
				if( energy > 0.0 ) { energy = numeric_limits<double>::infinity(); }
				else {
					stringstream stream( stringstream::in | stringstream::out );
					stream << fixed << setprecision( 1 ) << energy;
					stream >> energy;
				}
				plotHandler->addDotValue( i, j, energy );
			}
		}

		// Print a message saying that the dot plot data has been prepared.
		cout << "done." << endl;
	}
///////////////////////////////////////////////////////////////////////////////
// Run calculations.
///////////////////////////////////////////////////////////////////////////////
void DrawStructure::run() {

    // Create a variable that tracks the result of calculations.
    // Throughout, the calculation continues if result = "".
    string result = "";

    // Create variables to determine the range of structures that will be drawn.
    // Initialize them so the range of structures will only be the specific structure given, if any.
    int start = number;
    int end = number;

    // Create a temporary strand for the purpose of determining the number and range of structures to be drawn.
    int structures = 0;
    if( result == "" ) {

        // Show a message that the information gathering phase has begun.
        cout << "Determining structure information..." << flush;

        // Create the temporary strand to get the number of structures, then delete it.
        RNA* strand = new RNA( inputFile.c_str(), 1 );
        ErrorChecker<RNA>* checker = new ErrorChecker<RNA>( strand );
        result = checker->returnError();
        structures = strand->GetStructureNumber();
        delete strand;
        delete checker;

        // Use the number of structures to determine if the user gave an appropriate specific structure value, if necessary.
        // If the structure value is invalid, set the error string to show this.
        if( result == "" ) {
            if( number != -1 && !( number >= 1 && number <= structures ) ) {
                result = "Structure number out of range.\n";
            }
        }

        // If the user did not give a specific structure, set the range of structures to all possible structures.
        if( number == -1 ) {
            start = 1;
            end = structures;
        }

        // If no error occurred, print a message saying that the information gathering phase is done.
        // Otherwise, print the error message.
        if( result == "" ) {
            cout << "done." << endl;
        }
        else {
            cerr << endl << result;
        }
    }

    // If no errors have occurred in structure preparation, draw the structure(s).
    if( result == "" ) {

        // Print a message saying that the drawing phase has started.
        cout << "Drawing..." << endl;

        // If the output file already exists, delete the file so a new file with that name can be generated.
        ifstream test( outputFile.c_str() );
        bool exists = test.good();
        test.close();
        if( exists ) {
            remove( outputFile.c_str() );
        }

        // For each structure in the range, get its data and draw it appropriately.
        for( int i = start; i <= end; i++ ) {

            // Print a message saying that a particular structure has started drawing.
            cout << "    Drawing structure " << i << "..." << flush;

            // Create the drawing handler and set whether it should circle nucleotides.
            StructureImageHandler* handler = new StructureImageHandler();
            handler->setNucleotidesCircled( encircle );

            // Read in the structure.
            // Note that if a pseudoknotted structure is fed to the radial routine, the radial routine automatically calls the circular routine.
            if( result == "" ) {
                if( circular ) {
                    result = handler->readCircular( inputFile, i );
                }
                else if( flat ) {
                    result = handler->readLinear( inputFile, i );
                }
                else {
                    handler->readRadial( inputFile, i );
                }
            }

            // Read in annotation, if necessary.
            if( result == "" ) {
                if( probabilityFile != "" ) {
                    result = handler->addAnnotationProbability( probabilityFile, textAnnotation );
                } else if( SHAPEFile != "" ) {
                    result = handler->addAnnotationSHAPE( SHAPEFile );
                }
            }

            // Flip the structure, if asked.
            if( ( result == "" ) && ( levorotatory == true ) ) {
                handler->flipHorizontally();
            }

            // Write the structure image, as either SVG or Postscript.
            if( result == "" ) {
                if( isSVG ) {
                    handler->writeSVG( outputFile );
                }
                else {
                    bool append = ( number == -1 );
                    if( i == start ) {
                        ifstream test( outputFile.c_str() );
                        if( test ) {
                            append = false;
                        }
                    }
                    handler->writePostscript( outputFile, append );
                }
            }

            // Delete the drawing handler.
            delete handler;

            // Print a message saying that a particular structure has finished drawing, if no error occurred.
            // If an error did occur, break out of the loop.
            if( result == "" ) {
                cout << "done." << endl;
            }
            else break;
        }

        // If no error occurred, print a message saying that the drawing phase is done.
        // Otherwise, print the error message.
        if( result == "" ) {
            cout << "done." << endl;
        }
        else {
            cerr << endl << result << endl;
        }
    }

    // Print confirmation of run finishing.
    if( result == "" ) {
        cout << calcType << " complete." << endl;
    }
    else {
        cerr << calcType << " complete with errors." << endl;
    }

}