GPL license added
[jalview.git] / src / jalview / io / FastaFile.java
1 /*\r
2 * Jalview - A Sequence Alignment Editor and Viewer\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4 *\r
5 * This program is free software; you can redistribute it and/or\r
6 * modify it under the terms of the GNU General Public License\r
7 * as published by the Free Software Foundation; either version 2\r
8 * of the License, or (at your option) any later version.\r
9 *\r
10 * This program is distributed in the hope that it will be useful,\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 * GNU General Public License for more details.\r
14 *\r
15 * You should have received a copy of the GNU General Public License\r
16 * along with this program; if not, write to the Free Software\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18 */\r
19 \r
20 package jalview.io;\r
21 \r
22 import jalview.datamodel.*;\r
23 import jalview.analysis.*;\r
24 \r
25 import java.io.*;\r
26 import java.util.*;\r
27 \r
28 public class FastaFile extends AlignFile {\r
29 \r
30   public FastaFile()\r
31   {}\r
32 \r
33   public FastaFile(String inStr) {\r
34     super(inStr);\r
35   }\r
36 \r
37   public FastaFile(String inFile, String type) throws IOException {\r
38     super(inFile,type);\r
39   }\r
40 \r
41   public void parse() throws IOException\r
42   {\r
43 \r
44     String       id    = "";\r
45     StringBuffer seq   = new StringBuffer();\r
46     int          count = 0;\r
47     boolean      flag  = false;\r
48 \r
49     int          sstart = 0;\r
50     int          send   = 0;\r
51 \r
52     String line;\r
53 \r
54       while ((line = nextLine()) != null) {\r
55 \r
56         if (line.length() > 0) {\r
57 \r
58           // Do we have an id line?\r
59 \r
60           if (line.substring(0,1).equals(">")) {\r
61 \r
62             if (count != 0) {\r
63               if (sstart != 0) {\r
64                 seqs.addElement(new Sequence(id,seq.toString().toUpperCase(),sstart,send));\r
65               } else {\r
66                 seqs.addElement(new Sequence(id,seq.toString().toUpperCase(),1,seq.length()));\r
67               }\r
68             }\r
69 \r
70             count++;\r
71 \r
72             StringTokenizer str = new StringTokenizer(line," ");\r
73 \r
74             id = str.nextToken();\r
75             id = id.substring(1);\r
76             com.stevesoft.pat.Regex dbId = new com.stevesoft.pat.Regex("[A-Za-z-]+/[A-Za-z-]+\\|(\\w+)\\|(.+)");\r
77             if (dbId.search(id))\r
78                {\r
79                  String dbid = dbId.stringMatched(1);\r
80                  String idname = dbId.stringMatched(2);\r
81                  if (idname.length()>0 && idname.indexOf("_") > -1)\r
82                  {\r
83                    id = idname; // just use friendly name // JBPNote: we may lose uniprot standardised ID here.\r
84                  }\r
85                  else\r
86                  {\r
87                    id = dbid; // use dbid to ensure sensible queries\r
88                  }\r
89 \r
90                }\r
91             if (id.indexOf("/") > 0 ) {\r
92 \r
93               StringTokenizer st = new StringTokenizer(id,"/");\r
94               if (st.countTokens() == 2) {\r
95                 id = st.nextToken();\r
96                 String tmp = st.nextToken();\r
97 \r
98                 st = new StringTokenizer(tmp,"-");\r
99 \r
100                 if (st.countTokens() == 2) {\r
101                   sstart = Integer.valueOf(st.nextToken()).intValue();\r
102                   send   = Integer.valueOf(st.nextToken()).intValue();\r
103                 }\r
104               }\r
105             }\r
106 \r
107             seq = new StringBuffer();\r
108 \r
109           } else {\r
110             seq = seq.append(line);\r
111           }\r
112         }\r
113       }\r
114       if (count > 0) {\r
115 \r
116         if(!isValidProteinSequence(seq.toString().toUpperCase()))\r
117           throw new IOException("Invalid protein sequence");\r
118 \r
119         if (sstart != 0) {\r
120           seqs.addElement(new Sequence(id,seq.toString().toUpperCase(),sstart,send));\r
121         } else {\r
122           seqs.addElement(new Sequence(id,seq.toString().toUpperCase(),1,seq.length()));\r
123         }\r
124       }\r
125 \r
126   }\r
127 \r
128   public static String print(SequenceI[] s) {\r
129     return print(s,72);\r
130   }\r
131   public static String print(SequenceI[] s, int len) {\r
132     return print(s,len,true);\r
133   }\r
134 \r
135   public static String print(SequenceI[] s, int len,boolean gaps) {\r
136     return print(s,len,gaps,true);\r
137   }\r
138 \r
139   public static String print(SequenceI[] s, int len,boolean gaps, boolean displayId) {\r
140     StringBuffer out = new StringBuffer();\r
141     int i = 0;\r
142     while (i < s.length && s[i] != null) {\r
143       String seq = "";\r
144       if (gaps) {\r
145         seq = s[i].getSequence();\r
146       } else {\r
147         seq = AlignSeq.extractGaps("-. ",s[i].getSequence());\r
148       }\r
149       // used to always put this here: + "/" + s[i].getStart() + "-" + s[i].getEnd() +\r
150       out.append(">" + ((displayId) ? s[i].getDisplayId() : s[i].getName())+"\n");\r
151 \r
152       int nochunks = seq.length() / len + 1;\r
153 \r
154       for (int j = 0; j < nochunks; j++) {\r
155         int start = j*len;\r
156         int end = start + len;\r
157 \r
158         if (end < seq.length()) {\r
159           out.append(seq.substring(start,end) + "\n");\r
160         } else if (start < seq.length()) {\r
161           out.append(seq.substring(start) + "\n");\r
162         }\r
163       }\r
164       i++;\r
165     }\r
166     return out.toString();\r
167   }\r
168 \r
169   public String print() {\r
170     return print(getSeqsAsArray());\r
171   }\r
172 }\r
173 \r
174 \r
175 \r