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