Clover icon

Coverage Report

  1. Project Clover database Thu Nov 7 2024 17:01:39 GMT
  2. Package jalview.io

File FastaFile.java

 

Coverage histogram

../../img/srcFileCovDistChart0.png
0% 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.00%
 

Contributing tests

No tests hitting this source file were found.

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