View Javadoc

1   package baseCode.bio.geneset;
2   
3   import java.io.FileInputStream;
4   import java.io.IOException;
5   import java.io.InputStream;
6   import java.util.HashSet;
7   import java.util.Iterator;
8   import java.util.Map;
9   import java.util.Set;
10  
11  import org.xml.sax.SAXException;
12  
13  import baseCode.bio.GOEntry;
14  import baseCode.dataStructure.graph.DirectedGraph;
15  import baseCode.dataStructure.graph.DirectedGraphNode;
16  import baseCode.xml.GOParser;
17  
18  /***
19   * Rea data from GO XML file, store in easy-to-use data structure.
20   * <p>
21   * Copyright (c) 2004 Columbia University
22   * 
23   * @author Paul Pavlidis
24   * @author Homin Lee
25   * @version $Id: GONames.java,v 1.3 2004/12/28 22:07:27 pavlidis Exp $
26   */
27  public class GONames {
28  
29     private static Map goNameMap;
30     private Set newGeneSets = new HashSet();
31     private GOParser parser;
32  
33     /***
34      * @param filename <code>String</code> The XML file containing class to name mappings. First column is the class
35      *        id, second is a description that will be used int program output.
36      * @throws IOException
37      * @throws SAXException
38      */
39     public GONames( String filename ) throws SAXException, IOException {
40        if ( filename == null || filename.length() == 0 ) {
41           throw new IllegalArgumentException(
42                 "Invalid filename " + filename + " or no filename was given" );
43        }
44  
45        InputStream i = new FileInputStream( filename );
46        parser = new GOParser( i );
47        goNameMap = parser.getGONameMap();
48     }
49  
50     /***
51      * @param inputStream
52      * @throws IOException
53      * @throws SAXException
54      */
55     public GONames( InputStream inputStream ) throws IOException, SAXException {
56        if ( inputStream == null ) {
57           throw new IOException( "Input stream was null" );
58        }
59  
60        parser = new GOParser( inputStream );
61        goNameMap = parser.getGONameMap();
62     }
63  
64     /***
65      * Get the graph representation of the GO hierarchy. This can be used to support JTree representations.
66      * 
67      * @return
68      */
69     public DirectedGraph getGraph() {
70        return parser.getGraph();
71     }
72  
73     /***
74      * @param id
75      * @return a Set containing the ids of geneSets which are immediately below the selected one in the hierarchy.
76      */
77     public Set getChildren( String id ) {
78        Set returnVal = new HashSet();
79        Set children = ( ( DirectedGraphNode ) getGraph().get( id ) )
80              .getChildNodes();
81        for ( Iterator it = children.iterator(); it.hasNext(); ) {
82           DirectedGraphNode child = ( DirectedGraphNode ) it.next();
83           String childKey = ( ( GOEntry ) child.getItem() ).getId()
84                 .intern();
85           returnVal.add( childKey.intern() );
86        }
87        return returnVal;
88     }
89  
90     /***
91      * @param id
92      * @return a Set containing the ids of geneSets which are immediately above the selected one in the hierarchy.
93      */
94     public Set getParents( String id ) {
95        Set returnVal = new HashSet();
96        Set parents = ( ( DirectedGraphNode ) getGraph().get( id ) )
97              .getParentNodes();
98        for ( Iterator it = parents.iterator(); it.hasNext(); ) {
99           DirectedGraphNode parent = ( DirectedGraphNode ) it.next();
100          String parentKey = ( ( GOEntry ) parent.getItem() ).getId()
101                .intern();
102          returnVal.add( parentKey.intern() );
103       }
104       return returnVal;
105    }
106 
107    /***
108     * Get the Map representation of the GO id - name associations.
109     * 
110     * @return Map
111     */
112    public Map getMap() {
113       return goNameMap;
114    }
115 
116    /***
117     * @param go_ID String
118     * @return String
119     */
120    public String getNameForId( String go_ID ) {
121 
122       if ( !goNameMap.containsKey( go_ID ) ) {
123          return "<no description available>";
124       }
125 
126       return ( ( String ) ( goNameMap.get( go_ID ) ) ).intern();
127    }
128    
129    
130    /***
131     * Get the aspect (molecular_function etc) for an id.
132     * @param go_ID
133     * @return
134     */
135    public String getAspectForId( String go_ID ) {
136       if ( !goNameMap.containsKey( go_ID ) ) {
137          return "<no aspect available>";
138       }
139       return ((GOEntry)getGraph().getNodeContents(go_ID)).getAspect();
140    }
141    
142 
143    /***
144     * @param id String
145     * @param name String
146     * @todo this should modify the tree representation too.
147     */
148    public void addClass( String id, String name ) {
149       goNameMap.put( id, name );
150       newGeneSets.add( id );
151    }
152 
153    /***
154     * @param id String
155     * @param name String
156     * @todo this should modify the tree representation too.
157     */
158    public void modifyClass( String id, String name ) {
159       goNameMap.put( id, name );
160       newGeneSets.add( id );
161    }
162 
163    /***
164     * Check if a gene set is already defined.
165     * 
166     * @param id
167     * @return
168     */
169    public boolean newSet( String id ) {
170       return newGeneSets.contains( id );
171    }
172 
173    /***
174     * Return the Set of all new gene sets (ones which were added after loading the file)
175     * 
176     * @return
177     */
178    public Set getNewGeneSets() {
179       return newGeneSets;
180    }
181 
182 }