Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.io

File MSFfile.java

 

Coverage histogram

../../img/srcFileCovDistChart9.png
12% of files have more coverage

Code metrics

70
137
6
1
412
273
49
0.36
22.83
6
8.17

Classes

Class Line # Actions
MSFfile 40 137 49 22
0.896713689.7%
 

Contributing tests

This file is covered by 2 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 jalview.datamodel.Sequence;
24    import jalview.datamodel.SequenceI;
25    import jalview.util.Comparison;
26    import jalview.util.Format;
27   
28    import java.io.IOException;
29    import java.util.ArrayList;
30    import java.util.Hashtable;
31    import java.util.List;
32    import java.util.StringTokenizer;
33   
34    /**
35    * DOCUMENT ME!
36    *
37    * @author $author$
38    * @version $Revision$
39    */
 
40    public class MSFfile extends AlignFile
41    {
42   
43    /**
44    * Creates a new MSFfile object.
45    */
 
46  2 toggle public MSFfile()
47    {
48    }
49   
50    /**
51    * Creates a new MSFfile object.
52    *
53    * @param inFile
54    * DOCUMENT ME!
55    * @param type
56    * DOCUMENT ME!
57    *
58    * @throws IOException
59    * DOCUMENT ME!
60    */
 
61  0 toggle public MSFfile(String inFile, DataSourceType type) throws IOException
62    {
63  0 super(inFile, type);
64    }
65   
 
66  2 toggle public MSFfile(FileParse source) throws IOException
67    {
68  2 super(source);
69    }
70   
71    /**
72    * Read and parse MSF sequence data
73    */
 
74  2 toggle @Override
75    public void parse() throws IOException
76    {
77  2 boolean seqFlag = false;
78  2 List<String> headers = new ArrayList<String>();
79  2 Hashtable<String, StringBuilder> seqhash = new Hashtable<String, StringBuilder>();
80   
81  2 try
82    {
83  2 String line;
84  ? while ((line = nextLine()) != null)
85    {
86  176 StringTokenizer str = new StringTokenizer(line);
87   
88  176 String key = null;
89  540 while (str.hasMoreTokens())
90    {
91  364 String inStr = str.nextToken();
92   
93    // If line has header information add to the headers vector
94  364 if (inStr.indexOf("Name:") != -1)
95    {
96  30 key = str.nextToken();
97  30 headers.add(key);
98    }
99   
100    // if line has // set SeqFlag so we know sequences are coming
101  364 if (inStr.indexOf("//") != -1)
102    {
103  2 seqFlag = true;
104    }
105   
106    // Process lines as sequence lines if seqFlag is set
107  364 if ((inStr.indexOf("//") == -1) && seqFlag)
108    {
109    // sequence id is the first field
110  120 key = inStr;
111   
112  120 StringBuilder tempseq;
113   
114    // Get sequence from hash if it exists
115  120 if (seqhash.containsKey(key))
116    {
117  90 tempseq = seqhash.get(key);
118    }
119    else
120    {
121  30 tempseq = new StringBuilder(64);
122  30 seqhash.put(key, tempseq);
123    }
124   
125    // loop through the rest of the words
126  600 while (str.hasMoreTokens())
127    {
128    // append the word to the sequence
129  480 String sequenceBlock = str.nextToken();
130  480 tempseq.append(sequenceBlock);
131    }
132    }
133    }
134    }
135    } catch (IOException e)
136    {
137  0 System.err.println("Exception parsing MSFFile " + e);
138  0 e.printStackTrace();
139    }
140   
141  2 this.noSeqs = headers.size();
142   
143    // Add sequences to the hash
144  32 for (int i = 0; i < headers.size(); i++)
145    {
146  30 if (seqhash.get(headers.get(i)) != null)
147    {
148  30 String head = headers.get(i);
149  30 String seq = seqhash.get(head).toString();
150   
151  30 if (maxLength < head.length())
152    {
153  6 maxLength = head.length();
154    }
155   
156    /*
157    * replace ~ (leading/trailing positions) with the gap character;
158    * use '.' as this is the internal gap character required by MSF
159    */
160  30 seq = seq.replace('~', '.');
161   
162  30 Sequence newSeq = parseId(head);
163   
164  30 newSeq.setSequence(seq);
165   
166  30 seqs.addElement(newSeq);
167    }
168    else
169    {
170  0 System.err.println("MSFFile Parser: Can't find sequence for "
171    + headers.get(i));
172    }
173    }
174    }
175   
176    /**
177    * DOCUMENT ME!
178    *
179    * @param seq
180    * DOCUMENT ME!
181    *
182    * @return DOCUMENT ME!
183    */
 
184  30 toggle public int checkSum(String seq)
185    {
186  30 int check = 0;
187  30 String sequence = seq.toUpperCase();
188   
189  4740 for (int i = 0; i < sequence.length(); i++)
190    {
191  4710 try
192    {
193   
194  4710 int value = sequence.charAt(i);
195  4710 if (value != -1)
196    {
197  4710 check += (i % 57 + 1) * value;
198    }
199    } catch (Exception e)
200    {
201  0 System.err.println("Exception during MSF Checksum calculation");
202  0 e.printStackTrace();
203    }
204    }
205   
206  30 return check % 10000;
207    }
208   
209    /**
210    * DOCUMENT ME!
211    *
212    * @param s
213    * DOCUMENT ME!
214    * @param is_NA
215    * DOCUMENT ME!
216    *
217    * @return DOCUMENT ME!
218    */
 
219  1 toggle @Override
220    public String print(SequenceI[] sqs, boolean jvSuffix)
221    {
222   
223  1 boolean is_NA = Comparison.isNucleotide(sqs);
224   
225  1 SequenceI[] s = new SequenceI[sqs.length];
226   
227  1 StringBuilder out = new StringBuilder(256);
228  1 out.append("!!").append(is_NA ? "NA" : "AA")
229    .append("_MULTIPLE_ALIGNMENT 1.0");
230    // TODO: JBPNote : Jalview doesn't remember NA or AA yet.
231  1 out.append(newline);
232  1 out.append(newline);
233  1 int max = 0;
234  1 int maxid = 0;
235  1 int i = 0;
236   
237  16 while ((i < sqs.length) && (sqs[i] != null))
238    {
239    /*
240    * modify to MSF format: uses '.' for internal gaps,
241    * and '~' for leading or trailing gaps
242    */
243  15 String seqString = sqs[i].getSequenceAsString().replace('-', '.');
244   
245  15 StringBuilder sb = new StringBuilder(seqString);
246   
247  192 for (int ii = 0; ii < sb.length(); ii++)
248    {
249  192 if (sb.charAt(ii) == '.')
250    {
251  177 sb.setCharAt(ii, '~');
252    }
253    else
254    {
255  15 break;
256    }
257    }
258   
259  65 for (int ii = sb.length() - 1; ii > 0; ii--)
260    {
261  65 if (sb.charAt(ii) == '.')
262    {
263  50 sb.setCharAt(ii, '~');
264    }
265    else
266    {
267  15 break;
268    }
269    }
270  15 s[i] = new Sequence(sqs[i].getName(), sb.toString(),
271    sqs[i].getStart(), sqs[i].getEnd());
272   
273  15 if (sb.length() > max)
274    {
275  1 max = sb.length();
276    }
277   
278  15 i++;
279    }
280   
281  1 Format maxLenpad = new Format(
282    "%" + (new String("" + max)).length() + "d");
283  1 Format maxChkpad = new Format(
284    "%" + (new String("1" + max)).length() + "d");
285  1 i = 0;
286   
287  1 int bigChecksum = 0;
288  1 int[] checksums = new int[s.length];
289  16 while (i < s.length)
290    {
291  15 checksums[i] = checkSum(s[i].getSequenceAsString());
292  15 bigChecksum += checksums[i];
293  15 i++;
294    }
295   
296  1 long maxNB = 0;
297  1 out.append(" MSF: " + s[0].getLength() + " Type: "
298  1 + (is_NA ? "N" : "P") + " Check: " + (bigChecksum % 10000)
299    + " ..");
300  1 out.append(newline);
301  1 out.append(newline);
302  1 out.append(newline);
303   
304  1 String[] nameBlock = new String[s.length];
305  1 String[] idBlock = new String[s.length];
306   
307  1 i = 0;
308  16 while ((i < s.length) && (s[i] != null))
309    {
310   
311  15 nameBlock[i] = new String(" Name: " + printId(s[i], jvSuffix) + " ");
312   
313  15 idBlock[i] = new String("Len: " + maxLenpad.form(s[i].getLength())
314    + " Check: " + maxChkpad.form(checksums[i])
315    + " Weight: 1.00" + newline);
316   
317  15 if (s[i].getName().length() > maxid)
318    {
319  3 maxid = s[i].getName().length();
320    }
321   
322  15 if (nameBlock[i].length() > maxNB)
323    {
324  3 maxNB = nameBlock[i].length();
325    }
326   
327  15 i++;
328    }
329   
330  1 if (maxid < 10)
331    {
332  0 maxid = 10;
333    }
334   
335  1 if (maxNB < 15)
336    {
337  0 maxNB = 15;
338    }
339   
340  1 Format nbFormat = new Format("%-" + maxNB + "s");
341   
342  16 for (i = 0; (i < s.length) && (s[i] != null); i++)
343    {
344  15 out.append(nbFormat.form(nameBlock[i]) + idBlock[i]);
345    }
346   
347  1 maxid++;
348  1 out.append(newline);
349  1 out.append(newline);
350  1 out.append("//");
351  1 out.append(newline);
352  1 out.append(newline);
353  1 int len = 50;
354   
355  1 int nochunks = (max / len) + (max % len > 0 ? 1 : 0);
356   
357  5 for (i = 0; i < nochunks; i++)
358    {
359  4 int j = 0;
360   
361  64 while ((j < s.length) && (s[j] != null))
362    {
363  60 String name = printId(s[j], jvSuffix);
364   
365  60 out.append(new Format("%-" + maxid + "s").form(name + " "));
366   
367  360 for (int k = 0; k < 5; k++)
368    {
369  300 int start = (i * 50) + (k * 10);
370  300 int end = start + 10;
371   
372  300 int length = s[j].getLength();
373  300 if ((end < length)
374    && (start < length))
375    {
376  225 out.append(s[j].getSequence(start, end));
377   
378  225 if (k < 4)
379    {
380  180 out.append(" ");
381    }
382    else
383    {
384  45 out.append(newline);
385    }
386    }
387    else
388    {
389  75 if (start < length)
390    {
391  15 out.append(s[j].getSequenceAsString().substring(start));
392  15 out.append(newline);
393    }
394    else
395    {
396  60 if (k == 0)
397    {
398  0 out.append(newline);
399    }
400    }
401    }
402    }
403   
404  60 j++;
405    }
406   
407  4 out.append(newline);
408    }
409   
410  1 return out.toString();
411    }
412    }