Note that there are some explanatory texts on larger screens.

plurals
  1. POLoading a Matrix from a file
    text
    copied!<p>I need to read afile with matrix data and create matrix within my prrogram. The Matrix file format is looks similar to this:</p> <pre><code># 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: &gt;= 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 </code></pre> <p>Letters have constant place so since I am interested only in four of them I could assign <code>x</code> and <code>y</code> 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'. </p> <p>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.<br></p> <pre><code>/* * algorytm.cpp * implementacja algorytmu * * Autor: Mateusz * */ #include &lt;cstdlib&gt; #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;stdio.h&gt; #include &lt;fstream&gt; #include &lt;vector&gt; #include "matryca_sub.h" #include &lt;sstream&gt; #include &lt;istream&gt; #include &lt;sstream&gt; using namespace std; int ScoreMatrix::MainMatrix(char *mat_file, int x, int y) { cout &lt;&lt; "Main matrix function start" &lt;&lt; endl; CreateMatrix(30); ReadMatrix(mat_file); int val; val=GetValue(x, y); return val; cout &lt;&lt; "Main matrix function end" &lt;&lt; endl; } void ScoreMatrix::CreateMatrix(int edge) { cout &lt;&lt; "Creating sub matrix start" &lt;&lt; endl; //int** scores = new int* [*edge-1]; //for (int i=0; i&lt;=23; i++) scores[i] = new int[*edge-1]; if( scores != 0 ) delete [] scores; scores = new int [edge*edge]; cols = edge; cout &lt;&lt; "Sub matrix created" &lt;&lt; endl; } void ScoreMatrix::SetValue(int x, int y, int val) { cout &lt;&lt; "write to sub matrix start" &lt;&lt; endl; //scores[x][y] = val; scores[(cols* y) + x] = val; cout &lt;&lt; "write to sub matrix end" &lt;&lt; endl; } int ScoreMatrix::GetValue(int x, int y) { //cout &lt;&lt; "GetValue start" &lt;&lt; endl; //return scores[x][y]; return scores[(cols * x) + y]; cout &lt;&lt; "GetValue end" &lt;&lt; endl; } void ScoreMatrix::ReadMatrix(char *mat_file) { cout &lt;&lt; "start reading matrix from file" &lt;&lt; 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&lt;=23; row++) { string line; getline( mfile, line); istringstream iss(line); if (line[0] !='#' &amp;&amp; line[0] != ' ') { int s; iss &gt;&gt; s; for (int i=1; !iss.eof(); i++) { iss &gt;&gt; s; SetValue(i, row, s); } } } } cout &lt;&lt; "end reading matrix from file" &lt;&lt; endl; } </code></pre> <p>Header file included:</p> <pre><code>/* * File: mat_sub.h * Author: mateusz * * Created on 6 luty 2011, 14:44 */ #ifndef MAT_SUB_H #define MAT_SUB_H #include &lt;cstdlib&gt; #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;fstream&gt; #include &lt;vector&gt; #include &lt;algorithm&gt; #include &lt;string.h&gt; 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 */ </code></pre> <p>Before seagfoult function print one of many <code>start reading matrix from file</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload