2 * @(#)SequenceUtil.java 1.0 September 2009 Copyright (c) 2009 Peter Troshin
\r
3 * Jalview Web Services version: 2.0 This library is free software; you can
\r
4 * redistribute it and/or modify it under the terms of the Apache License
\r
5 * version 2 as published by the Apache Software Foundation This library is
\r
6 * distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
\r
7 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
\r
8 * PARTICULAR PURPOSE. See the Apache License for more details. A copy of the
\r
9 * license is in apache_license.txt. It is also available here: see:
\r
10 * http://www.apache.org/licenses/LICENSE-2.0.txt Any republication or derived
\r
11 * work distributed in source code form must include this copyright and license
\r
15 package compbio.data.sequence;
\r
17 import java.io.BufferedReader;
\r
18 import java.io.BufferedWriter;
\r
19 import java.io.Closeable;
\r
20 import java.io.File;
\r
21 import java.io.FileInputStream;
\r
22 import java.io.IOException;
\r
23 import java.io.InputStream;
\r
24 import java.io.InputStreamReader;
\r
25 import java.io.OutputStream;
\r
26 import java.io.OutputStreamWriter;
\r
27 import java.util.ArrayList;
\r
28 import java.util.List;
\r
29 import java.util.Scanner;
\r
30 import java.util.logging.Level;
\r
31 import java.util.regex.Matcher;
\r
32 import java.util.regex.Pattern;
\r
35 * Utility class for operations on sequences
\r
37 * @author Petr Troshin
\r
40 public final class SequenceUtil {
\r
43 * A whitespace character: [\t\n\x0B\f\r]
\r
45 public static final Pattern WHITE_SPACE = Pattern.compile("\\s");
\r
50 public static final Pattern DIGIT = Pattern.compile("\\d");
\r
55 public static final Pattern NONWORD = Pattern.compile("\\W");
\r
60 public static final Pattern AA = Pattern.compile("[ARNDCQEGHILKMFPSTWYV]+",
\r
61 Pattern.CASE_INSENSITIVE);
\r
64 * inversion of AA pattern
\r
66 public static final Pattern NON_AA = Pattern.compile(
\r
67 "[^ARNDCQEGHILKMFPSTWYV]+", Pattern.CASE_INSENSITIVE);
\r
70 * Same as AA pattern but with two additional letters - XU
\r
72 public static final Pattern AMBIGUOUS_AA = Pattern.compile(
\r
73 "[ARNDCQEGHILKMFPSTWYVXU]+", Pattern.CASE_INSENSITIVE);
\r
76 * Nucleotides a, t, g, c, u
\r
78 public static final Pattern NUCLEOTIDE = Pattern.compile("[AGTCU]+",
\r
79 Pattern.CASE_INSENSITIVE);
\r
82 * Ambiguous nucleotide
\r
84 public static final Pattern AMBIGUOUS_NUCLEOTIDE = Pattern.compile(
\r
85 "[AGTCRYMKSWHBVDNU]+", Pattern.CASE_INSENSITIVE); // see IUPAC
\r
89 public static final Pattern NON_NUCLEOTIDE = Pattern.compile("[^AGTCU]+",
\r
90 Pattern.CASE_INSENSITIVE);
\r
92 private SequenceUtil() {
\r
93 } // utility class, no instantiation
\r
96 * public static void write_PirSeq(OutputStream os, FastaSequence seq)
\r
97 * throws IOException { BufferedWriter pir_out = new BufferedWriter(new
\r
98 * OutputStreamWriter(os)); pir_out.write(">P1;" + seq.getId() +
\r
99 * SysPrefs.newlinechar); pir_out.write(seq.getSequence() +
\r
100 * SysPrefs.newlinechar); pir_out.close(); } public static void
\r
101 * write_FastaSeq(OutputStream os, FastaSequence seq) throws IOException {
\r
102 * BufferedWriter fasta_out = new BufferedWriter( new
\r
103 * OutputStreamWriter(os)); fasta_out.write(">" + seq.getId() +
\r
104 * SysPrefs.newlinechar); fasta_out.write(seq.getSequence() +
\r
105 * SysPrefs.newlinechar); fasta_out.close(); }
\r
109 * @return true is the sequence contains only letters a,c, t, g, u
\r
111 public static boolean isNucleotideSequence(final FastaSequence s) {
\r
112 return SequenceUtil.isNonAmbNucleotideSequence(s.getSequence());
\r
116 * Ambiguous DNA chars : AGTCRYMKSWHBVDN // differs from protein in only one
\r
119 public static boolean isNonAmbNucleotideSequence(String sequence) {
\r
120 sequence = SequenceUtil.cleanSequence(sequence);
\r
121 if (SequenceUtil.DIGIT.matcher(sequence).find()) {
\r
124 if (SequenceUtil.NON_NUCLEOTIDE.matcher(sequence).find()) {
\r
127 * System.out.format("I found the text starting at " +
\r
128 * "index %d and ending at index %d.%n", nonDNAmatcher .start(),
\r
129 * nonDNAmatcher.end());
\r
132 final Matcher DNAmatcher = SequenceUtil.NUCLEOTIDE.matcher(sequence);
\r
133 return DNAmatcher.find();
\r
137 * Removes all whitespace chars in the sequence string
\r
140 * @return cleaned up sequence
\r
142 public static String cleanSequence(String sequence) {
\r
143 assert sequence != null;
\r
144 final Matcher m = SequenceUtil.WHITE_SPACE.matcher(sequence);
\r
145 sequence = m.replaceAll("").toUpperCase();
\r
150 * Removes all special characters and digits as well as whitespace chars
\r
151 * from the sequence
\r
154 * @return cleaned up sequence
\r
156 public static String deepCleanSequence(String sequence) {
\r
157 sequence = SequenceUtil.cleanSequence(sequence);
\r
158 sequence = SequenceUtil.DIGIT.matcher(sequence).replaceAll("");
\r
159 sequence = SequenceUtil.NONWORD.matcher(sequence).replaceAll("");
\r
160 final Pattern othernonSeqChars = Pattern.compile("[_-]+");
\r
161 sequence = othernonSeqChars.matcher(sequence).replaceAll("");
\r
167 * @return true is the sequence is a protein sequence, false overwise
\r
169 public static boolean isProteinSequence(String sequence) {
\r
170 sequence = SequenceUtil.cleanSequence(sequence);
\r
171 if (SequenceUtil.isNonAmbNucleotideSequence(sequence)) {
\r
174 if (SequenceUtil.DIGIT.matcher(sequence).find()) {
\r
177 if (SequenceUtil.NON_AA.matcher(sequence).find()) {
\r
180 final Matcher protmatcher = SequenceUtil.AA.matcher(sequence);
\r
181 return protmatcher.find();
\r
185 * Check whether the sequence confirms to amboguous protein sequence
\r
188 * @return return true only if the sequence if ambiguous protein sequence
\r
189 * Return false otherwise. e.g. if the sequence is non-ambiguous
\r
192 public static boolean isAmbiguosProtein(String sequence) {
\r
193 sequence = SequenceUtil.cleanSequence(sequence);
\r
194 if (SequenceUtil.isNonAmbNucleotideSequence(sequence)) {
\r
197 if (SequenceUtil.DIGIT.matcher(sequence).find()) {
\r
200 if (SequenceUtil.NON_AA.matcher(sequence).find()) {
\r
203 if (SequenceUtil.AA.matcher(sequence).find()) {
\r
206 final Matcher amb_prot = SequenceUtil.AMBIGUOUS_AA.matcher(sequence);
\r
207 return amb_prot.find();
\r
211 * Writes list of FastaSequeces into the outstream formatting the sequence
\r
212 * so that it contains width chars on each line
\r
217 * - the maximum number of characters to write in one line
\r
218 * @throws IOException
\r
220 public static void writeFasta(final OutputStream outstream,
\r
221 final List<FastaSequence> sequences, final int width)
\r
222 throws IOException {
\r
223 writeFastaKeepTheStream(outstream, sequences, width);
\r
227 public static void writeFastaKeepTheStream(final OutputStream outstream,
\r
228 final List<FastaSequence> sequences, final int width)
\r
229 throws IOException {
\r
230 final OutputStreamWriter writer = new OutputStreamWriter(outstream);
\r
231 final BufferedWriter fastawriter = new BufferedWriter(writer);
\r
232 for (final FastaSequence fs : sequences) {
\r
233 fastawriter.write(">" + fs.getId() + "\n");
\r
234 fastawriter.write(fs.getFormatedSequence(width));
\r
235 fastawriter.write("\n");
\r
237 fastawriter.flush();
\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 >Foobar M G D T T A G 0.48 0.42
\r
306 * 0.42 0.48 0.52 0.53 0.54 All values are tab delimited
\r
310 * @throws IOException
\r
311 * @throws UnknownFileFormatException
\r
313 public static List<AnnotatedSequence> readJRonn(final InputStream inStream)
\r
314 throws IOException, UnknownFileFormatException {
\r
315 final List<AnnotatedSequence> seqs = new ArrayList<AnnotatedSequence>();
\r
317 final BufferedReader infasta = new BufferedReader(
\r
318 new InputStreamReader(inStream, "UTF8"), 16000);
\r
323 line = infasta.readLine();
\r
324 if (line == null || line.isEmpty()) {
\r
325 // skip empty lines
\r
328 if (line.startsWith(">")) {
\r
330 sname = line.trim().substring(1);
\r
331 // read sequence line
\r
332 line = infasta.readLine();
\r
333 final String sequence = line.replace("\t", "");
\r
334 // read annotation line
\r
335 line = infasta.readLine();
\r
336 String[] annotValues = line.split("\t");
\r
337 float[] annotation = convertToNumber(annotValues);
\r
338 if (annotation.length != sequence.length()) {
\r
339 throw new UnknownFileFormatException(
\r
340 "File does not look like Jronn horizontally formatted output file!\n"
\r
341 + JRONN_WRONG_FORMAT_MESSAGE);
\r
343 seqs.add(new AnnotatedSequence(sname, sequence, annotation));
\r
345 } while (line != null);
\r
351 private static float[] convertToNumber(String[] annotValues)
\r
352 throws UnknownFileFormatException {
\r
353 float[] annotation = new float[annotValues.length];
\r
355 for (int i = 0; i < annotation.length; i++) {
\r
356 annotation[i] = Float.parseFloat(annotValues[i]);
\r
358 } catch (NumberFormatException e) {
\r
359 throw new UnknownFileFormatException(JRONN_WRONG_FORMAT_MESSAGE,
\r
365 private static final String JRONN_WRONG_FORMAT_MESSAGE = "Jronn file must be in the following format:\n"
\r
366 + ">sequence_name\n "
\r
368 + "0.43 0.22 0.65\n"
\r
369 + "Where first line is the sequence name,\n"
\r
370 + "second line is the tab delimited sequence,\n"
\r
371 + "third line contains tab delimited disorder prediction values.\n"
\r
372 + "No lines are allowed between these three. Additionally, the number of "
\r
373 + "sequence residues must be equal to the number of the disorder values.";
\r
376 * Closes the Closable and logs the exception if any
\r
381 public final static void closeSilently(java.util.logging.Logger log,
\r
382 Closeable stream) {
\r
383 if (stream != null) {
\r
386 } catch (IOException e) {
\r
387 log.log(Level.WARNING, e.getLocalizedMessage(), e.getCause());
\r
396 * # RESIDUE COILS REM465 HOTLOOPS M 0.86010 0.88512 0.37094 T 0.79983
\r
397 * 0.85864 0.44331 .... # RESIDUE COILS REM465 HOTLOOPS M 0.86010 0.88512
\r
402 * @throws IOException
\r
403 * @throws UnknownFileFormatException
\r
405 public static List<MultiAnnotatedSequence<DisemblResultAnnot>> readDisembl(
\r
406 final InputStream input) throws IOException,
\r
407 UnknownFileFormatException {
\r
408 Scanner scan = new Scanner(input);
\r
409 scan.useDelimiter("# RESIDUE COILS REM465 HOTLOOPS\n");
\r
410 if (!scan.hasNext()) {
\r
411 throw new UnknownFileFormatException(
\r
412 "In Disembl score format each seqeunce score is expected to start from the line: "
\r
413 + "'# RESIDUE COILS REM465 HOTLOOPS\\n'."
\r
414 + " No such line was found!");
\r
417 List<MultiAnnotatedSequence<DisemblResultAnnot>> results = new ArrayList<MultiAnnotatedSequence<DisemblResultAnnot>>();
\r
418 int seqCounter = 0;
\r
419 while (scan.hasNext()) {
\r
421 String singleSeq = scan.next();
\r
422 Scanner scansingle = new Scanner(singleSeq);
\r
423 StringBuffer seqbuffer = new StringBuffer();
\r
424 List<Float> coils = new ArrayList<Float>();
\r
425 List<Float> rem = new ArrayList<Float>();
\r
426 List<Float> hotloops = new ArrayList<Float>();
\r
428 MultiAnnotatedSequence<DisemblResultAnnot> disemblRes = new MultiAnnotatedSequence<DisemblResultAnnot>(
\r
429 DisemblResultAnnot.class);
\r
431 while (scansingle.hasNextLine()) {
\r
432 String valueLine = scansingle.nextLine();
\r
433 Scanner values = new Scanner(valueLine);
\r
434 seqbuffer.append(values.next());
\r
435 coils.add(values.nextFloat());
\r
436 rem.add(values.nextFloat());
\r
437 hotloops.add(values.nextFloat());
\r
440 disemblRes.addAnnotation(DisemblResultAnnot.COILS, coils);
\r
441 disemblRes.addAnnotation(DisemblResultAnnot.REM465, rem);
\r
442 disemblRes.addAnnotation(DisemblResultAnnot.HOTLOOPS, hotloops);
\r
444 // disemblRes.sequence = seqbuffer.toString();
\r
445 scansingle.close();
\r
446 results.add(disemblRes);
\r