001 /*
002 * The contents of this file are subject to the Mozilla Public
003 * License Version 1.1 (the "License"); you may not use this file
004 * except in compliance with the License. You may obtain a copy of
005 * the License at http://www.mozilla.org/MPL/
006 *
007 * Software distributed under the License is distributed on an "AS
008 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
009 * implied. See the License for the specific language governing
010 * rights and limitations under the License.
011 *
012 * The Original Code is Knowtator.
013 *
014 * The Initial Developer of the Original Code is University of Colorado.
015 * Copyright (C) 2005 - 2008. All Rights Reserved.
016 *
017 * Knowtator was developed by the Center for Computational Pharmacology
018 * (http://compbio.uchcs.edu) at the University of Colorado Health
019 * Sciences Center School of Medicine with support from the National
020 * Library of Medicine.
021 *
022 * Current information about Knowtator can be obtained at
023 * http://knowtator.sourceforge.net/
024 *
025 * Contributor(s):
026 * Philip V. Ogren <philip@ogren.info> (Original Author)
027 */
028 package edu.uchsc.ccp.knowtator.textsource.generif;
029
030 import java.awt.Component;
031 import java.io.File;
032 import java.io.IOException;
033 import java.util.ArrayList;
034 import java.util.List;
035
036 import javax.swing.JFileChooser;
037 import javax.swing.JOptionPane;
038
039 import edu.stanford.smi.protege.model.Cls;
040 import edu.stanford.smi.protege.model.KnowledgeBase;
041 import edu.stanford.smi.protege.model.Project;
042 import edu.stanford.smi.protege.model.Slot;
043 import edu.stanford.smi.protege.model.ValueType;
044 import edu.stanford.smi.protege.util.CollectionUtilities;
045 import edu.uchsc.ccp.knowtator.textsource.TextSource;
046 import edu.uchsc.ccp.knowtator.textsource.TextSourceCollection;
047 import edu.uchsc.ccp.knowtator.textsource.filelines.FileLinesTextSourceCollection;
048
049 /**
050 * This TextSourceCollection implementation is useful for GeneRIFs stored in in
051 * a single file line-by-line. The format of the file should be:
052 * <code><br>id|gene_name|gene_symbol|text<br></code> Where id is some local
053 * identifier (not provided by entrez) assigned to the geneRIF.
054 */
055
056 public class GeneRIFTextSourceCollection extends FileLinesTextSourceCollection {
057 public static final String DISPLAY_NAME = "GeneRIFs";
058
059 public static final String CLS_NAME = "generifs text source";
060
061 public static final String PROJECT_SETTING_RECENT_FILE = new String("GRIFTextSourceCollection_RECENT_FILE");
062
063 public GeneRIFTextSourceCollection(String fileName) throws IOException {
064 super(fileName);
065 }
066
067 public GeneRIFTextSourceCollection(File file) throws IOException {
068 super(file);
069 }
070
071 protected TextSource createTextSource(String fileLine) {
072 String id = fileLine.substring(0, fileLine.indexOf("|"));
073 int entrezIdStart = id.length() + 1;
074 String entrezId = fileLine.substring(entrezIdStart, fileLine.indexOf("|", entrezIdStart));
075 String[] entrezIDs = entrezId.split(":");
076 int pmidStart = entrezIdStart + entrezId.length() + 1;
077 String pmid = fileLine.substring(pmidStart, fileLine.indexOf("|", pmidStart));
078 int textStart = pmidStart + pmid.length() + 1;
079 String text = fileLine.substring(textStart);
080
081 return new GeneRIFTextSource(id, entrezIDs, pmid, text);
082 }
083
084 private static File getRecentFile(Project project) {
085 String recentFileName = (String) project.getClientInformation(PROJECT_SETTING_RECENT_FILE);
086 if (recentFileName != null) {
087 File recentFile = new File(recentFileName);
088 if (recentFile != null && recentFile.exists() && recentFile.isFile()) {
089 return recentFile;
090 }
091 }
092 return null;
093 }
094
095 public static TextSourceCollection open(Project project, Component parent) {
096 JFileChooser chooser = new JFileChooser();
097 File recentFile = getRecentFile(project);
098 if (recentFile != null) {
099 chooser.setSelectedFile(recentFile);
100 }
101 chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
102 int returnVal = chooser.showOpenDialog(parent);
103 if (returnVal == JFileChooser.APPROVE_OPTION) {
104 File file = chooser.getSelectedFile();
105 try {
106 return new GeneRIFTextSourceCollection(file);
107 } catch (IOException ioe) {
108 ioe.printStackTrace();
109 JOptionPane.showMessageDialog(parent, "Unable to open file as a text source collection", "",
110 JOptionPane.ERROR_MESSAGE);
111 }
112 }
113 return null;
114 }
115
116 public void save(Project project) {
117 project.setClientInformation(PROJECT_SETTING_RECENT_FILE, getFile().getPath());
118 }
119
120 public static TextSourceCollection open(Project project) {
121 File recentFile = getRecentFile(project);
122 if (recentFile != null) {
123 try {
124 TextSourceCollection tsc = new GeneRIFTextSourceCollection(recentFile);
125 return tsc;
126 } catch (IOException ioe) {
127 ioe.printStackTrace();
128 }
129 }
130 return null;
131 }
132
133 public static void createCls(KnowledgeBase kb) {
134
135 if (kb.getCls(CLS_NAME) == null) {
136 Cls textSourceParent = kb.getCls(FileLinesTextSourceCollection.CLS_NAME);
137 List<Cls> parents = new ArrayList<Cls>();
138 parents.add(textSourceParent);
139 Cls grifTextSourceCls = kb.createCls(CLS_NAME, parents);
140 Slot pmidSlot = kb.createSlot("generif_pmid");
141 pmidSlot.setValueType(ValueType.STRING);
142 grifTextSourceCls.addDirectTemplateSlot(pmidSlot);
143
144 Cls entrezGeneCls = kb.getCls("entrez_gene");
145 if (entrezGeneCls == null) {
146 entrezGeneCls = kb.createCls("entrez_gene", CollectionUtilities.createCollection(kb.getRootCls()));
147 Slot entrezAliasSlot = kb.createSlot("entrez_alias");
148 entrezAliasSlot.setAllowsMultipleValues(true);
149 entrezAliasSlot.setValueType(ValueType.STRING);
150 entrezGeneCls.addDirectTemplateSlot(entrezAliasSlot);
151 Slot entrezGeneIDSlot = kb.createSlot("entrez_gene_id");
152 entrezGeneIDSlot.setAllowsMultipleValues(false);
153 entrezGeneIDSlot.setMinimumCardinality(1);
154 entrezGeneIDSlot.setValueType(ValueType.STRING);
155 entrezGeneCls.addDirectTemplateSlot(entrezGeneIDSlot);
156 Slot entrezOfficialNameSlot = kb.createSlot("entrez_official_name");
157 entrezOfficialNameSlot.setAllowsMultipleValues(false);
158 entrezOfficialNameSlot.setValueType(ValueType.STRING);
159 entrezGeneCls.addDirectTemplateSlot(entrezOfficialNameSlot);
160 Slot entrezOfficialSymbolSlot = kb.createSlot("entrez_official_symbol");
161 entrezOfficialSymbolSlot.setAllowsMultipleValues(false);
162 entrezOfficialSymbolSlot.setValueType(ValueType.STRING);
163 entrezGeneCls.addDirectTemplateSlot(entrezOfficialSymbolSlot);
164 Slot entrezSpeciesSlot = kb.createSlot("entrez_species");
165 entrezSpeciesSlot.setAllowsMultipleValues(false);
166 entrezSpeciesSlot.setValueType(ValueType.STRING);
167 entrezGeneCls.addDirectTemplateSlot(entrezSpeciesSlot);
168 }
169 Slot entrezGeneSlot = kb.createSlot("generif_entrez_gene");
170 entrezGeneSlot.setValueType(ValueType.INSTANCE);
171 entrezGeneSlot.setAllowedClses(CollectionUtilities.createCollection(entrezGeneCls));
172 entrezGeneSlot.setAllowsMultipleValues(true);
173 grifTextSourceCls.addDirectTemplateSlot(entrezGeneSlot);
174
175 }
176 }
177 }