Browse code

Updating pwiz to 3_0_21263

Steffen Neumann authored on 23/09/2021 12:34:25
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,103 @@
1
+//
2
+// $Id$
3
+//
4
+//
5
+// Original author: Austin Keller <atkeller .@. uw.edu>
6
+//
7
+// Licensed under the Apache License, Version 2.0 (the "License"); 
8
+// you may not use this file except in compliance with the License. 
9
+// You may obtain a copy of the License at 
10
+//
11
+// http://www.apache.org/licenses/LICENSE-2.0
12
+//
13
+// Unless required by applicable law or agreed to in writing, software 
14
+// distributed under the License is distributed on an "AS IS" BASIS, 
15
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
16
+// See the License for the specific language governing permissions and 
17
+// limitations under the License.
18
+//
19
+
20
+#include "MatrixIO.hpp"
21
+
22
+namespace pwiz
23
+{
24
+namespace analysis
25
+{
26
+    using namespace std;
27
+    using namespace DemuxTypes;
28
+    using namespace Eigen;
29
+
30
+    void MatrixIO::GetWriteStream(ofstream& out, const string& filename)
31
+    {
32
+        out.open(filename, ios::out | ios::binary | ios::trunc);
33
+    }
34
+
35
+    void MatrixIO::WriteBinary(ofstream& out, boost::shared_ptr<MatrixType> matrix)
36
+    {
37
+        MatrixType::Index rows = matrix->rows(), cols = matrix->cols();
38
+        out.write(reinterpret_cast<char*>(&rows), sizeof(MatrixType::Index));
39
+        out.write(reinterpret_cast<char*>(&cols), sizeof(MatrixType::Index));
40
+        // Store as row-major
41
+        if (matrix->IsRowMajor)
42
+            out.write(reinterpret_cast<char*>(matrix->data()), rows * cols * sizeof(MatrixType::Scalar));
43
+        else
44
+        {
45
+            for (int row = 0; row < rows; ++row)
46
+            {
47
+                for (int col = 0; col < cols; ++col)
48
+                {
49
+                    out.write(reinterpret_cast<char*>(&(*matrix)(row, col)), sizeof(MatrixType::Scalar));
50
+                }
51
+            }
52
+        }
53
+    }
54
+
55
+    void MatrixIO::WriteBinary(const string& filename, boost::shared_ptr<MatrixType> matrix)
56
+    {
57
+        ofstream out;
58
+        GetWriteStream(out, filename);
59
+        if (!out.is_open())
60
+        {
61
+            throw runtime_error("Could not open file");
62
+        }
63
+        WriteBinary(out, matrix);
64
+        out.flush();
65
+        out.close();
66
+    }
67
+
68
+    void MatrixIO::GetReadStream(ifstream& in, const string& filename)
69
+    {
70
+        in.open(filename, ios::in | ios::binary);
71
+    }
72
+
73
+    void MatrixIO::ReadBinary(ifstream& in, boost::shared_ptr<MatrixType> matrix)
74
+    {
75
+        MatrixType::Index rows = 0, cols = 0;
76
+        in.read(reinterpret_cast<char*>(&rows), sizeof(MatrixType::Index));
77
+        in.read(reinterpret_cast<char*>(&cols), sizeof(MatrixType::Index));
78
+        matrix->resize(rows, cols);
79
+        // File should be stored as row major
80
+        if (matrix->IsRowMajor)
81
+            in.read(reinterpret_cast<char *>(matrix->data()), rows * cols * sizeof(MatrixType::Scalar));
82
+        else
83
+        {
84
+            vector<double> flattened(rows * cols);
85
+            in.read(reinterpret_cast<char *>(&flattened[0]), rows * cols * sizeof(MatrixType::Scalar));
86
+            Map<Matrix<MatrixType::Scalar, Dynamic, Dynamic, RowMajor>> readMatrix(&flattened[0], rows, cols);
87
+            *matrix = readMatrix;
88
+        }
89
+    }
90
+
91
+    void MatrixIO::ReadBinary(const string& filename, boost::shared_ptr<MatrixType> matrix)
92
+    {
93
+        ifstream in;
94
+        GetReadStream(in, filename);
95
+        if (!in.is_open())
96
+        {
97
+            throw runtime_error("Could not open file");
98
+        }
99
+        ReadBinary(in, matrix);
100
+        in.close();
101
+    }
102
+} // namespace analysis
103
+} // namespace pwiz
0 104
\ No newline at end of file