1 package jspecview.common;
2 
3 //import jspecview.util.Logger;
4 
5 import javajs.util.PT;
6 
7 public class PeakInfo {
8   public final static PeakInfo nullPeakInfo = new PeakInfo();
9 
10   private double xMin, xMax, yMin, yMax;
11   private int px0, px1;
12   private String stringInfo;
13   private String type;
14   private String type2;
15   private String index;
16   private String file;
17   private String filePathForwardSlash;
18 
19   private String title;
20   private String model;
21   private String atoms;
22   private String id;
23   public Spectrum spectrum;
24 
25   private String _match;
26 
27 	private String atomKey;
28 
29 
PeakInfo()30   public PeakInfo() {
31   }
32 
PeakInfo(String s)33   public PeakInfo(String s) {
34     stringInfo = s;
35     type = PT.getQuotedAttribute(s, "type");
36     if (type == null)
37       type = "";
38     type = type.toUpperCase();
39     int pt = type.indexOf('/');
40     type2 = (pt < 0 ? "" : fixType(type.substring(type.indexOf('/') + 1)));
41     if (pt >= 0)
42       type = fixType(type.substring(0, pt)) + "/" + type2;
43     else
44       type = fixType(type);
45     id = PT.getQuotedAttribute(s, "id");
46     index = PT.getQuotedAttribute(s, "index");
47     file = PT.getQuotedAttribute(s, "file");
48     System.out.println("pi file=" + file);
49     filePathForwardSlash = (file == null ? null : file.replace('\\','/'));
50 
51     model = PT.getQuotedAttribute(s, "model");
52     boolean isBaseModel = s.contains("baseModel=\"\"");
53     if (!isBaseModel)
54       atoms = PT.getQuotedAttribute(s, "atoms");
55     atomKey = "," + atoms + ",";
56     title = PT.getQuotedAttribute(s, "title");
57     _match = PT.getQuotedAttribute(s, "_match"); // PEAK command creates this
58     xMax = PT.parseFloat(PT.getQuotedAttribute(s, "xMax"));
59     xMin = PT.parseFloat(PT.getQuotedAttribute(s, "xMin"));
60     yMax = PT.parseFloat(PT.getQuotedAttribute(s, "yMax"));
61     yMin = PT.parseFloat(PT.getQuotedAttribute(s, "yMin"));
62   }
63 
isClearAll()64   public boolean isClearAll() {
65     return (spectrum == null);
66   }
67 
getType()68   public String getType() {
69     return type;
70   }
71 
getAtoms()72   public String getAtoms() {
73     return atoms;
74   }
75 
getXMax()76   public double getXMax() {
77     return xMax;
78   }
79 
getXMin()80   public double getXMin() {
81     return xMin;
82   }
83 
getYMin()84   public double getYMin() {
85     return yMin;
86   }
87 
getYMax()88   public double getYMax() {
89     return yMax;
90   }
91 
getX()92   public double getX() {
93     return (xMax + xMin) / 2;
94   }
95 
getMatch()96   public String getMatch() {
97     return _match;
98   }
99 
fixType(String type)100   private static String fixType(String type) {
101     return (type.equals("HNMR") ? "1HNMR" : type.equals("CNMR") ? "13CNMR"
102         : type);
103   }
104 
105   @Override
toString()106   public String toString() {
107     return stringInfo;
108   }
109 
getIndex()110   public String getIndex() {
111     return index;
112   }
113 
getTitle()114   public String getTitle() {
115     return title;
116   }
117 
checkFileIndex(String filePath, String sIndex, String sAtomKey)118 	public boolean checkFileIndex(String filePath, String sIndex, String sAtomKey) {
119 		return (sAtomKey != null ? atomKey.indexOf(sAtomKey) >= 0
120 				: sIndex.equals(index) && (filePath.equals(file) || filePath
121 				.equals(filePathForwardSlash)));
122 	}
123 
124   /**
125    * type checks true for MS in GC/MS; reverse of checkType
126    *
127    * @param filePath
128    * @param type
129    * @param model
130    * @return true for MS in GC/MS; reverse of checkType
131    */
checkFileTypeModel(String filePath, String type, String model)132 	public boolean checkFileTypeModel(String filePath,
133 			String type, String model) {
134 		return filePath.equals(file) && checkModel(model)
135 		  && this.type.endsWith(type);
136 	}
137 
checkTypeModel(String type, String model)138   public boolean checkTypeModel(String type, String model) {
139     return checkType(type) //   GC/MS matches MS
140         && checkModel(model);
141   }
142 
checkModel(String model)143   private boolean checkModel(String model) {
144     return (model != null && model.equals(this.model));
145   }
146 
checkType(String type)147   private boolean checkType(String type) {
148     return (type.endsWith(this.type));
149   }
150 
checkTypeMatch(PeakInfo pi)151   public boolean checkTypeMatch(PeakInfo pi) {
152     return (checkType(pi.type)
153         && (checkId(pi._match) || checkModel(pi._match) || title.toUpperCase().indexOf(pi._match) >= 0));
154   }
155 
checkId(String match)156 	private boolean checkId(String match) {
157 		if (match == null)
158 			return false;
159 		return (id != null && match.toUpperCase().startsWith("ID=") && match.substring(3).equals(id)
160 						|| (match = match.toUpperCase()).startsWith("INDEX=") && match.equals("INDEX=" + index)
161 						|| match.startsWith("#=") && match.equals("#=" + index)
162 		);
163 	}
164 
getModel()165 	public String getModel() {
166     return model;
167   }
168 
getFilePath()169   public String getFilePath() {
170     return file;
171   }
172 
173   /**
174    * a spectrum which, when loaded, should fire a message to load first peak --
175    * GC for now
176    *
177    * @return can autoselect on loading
178    */
autoSelectOnLoad()179   public boolean autoSelectOnLoad() {
180     return (type.startsWith("GC"));
181   }
182 
setPixelRange(int x0, int x1)183   public void setPixelRange(int x0, int x1) {
184   	px0 = x0;
185   	px1 = x1;
186   }
187 
checkRange(int xPixel, double xVal)188   public double checkRange(int xPixel, double xVal) {
189     if (xPixel != Integer.MAX_VALUE ? (px0 <= xPixel && px1 >= xPixel) :
190     	xVal >= xMin && xVal <= xMax) {
191     	return Math.abs(xVal - getX());
192     }
193     return 1e100;
194   }
195 
getXPixel()196 	public int getXPixel() {
197 		return (px0 + px1) / 2;
198 	}
199 
200 }
201