001    /*
002     * Copyright 2007, 2012 Stephen Fisher and Junhyong Kim, University of
003     * Pennsylvania.
004     *
005     * This file is part of Glo-DB.
006     * 
007     * Glo-DB is free software: you can redistribute it and/or modify it
008     * under the terms of the GNU General Public License as published by
009     * the Free Software Foundation, either version 3 of the License, or
010     * (at your option) any later version.
011     * 
012     * Glo-DB is distributed in the hope that it will be useful, but
013     * WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015     * General Public License for more details.
016     * 
017     * You should have received a copy of the GNU General Public License
018     * along with Glo-DB. If not, see <http://www.gnu.org/licenses/>.
019     *
020     * @(#)GenomeBrowser.java
021     */
022    
023    package edu.upenn.gloDB.io;
024    
025    import edu.upenn.gloDB.*;
026    import java.util.Random;
027    import java.io.BufferedReader;
028    import java.io.DataOutputStream;
029    import java.io.InputStreamReader;
030    import java.net.*;
031    
032    
033    /**
034     * Import/Export Track data from/to Genome Browser. <br><br>
035     *
036     * <A HREF="http://www.genome.ucsc.edu/">http://www.genome.ucsc.edu/</A>
037     *
038     *
039     * @author  Stephen Fisher
040     * @version $Id: GenomeBrowser.java,v 1.1.2.11 2007/03/01 21:17:33 fisher Exp $
041     */
042    
043    public class GenomeBrowser {
044             
045             private final static String SURL = "http://www.genome.ucsc.edu/";
046             // private final static String SURL = "http://192.168.189.10/";
047    
048             private static Random random = new Random();
049    
050        //--------------------------------------------------------------------------
051        // Miscellaneous Methods
052    
053             private static String randomString() { return Long.toString(random.nextLong(), 36); }
054    
055             /**
056              * Posts 'track' to Genome Browser.  The track being viewed must
057              * have a valid source or the Genome Browser won't be properly
058              * oriented.
059              */
060             public static String viewTrack(String id) {
061                      Track track = ObjectHandles.getTrack(id);
062                      if (track == null) {
063                                    GloDBUtils.printError("Not a valid track");
064                                    return "";
065                      }
066    
067                      if (track.numSources() == 0) {
068                                    GloDBUtils.printError("No valid source");
069                                    return "";
070                      }
071                                    
072                      // use first source
073                      String header = "browser position ";
074                      header += track.getSourceSet().toArray()[0] + ":";
075                      header += track.getMin() + "-" + track.getMax() + "\n";
076    
077                      GFFTrack gff = new GFFTrack();
078                      String data = header + gff.format(id);
079    
080                      return post(data);
081             }
082    
083             /*
084             public static void viewGenomeBrowser(String data) {
085                      // da += fb + "\"hgt.customFile\"; filename=\"gbtest.bed\"\r\nContent-Type: text/plain\r\n\r\n"
086                      // da += "browser position chr22:1000-10000\r\nbrowser hide all\r\n"
087                      // da += "track name=pairedReads description=\"Clone Paired Reads\" visibility=2 color=0,128,0 useScore=1\r\n"
088                      // da += "chr22 1000 5000 cloneA 960 + 1000 5000 0 2 567,488, 0,3512\r\n"
089                      // da += "chr22 2000 6000 cloneB 200 - 2000 6000 0 2 433,399, 0,3601\n\r\n"
090    
091                      // data = "browser position chr22:1000-10000"
092                      String html = postToGenomeBrowser(data);
093                      
094                      // view response
095                      new ViewHTML(html, STRING);
096             }
097    
098    browser position chr1:6554929-6594816
099    chr1    DoTS2Gene       exon    6554929 6555181 200     +       .       DG.1
100    chr1    DoTS2Gene       exon    6576588 6576694 200     +       .       DG.2
101    chr1    DoTS2Gene       exon    6589784 6589856 200     +       .       DG.3
102    chr1    DoTS2Gene       exon    6594695 6594816 200     +       .       DG.4
103             */
104    
105             public static String post(String data) {
106                      String html = "";
107                      try {
108                                    // open connection
109                                    URL url = new URL(SURL + "cgi-bin/hgTracks");
110                                    URLConnection connection = url.openConnection();
111                                    connection.setDoOutput(true);
112    
113                                    String boundary = "---------------------------" + randomString();
114    
115                                    // tell server this is a form
116                                    connection.setRequestProperty("Content-Type","multipart/form-data; boundary=" + boundary);
117    
118                                    // the boundary is actually 2 "-" larger than that in the header above
119                                    boundary = "--" + boundary;
120    
121                                    // open output stream, write data, then close stream
122                                    DataOutputStream out = new DataOutputStream(connection.getOutputStream());
123                                    out.writeBytes(boundary + "\r\n");
124                                    out.writeBytes("Content-Disposition: form-data; name=\"hgt.customText\"\r\n\r\n");
125                                    out.writeBytes(data + "\r\n");
126                                    out.writeBytes(boundary + "--\r\n");
127                                    out.flush();
128                                    out.close();
129    
130                                    /*
131                                    String tmp = "";
132                                    tmp += boundary + "\r\n";
133                                    tmp += "Content-Disposition: form-data; name=\"hgt.customText\"\r\n\r\n";
134                                    tmp += data + "\r\n";
135                                    tmp += boundary + "--\r\n";
136                                    System.out.println(SURL + "cgi-bin/hgTracks");
137                                    System.out.println(tmp);
138                                    */
139    
140                                    // get response
141                                    //                              new edu.upenn.gloDB.gui.ViewHTML(connection.getInputStream());
142                                    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
143                                    String inputLine;
144                                    while ((inputLine = in.readLine()) != null) {
145                                             html += inputLine + "\n";
146                                    }
147    
148                                    // the html viewer assumes the file is local and thus
149                                    // doesn't properly display images which are on the web
150                                    // server, so we're hardcoding their locations here
151                                    String replacement = "= \"" + SURL;
152                                    String regExp1 = "=\\s*\"/";
153                                    String regExp2 = "=\\s*\"../";
154                                    html = html.replaceAll(regExp1, replacement);
155                                    html = html.replaceAll(regExp2, replacement);
156    
157                                    // close connection
158                                    in.close();
159                      } catch (Exception e) {
160                                    ;
161                      }
162    
163                      return html;
164             }
165    
166    } // GenomeBrowser.java
167    
168