Beispiel #1
0
static bool
stream_next_seq(
	panda_seq_identifier *id,
	panda_qual **forward,
	size_t *forward_length,
	panda_qual **reverse,
	size_t *reverse_length,
	struct fastq_data *data) {
	panda_seq_identifier rid;
	char buffer[BUFFER_SIZE];
	size_t fsize;
	size_t rsize;
	int fdir;
	int rdir;

	*forward = NULL;
	*reverse = NULL;

	if (!read_line(buffer, BUFFER_SIZE, &data->forward, &fsize)) {
		return false;
	}
	if ((fdir = panda_seqid_parse(id, &buffer[1], data->policy)) == 0) {
		LOGV(PANDA_DEBUG_FILE, PANDA_CODE_ID_PARSE_FAILURE, "%s", buffer);
		return false;
	}
	if (!read_line(buffer, BUFFER_SIZE, &data->reverse, &rsize)) {
		return false;
	}
	if ((rdir = panda_seqid_parse(&rid, &buffer[1], data->policy)) == 0) {
		LOGV(PANDA_DEBUG_FILE, PANDA_CODE_ID_PARSE_FAILURE, "%s", buffer);
		return false;
	}
	if (!panda_seqid_equal(id, &rid)) {
		LOG(PANDA_DEBUG_FILE, PANDA_CODE_NOT_PAIRED);
		return false;
	}
	if (!read_seq(id, data->forward_seq, PANDA_MAX_LEN, &data->forward, iupac_forward, data, forward_length)) {
		*forward_length = 0;
		*reverse_length = 0;
		LOG(PANDA_DEBUG_FILE, PANDA_CODE_PARSE_FAILURE);
		return false;
	}
	if (!read_seq(id, data->reverse_seq, PANDA_MAX_LEN, &data->reverse, iupac_reverse, data, reverse_length)) {
		*forward_length = 0;
		*reverse_length = 0;
		LOG(PANDA_DEBUG_FILE, PANDA_CODE_PARSE_FAILURE);
		return false;
	}
	*forward = data->forward_seq;
	*reverse = data->reverse_seq;
	return true;
}
/*--------------------------------------------------------------------------*/
sequ *input(int argc, char **argv)
{
  sequ *sq;  
  int istty;

  istty=isatty(fileno(stdout))&&isatty(fileno(stdin));
  if(istty)
    {
      sq=lies_seq(stdin);
    }
  else
    { 
      sq=read_seq(stdin);
    }
  return(sq);  
}
Beispiel #3
0
int main(int argc, char *argv[])
{
    vector<HeuristicChooseReversal *> hcr_seq;

    /* Set random seed */
    unsigned long seed = mix(clock(), time(NULL), getpid());

    /* Set random seed */
    srand(seed);

    /* Exact Unitaty Parameters */
    ofstream fout;

    /* Time results */
    timestamp_t time_begin;
    timestamp_t time_end;
    time_begin = get_timestamp();

    /* Log result file */
    ostream *logOut = NULL;
    ostream *logTime = NULL;

    /* Check essential parameters */
    // TODO: start to use parameter names instead of parameter sequence, ex: -p 1,2,4...
    if (argc < 7) {
        printf("Usage:\n");
        printf("lwr_test_metaheuristic permutation number_of_iteration inversion_limit frame_limit is_signed cost_function {-r=[[...]]}.\n\n");
        printf("Parameters description:\n");
        printf(" 1. permutation: the input permutation.\n\n");
        printf(" 2. number_of_iteration: number of iterations that will\n");
            printf("be performed by the Metaheuristic.\n\n");
        printf(" 3. inversion_limit: limit (upper bound) for the number of inversions\n");
            printf("that will be selected at each iteration of the heuristic for building solutions.\n\n");
        printf(" 4. frame_limit: percentege (in the interval (0,100]) of frames that \n");
            printf("will be selected at each iteration of the Metaheuristic.\n\n");
        printf(" 5. is_signed: 1 if the input permutation is signed, 0 otherwise.\n\n");
        printf(" 6. cost_function: 1 for linear function, 2 for quadratic function and 3 logaritmic function.\n\n");
        printf(" 7. -r=[[...]]: Optional parameter to give a seed solution as a input parameter\n");

        return 0;
    }

    /* Seed result */
    Result seed_result;

    /* Permutation parameters */
    parameter perm_param;

    /* Exact Unitary parameters */
    parameter exactP;

    /* Improve parameters */

    parameter improveP;

    /* ChooseHeuristic parameters*/
    parameter hcrP;

    /* list of solvers for choose reversals */
    vector<Solver *> solver_hcr;

    string intervalMethod = "WINDOW";
    string heuristicReversalsName = "BenefitLoss";

    /* -------------------Read the parameters  From terminal----------------- */

    int p_read = 1;

    // Read the permutation's sequence
    vector<ll> perm_seq = read_seq(argv[p_read++]);

    int num_it;
    sscanf(argv[p_read++], "%d", &num_it);

    int inv_limit;
    sscanf(argv[p_read++], "%d", &inv_limit);

    double frame_limit;
    sscanf(argv[p_read++], "%lf", &frame_limit);

    int is_signed;
    sscanf(argv[p_read++], "%d", &is_signed);

    int cost_function;
    sscanf(argv[p_read++], "%d", &cost_function);

    /* Construct the permutation */
    if (is_signed) {
        perm_param = getSignedParameters();
    } else {
        perm_param = getUnsignedGraspParameters();
    }
    Permutation perm = Permutation(perm_param, perm_seq);
    // makes a copy from the original input permutation and set the normal mode
    Permutation perm_orig = perm;

    // Look for a input solution
    Result input_seed_result;
    bool has_input_seed_result = false;
    if (p_read < argc) {
        string solution = string(argv[p_read++]);
        if (solution.substr(0, 2) == "-r") {
            // in this caseheuristicName is supposed to be like -r=[[...]]
            string str_result = solution.substr(3);
	    // Alterado por Ulisses
            input_seed_result = Util::getResultFromStr(str_result.c_str(), cost_function);
            has_input_seed_result = true;

            Permutation perm_copy = perm;
            bool valid = Solver::validateResult(perm_copy, input_seed_result, cout);
            if (!valid) {
                cout<<"Invalid input solution."<<endl;
                exit(1);
            }
        }
    }


    /* -------------Check if the permutation is already sorted--------------- */
    if (perm.isSorted(1, perm.size())) {
        Result::printResult(seed_result, cout);
        cout<<endl;
        return 0;
    }

    /* ----------------------- Set Default Parameters ----------------------- */
 
    parameter default_parameters = getDefaultParameters();

    /* Set the output for results logging */
    if (p_read < argc) {
        logOut = new ofstream(argv[p_read++], std::ofstream::out|ofstream::app);
    }
    if (p_read < argc) {
        logTime = new ofstream(argv[p_read++], std::ofstream::out|ofstream::app);
    }

    /* Look for Parameters */
    string type;
    parameter::iterator it;
    for (it = default_parameters.begin(); it != default_parameters.end(); it++) {
        pair<string, string> p = getParameterPair(it->F);
        string type = p.F;
        string name = p.S;
        double value = it->S;
        // get Num selected rev
        if (type == "HCR") {
            hcrP[name] = value;
        } else if (type == "IMP") {
            improveP[name] = value;
        } else if (type == "BUILD_SOLUTION") {
            exactP[name] = value;
        }
        p_read++;
    }
    /* Look for the unsigned mode */
    improveP["UNSIGNED"] = !is_signed;

    /* Create log file */
    //ofstream log_out("log_test_signed.txt",ios_base::app);

    /* ----------------------Set Parameters from terminal---------------------- */
    hcrP["COST_FUNCTION"]  = cost_function;
    improveP["COST_FUNCTION"]  = cost_function;
    exactP["COST_FUNCTION"]  = cost_function;

    improveP["NUM_ROUND"] = num_it;
    exactP["NUM_SELECTED_REV"] = inv_limit;

    if (frame_limit <= 0 || frame_limit > 100) {
        cerr<<"Error: frame_limit must be in the interval (0, 100]"<<endl;
        exit(1);
    }
    improveP["ROLETTE_PERCENT_DISCARD"] = (100.0 - frame_limit);


    /*---------------- Build the Choosen reversal Heuristic -------------------*/
    hcr_seq = build_hcr_seq(
      intervalMethod,
      improveP,
      hcrP,
      exactP,
      heuristicReversalsName
    );
    if (improveP["EXACT_UNITATY_LS"]) {
        solver_hcr.push_back(new ExactUnitary(exactP, hcr_seq));
    } else if (improveP["GRASP_HEURISTIC_LS"]) {
        solver_hcr.push_back(new GRASPHeuristic(exactP, hcr_seq));
    }


    /*---------------------- Build The seed solution -------------------------*/
    if (has_input_seed_result) {
        seed_result = input_seed_result;
    } else if (is_signed){
      // Alterado por Ulisses
      seed_result = sortByGRIMM(perm, true, cost_function);
    } else {
      seed_result = sortByGRIMMUnsigned(perm, NUM_IT_GRIMM_UNSIGNED, true, cost_function);
    }
        

    /*--------------------- Build Improvement Metaheuristic ------------------*/
    ImproveReversals * ir = new ImproveReversals(
        solver_hcr,
        intervalMethod,
        improveP,
        logOut
    );

    /*-------------------------- Try improve the result ----------------------*/
    Result r_improved = tryImprove(
        seed_result,
        ir,
        perm_orig,
	cost_function
     );


    /*----------------------- Validate result --------------------------------*/
    bool valid = Solver::validateResult(perm_orig, r_improved, cout);

    Result seedR = seed_result;
    //seedR.reversals = seed_result;
    seedR = Solver::calcTotalCost(seedR, cost_function);


    /*------------------------- Print the result -----------------------------*/
    if (valid) {
        Result::printResult(r_improved, cout);
        cout<<endl;
    } else {
        cout<<"ERROR";
        return 1;
    }

    /*------------------------- Print time -----------------------------------*/
    time_end = get_timestamp();
    double time_sec = get_interval_time(time_begin, time_end);

    if (logTime != NULL)
        *(logTime)<<time_sec<<endl;

    /* Clean all resources */
    for (size_t i = 0; i < solver_hcr.size(); i++) {
        delete solver_hcr[i];
    }
    solver_hcr.clear();
    if (logOut != NULL)
        delete logOut;
    delete ir;
    cleanResources();

    return valid;
}
 void Forwarder::read_seq(const std::string &seq_filename, const size_t alphabet_size, const size_t min_no_eval) {
   std::vector<size_t> nStatesSave;
   read_seq(seq_filename, alphabet_size, nStatesSave, min_no_eval);
 }