File: NCBI.java

package info (click to toggle)
rockhopper 2.0.3%2Bdfsg2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 33,136 kB
  • sloc: java: 10,831; sh: 31; xml: 29; makefile: 14
file content (283 lines) | stat: -rw-r----- 10,429 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
 * Copyright 2013 Brian Tjaden
 *
 * This file is part of Rockhopper.
 *
 * Rockhopper is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * Rockhopper is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * (in the file gpl.txt) along with Rockhopper.  
 * If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Collections;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
import java.net.URL;
import java.net.URLConnection;
import java.io.IOException;
import java.io.File;
import java.io.PrintWriter;

/**
 * Maintains a list of bacterial genomes and their annotations available from NCBI.
 */
public class NCBI {

    /*****************************************
     **********   CLASS VARIABLES   **********
     *****************************************/

    public static int HASH_SIZE = 3;
    private static String fileName = "replicons.txt";
    private static String Genome_url = "http://cs.wellesley.edu/~btjaden/genomes/";
    private static String Rockhopper_url = "http://cs.wellesley.edu/~btjaden/Rockhopper/";
    public static String genome_DIR = Rockhopper.output_DIR + "genomes/";



    /********************************************
     **********   INSTANCE VARIABLES   **********
     ********************************************/

    private boolean Genome_valid;  // We are able to establish a connection to the server.
    private boolean Rockhopper_valid;  // We are able to establish a connection to the server.
    private ArrayList<String> repliconList;
    private HashMap<String,HashMap<Integer,Boolean>> repliconDictionary;



    /**************************************
     **********   CONSTRUCTORS   **********
     **************************************/

    public NCBI() {
	Rockhopper_valid = establishServerConnection(Rockhopper_url + fileName);
	repliconList = readInRepliconList();
	repliconDictionary = createRepliconDictionary(repliconList);
    }



    /*************************************************
     **********   PUBLIC INSTANCE METHODS   **********
     *************************************************/

    /**
     * Returns true if a connection to the Genome server has been successfully established.
     * Returns false otherwise.
     */
    public boolean isValid_Genome() {
	return this.Genome_valid;
    }

    /**
     * Returns true if a connection to the NCBI server has been successfully established.
     * Returns false otherwise.
     */
    public boolean isValid_Rockhopper() {
	return this.Rockhopper_valid;
    }

    /**
     * Return a list of all replicon names that contain
     * the substring s.
     */
    public ArrayList<String> getRepliconNames(String s) {
	s = s.toLowerCase();
	ArrayList<String> reps = new ArrayList<String>();
	if (s.length() < HASH_SIZE) return reps;
	String seed = s.substring(0, HASH_SIZE);
	if (!repliconDictionary.containsKey(seed)) return reps;
	HashMap<Integer,Boolean> list = repliconDictionary.get(seed);
	for (Integer i : list.keySet()) {
	    String repliconName = repliconList.get(i.intValue()).split("\t")[0];
	    if (repliconName.toLowerCase().indexOf(s) >= 0) reps.add(repliconName);
	}
	Collections.sort(reps);
	return reps;
    }

    /**
     * Return the name of a FASTA file on the Genome server corresponding
     * to the given replicon name.
     */
    public String getRepliconFileName(String repliconName) {
	repliconName = repliconName.toLowerCase();
	if (repliconName.length() < HASH_SIZE) return "";
	String seed = repliconName.substring(0, HASH_SIZE);
	if (!repliconDictionary.containsKey(seed)) return "";
	HashMap<Integer,Boolean> list = repliconDictionary.get(seed);
	for (Integer i : list.keySet()) {
	    String[] parse_repliconInfo = repliconList.get(i.intValue()).split("\t");
	    if (repliconName.equalsIgnoreCase(parse_repliconInfo[0])) return parse_repliconInfo[1];
	}
	return "";
    }

    /**
     * Ensures the appropriate genomes files (*.fna, *.ptt, *.rnt) are
     * available locally for the specified replicon. If not, the
     * files are copied from the server and created locally.
     * Returns true if successful, false otherwise (in which
     * case the user must specifify the files locally).
     */
    public boolean prepGenomeFiles(String repliconName) {
	boolean successful = true;
	String serverFileName = getRepliconFileName(repliconName);
	if (serverFileName.length() == 0) return false;
	int slashIndex = serverFileName.lastIndexOf("/");
	int periodIndex = serverFileName.lastIndexOf(".");
	String baseFileName = serverFileName.substring(slashIndex+1, periodIndex);
	String replicon_DIR = getRepliconDir(repliconName);
	File resultsDIR = new File(Rockhopper.output_DIR);
	if (!resultsDIR.exists()) resultsDIR.mkdir();
	File genDIR = new File(genome_DIR);
	if (!genDIR.exists()) genDIR.mkdir();
	File repliconDIR = new File(genome_DIR + replicon_DIR);
	if (!repliconDIR.exists()) repliconDIR.mkdir();
	File genomeFile = new File(genome_DIR + replicon_DIR + baseFileName + ".fna");
	if (!genomeFile.exists()) successful = downloadFile(serverFileName, genome_DIR + replicon_DIR + baseFileName + ".fna");
	File geneFile = new File(genome_DIR + replicon_DIR + baseFileName + ".ptt");
	if (!geneFile.exists()) downloadFile(serverFileName.substring(0, serverFileName.length()-4) + ".ptt", genome_DIR + replicon_DIR + baseFileName + ".ptt");
	File rnaFile = new File(genome_DIR + replicon_DIR + baseFileName + ".rnt");
	if (!rnaFile.exists()) downloadFile(serverFileName.substring(0, serverFileName.length()-4) + ".rnt", genome_DIR + replicon_DIR + baseFileName + ".rnt");
	return successful;
    }



    /**************************************************
     **********   PRIVATE INSTANCE METHODS   **********
     **************************************************/

    /**
     * Returns true if we can connect to the servers, false otherwise.
     */
    private boolean establishServerConnection(String url) {
	try {
	    Scanner reader = new Scanner(new URL(url).openStream());
	    return true;
	} catch (IOException e) {
	    return false;
	}
    }

    /**
     * Read in the list of replicons either from a local file or from
     * the Rockhopper server. If there is no local file or its file
     * size differs from that of the server file, then a new local
     * file is created. Returns an empty list if neither a local
     * file nor the server file could be found/accessed.
     */
    private ArrayList<String> readInRepliconList() {
	ArrayList<String> repliconList = new ArrayList<String>();

	try {
	    // Local file
	    File resultsDIR = new File(Rockhopper.output_DIR);
	    if (!resultsDIR.exists()) resultsDIR.mkdir();
	    File genDIR = new File(genome_DIR);
	    if (!genDIR.exists()) genDIR.mkdir();
	    File repFile = new File(genome_DIR + fileName);
	    long fileLength = 0;
	    if (repFile.exists()) fileLength = repFile.length();

	    // Server file
	    long serverLength = 0;
	    if (isValid_Rockhopper())
		serverLength = new URL(Rockhopper_url + fileName).openConnection().getContentLength();

	    // Neither local file nor server file is available.
	    if (!repFile.exists() && !isValid_Rockhopper()) return repliconList;

	    // Use appropriate file (local or server) to read in replicon information.
	    Scanner reader;
	    PrintWriter writer = null;
	    if (serverLength == fileLength) reader = new Scanner(repFile);
	    else {  // Read from server rather than local file. Create new local file.
		reader = new Scanner(new URL(Rockhopper_url + fileName).openStream());
		writer = new PrintWriter(repFile);
	    }
	    while (reader.hasNextLine()) {
		String repliconInfo = reader.nextLine();
		repliconList.add(repliconInfo);
		if (serverLength != fileLength) writer.println(repliconInfo);
	    }
	    reader.close();
	    if (serverLength != fileLength) writer.close();
	} catch (IOException e) {
	    Rockhopper.output("Could not process file " + genome_DIR + fileName + " and file " + Rockhopper_url + fileName + "\n");
	}
	return repliconList;
    }

    /**
     * Create dictionary of replicon tokens. Values are indices in ArrayList.
     */
    private HashMap<String,HashMap<Integer,Boolean>> createRepliconDictionary(ArrayList<String> repliconList) {
	HashMap<String,HashMap<Integer,Boolean>> repliconDictionary = new HashMap<String,HashMap<Integer,Boolean>>();
	for (int i=0; i<repliconList.size(); i++) {
	    String name = repliconList.get(i).split("\t")[0].toLowerCase();
	    for (int j=0; j<name.length()-HASH_SIZE+1; j++) {
		String key = name.substring(j, j+HASH_SIZE);
		if (!repliconDictionary.containsKey(key)) repliconDictionary.put(key, new HashMap<Integer,Boolean>());
		repliconDictionary.get(key).put(i, true);
	    }
	}
	return repliconDictionary;
    }

    /**
     * Retrieves a file from a server and stores it locally.
     * Returns true on success, false otherwise.
     */
    private boolean downloadFile(String serverFileName, String localFileName) {
	try {
	    //System.setProperty("java.net.preferIPv4Stack", "true");  // Windows firewall bug
	    Scanner reader = new Scanner(new URL(serverFileName).openStream());
	    PrintWriter writer = new PrintWriter(new File(localFileName));
	    while (reader.hasNextLine()) writer.println(reader.nextLine());
	    reader.close();
	    writer.close();
	    return true;
	} catch (IOException e) {
	    return false;
	}
    }



    /**********************************************
     **********   PUBLIC CLASS METHODS   **********
     **********************************************/

    public static String getRepliconDir(String repliconName) {
	for (int i=0; i<repliconName.length(); i++) {
	    char ch = repliconName.charAt(i);
	    if (!Character.isLetterOrDigit(ch)) repliconName = repliconName.replace(ch, '_');
	}
	return repliconName + "/";
    }



    /*************************************
     **********   MAIN METHOD   **********
     *************************************/

    public static void main(String[] args) {

    }

}