7c82d82418dae1cc99872454a5981d8e423cc59f
[vamsas.git] / src / uk / ac / 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 uk.ac.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 uk.ac.vamsas.objects.core.AlignmentSequence;
16 import uk.ac.vamsas.objects.core.Sequence;
17 import uk.ac.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 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!)
61    * @param s
62          * @param dict TODO
63    * @return true if a valid amino acid sequence Vobject
64          */
65         private static boolean valid_aadictionary_string(String s, String dict) {
66     if (s==null)
67       return false;
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+");
73   }
74
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);
79      seq.setName(Name);
80      seq.setSequence(Sequence);
81      seq.setStart(start);
82      if (start<=end) {
83        if ((end-start)!=Sequence.length())
84          seq.setEnd(start+Sequence.length());
85      } else {
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());
89      }
90      return seq;
91   }
92   public static AlignmentSequence newAlignmentSequence(String name, String alSequence, Sequence refseq, long start, long end) {
93     if (refseq!=null) {
94       AlignmentSequence asq = new AlignmentSequence();
95       asq.setName(name);
96       asq.setSequence(alSequence);
97       asq.setRefid(refseq);
98       if (end>refseq.getEnd() || end<start || end==-1)
99         end = refseq.getEnd();
100       asq.setEnd(end);
101       if (start<refseq.getStart())
102         start = refseq.getStart();
103       asq.setStart(start);
104       return asq;
105     }
106     return null;
107   }
108   public static boolean is_valid_aa_seq(SequenceType s) {
109     Sequence q;
110     boolean validref=false;
111     if (s instanceof Sequence) {
112       q=(Sequence) s;
113       if (q.getDictionary()!=null 
114     
115         && q.getDictionary().length()>0
116         || !q.getDictionary().equals(SymbolDictionary.STANDARD_AA))
117         return false;
118       return valid_aadictionary_string(q.getSequence(), SymbolDictionary.STANDARD_AA);
119     }
120     
121     // follow references
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);
127     }
128     
129     return false;
130   }
131 }