Clover icon

Coverage Report

  1. Project Clover database Wed Nov 6 2024 14:47:21 GMT
  2. Package jalview.io

File FastaFile.java

 

Coverage histogram

../../img/srcFileCovDistChart7.png
29% 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.621621662.2%
 

Contributing tests

This file is covered by 191 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  96 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  401 toggle public FastaFile(FileParse source) throws IOException
71    {
72  401 this(source, true);
73    }
74   
 
75  405 toggle public FastaFile(FileParse source, boolean closeData) throws IOException
76    {
77  405 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  405 toggle @Override
92    public void parse() throws IOException
93    {
94  405 StringBuffer sb = new StringBuffer();
95  405 boolean firstLine = true;
96   
97  405 String line, uline;
98  405 Sequence seq = null;
99   
100  405 boolean annotation = false;
101   
102  ? while ((uline = nextLine()) != null)
103    {
104  11858 line = uline.trim();
105  11858 if (line.length() > 0)
106    {
107  11858 if (line.charAt(0) == '>')
108    {
109  3120 if (line.startsWith(">#_"))
110    {
111  0 if (annotation)
112    {
113  0 annotations.addElement(makeAnnotation(seq, sb));
114    }
115    }
116    else
117    {
118  3120 annotation = false;
119    }
120   
121  3120 if (!firstLine)
122    {
123  2715 seq.setSequence(sb.toString());
124   
125  2715 if (!annotation)
126    {
127  2715 seqs.addElement(seq);
128    }
129    }
130   
131  3120 seq = parseId(line.substring(1));
132  3120 firstLine = false;
133   
134  3120 sb = new StringBuffer();
135   
136  3120 if (line.startsWith(">#_"))
137    {
138  0 annotation = true;
139    }
140    }
141    else
142    {
143  8738 sb.append(annotation ? uline : line);
144    }
145    }
146    }
147   
148  405 if (annotation)
149    {
150  0 annotations.addElement(makeAnnotation(seq, sb));
151    }
152   
153  405 else if (!firstLine)
154    {
155  405 seq.setSequence(sb.toString());
156  405 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  96 toggle @Override
195    public String print(SequenceI[] s, boolean jvsuffix)
196    {
197  96 out = new StringBuffer();
198  96 int i = 0;
199   
200  2063 while ((i < s.length) && (s[i] != null))
201    {
202  1967 out.append(">" + printId(s[i], jvsuffix));
203  1967 if (s[i].getDescription() != null)
204    {
205  190 out.append(" " + s[i].getDescription());
206    }
207   
208  1967 out.append(newline);
209   
210  1967 int nochunks = (s[i].getLength() / len)
211  1967 + (s[i].getLength() % len > 0 ? 1 : 0);
212   
213  6526 for (int j = 0; j < nochunks; j++)
214    {
215  4559 int start = j * len;
216  4559 int end = start + len;
217   
218  4559 if (end < s[i].getLength())
219    {
220  2592 out.append(s[i].getSequenceAsString(start, end) + newline);
221    }
222  1967 else if (start < s[i].getLength())
223    {
224  1967 out.append(s[i].getSequenceAsString(start, s[i].getLength())
225    + newline);
226    }
227    }
228   
229  1967 i++;
230    }
231   
232  96 return out.toString();
233    }
234    }