1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package ubic.basecode.io.reader;
20
21 import java.io.BufferedReader;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.util.*;
26
27 import ubic.basecode.dataStructure.matrix.DoubleMatrix;
28 import ubic.basecode.dataStructure.matrix.SparseDoubleMatrix;
29
30
31
32
33
34 public class SparseDoubleMatrixReader extends DoubleMatrixReader {
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 @Override
54 public DoubleMatrix<String, String> read( InputStream stream ) throws IOException {
55
56 Set<String> itemNames = new HashSet<String>();
57 Map<String, Collection<IndexScoreDyad>> rows = new HashMap<String, Collection<IndexScoreDyad>>();
58
59 BufferedReader dis = new BufferedReader( new InputStreamReader( stream ) );
60
61 String row;
62 int index = 0;
63 Map<String, Integer> nameIndexMap = new HashMap<String, Integer>();
64 while ( ( row = dis.readLine() ) != null ) {
65 StringTokenizer st = new StringTokenizer( row, " \t", false );
66
67 String itemA = "";
68
69 if ( st.hasMoreTokens() ) {
70 itemA = st.nextToken();
71
72 if ( !itemNames.contains( itemA ) ) {
73 rows.put( itemA, new HashSet<IndexScoreDyad>() );
74 itemNames.add( itemA );
75 nameIndexMap.put( itemA, index );
76 index++;
77 }
78 } else {
79
80 }
81
82 String itemB = "";
83 if ( st.hasMoreTokens() ) {
84 itemB = st.nextToken();
85 if ( !itemNames.contains( itemB ) ) {
86 rows.put( itemB, new HashSet<IndexScoreDyad>() );
87 itemNames.add( itemB );
88 nameIndexMap.put( itemB, index );
89 index++;
90 }
91 } else {
92
93 }
94
95 double weight;
96 if ( st.hasMoreTokens() ) {
97 weight = Double.parseDouble( st.nextToken() );
98 } else {
99 weight = 1.0;
100 }
101
102 rows.get( itemA ).add( new IndexScoreDyad( nameIndexMap.get( itemB ).intValue(), weight ) );
103 rows.get( itemB ).add( new IndexScoreDyad( nameIndexMap.get( itemA ).intValue(), weight ) );
104 }
105
106 SparseDoubleMatrix<String, String> matrix = new SparseDoubleMatrix<String, String>( itemNames.size(),
107 itemNames.size() );
108
109 List<String> itemVec = new Vector<String>( itemNames );
110 Collections.sort( itemVec );
111
112 matrix.setColumnNames( itemVec );
113 matrix.setRowNames( itemVec );
114 for ( Object element2 : itemNames ) {
115 String itemA = ( String ) element2;
116 int rowIndex = matrix.getRowIndexByName( itemA );
117 Collection<IndexScoreDyad> arow = rows.get( itemA );
118 for ( Iterator<IndexScoreDyad> iterator = arow.iterator(); iterator.hasNext(); ) {
119 IndexScoreDyad element = iterator.next();
120 int ind = element.getKey();
121 double weight = element.getValue();
122
123 matrix.set( rowIndex, ind, weight );
124 matrix.set( ind, rowIndex, weight );
125 }
126
127 }
128
129 dis.close();
130 return matrix;
131 }
132
133 @Override
134 public DoubleMatrix<String, String> read( InputStream stream, Collection<String> wantedRowNames ) {
135 throw new UnsupportedOperationException();
136 }
137
138 @Override
139 public DoubleMatrix<String, String> read( InputStream stream, Collection<String> wantedRowNames,
140 boolean createEmptyRows, int skipColumns, int maxRows ) {
141 throw new UnsupportedOperationException();
142 }
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167 @SuppressWarnings("resource")
168 public DoubleMatrix<String, String> readJW( InputStream stream ) throws IOException {
169
170 BufferedReader dis = new BufferedReader( new InputStreamReader( stream ) );
171
172 Scanner ff = new Scanner( dis ).useLocale( Locale.ENGLISH );
173
174 int index = 0;
175 int amount = 0;
176 double eval = 0;
177
178 int dim = Integer.parseInt( dis.readLine() );
179 SparseDoubleMatrix<String, String> returnVal = new SparseDoubleMatrix<String, String>( dim, dim );
180
181 for ( int k = 1; k <= dim; k++ ) {
182
183 returnVal.setColumnName( new Integer( k ).toString(), k - 1 );
184 returnVal.setRowName( new Integer( k ).toString(), k - 1 );
185
186 index = ff.nextInt();
187
188 amount = ff.nextInt();
189
190 if ( index % 500 == 0 ) {
191 log.debug( String.format( "loading %2.1f%% complete (%dth entry)... \n", 100.0 * index / dim, index ) );
192 }
193
194 int[] rowind = new int[amount];
195 for ( int i = 0; i < amount; i++ ) {
196
197 index = ff.nextInt();
198 int ind = index;
199
200 if ( ind > dim || ind < 1 ) {
201 ff.close();
202 throw new IllegalStateException( "Illegal value " + ind + " found in index list for item " + k );
203 }
204 rowind[i] = ind;
205 }
206
207 for ( int i = 0; i < amount; i++ ) {
208 eval = ff.nextDouble();
209 returnVal.set( k - 1, rowind[i] - 1, eval );
210 }
211
212 }
213 ff.close();
214 return returnVal;
215 }
216
217 }