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 public Phyre2Client(StructureFile structureFile)
39 this.structureFile = structureFile;
43 public StructureMapping getStructureMapping(SequenceI seq,
44 String pdbFile, String chain)
46 final StringBuilder mappingDetails = new StringBuilder(128);
47 PrintStream ps = new PrintStream(System.out)
50 public void print(String x)
52 mappingDetails.append(x);
58 mappingDetails.append(NEWLINE);
61 HashMap<Integer, int[]> mapping = getPhyre2FastaMapping(seq, ps);
63 String mappingOutput = mappingDetails.toString();
64 StructureMapping phyre2ModelMapping = new StructureMapping(seq,
65 pdbFile, structureFile.getId(), chain, mapping, mappingOutput);
66 return phyre2ModelMapping;
69 public HashMap<Integer, int[]> getPhyre2FastaMapping(SequenceI inputSeq,
70 java.io.PrintStream os)
72 HashMap<Integer, int[]> mapping = new HashMap<Integer, int[]>();
73 AlignmentI seq2Phyre2ModelFastaMapping = null;
76 seq2Phyre2ModelFastaMapping = new FormatAdapter().readFile(
77 getFastaMappingFile(), DataSourceType.FILE, FileFormat.Fasta);
78 } catch (IOException e1)
82 SequenceI[] seqs = seq2Phyre2ModelFastaMapping.getSequencesArray();
83 SequenceI tSequenceRes = seqs[0];
84 SequenceI tStructureRes = seqs[1];
86 // Essential to resolve fastaAlignment to input sequence and model sequence
88 tSequenceRes.setStart(inputSeq.getStart());
89 tSequenceRes.setEnd(inputSeq.getEnd());
91 tStructureRes.setStart(structureFile.getSeqsAsArray()[0].getStart());
92 tStructureRes.setEnd(structureFile.getSeqsAsArray()[0].getEnd());
95 int sequenceResLenght = tSequenceRes.getLength();
96 int structureResLenght = tStructureRes.getLength();
97 if (sequenceResLenght == structureResLenght)
99 int prevStructResNum = -1;
100 int alignmentLenght = sequenceResLenght + tSequenceRes.getStart();
101 for (int x = 0; x < alignmentLenght; x++)
103 int alignSeqResidueIndex = tSequenceRes.findIndex(x);
104 int structResNum = tStructureRes
105 .findPosition(alignSeqResidueIndex);
106 int sequenceResNum = tSequenceRes
107 .findPosition(alignSeqResidueIndex - 1);
108 boolean sameResNum = (structResNum == prevStructResNum);
109 // System.out.println(sequenceResNum + " : "
110 // + (sameResNum ? -1 : prevStructResNum));
111 mapping.put(sequenceResNum, new int[] {
112 sameResNum ? -1 : prevStructResNum, -1 });
113 prevStructResNum = structResNum;
116 } catch (Exception e)
123 populateAtomPositions(" ", mapping);
124 } catch (IllegalArgumentException e)
127 } catch (StructureMappingException e)
134 MappingOutputModel mop = new MappingOutputModel();
135 mop.setSeqStart(tSequenceRes.getStart());
136 mop.setSeqEnd(tSequenceRes.getEnd());
137 mop.setSeqName(tSequenceRes.getName());
138 mop.setSeqResidue(tSequenceRes.getSequenceAsString());
140 mop.setStrStart(tStructureRes.getStart());
141 mop.setStrEnd(tStructureRes.getEnd());
142 mop.setStrName(tStructureRes.getName());
143 mop.setStrResidue(tStructureRes.getSequenceAsString());
148 os.print(getMappingOutput(mop).toString());
149 } catch (Exception e)
158 private String getFastaMappingFile()
160 File phyre2ModelFile = new File(structureFile.getDataName());
161 String phyre2ModelResultDir = phyre2ModelFile.getParent();
162 String modelId = structureFile.getId().substring(0,
163 structureFile.getId().lastIndexOf(".pdb"));
164 return phyre2ModelResultDir + PATH_SEPARATOR + modelId + ".fasta";
168 public StringBuffer getMappingOutput(MappingOutputModel mp)
169 throws StructureMappingException
171 String seqRes = mp.getSeqResidue();
172 String seqName = mp.getSeqName();
173 int sStart = mp.getSeqStart();
174 int sEnd = mp.getSeqEnd();
176 String strRes = mp.getStrResidue();
177 String strName = mp.getStrName();
178 int pdbStart = mp.getStrStart();
179 int pdbEnd = mp.getStrEnd();
181 String type = mp.getType();
183 int maxid = (seqName.length() >= strName.length()) ? seqName.length()
185 int len = 72 - maxid - 1;
187 int nochunks = ((seqRes.length()) / len)
188 + ((seqRes.length()) % len > 0 ? 1 : 0);
190 StringBuffer output = new StringBuffer();
191 output.append(NEWLINE);
192 output.append("Sequence \u27f7 Structure mapping details").append(
194 output.append("Method: Phyre2 Alignment");
195 output.append(NEWLINE).append(NEWLINE);
197 output.append(new Format("%" + maxid + "s").form(seqName));
198 output.append(" : ");
199 output.append(String.valueOf(sStart));
200 output.append(" - ");
201 output.append(String.valueOf(sEnd));
202 output.append(" Maps to ");
203 output.append(NEWLINE);
204 output.append(new Format("%" + maxid + "s").form(strName));
205 output.append(" : ");
206 output.append(String.valueOf(pdbStart));
207 output.append(" - ");
208 output.append(String.valueOf(pdbEnd));
209 output.append(NEWLINE).append(NEWLINE);
211 int matchedSeqCount = 0;
212 for (int j = 0; j < nochunks; j++)
214 // Print the first aligned sequence
215 output.append(new Format("%" + (maxid) + "s").form(seqName)).append(
218 for (int i = 0; i < len; i++)
220 if ((i + (j * len)) < seqRes.length())
222 output.append(seqRes.charAt(i + (j * len)));
226 output.append(NEWLINE);
227 output.append(new Format("%" + (maxid) + "s").form(" ")).append(" ");
229 // Print out the matching chars
230 for (int i = 0; i < len; i++)
234 if ((i + (j * len)) < seqRes.length())
236 boolean sameChar = Comparison.isSameResidue(
237 seqRes.charAt(i + (j * len)),
238 strRes.charAt(i + (j * len)), false);
240 && !jalview.util.Comparison.isGap(seqRes.charAt(i
246 else if (type.equals("pep"))
248 if (ResidueProperties.getPAM250(seqRes.charAt(i + (j * len)),
249 strRes.charAt(i + (j * len))) > 0)
263 } catch (IndexOutOfBoundsException e)
268 // Now print the second aligned sequence
269 output = output.append(NEWLINE);
270 output = output.append(new Format("%" + (maxid) + "s").form(strName))
272 for (int i = 0; i < len; i++)
274 if ((i + (j * len)) < strRes.length())
276 output.append(strRes.charAt(i + (j * len)));
279 output.append(NEWLINE).append(NEWLINE);
281 float pid = (float) matchedSeqCount / seqRes.length() * 100;
282 // if (pid < SiftsSettings.getFailSafePIDThreshold())
284 // throw new Exception(">>> Low PID detected for Phyre2 mapping...");
286 output.append("Length of alignment = " + seqRes.length()).append(
288 output.append(new Format("Percentage ID = %2.2f").form(pid));
293 public static List<Phyre2SummaryPojo> parsePhyreCrudeList(String crudeList)
295 List<Phyre2SummaryPojo> phyre2Results = new ArrayList<Phyre2SummaryPojo>();
296 try (BufferedReader br = new BufferedReader(new FileReader(crudeList)))
299 while ((line = br.readLine()) != null)
301 String[] lineData = line.split(" ");
302 Phyre2SummaryPojo psp = new Phyre2SummaryPojo();
303 psp.setSerialNo(Integer.valueOf(lineData[0]));
304 psp.setTemplateId(lineData[1]);
305 psp.setConfidence(100 * Double.valueOf(lineData[2]));
306 psp.setPid(Integer.valueOf(lineData[3]));
307 psp.setAlignedRange(lineData[4] + " - " + lineData[5]);
308 // psp.setCoverage(coverage);
309 // psp.setTemplateSummary(templateSummary);
310 phyre2Results.add(psp);
312 } catch (Exception e)
316 return phyre2Results;
319 public static DefaultTableModel getTableModel(
320 List<Phyre2SummaryPojo> phyreResults)
322 if (phyreResults == null)
326 DefaultTableModel tableModel = new DefaultTableModel()
329 public boolean isCellEditable(int row, int column)
335 public Class<?> getColumnClass(int columnIndex)
340 return Integer.class;
350 return Integer.class;
360 tableModel.addColumn("#");
361 tableModel.addColumn("Template");
362 tableModel.addColumn("Aligned Range");
363 tableModel.addColumn("Coverage");
364 tableModel.addColumn("Confidence");
365 tableModel.addColumn("%.i.d");
366 tableModel.addColumn("Template Information");
368 for (Phyre2SummaryPojo res : phyreResults)
370 tableModel.addRow(new Object[] { res.getSerialNo(),
371 res.getTemplateId(), res.getAlignedRange(), res.getCoverage(),
372 res.getConfidence(), res.getPid(), res.getTemplateSummary() });
377 public static void configurePhyreResultTable(JTable phyreResultTable)
380 DecimalFormatTableCellRenderer idCellRender = new DecimalFormatTableCellRenderer(
382 DecimalFormatTableCellRenderer pidCellRender = new DecimalFormatTableCellRenderer(
384 DecimalFormatTableCellRenderer confidenceCellRender = new DecimalFormatTableCellRenderer(
387 phyreResultTable.getColumn("#").setMinWidth(20);
388 phyreResultTable.getColumn("#").setPreferredWidth(30);
389 phyreResultTable.getColumn("#").setMaxWidth(40);
390 phyreResultTable.getColumn("#").setCellRenderer(idCellRender);
392 phyreResultTable.getColumn("Template").setMinWidth(60);
393 phyreResultTable.getColumn("Template").setPreferredWidth(90);
394 phyreResultTable.getColumn("Template").setMaxWidth(150);
396 phyreResultTable.getColumn("Aligned Range").setMinWidth(80);
397 phyreResultTable.getColumn("Aligned Range").setPreferredWidth(80);
398 phyreResultTable.getColumn("Aligned Range").setMaxWidth(120);
400 phyreResultTable.getColumn("Coverage").setMinWidth(60);
401 phyreResultTable.getColumn("Coverage").setPreferredWidth(60);
402 phyreResultTable.getColumn("Coverage").setMaxWidth(90);
404 phyreResultTable.getColumn("Confidence").setMinWidth(60);
405 phyreResultTable.getColumn("Confidence").setPreferredWidth(60);
406 phyreResultTable.getColumn("Confidence").setMaxWidth(90);
407 phyreResultTable.getColumn("Confidence").setCellRenderer(
408 confidenceCellRender);
410 phyreResultTable.getColumn("%.i.d").setMinWidth(45);
411 phyreResultTable.getColumn("%.i.d").setPreferredWidth(450);
412 phyreResultTable.getColumn("%.i.d").setMaxWidth(65);
413 phyreResultTable.getColumn("%.i.d").setCellRenderer(pidCellRender);
415 phyreResultTable.getColumn("Template Information").setMinWidth(400);
416 phyreResultTable.getColumn("Template Information").setPreferredWidth(
418 phyreResultTable.getColumn("Template Information").setMaxWidth(1500);