root/trunk/plugins/zscore/org/pathvisio/plugins/statistics/IsDataInPathways.java @ 2979

Revision 2979, 4.6 KB (checked in by martijn, 9 months ago)

For the sake of uniformity, removed all trailing whitespace in Java code

Line 
1// PathVisio,
2// a tool for data visualization and analysis using Biological Pathways
3// Copyright 2006-2009 BiGCaT Bioinformatics
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
17package org.pathvisio.plugins.statistics;
18
19import java.io.File;
20import java.io.FileNotFoundException;
21import java.io.FileOutputStream;
22import java.io.PrintStream;
23import java.util.ArrayList;
24import java.util.HashMap;
25import java.util.List;
26import java.util.Map;
27import java.util.regex.Matcher;
28import java.util.regex.Pattern;
29
30import org.bridgedb.IDMapperException;
31import org.bridgedb.Xref;
32import org.bridgedb.rdb.DataDerby;
33import org.bridgedb.rdb.SimpleGdb;
34import org.bridgedb.rdb.SimpleGdbFactory;
35import org.pathvisio.data.XrefWithSymbol;
36import org.pathvisio.debug.Logger;
37import org.pathvisio.gex.GexManager;
38import org.pathvisio.gex.ReporterData;
39import org.pathvisio.gex.SimpleGex;
40import org.pathvisio.preferences.PreferenceManager;
41import org.pathvisio.util.FileUtils;
42import org.pathvisio.util.PathwayParser;
43import org.pathvisio.util.PathwayParser.ParseException;
44import org.pathvisio.util.Utils;
45import org.xml.sax.SAXException;
46import org.xml.sax.XMLReader;
47import org.xml.sax.helpers.XMLReaderFactory;
48
49/**
50 * Checks which genes from a dataset are not mapped in any pathway
51 * Useful for targeted development of new pathways.
52 *
53 * TODO: make a plug-in out of this.
54 */
55public class IsDataInPathways
56{
57
58//      static File fGex = new File ("/home/martijn/uni-wrk/datasets/Starvation/Intestine/only_protemics_genmapp_format.pgex");
59//      static File fGdb = new File ("/home/martijn/PathVisio-Data/gene databases/Mm_Derby_20080102.pgdb");
60//      static File pwDir = new File ("/home/martijn/wikipathways/Mus_musculus");
61
62        static File fGex = new File ("/media/KINGSTON/muscle_t12_vs_t0_PathVisio.pgex");
63        static File fGdb = new File ("/home/martijn/PathVisio-Data/gene databases/Mm_Derby_20080102.pgdb");
64        static File pwDir = new File ("/home/martijn/wikipathways/Mus_musculus");
65
66        static File outFile = new File ("/home/martijn/Desktop/isdatainpahtways.txt");
67
68        public static void main(String[] args) throws IDMapperException, ParseException, FileNotFoundException
69        {
70                PreferenceManager.init();
71                GexManager gexManager = new GexManager();
72                gexManager.setCurrentGex("" + fGex, false);
73
74                XMLReader xmlReader;
75
76                try
77                {
78                        xmlReader = XMLReaderFactory.createXMLReader();
79                }
80                catch (SAXException e)
81                {
82                        Logger.log.error("Problem while searching pathways", e);
83                        return;
84                }
85
86                // read all rows of gex;
87                SimpleGex gex = gexManager.getCurrentGex();
88
89                Map<Xref, Xref> dataRefs = new HashMap<Xref, Xref>();
90                Map<Xref, List<String>> counts = new HashMap<Xref, List<String>>();
91
92                SimpleGdb gdb = SimpleGdbFactory.createInstance("" + fGdb, new DataDerby(), 0);
93
94                for (int i = 0; i < gex.getNrRow(); ++i)
95                {
96                        ReporterData data = gex.getRow(i);
97                        Xref src = data.getXref();
98                        counts.put (src, new ArrayList<String>());
99                        for (Xref dest : gdb.mapID(data.getXref()))
100                        {
101                                dataRefs.put (dest, src);
102                        }
103                        dataRefs.put (src, src);
104                }
105
106                for (File f : FileUtils.getFiles(pwDir, "gpml", false))
107                {
108                        PathwayParser pp = new PathwayParser (f, xmlReader);
109
110                        for (XrefWithSymbol ref : pp.getGenes())
111                        {
112                                Xref ref2 = new Xref (ref.getId(), ref.getDataSource());
113                                if (dataRefs.containsKey(ref2))
114                                {
115                                        counts.get(dataRefs.get(ref2)).add(pp.getName());
116                                }
117                        }
118                }
119
120                PrintStream out = new PrintStream(new FileOutputStream(outFile));
121                for (int i = 0; i < gex.getNrRow(); ++i)
122                {
123                        ReporterData data = gex.getRow(i);
124                        Xref ref = data.getXref();
125                        String bpText = Utils.oneOf (gdb.getAttributes(ref, "Backpage"));
126                        String desc = "";
127                        if (bpText != null)
128                        {
129                                Pattern pat = Pattern.compile("<TH>Description:<TH>(.*)<TR>");
130                                Matcher mat = pat.matcher (bpText);
131                                if (mat.find())
132                                {
133                                        desc = mat.group(1);
134                                }
135                        }
136                        List<String> pwyNames = counts.get(ref);
137                        out.print (i + "\t" +
138                                        ref.getId() + "\t" +
139                                        ref.getDataSource().getSystemCode()  + "\t" +
140                                        desc + "\t" +
141                                        pwyNames.size() + "\t");
142
143                        boolean first = true;
144                        for (String name : pwyNames)
145                        {
146                                if (!first)
147                                {
148                                        out.print (" \\\\\\ ");
149                                }
150                                first = false;
151                                out.print (name);
152                        }
153                        out.println();
154                }
155                out.close();
156        }
157
158}
Note: See TracBrowser for help on using the browser.