2 * Created on 17-May-2005
4 * TODO To change the template for this generated file go to
5 * Window - Preferences - Java - Code Style - Code Templates
7 package org.vamsas.objects.utils;
9 import java.io.BufferedWriter;
10 import java.io.IOException;
11 import java.io.OutputStream;
12 import java.io.OutputStreamWriter;
13 import java.util.regex.Pattern;
15 import org.vamsas.objects.core.AlignmentSequence;
16 import org.vamsas.objects.core.Sequence;
17 import org.vamsas.objects.core.SequenceType;
22 * TODO To change the template for this generated type comment go to
23 * Window - Preferences - Java - Code Style - Code Templates
27 public static void write_PirSeq(OutputStream os, SequenceType seq, int wid) throws IOException {
28 BufferedWriter pir_out = new BufferedWriter(new OutputStreamWriter(os));
29 pir_out.write(">P1;"+seq.getName()+"\n");
30 int width = (wid<1) ? 80 : wid;
31 for (int j=0,k=seq.getSequence().length(); j<k; j+=width)
33 pir_out.write(seq.getSequence().substring(j,j+width)+"\n");
35 pir_out.write(seq.getSequence().substring(j)+"\n");
39 public static void write_FastaSeq(OutputStream os, SequenceType seq) throws IOException {
40 BufferedWriter fasta_out = new BufferedWriter(new OutputStreamWriter(os));
41 fasta_out.write(">"+seq.getName()+"\n");
42 fasta_out.write(seq.getSequence()+"\n");
46 public static void write_FastaSeq(OutputStream os, SequenceType seq, int wid) throws IOException {
47 BufferedWriter fasta_out = new BufferedWriter(new OutputStreamWriter(os));
48 fasta_out.write(">"+seq.getName()+"\n");
49 int width = (wid<1) ? 80 : wid;
50 for (int j=0,k=seq.getSequence().length(); j<k; j+=width)
52 fasta_out.write(seq.getSequence().substring(j,j+width)+"\n");
54 fasta_out.write(seq.getSequence().substring(j)+"\n");
58 *validate a SequenceType object as an info:iubmb.org/aminoacid SequenceType
59 *This version resolves references to Sequence objects from AlignmentSequence
60 *TODO: Define info: urn for dictionary string (could also be regex of valid characters!)
62 * @return true if a valid amino acid sequence object
64 private static boolean valid_aadictionary_string(String s) {
67 // validate against dictionary
68 // TODO generalise to resolve dictionary against info: urn for dictionary type
69 Pattern aa_repl = Pattern.compile("[ARNDCQEGHILKMFPSTWYVUX]+", Pattern.CASE_INSENSITIVE);
70 String remnants = aa_repl.matcher(s).replaceAll("");
71 return !remnants.matches("//S+");
74 public static boolean is_valid_aa_seq(SequenceType s) {
76 boolean validref=false;
77 if (s instanceof Sequence) {
79 if (q.getDictionary()!=null
81 && q.getDictionary().length()>0
82 || !q.getDictionary().equals("info:iubmb.org/aminoacid"))
84 return valid_aadictionary_string(q.getSequence());
88 if (s instanceof AlignmentSequence) {
89 Object w = (((AlignmentSequence) s).getRefid());
90 if (w!=null && w!=s && w instanceof SequenceType)
91 return is_valid_aa_seq((SequenceType) w)
92 && valid_aadictionary_string(((AlignmentSequence) s).getSequence());