From: amwaterhouse Date: Wed, 8 Feb 2006 15:21:42 +0000 (+0000) Subject: Report invalid chars X-Git-Tag: Root_VamJalview_2_07b+~164 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=8d3d3b4edf47e543b8b352c1b2c247cfc59122a4;p=jalview.git Report invalid chars --- diff --git a/src/jalview/appletgui/CutAndPasteTransfer.java b/src/jalview/appletgui/CutAndPasteTransfer.java index 4fb8ec1..51d8d7f 100755 --- a/src/jalview/appletgui/CutAndPasteTransfer.java +++ b/src/jalview/appletgui/CutAndPasteTransfer.java @@ -103,8 +103,12 @@ public class CutAndPasteTransfer extends Panel implements ActionListener, MouseL SequenceI[] sequences = null; String format = IdentifyFile.Identify(text, "Paste"); - sequences = new AppletFormatAdapter().readFile(text, "Paste", format); - + try{ + sequences = new AppletFormatAdapter().readFile(text, "Paste", format); + }catch(java.io.IOException ex) + { + ex.printStackTrace(); + } if (sequences != null) { AlignFrame af = new AlignFrame(new Alignment(sequences), applet, diff --git a/src/jalview/io/AlignFile.java b/src/jalview/io/AlignFile.java index f933961..ceec54b 100755 --- a/src/jalview/io/AlignFile.java +++ b/src/jalview/io/AlignFile.java @@ -135,12 +135,15 @@ public abstract class AlignFile extends FileParse if (!jalview.schemes.ResidueProperties.aaHash.containsKey( String.valueOf(sequence.charAt(i)))) { + invalidCharacter = sequence.charAt(i); return false; } return true; } + char invalidCharacter; + /** * This method must be implemented to parse the contents of the file. */ diff --git a/src/jalview/io/AppletFormatAdapter.java b/src/jalview/io/AppletFormatAdapter.java index f8b809e..dca2d0c 100755 --- a/src/jalview/io/AppletFormatAdapter.java +++ b/src/jalview/io/AppletFormatAdapter.java @@ -34,6 +34,11 @@ /** DOCUMENT ME!! */ public static final Vector formats = new Vector(); + public static String INVALID_CHARACTERS = "Contains invalid characters"; + + public static String SUPPORTED_FORMATS = "Formats currently supported are\n" + + "Fasta, MSF, Clustal, BLC, PIR, MSP, and PFAM"; + static { formats.addElement("BLC"); @@ -57,6 +62,7 @@ * @return DOCUMENT ME! */ public SequenceI[] readFile(String inFile, String type, String format) + throws java.io.IOException { try { @@ -96,6 +102,9 @@ System.err.println("Failed to read alignment using the '" + format + "' reader.\n"+e); + if(e.getMessage()!=null && e.getMessage().startsWith(INVALID_CHARACTERS)) + throw new java.io.IOException(e.getMessage()); + // Finally test if the user has pasted just the sequence, no id if(type.equalsIgnoreCase("Paste")) { @@ -103,16 +112,19 @@ // Possible sequence is just residues with no label afile = new FastaFile(">UNKNOWN\n" + inFile, "Paste"); return afile.getSeqsAsArray(); - }catch(Exception ex) + } + catch(Exception ex) { - System.err.println("Failed to read alignment using the 'FASTA' reader.\n"+e); + if(ex.toString().startsWith(INVALID_CHARACTERS)) + throw new java.io.IOException(e.getMessage()); + ex.printStackTrace(); } - } - } - return null; + // If we get to this stage, the format was not supported + throw new java.io.IOException(SUPPORTED_FORMATS); + } } diff --git a/src/jalview/io/BLCFile.java b/src/jalview/io/BLCFile.java index a7cc1ea..56d40f9 100755 --- a/src/jalview/io/BLCFile.java +++ b/src/jalview/io/BLCFile.java @@ -77,7 +77,7 @@ public class BLCFile extends AlignFile /** * DOCUMENT ME! */ - public void parse() + public void parse() throws IOException { boolean idsFound = false; Vector ids = new Vector(); @@ -87,8 +87,6 @@ public class BLCFile extends AlignFile String line = null; - try - { do { line = nextLine(); @@ -146,17 +144,23 @@ public class BLCFile extends AlignFile for (int i = 0; i < ids.size(); i++) { - Sequence newSeq = new Sequence(ids.elementAt(i).toString(), - seqstrings[i].toString(), - Integer.parseInt(starts.elementAt(i).toString()), - Integer.parseInt(ends.elementAt(i).toString())); + Sequence newSeq = new Sequence(ids.elementAt(i).toString(), + seqstrings[i].toString(), + Integer.parseInt(starts.elementAt(i). + toString()), + Integer.parseInt(ends.elementAt(i).toString())); + + if (!isValidProteinSequence(newSeq.getSequence())) + { + throw new IOException(AppletFormatAdapter.INVALID_CHARACTERS + +" : "+ newSeq.getName() + +" : "+invalidCharacter); + + } + seqs.addElement(newSeq); } - } - catch (Exception ex) - { - ex.printStackTrace(); - } + } /** diff --git a/src/jalview/io/ClustalFile.java b/src/jalview/io/ClustalFile.java index e72e0df..2ea1d2f 100755 --- a/src/jalview/io/ClustalFile.java +++ b/src/jalview/io/ClustalFile.java @@ -48,7 +48,7 @@ public class ClustalFile super.initData(); } - public void parse() + public void parse() throws IOException { int i = 0; boolean flag = false; @@ -131,6 +131,14 @@ public class ClustalFile Sequence newSeq = parseId(headers.elementAt(i).toString()); newSeq.setSequence( seqhash.get(headers.elementAt(i).toString()).toString() ); + if (!isValidProteinSequence(newSeq.getSequence())) + { + throw new IOException(AppletFormatAdapter.INVALID_CHARACTERS + + " : " + newSeq.getName() + + " : " + invalidCharacter); + } + + seqs.addElement(newSeq); } else diff --git a/src/jalview/io/FastaFile.java b/src/jalview/io/FastaFile.java index 37c96f6..2e203af 100755 --- a/src/jalview/io/FastaFile.java +++ b/src/jalview/io/FastaFile.java @@ -82,6 +82,13 @@ public class FastaFile extends AlignFile { if (count != 0) { + if (!isValidProteinSequence(sb.toString())) + { + throw new IOException(AppletFormatAdapter.INVALID_CHARACTERS + +" : "+seq.getName() + +" : "+invalidCharacter); + } + seq.setSequence(sb.toString()); seqs.addElement(seq); } @@ -100,9 +107,11 @@ public class FastaFile extends AlignFile if (count > 0) { - if (!isValidProteinSequence(sb.toString().toUpperCase())) + if (!isValidProteinSequence(sb.toString())) { - throw new IOException("Invalid protein sequence"); + throw new IOException(AppletFormatAdapter.INVALID_CHARACTERS + +" : "+seq.getName() + +" : "+invalidCharacter); } seq.setSequence(sb.toString()); diff --git a/src/jalview/io/FileLoader.java b/src/jalview/io/FileLoader.java index 26aff08..38959a7 100755 --- a/src/jalview/io/FileLoader.java +++ b/src/jalview/io/FileLoader.java @@ -78,9 +78,16 @@ public class FileLoader } else { + String errorMessage = AppletFormatAdapter.SUPPORTED_FORMATS; + if (FormatAdapter.formats.contains(format)) { - sequences = new FormatAdapter().readFile(file, protocol, format); + try{ + sequences = new FormatAdapter().readFile(file, protocol, format); + }catch(java.io.IOException ex) + { + errorMessage = ex.getMessage(); + } } if ( (sequences != null) && (sequences.length > 0)) @@ -104,10 +111,9 @@ public class FileLoader else { JOptionPane.showInternalMessageDialog(Desktop.desktop, - "Couldn't open file.\n" + - "Formats currently supported are\n" + - "Fasta, MSF, Clustal, BLC, PIR, MSP, and PFAM" // JBPNote - message should be generated through FormatAdapter! - , "Error loading file", + "Couldn't load file "+file+"\n" + +errorMessage, + "Error loading file", JOptionPane.WARNING_MESSAGE); } } diff --git a/src/jalview/io/JPredFile.java b/src/jalview/io/JPredFile.java index cadf153..f5ba7d6 100755 --- a/src/jalview/io/JPredFile.java +++ b/src/jalview/io/JPredFile.java @@ -283,9 +283,9 @@ public class JPredFile extends AlignFile if (!Symscores.containsKey(ids.elementAt(i)) && !isValidProteinSequence(newSeq.getSequence())) { - throw new IOException( - "JPredConcise: Not a valid protein sequence - (" + - ids.elementAt(i).toString() + ")"); + throw new IOException("JPredConcise: " + +AppletFormatAdapter.INVALID_CHARACTERS +" : " + +ids.elementAt(i).toString() + ")"); } if (maxLength != seq_entries.elementAt(i).toString().length()) diff --git a/src/jalview/io/MSFfile.java b/src/jalview/io/MSFfile.java index 179204e..bae4342 100755 --- a/src/jalview/io/MSFfile.java +++ b/src/jalview/io/MSFfile.java @@ -70,7 +70,7 @@ public class MSFfile extends AlignFile /** * DOCUMENT ME! */ - public void parse() + public void parse() throws IOException { int i = 0; boolean seqFlag = false; @@ -147,9 +147,6 @@ public class MSFfile extends AlignFile String head = headers.elementAt(i).toString(); String seq = seqhash.get(head).toString(); - int start = -1; - int end = -1; - if (maxLength < head.length()) { maxLength = head.length(); @@ -157,8 +154,17 @@ public class MSFfile extends AlignFile // Replace ~ with a sensible gap character seq = seq.replace('~', '-'); + if (!isValidProteinSequence(seq)) + { + throw new IOException(AppletFormatAdapter. + INVALID_CHARACTERS + + " : " + head + + " : " + invalidCharacter); + } + Sequence newSeq = parseId(head); + newSeq.setSequence(seq); seqs.addElement(newSeq); diff --git a/src/jalview/io/PIRFile.java b/src/jalview/io/PIRFile.java index 724fea8..9e05d8f 100755 --- a/src/jalview/io/PIRFile.java +++ b/src/jalview/io/PIRFile.java @@ -21,7 +21,6 @@ package jalview.io; import java.io.*; import java.util.*; -import jalview.analysis.*; import jalview.datamodel.*; public class PIRFile @@ -44,10 +43,8 @@ public class PIRFile super(inFile, type); } - public void parse() + public void parse() throws IOException { - try - { StringBuffer sequence; String line = null; @@ -88,17 +85,19 @@ public class PIRFile { sequence.setLength(sequence.length() - 1); newSeq.setSequence(sequence.toString()); + if (!isValidProteinSequence(newSeq.getSequence())) + { + throw new IOException(AppletFormatAdapter.INVALID_CHARACTERS + +" : "+ newSeq.getName() + +" : "+invalidCharacter); + } + seqs.addElement(newSeq); ModellerDescription md = new ModellerDescription(newSeq. getDescription()); md.updateSequenceI(newSeq); } } - } - catch (Exception ex) - { - ex.printStackTrace(); - } } public String print() diff --git a/src/jalview/io/PfamFile.java b/src/jalview/io/PfamFile.java index ec52bac..8056f38 100755 --- a/src/jalview/io/PfamFile.java +++ b/src/jalview/io/PfamFile.java @@ -48,8 +48,7 @@ public class PfamFile super.initData(); } - public void parse() - throws IOException + public void parse() throws IOException { int i = 0; String line; @@ -118,8 +117,9 @@ public class PfamFile if (!isValidProteinSequence(newSeq.getSequence())) { - throw new IOException( - "Not a valid protein sequence - (PFAM input)"); + throw new IOException(AppletFormatAdapter.INVALID_CHARACTERS + +" : "+ newSeq.getName() + +" : "+invalidCharacter); } } else