Merge branch 'develop' into features/filetypeEnum
[jalview.git] / src / jalview / io / AMSAFile.java
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.AlignmentAnnotation;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.SequenceI;
26
27 public class AMSAFile extends jalview.io.FastaFile
28 {
29
30   AlignmentI al;
31
32   /**
33    * Creates a new AMSAFile object for output.
34    */
35   public AMSAFile(AlignmentI al)
36   {
37     this.al = al;
38   }
39
40   /**
41    * DOCUMENT ME!
42    * 
43    * @return DOCUMENT ME!
44    */
45   public String print(SequenceI[] sqs, boolean jvsuffix)
46   {
47     super.print(sqs, jvsuffix);
48
49     AlignmentAnnotation aa;
50     if (al.getAlignmentAnnotation() != null)
51     {
52
53       for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
54       {
55         aa = al.getAlignmentAnnotation()[i];
56
57         if (aa.autoCalculated || !aa.visible)
58         {
59           continue;
60         }
61
62         out.append(">#_" + aa.label);
63         if (aa.description != null)
64         {
65           out.append(" " + aa.description);
66         }
67
68         out.append(newline);
69
70         int nochunks = Math.min(aa.annotations.length, al.getWidth()) / len
71                 + 1;
72
73         for (int j = 0; j < nochunks; j++)
74         {
75           int start = j * len;
76           int end = start + len;
77           if (end > aa.annotations.length)
78           {
79             end = aa.annotations.length;
80           }
81
82           String ch;
83           for (int k = start; k < end; k++)
84           {
85             if (aa.annotations[k] == null)
86             {
87               ch = " ";
88             }
89             else
90             {
91               ch = aa.annotations[k].displayCharacter;
92             }
93             if (ch.length() > 1)
94             {
95               this.warningMessage = "Truncated column annotation to first letter.";
96               ch = ch.substring(0, 1);
97             }
98             out.append(ch);
99
100           }
101           out.append(newline);
102         }
103       }
104     }
105     return out.toString();
106   }
107 }