methods for making and accessing objects in org.vamsa.objects.core
[vamsas.git] / src / org / vamsas / objects / utils / Seq.java
1 /*
2  * Created on 17-May-2005
3  *
4  * TODO To change the template for this generated file go to
5  * Window - Preferences - Java - Code Style - Code Templates
6  */
7 package org.vamsas.objects.utils;
8
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;
14
15 import org.vamsas.objects.core.AlignmentSequence;
16 import org.vamsas.objects.core.Sequence;
17 import org.vamsas.objects.core.SequenceType;
18
19 /**
20  * @author jimp
21  *
22  * TODO To change the template for this generated type comment go to
23  * Window - Preferences - Java - Code Style - Code Templates
24  */
25 public class Seq {
26
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)
32                         if (j+width<k)
33                                 pir_out.write(seq.getSequence().substring(j,j+width)+"\n");
34                         else
35                                 pir_out.write(seq.getSequence().substring(j)+"\n");
36                 pir_out.flush();
37           }
38   
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");
43                 fasta_out.flush();
44           }
45
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)
51                         if (j+width<k)
52                                 fasta_out.write(seq.getSequence().substring(j,j+width)+"\n");
53                         else
54                                 fasta_out.write(seq.getSequence().substring(j)+"\n");
55                 fasta_out.flush();
56           }
57         /**
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!)
61    * @param s
62    * @return true if a valid amino acid sequence object
63          */
64         private static boolean valid_aadictionary_string(String s) {
65     if (s==null)
66       return false;
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+");
72   }
73   
74   public static boolean is_valid_aa_seq(SequenceType s) {
75     Sequence q;
76     boolean validref=false;
77     if (s instanceof Sequence) {
78       q=(Sequence) s;
79       if (q.getDictionary()!=null 
80     
81         && q.getDictionary().length()>0
82         || !q.getDictionary().equals("info:iubmb.org/aminoacid"))
83         return false;
84       return valid_aadictionary_string(q.getSequence());
85     }
86     
87     // follow references
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());
93     }
94     
95     return false;
96   }
97 }