def run_for_file(filein, location, solver, restrict):
    data_in = read_file(os.path.join(location, filein))
    output_folder = get_output_folder(filein, "output_files", restrict)
    t_start = datetime.datetime.utcnow()
    if solver in solvers_7rules:
        output_file = os.path.join(output_folder, prettyprint_solver(solver) + '.txt')
        file_handler = write_file_header(output_file, False)
        for (mass, tolerance) in data_in:
            t1 = datetime.datetime.utcnow()
            formulas = solver.search(mass, tolerance, delta, restrict)
            t2 = datetime.datetime.utcnow()
            write_file_formulas(file_handler, formulas, mass, tolerance, t2 - t1)
        file_handler.close()
    elif solver in solvers_basic:
        output_file = os.path.join(output_folder, prettyprint_solver(solver) + '.txt')
        output_file_filtered = os.path.join(output_folder, prettyprint_solver(solver) + '_post_7rules.txt')
        file_handler = write_file_header(output_file, False)
        file_handler_filtered = write_file_header(output_file_filtered, True)
        for (mass, tolerance) in data_in:
            t1 = datetime.datetime.utcnow()
            formulas = solver.search(mass, tolerance, delta, restrict)
            t2 = datetime.datetime.utcnow()
            formulas_filtered = the_7rules.filter_all(formulas, restrict)
            t3 = datetime.datetime.utcnow()
            write_file_formulas(file_handler, formulas, mass, tolerance, t2 - t1)
            write_file_formulas(file_handler_filtered, formulas_filtered, mass, tolerance, t3 - t1)
        file_handler.close()
        file_handler_filtered.close()
    t_end = datetime.datetime.utcnow()
    print prettyprint_solver(solver) + " processed " + filein + " in " + str(t_end - t_start)
def run_locally():
    """
    Run from console
    Enter manually: 1 mass and 1 tolerance (in ppm)
    :return: print formulas
    """

    # message for run_locally
    incorrect_input = "\n The input was not correctly formatted \n restarting..."

    try:
        # display algorithms
        i = 1
        for solver in solvers_basic:
            print str(i) + " " + prettyprint_solver(solver)
            i += 1
        for solver in solvers_7rules:
            print str(i) + " " + prettyprint_solver(solver)
            i += 1
        print

        # get input from user
        algorithm = int(input("Enter number for your choice: "))
        mass = float(input("Enter a mass: "))
        tolerance = float(input("Enter a tolerance (in ppm): ")) / 1000000
        if algorithm <= len(solvers_basic):
            function = solvers_basic[algorithm - 1]
        else:
            function = solvers_7rules[algorithm - len(solvers_basic) - 1]
        print
        print "Would you like to restrict the search to CHNOPS? (recommended)"
        choice = raw_input("Enter y/n: ")
        restrict = False
        if choice == 'y':
            restrict = True
        print

        # perform search using input
        t1 = datetime.datetime.utcnow()
        formulas = function.search(mass, tolerance, delta, restrict)
        t2 = datetime.datetime.utcnow()
        for formula in formulas:
            print get_formula_string(formula)
        if len(formulas) == 0:
            print "No results"
        print "Search time: " + str(t2 - t1)
        print

        # get input for filtering and proceed as required
        print "Would you like to filter these? "
        choice = raw_input("Enter y/n: ")
        if choice == 'y':
            t1 = datetime.datetime.utcnow()
            formulas_filtered = the_7rules.filter_all(formulas, restrict)
            for formula in formulas_filtered:
                print get_formula_string(formula)
            if len(formulas_filtered) == 0:
                print "No results"
            t2 = datetime.datetime.utcnow()
            print "Filter time: " + str(t2 - t1)
        print

        # ask if to terminate
        print "This iteration is done, would you like to go again? "
        choice = raw_input("Enter y/n: ")
        if choice == 'y':
            run_locally()
    except SyntaxError as se:
        print incorrect_input
        run_locally()
    except NameError as ne:
        print incorrect_input
        run_locally()
    except IndexError as ie:
        print incorrect_input
        run_locally()