MASA-Core
SequenceData.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 "SequenceData.hpp"
00023 
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <string>
00027 using namespace std;
00028 
00029 #include "Constants.hpp"
00030 #include "SequenceModifiers.hpp"
00031 
00032 /*SequenceData::SequenceData(char* data, int size, SequenceModifiers* modifiers) {
00033         this->modifiers = modifiers;
00034         this->forwardData = data;
00035         this->originalSize = size;
00036         this->size = size;
00037         this->reverseData = createReverseData(this->forwardData, this->size);
00038 }*/
00039 
00040 SequenceData::SequenceData(string filename, SequenceModifiers* modifiers) {
00041         this->modifiers = modifiers;
00042         loadFile(filename);
00043         if (this->modifiers->getTrimStart() == 0) {
00044                 this->modifiers->setTrimStart(1);
00045         }
00046         if (this->modifiers->getTrimEnd() == 0) {
00047                 this->modifiers->setTrimEnd(this->size);
00048         }
00049 }
00050 
00051 SequenceData::~SequenceData() {
00052         free(forwardData);
00053         forwardData = NULL;
00054         free(reverseData);
00055         reverseData = NULL;
00056 }
00057 
00058 char* SequenceData::createReverseData(char* forwardData, int size) {
00059         char* reverseData = (char*) (malloc(size + 1));
00060         for (int i = 0; i < size; i++) {
00061                 reverseData[size - 1 - i] = forwardData[i];
00062         }
00063         reverseData[size] = '\0';
00064         return reverseData;
00065 }
00066 
00067 void SequenceData::loadFile(string filename) {
00068     FILE* file = fopen(filename.c_str(), "rt");
00069     if (file == NULL) {
00070                 fprintf(stderr, "Error opening fasta file: %s\n", filename.c_str());
00071                 exit(1);
00072     }
00073 
00074     fseek(file, 0L, SEEK_END);
00075     long fileSize = ftell(file);
00076     rewind(file);
00077 
00078     char line[500];
00079     fgets(line, sizeof(line), file);
00080     description = line;
00081 
00082     this->forwardData = (char*)(malloc(fileSize+1));
00083 
00084 
00085         char complement_map[256];
00086         for (int i=0; i<256; i++) {
00087                 complement_map[i] = toupper(i);
00088         }
00089         if (modifiers->isComplement()) {
00090                 complement_map['A'] = complement_map['a'] = 'T';
00091                 complement_map['T'] = complement_map['t'] = 'A';
00092                 complement_map['C'] = complement_map['c'] = 'G';
00093                 complement_map['G'] = complement_map['g'] = 'C';
00094         }
00095         if (modifiers->isClearN()) {
00096                 complement_map['N'] = complement_map['n'] = 'n'; // lower case
00097         }
00098 
00099         this->size = 0;
00100         this->originalSize = 0;
00101     int i;
00102     while ((i=fgetc(file)) != EOF) {
00103         if (i == '\r' || i == '\n' || i == ' ') continue;
00104         this->originalSize++;
00105                 //if (modifiers->isClearN() && ((char)i == 'N' || (char)i == 'n')) continue;
00106                 this->forwardData[this->size] = complement_map[(char)i];
00107                 this->size++;
00108     }
00109     this->forwardData[this->size] = '\0';
00110     this->forwardData = (char*)realloc(this->forwardData, this->size+1);
00111     this->reverseData = createReverseData(this->forwardData, this->size);
00112 
00113     fclose(file);
00114 }
00115 
00116 char* SequenceData::getForwardData() const {
00117         return forwardData;
00118 }
00119 
00120 char* SequenceData::getReverseData() const {
00121         return reverseData;
00122 }
00123 
00124 int SequenceData::getSize() const {
00125         return size;
00126 }
00127 
00128 string SequenceData::getDescription() const
00129 {
00130     return description;
00131 }
00132 
00133 int SequenceData::getOriginalSize() const {
00134         return originalSize;
00135 }
00136