MASA-Core
CrosspointsFile.cpp
Go to the documentation of this file.
00001 /*******************************************************************************
00002  *
00003  * Copyright (c) 2010-2015   Edans Sandes
00004  *
00005  * This file is part of MASA-Core.
00006  * 
00007  * MASA-Core is free software: you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation, either version 3 of the License, or
00010  * (at your option) any later version.
00011  * 
00012  * MASA-Core is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with MASA-Core.  If not, see <http://www.gnu.org/licenses/>.
00019  *
00020  ******************************************************************************/
00021 
00022 #include "CrosspointsFile.hpp"
00023 
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <algorithm>
00027 using namespace std;
00028 
00029 CrosspointsFile::CrosspointsFile(string filename) {
00030     this->filename = filename;
00031     this->tmpFilename = filename + ".tmp";
00032     this->autoSave = false;
00033     this->file = NULL;
00034 }
00035 
00036 CrosspointsFile::~CrosspointsFile() {
00037         close();
00038 }
00039 
00040 void CrosspointsFile::loadCrosspoints() {
00041     FILE * file = fopen(filename.c_str(), "r");
00042     if (file == NULL) {
00043         this->clear();
00044         printf("NO CROSSPOINTS: %s\n", filename.c_str());
00045         return;
00046     }
00047 
00048     char line[500];
00049     int started = 0;
00050     while (fgets(line, sizeof(line), file) != NULL) {
00051         if (strcmp(line, "END\n")==0) break;
00052         if (started) {
00053             crosspoint_t temp;
00054             sscanf(line, "%d,%d,%d,%d", &temp.type, &temp.i, &temp.j, &temp.score);
00055             this->push_back(temp);
00056 
00057         }
00058         if(strcmp(line, "START\n")==0) {
00059             started = 1;
00060             this->clear();
00061         }
00062     }
00063     fclose(file);
00064 
00065         printf("LOAD CROSSPOINTS: count %d\n", this->size());
00066     /*if (this->front().i < this->back().i || this->front().j < this->back().j) {
00067         std::reverse(this->begin(),this->end());
00068     }*/
00069 }
00070 
00071 int CrosspointsFile::getLargestPartitionSize(int* max_i, int* max_j) {
00072     int max_size_i = 0;
00073         int max_size_j = 0;
00074         for (int i=1; i < this->size(); i++) {
00075         crosspoint_t curr = this->at(i);
00076         crosspoint_t prev = this->at(i-1);
00077         int delta_i = abs(prev.i-curr.i);
00078         int delta_j = abs(prev.j-curr.j);
00079         if (delta_i != 0 && delta_j != 0) {
00080                 // partitions with full gap are discarded (trivial case)
00081                         if (max_size_i < delta_i) max_size_i = delta_i;
00082                         if (max_size_j < delta_j) max_size_j = delta_j;
00083         }
00084     }
00085         if (max_i != NULL) *max_i = max_size_i;
00086         if (max_j != NULL) *max_j = max_size_j;
00087         if (max_size_i > max_size_j) {
00088                 return max_size_i;
00089         } else {
00090                 return max_size_j;
00091         }
00092 }
00093 
00094 void CrosspointsFile::setAutoSave() {
00095         autoSave = true;
00096         open();
00097 }
00098 
00099 void CrosspointsFile::writeToFile(string filename) {
00100         FILE* file = fopen(filename.c_str(), "w");
00101         fprintf(file, "START\n");
00102         for (int i=0; i < this->size(); i++) {
00103                 crosspoint_t c = at(i);
00104                 fprintf(file, "%d,%d,%d,%d\n", c.type, c.i, c.j, c.score);
00105         }
00106         fprintf(file, "END\n");
00107     fclose(file);
00108 }
00109 
00110 void CrosspointsFile::open() {
00111     file = fopen(tmpFilename.c_str(), "w");
00112         if (file == NULL) {
00113                 fprintf(stderr, "Error opening crosspoints file: %s\n", tmpFilename.c_str());
00114                 exit(1);
00115         }
00116     fprintf(stderr, "START\n");
00117     fprintf(file, "START\n");
00118 }
00119 
00120 void CrosspointsFile::close() {
00121     if (file != NULL) {
00122         fprintf(stderr, "END\n");
00123         fprintf(file, "END\n");
00124 
00125         fclose(file);
00126         file = NULL;
00127 
00128         rename(tmpFilename.c_str(), filename.c_str());
00129     }
00130 }
00131 
00132 void CrosspointsFile::write(int i, int j, int score, int type) {
00133         crosspoint_t crosspoint;
00134         crosspoint.i = i;
00135         crosspoint.j = j;
00136         crosspoint.score = score;
00137         crosspoint.type = type;
00138         write(crosspoint);
00139 }
00140 
00141 void CrosspointsFile::write(crosspoint_t crosspoint) {
00142         if (file == NULL) {
00143                 fprintf(stderr, "Crosspoints File not opened.\n");
00144                 exit(1);
00145         }
00146     fprintf(file, "%d,%d,%d,%d\n", crosspoint.type, crosspoint.i, crosspoint.j, crosspoint.score);
00147     fprintf(stderr, "%d,%d,%d,%d\n", crosspoint.type, crosspoint.i, crosspoint.j, crosspoint.score);
00148     push_back(crosspoint);
00149     fflush(file);
00150 }
00151 
00152 void CrosspointsFile::save() {
00153         open();
00154         for (iterator it=begin() ; it != end(); it++ ) {
00155             fprintf(file, "%d,%d,%d,%d\n", it->type, it->i, it->j, it->score);
00156             //fprintf(stderr, "%d,%d,%d,%d\n", it->type, it->i, it->j, it->score);
00157         }
00158         close();
00159 }
00160 
00161 void CrosspointsFile::reverse(int seq0_len, int seq1_len) {
00162         for (int i=0; i < this->size(); i++) {
00163                 at(i) = at(i).reverse(seq0_len, seq1_len);
00164         }
00165         std::reverse(this->begin(),this->end());
00166 }
00167