de029392260b7c9c3d3c5505d27dc094331448a8
[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   protected static final String CONTACT_PREDICTION = "CONTACT_PREDICTION";
48
49   public PContactPredictionFile(String inFile, DataSourceType fileSourceType)
50           throws IOException
51   {
52     super(inFile, fileSourceType);
53
54   }
55
56   public PContactPredictionFile(FileParse source) throws IOException
57   {
58     super(source);
59   }
60
61   Integer fWidth;
62
63   List<ContactMatrix> models = new ArrayList<ContactMatrix>();
64
65   public List<ContactMatrix> getContactMatrices()
66   {
67     return models;
68   }
69
70   /*
71    * RaptorX pattern:
72    * for a contact prediction
73    * Target sequence
74    * alignment for target sequence
75    * contact matrix for sequence
76    * models generated that fit matrix
77    */
78
79   /*
80    * TODO: create annotation rows from contact matrices.
81    * (non-Javadoc)
82    * @see jalview.io.AlignFile#parse()
83    */
84   @Override
85   public void parse() throws IOException
86   {
87     String line;
88     /*
89      * stash any header lines if we've been given a CASP-RR file 
90      */
91     Map<String, String> header = new HashMap<String, String>();
92     ContactMatrix cm = null;
93
94     while ((line = nextLine()) != null)
95     {
96       int left, right;
97       double strength = Float.NaN;
98       String parts[] = line.split("\\s+");
99
100       // check for header tokens in parts[0]
101
102       // others - stash details
103       // MODEL - start a new matrix
104       // skip comments ?
105
106       if (parts.length == 3) // and all are integers
107       {
108
109         if (cm == null)
110         {
111           cm = new ContactMatrix(true) {
112             @Override
113             public String getType()
114             {
115               return CONTACT_PREDICTION;
116             }
117           };
118           models.add(cm);
119         }
120
121         try
122         {
123           left = Integer.parseInt(parts[0]);
124           right = Integer.parseInt(parts[1]);
125           strength = Double.parseDouble(parts[2]);
126         } catch (Exception x)
127         {
128           error = true;
129           errormessage = "Couldn't process line: "
130                   + x.getLocalizedMessage() + "\n" + line;
131           return;
132         }
133         cm.addContact(left, right, (float) strength);
134       }
135     }
136   }
137
138   @Override
139   public String print(SequenceI[] sqs, boolean jvsuffix)
140   {
141     // TODO Auto-generated method stub
142     return "Not valid.";
143   }
144 }