X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=datamodel%2Fcompbio%2Fdata%2Fsequence%2FSequenceUtil.java;h=6e20988d0b4c1bdd2ab4751e672402ac2779e7d9;hb=ba2d4e920e433bd080f448248e7dacade69a27a0;hp=d0a6cd7039f50d43707ce9054070bc06e9f8bce3;hpb=9ba9d7fd62b1f98b09ed4097c4b38ae94e25a72a;p=jabaws.git diff --git a/datamodel/compbio/data/sequence/SequenceUtil.java b/datamodel/compbio/data/sequence/SequenceUtil.java index d0a6cd7..6e20988 100644 --- a/datamodel/compbio/data/sequence/SequenceUtil.java +++ b/datamodel/compbio/data/sequence/SequenceUtil.java @@ -1,15 +1,19 @@ -/* - * @(#)SequenceUtil.java 1.0 September 2009 Copyright (c) 2009 Peter Troshin - * Jalview Web Services version: 2.0 This library is free software; you can - * redistribute it and/or modify it under the terms of the Apache License - * version 2 as published by the Apache Software Foundation This library 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 Apache License for more details. A copy of the - * license is in apache_license.txt. It is also available here: see: - * http://www.apache.org/licenses/LICENSE-2.0.txt Any republication or derived - * work distributed in source code form must include this copyright and license - * notice. +/* Copyright (c) 2011 Peter Troshin + * + * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0 + * + * This library is free software; you can redistribute it and/or modify it under the terms of the + * Apache License version 2 as published by the Apache Software Foundation + * + * This library 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 Apache + * License for more details. + * + * A copy of the license is in apache_license.txt. It is also available here: + * @see: http://www.apache.org/licenses/LICENSE-2.0.txt + * + * Any republication or derived work distributed in source code form + * must include this copyright and license notice. */ package compbio.data.sequence; @@ -41,8 +45,9 @@ import compbio.util.Util; /** * Utility class for operations on sequences * - * @author Petr Troshin - * @version 1.0 + * @author Peter Troshin + * @since 1.0 + * @version 2.0 June 2011 */ public final class SequenceUtil { @@ -99,19 +104,6 @@ public final class SequenceUtil { private SequenceUtil() { } // utility class, no instantiation - /* - * public static void write_PirSeq(OutputStream os, FastaSequence seq) - * throws IOException { BufferedWriter pir_out = new BufferedWriter(new - * OutputStreamWriter(os)); pir_out.write(">P1;" + seq.getId() + - * SysPrefs.newlinechar); pir_out.write(seq.getSequence() + - * SysPrefs.newlinechar); pir_out.close(); } public static void - * write_FastaSeq(OutputStream os, FastaSequence seq) throws IOException { - * BufferedWriter fasta_out = new BufferedWriter( new - * OutputStreamWriter(os)); fasta_out.write(">" + seq.getId() + - * SysPrefs.newlinechar); fasta_out.write(seq.getSequence() + - * SysPrefs.newlinechar); fasta_out.close(); } - */ - /** * @return true is the sequence contains only letters a,c, t, g, u */ @@ -170,6 +162,17 @@ public final class SequenceUtil { } /** + * Remove all non AA chars from the sequence + * + * @param sequence + * the sequence to clean + * @return cleaned sequence + */ + public static String cleanProteinSequence(String sequence) { + return SequenceUtil.NON_AA.matcher(sequence).replaceAll(""); + } + + /** * @param sequence * @return true is the sequence is a protein sequence, false overwise */ @@ -257,28 +260,11 @@ public final class SequenceUtil { public static List readFasta(final InputStream inStream) throws IOException { final List seqs = new ArrayList(); - - final BufferedReader infasta = new BufferedReader( - new InputStreamReader(inStream, "UTF8"), 16000); - final Pattern pattern = Pattern.compile("//s+"); - - String line; - String sname = "", seqstr = null; - do { - line = infasta.readLine(); - if ((line == null) || line.startsWith(">")) { - if (seqstr != null) { - seqs.add(new FastaSequence(sname.substring(1), seqstr)); - } - sname = line; // remove > - seqstr = ""; - } else { - final String subseq = pattern.matcher(line).replaceAll(""); - seqstr += subseq; - } - } while (line != null); - - infasta.close(); + FastaReader reader = new FastaReader(inStream); + while (reader.hasNext()) { + seqs.add(reader.next()); + } + inStream.close(); return seqs; } @@ -300,6 +286,127 @@ public final class SequenceUtil { outWriter.close(); } + /** + * Read IUPred output + * + * @param result + * @return Map key->sequence name, value->Score + * @throws IOException + * @throws UnknownFileFormatException + */ + public static Map readIUPred(final File result) + throws IOException, UnknownFileFormatException { + InputStream input = new FileInputStream(result); + Map sequences = readIUPred(input, + IUPredResult.getType(result)); + input.close(); + return sequences; + } + + // Check the type of the file e.g. long| short or domain + // and read + /** + * ## Long Disorder + * + * # P53_HUMAN + * + * 1 M 0.9943 + * + * 2 E 0.9917 + * + * 3 E 0.9879 + * + * (every line) + * + * @throws IOException + * @throws UnknownFileFormatException + * + * + */ + private static Map readIUPred(InputStream input, + IUPredResult type) throws IOException, UnknownFileFormatException { + + Score score = null; + final Map seqs = new HashMap(); + Scanner scan = new Scanner(input); + scan.useDelimiter("#"); + while (scan.hasNext()) { + String nextEntry = scan.next(); + Scanner entry = new Scanner(nextEntry); + String name = entry.nextLine().trim(); + // inside entry: + if (IUPredResult.Glob == type) { + // parse domains + TreeSet ranges = parseIUPredDomains(entry); + score = new Score(type, ranges); + } else { + // parse short | long + float[] scores = parseIUPredScores(entry); + score = new Score(type, scores); + } + entry.close(); + seqs.put(name, score); + } + + scan.close(); + return seqs; + } + + /** + * # P53_HUMA + * + * Number of globular domains: 2 + * + * globular domain 1. 98 - 269 + * + * globular domain 2. 431 - 482 + * + * >P53_HUMA + * + * meepqsdpsv epplsqetfs dlwkllpenn vlsplpsqam ddlmlspddi eqwftedpgp + * + * @param scan + */ + private static TreeSet parseIUPredDomains(Scanner scan) { + String header = "Number of globular domains:"; + String domainPref = "globular domain"; + TreeSet ranges = new TreeSet(); + String line = scan.nextLine().trim(); + assert line.startsWith(header); + line = line.substring(header.length()).trim(); + int domainNum = Integer.parseInt(line); + if (domainNum == 0) { + return ranges; + } + + for (int i = 0; i < domainNum; i++) { + assert scan.hasNextLine(); + line = scan.nextLine(); + assert line.trim().startsWith(domainPref); + line = line.substring(line.indexOf(".") + 1).trim(); + Range r = new Range(line.split("-")); + ranges.add(r); + } + + return ranges; + } + /* + * 1 M 0.9943 + * + * 2 E 0.9917 + */ + private static float[] parseIUPredScores(Scanner scan) + throws UnknownFileFormatException { + List annotation = new ArrayList(); + while (scan.hasNextLine()) { + String line = scan.nextLine().trim(); + String[] val = line.split("\\s+"); + annotation.add(val[2]); + } + return convertToNumber(annotation + .toArray(new String[annotation.size()])); + } + public static Map readJRonn(final File result) throws IOException, UnknownFileFormatException { InputStream input = new FileInputStream(result); @@ -320,7 +427,7 @@ public final class SequenceUtil { * * @param inStream * the InputStream connected to the JRonn output file - * @return List of {@link AnnotatedSequence} objects + * @return Map key=sequence name value=Score * @throws IOException * is thrown if the inStream has problems accessing the data * @throws UnknownFileFormatException @@ -426,7 +533,8 @@ public final class SequenceUtil { * * * @param input - * @return + * the InputStream + * @return Map key=sequence name, value=set of score * @throws IOException * @throws UnknownFileFormatException */ @@ -548,7 +656,7 @@ public final class SequenceUtil { * * * @param input - * @return + * @return Map key=sequence name, value=set of score * @throws IOException * @throws UnknownFileFormatException */ @@ -599,7 +707,7 @@ public final class SequenceUtil { * Also possible FastaSequence fs = new FastaSequence(sequenceName, * seqbuffer.toString()); */ - HashSet scores = new HashSet(); + Set scores = new TreeSet(); scores.add(new Score(GlobProtResult.Disorder, disorderR)); scores.add(new Score(GlobProtResult.GlobDoms, domsR)); scores.add(new Score(GlobProtResult.Dydx, dydxScore)); @@ -700,4 +808,37 @@ enum GlobProtResult { SmoothedScore, /** This a score with no range */ RawScore +} + +enum IUPredResult { + /** + * Short disorder + */ + Short, + /** + * Long disorder + */ + Long, + /** + * Globular domains + */ + Glob; + + static IUPredResult getType(File file) { + assert file != null; + String name = file.getName(); + if (name.endsWith(Long.toString().toLowerCase())) { + return Long; + } + if (name.endsWith(Short.toString().toLowerCase())) { + return Short; + } + if (name.endsWith(Glob.toString().toLowerCase())) { + return Glob; + } + throw new AssertionError( + "IUPred result file type cannot be recognised! " + + "\nFile must ends with one of [glob, long or short]" + + "\n but given file name was: " + file.getName()); + } } \ No newline at end of file