Debian Codegraph @

display/src/amap/AmapReader.java

raw
1
2 //============================================================================
3 //
4 // file: AmapReader.java
5 //
6 // Copyright (c) 2007, Michael E. Smoot
7 //
8 // This program is free software; you can redistribute it and/or modify it
9 // under the terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 2 of the License, or (at your
11 // option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 // for more details.
17 //
18 // You should have received a copy of the GNU General Public License along
19 // with this program; if not, write to the Free Software Foundation, Inc.,
20 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 //
22 //============================================================================
23
24 package amap;
25
26 import java.util.regex.Pattern;
27 import java.util.regex.Matcher;
28 import java.util.*;
29 import java.io.*;
30
31 public class AmapReader {
32
33 private Map<String,String> nameSeqMap;
34 private Map<String,String> nameColorMap;
35 List<Alignment> alignments;
36 List<String> keys;
37 InputStream is;
38 boolean firstTime;
39
40 double pWeight;
41
42 public AmapReader(InputStream is) {
43 this.is = is;
44 alignments = new ArrayList<Alignment>();
45 keys = new ArrayList<String>();
46 reset();
47 read();
48 }
49
50 public List<Alignment> getAlignments() {
51 return alignments;
52 }
53
54 private void read() {
55 Pattern pw = Pattern.compile("^Weight\\s+([+-]?(?=\\d|\\.\\d)\\d*(\\.\\d*)?([Ee]([+-]?\\d+))?)$");
56 Matcher pwm = pw.matcher("");
57
58 Pattern s = Pattern.compile("^>(\\S+)\\s+(\\S+)$");
59 Matcher sm = s.matcher("");
60
61 Pattern c = Pattern.compile("^@(\\S+)\\s+(\\S+)$");
62 Matcher cm = c.matcher("");
63
64 firstTime = true;
65
66 try {
67 BufferedReader br = new BufferedReader(new InputStreamReader(is));
68
69 String line = null;
70 while ( (line = br.readLine()) != null ) {
71 pwm.reset(line);
72 if ( pwm.matches() ) {
73 saveAlignment();
74 pWeight = new Double( pwm.group(1) ).doubleValue();
75 continue;
76 }
77
78 sm.reset(line);
79 if ( sm.matches() ) {
80 String key = sm.group(1);
81 String val = sm.group(2);
82 if ( nameSeqMap.containsKey( key ) )
83 val = nameSeqMap.get(key) + val;
84 nameSeqMap.put(key,val);
85 if ( firstTime )
86 keys.add(key);
87 continue;
88 }
89
90 cm.reset(line);
91 if ( cm.matches() ) {
92 String key = cm.group(1);
93 String val = cm.group(2);
94 if ( nameColorMap.containsKey( key ) )
95 val = nameColorMap.get(key) + val;
96 nameColorMap.put(key,val);
97 continue;
98 }
99 }
100 saveAlignment();
101 } catch (IOException ioe) {
102 ioe.printStackTrace();
103 }
104 }
105
106 private void saveAlignment() {
107 if ( pWeight > 0 ) {
108 Alignment a = new Alignment( keys, nameSeqMap, nameColorMap, pWeight );
109 alignments.add(a);
110 reset();
111 }
112 }
113
114 private void reset() {
115 nameSeqMap = new HashMap<String,String>();
116 nameColorMap = new HashMap<String,String>();
117 pWeight = -1;
118 firstTime = false;
119 }
120 }
121
122