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 uk.ac.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 uk.ac.vamsas.objects.core.AlignmentSequence;
16 import uk.ac.vamsas.objects.core.Sequence;
17 import uk.ac.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 Vobject 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!)
63 * @return true if a valid amino acid sequence Vobject
65 private static boolean valid_aadictionary_string(String s, String dict) {
68 // validate against dictionary
69 // TODO generalise to resolve dictionary against info: urn for dictionary type
70 Pattern aa_repl = Pattern.compile("[ARNDCQEGHILKMFPSTWYVUX]+", Pattern.CASE_INSENSITIVE);
71 String remnants = aa_repl.matcher(s).replaceAll("");
72 return !remnants.matches("//S+");
75 public static Sequence newSequence(String Name, String Sequence, String Dictionary, int start, int end) {
76 //TODO: make hierarchy reflecting the SeqType Vobject.
77 Sequence seq= new Sequence();
78 seq.setDictionary(Dictionary);
80 seq.setSequence(Sequence);
83 if ((end-start)!=Sequence.length())
84 seq.setEnd(start+Sequence.length());
86 // reverse topology mapping. TODO: VAMSAS: decide if allowed to do start>end on Sequence Vobject
87 if ((start-end)!=Sequence.length())
88 seq.setEnd(end+Sequence.length());
92 public static AlignmentSequence newAlignmentSequence(String name, String alSequence, Sequence refseq, int start, int end) {
94 AlignmentSequence asq = new AlignmentSequence();
96 asq.setSequence(alSequence);
98 if (start<refseq.getStart())
99 start = refseq.getStart();
101 if (end>refseq.getEnd())
102 end = refseq.getEnd();
108 public static boolean is_valid_aa_seq(SequenceType s) {
110 boolean validref=false;
111 if (s instanceof Sequence) {
113 if (q.getDictionary()!=null
115 && q.getDictionary().length()>0
116 || !q.getDictionary().equals(SymbolDictionary.STANDARD_AA))
118 return valid_aadictionary_string(q.getSequence(), SymbolDictionary.STANDARD_AA);
122 if (s instanceof AlignmentSequence) {
123 Object w = (((AlignmentSequence) s).getRefid());
124 if (w!=null && w!=s && w instanceof SequenceType)
125 return is_valid_aa_seq((SequenceType) w)
126 && valid_aadictionary_string(((AlignmentSequence) s).getSequence(), SymbolDictionary.STANDARD_AA);