2 * @(#)SequenceUtil.java 1.0 September 2009
\r
4 * Copyright (c) 2009 Peter Troshin
\r
6 * Jalview Web Services version: 2.0
\r
8 * This library is free software; you can redistribute it and/or modify it under the terms of the
\r
9 * Apache License version 2 as published by the Apache Software Foundation
\r
11 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
\r
12 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache
\r
13 * License for more details.
\r
15 * A copy of the license is in apache_license.txt. It is also available here:
\r
16 * see: http://www.apache.org/licenses/LICENSE-2.0.txt
\r
18 * Any republication or derived work distributed in source code form
\r
19 * must include this copyright and license notice.
\r
22 package compbio.data.sequence;
\r
24 import java.io.BufferedReader;
\r
25 import java.io.BufferedWriter;
\r
26 import java.io.Closeable;
\r
27 import java.io.File;
\r
28 import java.io.FileInputStream;
\r
29 import java.io.IOException;
\r
30 import java.io.InputStream;
\r
31 import java.io.InputStreamReader;
\r
32 import java.io.OutputStream;
\r
33 import java.io.OutputStreamWriter;
\r
34 import java.util.ArrayList;
\r
35 import java.util.List;
\r
36 import java.util.logging.Level;
\r
37 import java.util.regex.Matcher;
\r
38 import java.util.regex.Pattern;
\r
41 * Utility class for operations on sequences
\r
43 * @author Petr Troshin
\r
46 public final class SequenceUtil {
\r
49 * A whitespace character: [\t\n\x0B\f\r]
\r
51 public static final Pattern WHITE_SPACE = Pattern.compile("\\s");
\r
56 public static final Pattern DIGIT = Pattern.compile("\\d");
\r
61 public static final Pattern NONWORD = Pattern.compile("\\W");
\r
66 public static final Pattern AA = Pattern.compile("[ARNDCQEGHILKMFPSTWYV]+",
\r
67 Pattern.CASE_INSENSITIVE);
\r
70 * inversion of AA pattern
\r
72 public static final Pattern NON_AA = Pattern.compile(
\r
73 "[^ARNDCQEGHILKMFPSTWYV]+", Pattern.CASE_INSENSITIVE);
\r
76 * Same as AA pattern but with two additional letters - XU
\r
78 public static final Pattern AMBIGUOUS_AA = Pattern.compile(
\r
79 "[ARNDCQEGHILKMFPSTWYVXU]+", Pattern.CASE_INSENSITIVE);
\r
82 * Nucleotides a, t, g, c, u
\r
84 public static final Pattern NUCLEOTIDE = Pattern.compile("[AGTCU]+",
\r
85 Pattern.CASE_INSENSITIVE);
\r
88 * Ambiguous nucleotide
\r
90 public static final Pattern AMBIGUOUS_NUCLEOTIDE = Pattern.compile(
\r
91 "[AGTCRYMKSWHBVDNU]+", Pattern.CASE_INSENSITIVE); // see IUPAC
\r
95 public static final Pattern NON_NUCLEOTIDE = Pattern.compile("[^AGTCU]+",
\r
96 Pattern.CASE_INSENSITIVE);
\r
98 private SequenceUtil() {
\r
99 } // utility class, no instantiation
\r
102 * public static void write_PirSeq(OutputStream os, FastaSequence seq)
\r
103 * throws IOException { BufferedWriter pir_out = new BufferedWriter(new
\r
104 * OutputStreamWriter(os)); pir_out.write(">P1;" + seq.getId() +
\r
105 * SysPrefs.newlinechar); pir_out.write(seq.getSequence() +
\r
106 * SysPrefs.newlinechar); pir_out.close(); }
\r
108 * public static void write_FastaSeq(OutputStream os, FastaSequence seq)
\r
109 * throws IOException { BufferedWriter fasta_out = new BufferedWriter( new
\r
110 * OutputStreamWriter(os)); fasta_out.write(">" + seq.getId() +
\r
111 * SysPrefs.newlinechar); fasta_out.write(seq.getSequence() +
\r
112 * SysPrefs.newlinechar); fasta_out.close(); }
\r
116 * @return true is the sequence contains only letters a,c, t, g, u
\r
118 public static boolean isNucleotideSequence(final FastaSequence s) {
\r
119 return SequenceUtil.isNonAmbNucleotideSequence(s.getSequence());
\r
123 * Ambiguous DNA chars : AGTCRYMKSWHBVDN // differs from protein in only one
\r
126 public static boolean isNonAmbNucleotideSequence(String sequence) {
\r
127 sequence = SequenceUtil.cleanSequence(sequence);
\r
128 if (SequenceUtil.DIGIT.matcher(sequence).find()) {
\r
131 if (SequenceUtil.NON_NUCLEOTIDE.matcher(sequence).find()) {
\r
134 * System.out.format("I found the text starting at " +
\r
135 * "index %d and ending at index %d.%n", nonDNAmatcher .start(),
\r
136 * nonDNAmatcher.end());
\r
139 final Matcher DNAmatcher = SequenceUtil.NUCLEOTIDE.matcher(sequence);
\r
140 return DNAmatcher.find();
\r
144 * Removes all whitespace chars in the sequence string
\r
147 * @return cleaned up sequence
\r
149 public static String cleanSequence(String sequence) {
\r
150 assert sequence != null;
\r
151 final Matcher m = SequenceUtil.WHITE_SPACE.matcher(sequence);
\r
152 sequence = m.replaceAll("").toUpperCase();
\r
157 * Removes all special characters and digits as well as whitespace chars
\r
158 * from the sequence
\r
161 * @return cleaned up sequence
\r
163 public static String deepCleanSequence(String sequence) {
\r
164 sequence = SequenceUtil.cleanSequence(sequence);
\r
165 sequence = SequenceUtil.DIGIT.matcher(sequence).replaceAll("");
\r
166 sequence = SequenceUtil.NONWORD.matcher(sequence).replaceAll("");
\r
167 final Pattern othernonSeqChars = Pattern.compile("[_-]+");
\r
168 sequence = othernonSeqChars.matcher(sequence).replaceAll("");
\r
175 * @return true is the sequence is a protein sequence, false overwise
\r
177 public static boolean isProteinSequence(String sequence) {
\r
178 sequence = SequenceUtil.cleanSequence(sequence);
\r
179 if (SequenceUtil.isNonAmbNucleotideSequence(sequence)) {
\r
182 if (SequenceUtil.DIGIT.matcher(sequence).find()) {
\r
185 if (SequenceUtil.NON_AA.matcher(sequence).find()) {
\r
188 final Matcher protmatcher = SequenceUtil.AA.matcher(sequence);
\r
189 return protmatcher.find();
\r
193 * Check whether the sequence confirms to amboguous protein sequence
\r
196 * @return return true only if the sequence if ambiguous protein sequence
\r
197 * Return false otherwise. e.g. if the sequence is non-ambiguous
\r
200 public static boolean isAmbiguosProtein(String sequence) {
\r
201 sequence = SequenceUtil.cleanSequence(sequence);
\r
202 if (SequenceUtil.isNonAmbNucleotideSequence(sequence)) {
\r
205 if (SequenceUtil.DIGIT.matcher(sequence).find()) {
\r
208 if (SequenceUtil.NON_AA.matcher(sequence).find()) {
\r
211 if (SequenceUtil.AA.matcher(sequence).find()) {
\r
214 final Matcher amb_prot = SequenceUtil.AMBIGUOUS_AA.matcher(sequence);
\r
215 return amb_prot.find();
\r
219 * Writes list of FastaSequeces into the outstream formatting the sequence
\r
220 * so that it contains width chars on each line
\r
225 * - the maximum number of characters to write in one line
\r
226 * @throws IOException
\r
228 public static void writeFasta(final OutputStream outstream,
\r
229 final List<FastaSequence> sequences, final int width)
\r
230 throws IOException {
\r
231 final OutputStreamWriter writer = new OutputStreamWriter(outstream);
\r
232 final BufferedWriter fastawriter = new BufferedWriter(writer);
\r
233 for (final FastaSequence fs : sequences) {
\r
234 fastawriter.write(fs.getFormatedSequence(width));
\r
237 fastawriter.close();
\r
242 * Reads fasta sequences from inStream into the list of FastaSequence
\r
247 * @return list of FastaSequence objects
\r
248 * @throws IOException
\r
250 public static List<FastaSequence> readFasta(final InputStream inStream)
\r
251 throws IOException {
\r
252 final List<FastaSequence> seqs = new ArrayList<FastaSequence>();
\r
254 final BufferedReader infasta = new BufferedReader(
\r
255 new InputStreamReader(inStream, "UTF8"), 16000);
\r
256 final Pattern pattern = Pattern.compile("//s+");
\r
259 String sname = "", seqstr = null;
\r
261 line = infasta.readLine();
\r
262 if ((line == null) || line.startsWith(">")) {
\r
263 if (seqstr != null) {
\r
264 seqs.add(new FastaSequence(sname.substring(1), seqstr));
\r
266 sname = line; // remove >
\r
269 final String subseq = pattern.matcher(line).replaceAll("");
\r
272 } while (line != null);
\r
279 * Writes FastaSequence in the file, each sequence will take one line only
\r
283 * @throws IOException
\r
285 public static void writeFasta(final OutputStream os,
\r
286 final List<FastaSequence> sequences) throws IOException {
\r
287 final OutputStreamWriter outWriter = new OutputStreamWriter(os);
\r
288 final BufferedWriter fasta_out = new BufferedWriter(outWriter);
\r
289 for (final FastaSequence fs : sequences) {
\r
290 fasta_out.write(fs.getOnelineFasta());
\r
296 public static List<AnnotatedSequence> readJRonn(final File result)
\r
297 throws IOException, UnknownFileFormatException {
\r
298 InputStream input = new FileInputStream(result);
\r
299 List<AnnotatedSequence> sequences = readJRonn(input);
\r
305 * Reader for JRonn horizontal file format
\r
311 * 0.48 0.42 0.42 0.48 0.52 0.53 0.54
\r
313 * All values are tab delimited
\r
317 * @throws IOException
\r
318 * @throws UnknownFileFormatException
\r
320 public static List<AnnotatedSequence> readJRonn(final InputStream inStream)
\r
321 throws IOException, UnknownFileFormatException {
\r
322 final List<AnnotatedSequence> seqs = new ArrayList<AnnotatedSequence>();
\r
324 final BufferedReader infasta = new BufferedReader(
\r
325 new InputStreamReader(inStream, "UTF8"), 16000);
\r
330 line = infasta.readLine();
\r
331 if (line == null || line.isEmpty()) {
\r
332 // skip empty lines
\r
335 if (line.startsWith(">")) {
\r
337 sname = line.trim().substring(1);
\r
338 // read sequence line
\r
339 line = infasta.readLine();
\r
340 final String sequence = line.replace("\t", "");
\r
341 // read annotation line
\r
342 line = infasta.readLine();
\r
343 String[] annotValues = line.split("\t");
\r
344 float[] annotation = convertToNumber(annotValues);
\r
345 if (annotation.length != sequence.length()) {
\r
346 throw new UnknownFileFormatException(
\r
347 "File does not look like Jronn horizontally formatted output file!\n"
\r
348 + JRONN_WRONG_FORMAT_MESSAGE);
\r
350 seqs.add(new AnnotatedSequence(sname, sequence, annotation));
\r
352 } while (line != null);
\r
358 private static float[] convertToNumber(String[] annotValues)
\r
359 throws UnknownFileFormatException {
\r
360 float[] annotation = new float[annotValues.length];
\r
362 for (int i = 0; i < annotation.length; i++) {
\r
363 annotation[i] = Float.parseFloat(annotValues[i]);
\r
365 } catch (NumberFormatException e) {
\r
366 throw new UnknownFileFormatException(JRONN_WRONG_FORMAT_MESSAGE, e
\r
372 private static final String JRONN_WRONG_FORMAT_MESSAGE = "Jronn file must be in the following format:\n"
\r
373 + ">sequence_name\n "
\r
375 + "0.43 0.22 0.65\n"
\r
376 + "Where first line is the sequence name,\n"
\r
377 + "second line is the tab delimited sequence,\n"
\r
378 + "third line contains tab delimited disorder prediction values.\n"
\r
379 + "No lines are allowed between these three. Additionally, the number of "
\r
380 + "sequence residues must be equal to the number of the disorder values.";
\r
383 * Closes the Closable and logs the exception if any
\r
388 public final static void closeSilently(java.util.logging.Logger log,
\r
389 Closeable stream) {
\r
390 if (stream != null) {
\r
393 } catch (IOException e) {
\r
394 log.log(Level.WARNING, e.getLocalizedMessage(), e.getCause());
\r