1 package jalview.ws.phyre2;
3 import jalview.datamodel.AlignmentI;
4 import jalview.datamodel.SequenceI;
5 import jalview.fts.core.DecimalFormatTableCellRenderer;
6 import jalview.io.DataSourceType;
7 import jalview.io.FileFormat;
8 import jalview.io.FormatAdapter;
9 import jalview.io.StructureFile;
10 import jalview.schemes.ResidueProperties;
11 import jalview.structure.StructureMapping;
12 import jalview.structure.StructureMappingClient;
13 import jalview.structures.models.MappingOutputModel;
14 import jalview.util.Comparison;
15 import jalview.util.Format;
17 import java.io.BufferedReader;
19 import java.io.FileReader;
20 import java.io.IOException;
21 import java.io.PrintStream;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
26 import javax.swing.JTable;
27 import javax.swing.table.DefaultTableModel;
29 public class Phyre2Client extends StructureMappingClient
31 private final static String NEWLINE = System.lineSeparator();
33 public static final int UNASSIGNED = -1;
35 private final static String PATH_SEPARATOR = File.separator;
37 private String fastaMappingFile;
39 public Phyre2Client(StructureFile structureFile)
41 this.structureFile = structureFile;
44 public StructureMapping getStructureMapping(SequenceI seq,
45 String pdbFile, String fMappingFile, String chain)
47 this.fastaMappingFile = fMappingFile;
48 return getStructureMapping(seq, pdbFile, chain);
52 public StructureMapping getStructureMapping(SequenceI seq,
53 String pdbFile, String chain)
55 final StringBuilder mappingDetails = new StringBuilder(128);
56 PrintStream ps = new PrintStream(System.out)
59 public void print(String x)
61 mappingDetails.append(x);
67 mappingDetails.append(NEWLINE);
70 HashMap<Integer, int[]> mapping = getPhyre2FastaMapping(seq, ps);
72 String mappingOutput = mappingDetails.toString();
73 StructureMapping phyre2ModelMapping = new StructureMapping(seq,
74 pdbFile, structureFile.getId(), chain, mapping, mappingOutput);
75 return phyre2ModelMapping;
78 public HashMap<Integer, int[]> getPhyre2FastaMapping(SequenceI inputSeq,
79 java.io.PrintStream os)
81 HashMap<Integer, int[]> mapping = new HashMap<Integer, int[]>();
82 AlignmentI seq2Phyre2ModelFastaMapping = null;
85 seq2Phyre2ModelFastaMapping = new FormatAdapter().readFile(
86 getFastaMappingFile(), DataSourceType.FILE, FileFormat.Fasta);
87 } catch (IOException e1)
91 SequenceI[] seqs = seq2Phyre2ModelFastaMapping.getSequencesArray();
92 SequenceI tSequenceRes = seqs[0];
93 SequenceI tStructureRes = seqs[1];
95 // Essential to resolve fastaAlignment to input sequence and model sequence
97 tSequenceRes.setStart(inputSeq.getStart());
98 tSequenceRes.setEnd(inputSeq.getEnd());
100 tStructureRes.setStart(structureFile.getSeqsAsArray()[0].getStart());
101 tStructureRes.setEnd(structureFile.getSeqsAsArray()[0].getEnd());
104 int sequenceResLenght = tSequenceRes.getLength();
105 int structureResLenght = tStructureRes.getLength();
106 if (sequenceResLenght == structureResLenght)
108 int prevStructResNum = -1;
109 int alignmentLenght = sequenceResLenght + tSequenceRes.getStart();
110 for (int x = 0; x < alignmentLenght; x++)
112 int alignSeqResidueIndex = tSequenceRes.findIndex(x);
113 int structResNum = tStructureRes
114 .findPosition(alignSeqResidueIndex);
115 int sequenceResNum = tSequenceRes
116 .findPosition(alignSeqResidueIndex - 1);
117 boolean sameResNum = (structResNum == prevStructResNum);
118 // System.out.println(sequenceResNum + " : "
119 // + (sameResNum ? -1 : prevStructResNum));
120 mapping.put(sequenceResNum, new int[] {
121 sameResNum ? -1 : prevStructResNum, -1 });
122 prevStructResNum = structResNum;
125 } catch (Exception e)
132 populateAtomPositions(" ", mapping);
133 } catch (IllegalArgumentException e)
136 } catch (StructureMappingException e)
143 MappingOutputModel mop = new MappingOutputModel();
144 mop.setSeqStart(tSequenceRes.getStart());
145 mop.setSeqEnd(tSequenceRes.getEnd());
146 mop.setSeqName(tSequenceRes.getName());
147 mop.setSeqResidue(tSequenceRes.getSequenceAsString());
149 mop.setStrStart(tStructureRes.getStart());
150 mop.setStrEnd(tStructureRes.getEnd());
151 mop.setStrName(tStructureRes.getName());
152 mop.setStrResidue(tStructureRes.getSequenceAsString());
157 os.print(getMappingOutput(mop).toString());
158 } catch (Exception e)
167 private String getFastaMappingFile()
169 return fastaMappingFile;
173 public StringBuffer getMappingOutput(MappingOutputModel mp)
174 throws StructureMappingException
176 String seqRes = mp.getSeqResidue();
177 String seqName = mp.getSeqName();
178 int sStart = mp.getSeqStart();
179 int sEnd = mp.getSeqEnd();
181 String strRes = mp.getStrResidue();
182 String strName = mp.getStrName();
183 int pdbStart = mp.getStrStart();
184 int pdbEnd = mp.getStrEnd();
186 String type = mp.getType();
188 int maxid = (seqName.length() >= strName.length()) ? seqName.length()
190 int len = 72 - maxid - 1;
192 int nochunks = ((seqRes.length()) / len)
193 + ((seqRes.length()) % len > 0 ? 1 : 0);
195 StringBuffer output = new StringBuffer();
196 output.append(NEWLINE);
197 output.append("Sequence \u27f7 Structure mapping details").append(
199 output.append("Method: Phyre2 Alignment");
200 output.append(NEWLINE).append(NEWLINE);
202 output.append(new Format("%" + maxid + "s").form(seqName));
203 output.append(" : ");
204 output.append(String.valueOf(sStart));
205 output.append(" - ");
206 output.append(String.valueOf(sEnd));
207 output.append(" Maps to ");
208 output.append(NEWLINE);
209 output.append(new Format("%" + maxid + "s").form(strName));
210 output.append(" : ");
211 output.append(String.valueOf(pdbStart));
212 output.append(" - ");
213 output.append(String.valueOf(pdbEnd));
214 output.append(NEWLINE).append(NEWLINE);
216 int matchedSeqCount = 0;
217 for (int j = 0; j < nochunks; j++)
219 // Print the first aligned sequence
220 output.append(new Format("%" + (maxid) + "s").form(seqName)).append(
223 for (int i = 0; i < len; i++)
225 if ((i + (j * len)) < seqRes.length())
227 output.append(seqRes.charAt(i + (j * len)));
231 output.append(NEWLINE);
232 output.append(new Format("%" + (maxid) + "s").form(" ")).append(" ");
234 // Print out the matching chars
235 for (int i = 0; i < len; i++)
239 if ((i + (j * len)) < seqRes.length())
241 boolean sameChar = Comparison.isSameResidue(
242 seqRes.charAt(i + (j * len)),
243 strRes.charAt(i + (j * len)), false);
245 && !jalview.util.Comparison.isGap(seqRes.charAt(i
251 else if (type.equals("pep"))
253 if (ResidueProperties.getPAM250(seqRes.charAt(i + (j * len)),
254 strRes.charAt(i + (j * len))) > 0)
268 } catch (IndexOutOfBoundsException e)
273 // Now print the second aligned sequence
274 output = output.append(NEWLINE);
275 output = output.append(new Format("%" + (maxid) + "s").form(strName))
277 for (int i = 0; i < len; i++)
279 if ((i + (j * len)) < strRes.length())
281 output.append(strRes.charAt(i + (j * len)));
284 output.append(NEWLINE).append(NEWLINE);
286 float pid = (float) matchedSeqCount / seqRes.length() * 100;
287 output.append("Length of alignment = " + seqRes.length()).append(
289 output.append(new Format("Percentage ID = %2.2f").form(pid));
294 public static List<Phyre2SummaryPojo> parsePhyreCrudeList(String crudeList)
296 List<Phyre2SummaryPojo> phyre2Results = new ArrayList<Phyre2SummaryPojo>();
297 try (BufferedReader br = new BufferedReader(new FileReader(crudeList)))
300 while ((line = br.readLine()) != null)
302 String[] lineData = line.split(" ");
303 Phyre2SummaryPojo psp = new Phyre2SummaryPojo();
304 psp.setSerialNo(Integer.valueOf(lineData[0]));
305 psp.setTemplateId(lineData[1]);
306 psp.setConfidence(100 * Double.valueOf(lineData[2]));
307 psp.setPid(Integer.valueOf(lineData[3]));
308 psp.setAlignedRange(lineData[4] + " - " + lineData[5]);
309 // psp.setCoverage(coverage);
310 // psp.setTemplateSummary(templateSummary);
311 phyre2Results.add(psp);
313 } catch (Exception e)
317 return phyre2Results;
320 public static DefaultTableModel getTableModel(
321 List<Phyre2SummaryPojo> phyreResults)
323 if (phyreResults == null)
327 DefaultTableModel tableModel = new DefaultTableModel()
330 public boolean isCellEditable(int row, int column)
336 public Class<?> getColumnClass(int columnIndex)
341 return Integer.class;
351 return Integer.class;
361 tableModel.addColumn("#");
362 tableModel.addColumn("Template");
363 tableModel.addColumn("Aligned Range");
364 tableModel.addColumn("Coverage");
365 tableModel.addColumn("Confidence");
366 tableModel.addColumn("%.i.d");
367 tableModel.addColumn("Template Information");
369 for (Phyre2SummaryPojo res : phyreResults)
371 tableModel.addRow(new Object[] { res.getSerialNo(),
372 res.getTemplateId(), res.getAlignedRange(), res.getCoverage(),
373 res.getConfidence(), res.getPid(), res.getTemplateSummary() });
378 public static void configurePhyreResultTable(JTable phyreResultTable)
381 DecimalFormatTableCellRenderer idCellRender = new DecimalFormatTableCellRenderer(
383 DecimalFormatTableCellRenderer pidCellRender = new DecimalFormatTableCellRenderer(
385 DecimalFormatTableCellRenderer confidenceCellRender = new DecimalFormatTableCellRenderer(
388 phyreResultTable.getColumn("#").setMinWidth(20);
389 phyreResultTable.getColumn("#").setPreferredWidth(30);
390 phyreResultTable.getColumn("#").setMaxWidth(40);
391 phyreResultTable.getColumn("#").setCellRenderer(idCellRender);
393 phyreResultTable.getColumn("Template").setMinWidth(60);
394 phyreResultTable.getColumn("Template").setPreferredWidth(90);
395 phyreResultTable.getColumn("Template").setMaxWidth(150);
397 phyreResultTable.getColumn("Aligned Range").setMinWidth(80);
398 phyreResultTable.getColumn("Aligned Range").setPreferredWidth(80);
399 phyreResultTable.getColumn("Aligned Range").setMaxWidth(120);
401 phyreResultTable.getColumn("Coverage").setMinWidth(60);
402 phyreResultTable.getColumn("Coverage").setPreferredWidth(60);
403 phyreResultTable.getColumn("Coverage").setMaxWidth(90);
405 phyreResultTable.getColumn("Confidence").setMinWidth(60);
406 phyreResultTable.getColumn("Confidence").setPreferredWidth(60);
407 phyreResultTable.getColumn("Confidence").setMaxWidth(90);
408 phyreResultTable.getColumn("Confidence").setCellRenderer(
409 confidenceCellRender);
411 phyreResultTable.getColumn("%.i.d").setMinWidth(45);
412 phyreResultTable.getColumn("%.i.d").setPreferredWidth(450);
413 phyreResultTable.getColumn("%.i.d").setMaxWidth(65);
414 phyreResultTable.getColumn("%.i.d").setCellRenderer(pidCellRender);
416 phyreResultTable.getColumn("Template Information").setMinWidth(400);
417 phyreResultTable.getColumn("Template Information").setPreferredWidth(
419 phyreResultTable.getColumn("Template Information").setMaxWidth(1500);