Clover icon

Coverage Report

  1. Project Clover database Mon Nov 18 2024 09:38:20 GMT
  2. Package jalview.io

File FastaFile.java

 

Coverage histogram

../../img/srcFileCovDistChart7.png
21% of files have more coverage

Code metrics

40
62
9
1
234
149
30
0.48
6.89
9
3.33

Classes

Class Line # Actions
FastaFile 37 62 30
0.630630663.1%
 

Contributing tests

This file is covered by 112 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
7    * Jalview is free software: you can redistribute it and/or
8    * modify it under the terms of the GNU General Public License
9    * as published by the Free Software Foundation, either version 3
10    * of the License, or (at your option) any later version.
11    *
12    * Jalview is distributed in the hope that it will be useful, but
13    * WITHOUT ANY WARRANTY; without even the implied warranty
14    * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15    * PURPOSE. See the GNU General Public License for more details.
16    *
17    * You should have received a copy of the GNU General Public License
18    * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19    * The Jalview Authors are detailed in the 'AUTHORS' file.
20    */
21    package jalview.io;
22   
23    import java.io.IOException;
24   
25    import jalview.datamodel.Alignment;
26    import jalview.datamodel.AlignmentAnnotation;
27    import jalview.datamodel.Annotation;
28    import jalview.datamodel.Sequence;
29    import jalview.datamodel.SequenceI;
30   
31    /**
32    * DOCUMENT ME!
33    *
34    * @author $author$
35    * @version $Revision$
36    */
 
37    public class FastaFile extends AlignFile
38    {
39    /**
40    * Length of a sequence line
41    */
42    int len = 72;
43   
44    StringBuffer out;
45   
46    /**
47    * Creates a new FastaFile object.
48    */
 
49  20 toggle public FastaFile()
50    {
51    }
52   
53    /**
54    * Creates a new FastaFile object.
55    *
56    * @param inFile
57    * DOCUMENT ME!
58    * @param sourceType
59    * DOCUMENT ME!
60    *
61    * @throws IOException
62    * DOCUMENT ME!
63    */
 
64  0 toggle public FastaFile(String inFile, DataSourceType sourceType)
65    throws IOException
66    {
67  0 super(inFile, sourceType);
68    }
69   
 
70  256 toggle public FastaFile(FileParse source) throws IOException
71    {
72  256 this(source, true);
73    }
74   
 
75  256 toggle public FastaFile(FileParse source, boolean closeData) throws IOException
76    {
77  256 super(true, source, closeData);
78    }
79   
 
80  0 toggle public FastaFile(SequenceI[] seqs)
81    {
82  0 super(seqs);
83    }
84   
85    /**
86    * DOCUMENT ME!
87    *
88    * @throws IOException
89    * DOCUMENT ME!
90    */
 
91  256 toggle @Override
92    public void parse() throws IOException
93    {
94  256 StringBuffer sb = new StringBuffer();
95  256 boolean firstLine = true;
96   
97  256 String line, uline;
98  256 Sequence seq = null;
99   
100  256 boolean annotation = false;
101   
102  ? while ((uline = nextLine()) != null)
103    {
104  4860 line = uline.trim();
105  4860 if (line.length() > 0)
106    {
107  4859 if (line.charAt(0) == '>')
108    {
109  1392 if (line.startsWith(">#_"))
110    {
111  0 if (annotation)
112    {
113  0 annotations.addElement(makeAnnotation(seq, sb));
114    }
115    }
116    else
117    {
118  1392 annotation = false;
119    }
120   
121  1392 if (!firstLine)
122    {
123  1136 seq.setSequence(sb.toString());
124   
125  1136 if (!annotation)
126    {
127  1136 seqs.addElement(seq);
128    }
129    }
130   
131  1392 seq = parseId(line.substring(1));
132  1392 firstLine = false;
133   
134  1392 sb = new StringBuffer();
135   
136  1392 if (line.startsWith(">#_"))
137    {
138  0 annotation = true;
139    }
140    }
141    else
142    {
143  3467 sb.append(annotation ? uline : line);
144    }
145    }
146    }
147   
148  256 if (annotation)
149    {
150  0 annotations.addElement(makeAnnotation(seq, sb));
151    }
152   
153  256 else if (!firstLine)
154    {
155  256 seq.setSequence(sb.toString());
156  256 seqs.addElement(seq);
157    }
158    }
159   
 
160  0 toggle private AlignmentAnnotation makeAnnotation(SequenceI seq, StringBuffer sb)
161    {
162  0 Annotation[] anots = new Annotation[sb.length()];
163  0 char cb;
164  0 for (int i = 0; i < anots.length; i++)
165    {
166  0 char cn = sb.charAt(i);
167  0 if (cn != ' ')
168    {
169  0 anots[i] = new Annotation("" + cn, null, ' ', Float.NaN);
170    }
171    }
172  0 AlignmentAnnotation aa = new AlignmentAnnotation(
173    seq.getName().substring(2), seq.getDescription(), anots);
174  0 return aa;
175    }
176   
177    /**
178    * called by AppletFormatAdapter to generate an annotated alignment, rather
179    * than bare sequences.
180    *
181    * @param al
182    */
 
183  0 toggle public void addAnnotations(Alignment al)
184    {
185  0 addProperties(al);
186  0 for (int i = 0; i < annotations.size(); i++)
187    {
188  0 AlignmentAnnotation aa = annotations.elementAt(i);
189  0 aa.setPadGaps(true, al.getGapCharacter());
190  0 al.addAnnotation(aa);
191    }
192    }
193   
 
194  20 toggle @Override
195    public String print(SequenceI[] s, boolean jvsuffix)
196    {
197  20 out = new StringBuffer();
198  20 int i = 0;
199   
200  305 while ((i < s.length) && (s[i] != null))
201    {
202  285 out.append(">" + printId(s[i], jvsuffix));
203  285 if (s[i].getDescription() != null)
204    {
205  77 out.append(" " + s[i].getDescription());
206    }
207   
208  285 out.append(newline);
209   
210  285 int nochunks = (s[i].getLength() / len)
211  285 + (s[i].getLength() % len > 0 ? 1 : 0);
212   
213  852 for (int j = 0; j < nochunks; j++)
214    {
215  567 int start = j * len;
216  567 int end = start + len;
217   
218  567 if (end < s[i].getLength())
219    {
220  282 out.append(s[i].getSequenceAsString(start, end) + newline);
221    }
222  285 else if (start < s[i].getLength())
223    {
224  285 out.append(s[i].getSequenceAsString(start, s[i].getLength())
225    + newline);
226    }
227    }
228   
229  285 i++;
230    }
231   
232  20 return out.toString();
233    }
234    }