File: ProteinDatabase.java

package info (click to toggle)
libpj-java 0.0~20150107%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 13,396 kB
  • sloc: java: 99,543; ansic: 987; sh: 153; xml: 26; makefile: 10; sed: 4
file content (381 lines) | stat: -rw-r--r-- 11,328 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
//******************************************************************************
//
// File:    ProteinDatabase.java
// Package: edu.rit.compbio.seq
// Unit:    Class edu.rit.compbio.seq.ProteinDatabase
//
// This Java source file is copyright (C) 2008 by Alan Kaminsky. All rights
// reserved. For further information, contact the author, Alan Kaminsky, at
// ark@cs.rit.edu.
//
// This Java source file is part of the Parallel Java Library ("PJ"). PJ 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 (at your option) any later version.
//
// PJ 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.
//
// Linking this library statically or dynamically with other modules is making a
// combined work based on this library. Thus, the terms and conditions of the
// GNU General Public License cover the whole combination.
//
// As a special exception, the copyright holders of this library give you
// permission to link this library with independent modules to produce an
// executable, regardless of the license terms of these independent modules, and
// to copy and distribute the resulting executable under terms of your choice,
// provided that you also meet, for each linked independent module, the terms
// and conditions of the license of that module. An independent module is a
// module which is not derived from or based on this library. If you modify this
// library, you may extend this exception to your version of the library, but
// you are not obligated to do so. If you do not wish to do so, delete this
// exception statement from your version.
//
// A copy of the GNU General Public License is provided in the file gpl.txt. You
// may also obtain a copy of the GNU General Public License on the World Wide
// Web at http://www.gnu.org/licenses/gpl.html.
//
//******************************************************************************

package edu.rit.compbio.seq;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;

/**
 * Class ProteinDatabase encapsulates a protein sequence database stored in a
 * file. Class ProteinDatabase is used to read {@linkplain ProteinSequence}
 * objects from the database.
 * <P>
 * A protein sequence database file consists of one or more protein sequences
 * stored in FASTA format. A FASTA format protein sequence consists of one
 * description line and one or more sequence lines. The description line
 * consists of an initial <TT>'&gt;'</TT> character followed by zero or more
 * characters (the protein's description). A sequence line consists of one or
 * more characters <TT>'A'</TT> through <TT>'Z'</TT>, <TT>'a'</TT> through
 * <TT>'z'</TT>, <TT>'*'</TT>, or <TT>'-'</TT>. For further information, see
 * class {@linkplain ProteinSequence}.
 * <P>
 * Along with the protein sequence database file, there is a protein sequence
 * index file. The index file tells where each protein is located in the
 * database file. Class ProteinDatabase's constructor requires both the database
 * file and the index file to be supplied as arguments.
 * <P>
 * Class ProteinDatabase includes a main program that reads a protein sequence
 * database file and creates the requisite protein sequence index file. If the
 * <I>n</I> argument is specified, the program creates an index file for just
 * the first <I>n</I> sequences in the database. If the <I>n</I> argument is
 * omitted, the program creates an index file for all the sequences in the
 * database.
 * <P>
 * Usage: java edu.rit.compbio.seq.ProteinDatabase <I>databasefile</I>
 * <I>indexfile</I> [ <I>n</I> ]
 * <BR><I>databasefile</I> = Input protein sequence database file
 * <BR><I>indexfile</I> = Output protein sequence index file
 * <BR><I>n</I> = Number of sequences in the index (default: all)
 *
 * @author  Alan Kaminsky
 * @version 01-Jul-2008
 */
public class ProteinDatabase
	{

// Hidden data members.

	private File myDatabaseFile;
	private File myIndexFile;
	private RandomAccessFile myIndex;
	private long myProteinCount;
	private long myDatabaseLength;
	private long myFileLength;

// Exported constructors.

	/**
	 * Construct a new protein sequence database.
	 *
	 * @param  theDatabaseFile  Protein sequence database file.
	 * @param  theIndexFile     Protein sequence index file.
	 *
	 * @exception  NullPointerException
	 *     (unchecked exception) Thrown if <TT>theDatabaseFile</TT> is null.
	 *     Thrown if <TT>theIndexFile</TT> is null.
	 * @exception  IOException
	 *     Thrown if an I/O error occurred.
	 */
	public ProteinDatabase
		(File theDatabaseFile,
		 File theIndexFile)
		throws IOException
		{
		// Verify preconditions.
		if (theDatabaseFile == null)
			{
			throw new NullPointerException
				("ProteinDatabase(): theDatabaseFile is null");
			}
		if (theIndexFile == null)
			{
			throw new NullPointerException
				("ProteinDatabase(): theIndexFile is null");
			}

		// Record files.
		myDatabaseFile = theDatabaseFile;
		myIndexFile = theIndexFile;

		// Open index file.
		myIndex = new RandomAccessFile (theIndexFile, "r");

		// Determine number of entries in index file.
		long n = myIndex.length();
		if ((n & 0x7L) != 0L)
			{
			throw new IOException
				("ProteinDatabase(): Index file \""+theIndexFile+
				 "\" has invalid length (= "+n+")");
			}
		myProteinCount = (n >> 3) - 1;

		// Determine total length of all sequences in database.
		myDatabaseLength = myIndex.readLong();

		// Determine length of database file.
		myFileLength = theDatabaseFile.length();
		}

// Exported operations.

	/**
	 * Get the sum of the lengths of the protein sequences in this protein
	 * sequence database.
	 *
	 * @return  Total protein sequence length.
	 */
	public long getDatabaseLength()
		{
		return myDatabaseLength;
		}

	/**
	 * Get the number of protein sequences in this protein sequence database.
	 *
	 * @return  Number of protein sequences, <I>N</I>.
	 */
	public long getProteinCount()
		{
		return myProteinCount;
		}

	/**
	 * Get the protein sequence at the given index in this protein sequence
	 * database.
	 *
	 * @param  i  Index in the range 0 &le; <TT>i</TT> &le; <I>N</I>&minus;1.
	 *
	 * @return  Protein sequence.
	 *
	 * @exception  IndexOutOfBoundsException
	 *     (unchecked exception) Thrown if <TT>i</TT> is out of bounds.
	 * @exception  IOException
	 *     Thrown if an I/O error occurred.
	 */
	public ProteinSequence getProteinSequence
		(long i)
		throws IOException
		{
		// Verify preconditions.
		if (0 > i || i >= myProteinCount)
			{
			throw new IndexOutOfBoundsException
				("ProteinDatabase.getProteinSequence(): i (= "+i+
				 ") out of bounds");
			}

		// Get offset to protein sequence from index file, in a critical section
		// for multiple thread safety.
		long offset;
		synchronized (this)
			{
			myIndex.seek ((i + 1) << 3);
			offset = myIndex.readLong();
			}
		if (0 > offset || offset >= myFileLength)
			{
			throw new IOException
				("ProteinDatabase.getProteinSequence("+i+
				 "): Invalid offset (= "+offset+")");
			}

		// Get protein sequence from database file.
		InputStream fis = null;
		try
			{
			// Open database file.
			fis =
				(new BufferedInputStream
					(new FileInputStream (myDatabaseFile)));

			// Seek to offset.
			long remaining = offset;
			long skipped = 0;
			while (remaining > 0)
				{
				skipped = fis.skip (remaining);
				if (skipped == 0)
					{
					throw new IOException
						("ProteinDatabase.getProteinSequence("+i+
						 "): Unexpected end of file");
					}
				remaining -= skipped;
				}

			// Read protein sequence.
			return new ProteinSequence (fis);
			}
		finally
			{
			// Close database file.
			if (fis != null)
				{
				try { fis.close(); } catch (IOException exc) {}
				}
			}
		}

	/**
	 * Close this protein sequence database.
	 *
	 * @exception  IOException
	 *     Thrown if an I/O error occurred.
	 */
	public void close()
		throws IOException
		{
		myIndex.close();
		}

	/**
	 * Finalize this protein sequence database.
	 */
	protected void finalize()
		{
		try { close(); } catch (IOException exc) {}
		}

	/**
	 * Main program that reads a protein sequence database file and creates the
	 * requisite protein sequence index file. If the <I>n</I> argument is
	 * specified, the program creates an index file for just the first <I>n</I>
	 * sequences in the database. If the <I>n</I> argument is omitted, the
	 * program creates an index file for all the sequences in the database.
	 * <P>
	 * Usage: java edu.rit.compbio.seq.ProteinDatabase <I>databasefile</I>
	 * <I>indexfile</I> [ <I>n</I> ]
	 * <BR><I>databasefile</I> = Input protein sequence database file
	 * <BR><I>indexfile</I> = Output protein sequence index file
	 * <BR><I>n</I> = Number of sequences in the index (default: all)
	 */
	public static void main
		(String[] args)
		throws Exception
		{
		// Parse command line arguments.
		if (args.length < 2 || args.length > 3) usage();
		File databasefile = new File (args[0]);
		File indexfile = new File (args[1]);
		long n = Long.MAX_VALUE;
		if (args.length == 3) n = Long.parseLong (args[2]);

		// Open database file for reading.
		InputStream databasein =
			new BufferedInputStream
				(new FileInputStream (databasefile));

		// Open index file for writing. Set aside space for database length.
		RandomAccessFile indexout = new RandomAccessFile (indexfile, "rw");
		indexout.writeLong (0L);

		// Scan the database file. Whenever a '>' is encountered at the
		// beginning of a line, store its offset in the index file.
		long offset = 0L;
		long dblength = 0L;
		int state = 0;
		int b;
		readloop: while ((b = databasein.read()) != -1)
			{
			switch (state)
				{
				case 0: // Beginning of line
					if (b == '>')
						{
						if (n == 0) break readloop;
						indexout.writeLong (offset);
						-- n;
						state = 1;
						}
					else if (b == '\r' || b == '\n')
						{
						state = 0;
						}
					else
						{
						++ dblength;
						state = 2;
						}
					break;
				case 1: // In a description line
					if (b == '\r' || b == '\n')
						{
						state = 0;
						}
					else
						{
						state = 1;
						}
					break;
				case 2: // In a sequence line
					if (b == '\r' || b == '\n')
						{
						state = 0;
						}
					else
						{
						++ dblength;
						state = 2;
						}
					break;
				}
			++ offset;
			}

		// Write database length.
		indexout.seek (0L);
		indexout.writeLong (dblength);

		// All done.
		databasein.close();
		indexout.close();
		}

// Hidden operations.

	/**
	 * Print a usage message and exit.
	 */
	private static void usage()
		{
		System.err.println ("Usage: java edu.rit.compbio.seq.ProteinDatabase <databasefile> <indexfile> [<length>]");
		System.err.println ("<databasefile> = Input protein sequence database file");
		System.err.println ("<indexfile> = Output protein sequence index file");
		System.err.println ("<length> = Number of sequences in the index (default: all)");
		System.exit (1);
		}

	}