I need to read afile with matrix data and create matrix within my prrogram. The Matrix file format is looks similar to this:
# Matrix made by matblas from blosum62.iij
# * column uses minimum score
# BLOSUM Clustered Scoring Matrix in 1/2 Bit Units
# Blocks Database = /data/blocks_5.0/blocks.dat
# Cluster Percentage: >= 62
# Entropy = 0.6979, Expected = -0.5209
A R N D C Q E G H I L K M F P S T W Y V B Z X *
A 4 -1 -2 -2 0 -1 -1 0 -2 -1 -1 -1 -1 -2 -1 1 0 -3 -2 0 -2 -1 0 -4
R -1 5 0 -2 -3 1 0 -2 0 -3 -2 2 -1 -3 -2 -1 -1 -3 -2 -3 -1 0 -1 -4
N -2 0 6 1 -3 0 0 0 1 -3 -3 0 -2 -3 -2 1 0 -4 -2 -3 3 0 -1 -4
D -2 -2 1 6 -3 0 2 -1 -1 -3 -4 -1 -3 -3 -1 0 -1 -4 -3 -3 4 1 -1 -4
C 0 -3 -3 -3 9 -3 -4 -3 -3 -1 -1 -3 -1 -2 -3 -1 -1 -2 -2 -1 -3 -3 -2 -4
Q -1 1 0 0 -3 5 2 -2 0 -3 -2 1 0 -3 -1 0 -1 -2 -1 -2 0 3 -1 -4
E -1 0 0 2 -4 2 5 -2 0 -3 -3 1 -2 -3 -1 0 -1 -3 -2 -2 1 4 -1 -4
G 0 -2 0 -1 -3 -2 -2 6 -2 -4 -4 -2 -3 -3 -2 0 -2 -2 -3 -3 -1 -2 -1 -4
H -2 0 1 -1 -3 0 0 -2 8 -3 -3 -1 -2 -1 -2 -1 -2 -2 2 -3 0 0 -1 -4
I -1 -3 -3 -3 -1 -3 -3 -4 -3 4 2 -3 1 0 -3 -2 -1 -3 -1 3 -3 -3 -1 -4
L -1 -2 -3 -4 -1 -2 -3 -4 -3 2 4 -2 2 0 -3 -2 -1 -2 -1 1 -4 -3 -1 -4
K -1 2 0 -1 -3 1 1 -2 -1 -3 -2 5 -1 -3 -1 0 -1 -3 -2 -2 0 1 -1 -4
M -1 -1 -2 -3 -1 0 -2 -3 -2 1 2 -1 5 0 -2 -1 -1 -1 -1 1 -3 -1 -1 -4
F -2 -3 -3 -3 -2 -3 -3 -3 -1 0 0 -3 0 6 -4 -2 -2 1 3 -1 -3 -3 -1 -4
P -1 -2 -2 -1 -3 -1 -1 -2 -2 -3 -3 -1 -2 -4 7 -1 -1 -4 -3 -2 -2 -1 -2 -4
S 1 -1 1 0 -1 0 0 0 -1 -2 -2 0 -1 -2 -1 4 1 -3 -2 -2 0 0 0 -4
T 0 -1 0 -1 -1 -1 -1 -2 -2 -1 -1 -1 -1 -2 -1 1 5 -2 -2 0 -1 -1 0 -4
W -3 -3 -4 -4 -2 -2 -3 -2 -2 -3 -2 -3 -1 1 -4 -3 -2 11 2 -3 -4 -3 -2 -4
Y -2 -2 -2 -3 -2 -1 -2 -3 2 -1 -1 -2 -1 3 -3 -2 -2 2 7 -1 -3 -2 -1 -4
V 0 -3 -3 -3 -1 -2 -2 -3 -3 3 1 -2 1 -1 -2 -2 0 -3 -1 4 -3 -2 -1 -4
B -2 -1 3 4 -3 0 1 -1 0 -3 -4 0 -3 -3 -2 0 -1 -4 -3 -3 4 1 -1 -4
Z -1 0 0 1 -3 3 4 -2 0 -3 -3 1 -1 -3 -1 0 -1 -3 -2 -2 1 4 -1 -4
X 0 -1 -1 -1 -2 -1 -1 -1 -1 -1 -1 -1 -1 -1 -2 0 0 -2 -1 -1 -1 -1 -1 -4
* -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 1
Letters have constant place so since I am interested only in four of them I could assign x
and y
in program. What I need is only a matrix with values on which I could search for values by giving function 'GetValue' 'x' and 'y'.
Here is my code. It is only one part a class defined in header file, which contains and search through matrix for values. Maybe it is not elegant way to do that but I don't have so much time so right now I want to do it fast. Later I would have more time so I will do it in better way.
/*
* algorytm.cpp
* implementacja algorytmu
*
* Autor: Mateusz
*
*/
#include <cstdlib>
#include <iostream>
#include <string>
#include <stdio.h>
#include <fstream>
#include <vector>
#include "matryca_sub.h"
#include <sstream>
#include <istream>
#include <sstream>
using namespace std;
int ScoreMatrix::MainMatrix(char *mat_file, int x, int y)
{
cout << "Main matrix function start" << endl;
CreateMatrix(30);
ReadMatrix(mat_file);
int val;
val=GetValue(x, y);
return val;
cout << "Main matrix function end" << endl;
}
void ScoreMatrix::CreateMatrix(int edge)
{
cout << "Creating sub matrix start" << endl;
//int** scores = new int* [*edge-1];
//for (int i=0; i<=23; i++) scores[i] = new int[*edge-1];
if( scores != 0 ) delete [] scores;
scores = new int [edge*edge];
cols = edge;
cout << "Sub matrix created" << endl;
}
void ScoreMatrix::SetValue(int x, int y, int val)
{
cout << "write to sub matrix start" << endl;
//scores[x][y] = val;
scores[(cols* y) + x] = val;
cout << "write to sub matrix end" << endl;
}
int ScoreMatrix::GetValue(int x, int y)
{
//cout << "GetValue start" << endl;
//return scores[x][y];
return scores[(cols * x) + y];
cout << "GetValue end" << endl;
}
void ScoreMatrix::ReadMatrix(char *mat_file)
{
cout << "start reading matrix from file" << endl;
int row=0;
ifstream mfile;
mfile.open(mat_file);
mfile.precision(2);
mfile.setf(ios::fixed, ios::showpoint);
while(!mfile.eof())
{
for (row=0; row<=23; row++)
{
string line;
getline( mfile, line);
istringstream iss(line);
if (line[0] !='#' && line[0] != ' ')
{
int s;
iss >> s;
for (int i=1; !iss.eof(); i++)
{
iss >> s;
SetValue(i, row, s);
}
}
}
}
cout << "end reading matrix from file" << endl;
}
Header file included:
/*
* File: mat_sub.h
* Author: mateusz
*
* Created on 6 luty 2011, 14:44
*/
#ifndef MAT_SUB_H
#define MAT_SUB_H
#include <cstdlib>
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string.h>
using namespace std;
class ScoreMatrix
{
public:
ScoreMatrix():
cols (0)
,scores (0)
{}
char mat_file;
int MainMatrix(char *mat_file, int x, int y);
int GetValue(int x, int y);
private:
int cols, rows;
int* scores;
void CreateMatrix(int edge);
void SetValue(int x, int y, int val);
void ReadMatrix(char *mat_file);
};
#endif /* MAT_SUB_H */
Before seagfoult function print one of many start reading matrix from file
.