Merge branch 'features/JAL-4061_and_JAL-4062_findselectfeatures' into features/r2_11_...
[jalview.git] / src / jalview / io / PContactPredictionFile.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.ContactMatrix;
24 import jalview.datamodel.SequenceI;
25
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31
32 /**
33  * A file parser for contact prediction files.
34  * 
35  * An example file is the following
36  * 
37  * <pre>
38  * 
39  * </pre>
40  * 
41  * 
42  * @author jim procter
43  * 
44  */
45 public class PContactPredictionFile extends AlignFile
46 {
47   public PContactPredictionFile(String inFile, DataSourceType fileSourceType)
48           throws IOException
49   {
50     super(inFile, fileSourceType);
51
52   }
53
54   public PContactPredictionFile(FileParse source) throws IOException
55   {
56     super(source);
57   }
58
59   Integer fWidth;
60
61   List<ContactMatrix> models = new ArrayList<ContactMatrix>();
62
63   public List<ContactMatrix> getContactMatrices()
64   {
65     return models;
66   }
67
68   /*
69    * RaptorX pattern:
70    * for a contact prediction
71    * Target sequence
72    * alignment for target sequence
73    * contact matrix for sequence
74    * models generated that fit matrix
75    */
76
77   /*
78    * TODO: create annotation rows from contact matrices.
79    * (non-Javadoc)
80    * @see jalview.io.AlignFile#parse()
81    */
82   @Override
83   public void parse() throws IOException
84   {
85     String line;
86     /*
87      * stash any header lines if we've been given a CASP-RR file 
88      */
89     Map<String, String> header = new HashMap<String, String>();
90     ContactMatrix cm = null;
91
92     while ((line = nextLine()) != null)
93     {
94       int left, right;
95       double strength = Float.NaN;
96       String parts[] = line.split("\\s+");
97
98       // check for header tokens in parts[0]
99
100       // others - stash details
101       // MODEL - start a new matrix
102       // skip comments ?
103
104       if (parts.length == 3) // and all are integers
105       {
106
107         if (cm == null)
108         {
109           cm = new ContactMatrix(true);
110           models.add(cm);
111         }
112
113         try
114         {
115           left = Integer.parseInt(parts[0]);
116           right = Integer.parseInt(parts[1]);
117           strength = Double.parseDouble(parts[2]);
118         } catch (Exception x)
119         {
120           error = true;
121           errormessage = "Couldn't process line: "
122                   + x.getLocalizedMessage() + "\n" + line;
123           return;
124         }
125         cm.addContact(left, right, (float) strength);
126       }
127     }
128   }
129
130   @Override
131   public String print(SequenceI[] sqs, boolean jvsuffix)
132   {
133     // TODO Auto-generated method stub
134     return "Not valid.";
135   }
136 }