|
1 | 1 | package org.labkey.blast.model; |
2 | 2 |
|
3 | 3 | import org.apache.commons.lang3.StringUtils; |
4 | | -import org.apache.logging.log4j.Logger; |
5 | 4 | import org.apache.logging.log4j.LogManager; |
| 5 | +import org.apache.logging.log4j.Logger; |
6 | 6 | import org.jetbrains.annotations.Nullable; |
7 | 7 | import org.json.JSONException; |
8 | 8 | import org.json.JSONObject; |
|
23 | 23 | import org.labkey.blast.BLASTSchema; |
24 | 24 | import org.labkey.blast.BLASTWrapper; |
25 | 25 |
|
| 26 | +import javax.servlet.jsp.JspWriter; |
26 | 27 | import java.io.File; |
27 | 28 | import java.io.IOException; |
28 | 29 | import java.io.Serializable; |
29 | 30 | import java.io.StringWriter; |
30 | 31 | import java.io.Writer; |
31 | 32 | import java.util.ArrayList; |
32 | | -import java.util.Collections; |
33 | 33 | import java.util.Date; |
34 | 34 | import java.util.HashMap; |
35 | 35 | import java.util.List; |
36 | 36 | import java.util.Map; |
37 | 37 | import java.util.Scanner; |
38 | 38 |
|
| 39 | +import static org.labkey.api.util.HtmlString.unsafe; |
| 40 | + |
39 | 41 | /** |
40 | 42 | * User: bimber |
41 | 43 | * Date: 7/20/2014 |
@@ -320,7 +322,14 @@ public void getResults(BLAST_OUTPUT_FORMAT outputFormat, Writer out) throws IOEx |
320 | 322 | return; |
321 | 323 | } |
322 | 324 |
|
323 | | - outputFormat.processResults(output, out); |
| 325 | + if (out instanceof JspWriter jsp) |
| 326 | + { |
| 327 | + jsp.print(unsafe(outputFormat.processResults(output))); |
| 328 | + } |
| 329 | + else |
| 330 | + { |
| 331 | + out.write(outputFormat.processResults(output)); |
| 332 | + } |
324 | 333 | } |
325 | 334 |
|
326 | 335 | public boolean hasError(User u) |
@@ -381,16 +390,15 @@ public Alignment(int qLength, int sLength, int alignLength) |
381 | 390 | private Map<String, Summary> _perfectHitSummary; |
382 | 391 |
|
383 | 392 | @Override |
384 | | - public void processResults(File results, Writer out) throws IOException, PipelineJobException |
| 393 | + public String processResults(File results) throws IOException, PipelineJobException |
385 | 394 | { |
386 | 395 | //summary of perfect hits by query seq |
387 | | - try (StringWriter writer = new StringWriter()) |
| 396 | + try (StringWriter out = new StringWriter()) |
388 | 397 | { |
389 | 398 | _perfectHitSummary = new HashMap<>(); |
390 | 399 |
|
391 | | - new BLASTWrapper(_log).runBlastFormatter(results, BLAST_OUTPUT_FORMAT.alignmentSummary, writer); |
392 | | - |
393 | | - Scanner scan = new Scanner(writer.getBuffer().toString()); |
| 400 | + String resultsText = new BLASTWrapper(_log).runBlastFormatter(results, BLAST_OUTPUT_FORMAT.alignmentSummary); |
| 401 | + Scanner scan = new Scanner(resultsText); |
394 | 402 | while (scan.hasNextLine()) |
395 | 403 | { |
396 | 404 | String line = scan.nextLine(); |
@@ -420,7 +428,7 @@ public void processResults(File results, Writer out) throws IOException, Pipelin |
420 | 428 | String qseq = tokens[8]; |
421 | 429 | String sseq = tokens[9]; |
422 | 430 |
|
423 | | - int i=0; |
| 431 | + int i = 0; |
424 | 432 | int qmatch = 0; |
425 | 433 | int qmismatch = 0; |
426 | 434 | while (i < qseq.length()) |
@@ -448,58 +456,60 @@ public void processResults(File results, Writer out) throws IOException, Pipelin |
448 | 456 | } |
449 | 457 | } |
450 | 458 | } |
451 | | - } |
452 | 459 |
|
453 | | - out.write("<br><br><b>Summary of Perfect Hits:</b><br>"); |
454 | | - out.write("<table border=1 cellpadding=\"3\" style=\"border-collapse: collapse;\"><tr><td>Query</td><td># Perfect Hits</td><td>Reference Names</td><td>Alignment Length</td><td>Query Length</td><td>Reference Length</td></tr>"); |
455 | | - for (String qname : _perfectHitSummary.keySet()) |
456 | | - { |
457 | | - out.write("<tr>"); |
458 | | - out.write("<td>" + qname + "</td>"); |
459 | | - out.write("<td>" + _perfectHitSummary.get(qname).hitMap.size() + "</td>"); |
460 | | - |
461 | | - StringBuilder sNameCell = new StringBuilder(); |
462 | | - StringBuilder alignLengthCell = new StringBuilder(); |
463 | | - StringBuilder qLengthCell = new StringBuilder(); |
464 | | - StringBuilder sLengthCell = new StringBuilder(); |
465 | | - |
466 | | - Summary s = _perfectHitSummary.get(qname); |
467 | | - String br = ""; |
468 | | - for (String sname : s.hitMap.keySet()) |
| 460 | + out.write("<br><br><b>Summary of Perfect Hits:</b><br>"); |
| 461 | + out.write("<table border=1 cellpadding=\"3\" style=\"border-collapse: collapse;\"><tr><td>Query</td><td># Perfect Hits</td><td>Reference Names</td><td>Alignment Length</td><td>Query Length</td><td>Reference Length</td></tr>"); |
| 462 | + for (String qname : _perfectHitSummary.keySet()) |
469 | 463 | { |
470 | | - for (Alignment a : s.hitMap.get(sname)) |
| 464 | + out.write("<tr>"); |
| 465 | + out.write("<td>" + qname + "</td>"); |
| 466 | + out.write("<td>" + _perfectHitSummary.get(qname).hitMap.size() + "</td>"); |
| 467 | + |
| 468 | + StringBuilder sNameCell = new StringBuilder(); |
| 469 | + StringBuilder alignLengthCell = new StringBuilder(); |
| 470 | + StringBuilder qLengthCell = new StringBuilder(); |
| 471 | + StringBuilder sLengthCell = new StringBuilder(); |
| 472 | + |
| 473 | + Summary s = _perfectHitSummary.get(qname); |
| 474 | + String br = ""; |
| 475 | + for (String sname : s.hitMap.keySet()) |
471 | 476 | { |
472 | | - sNameCell.append(br); |
473 | | - sNameCell.append(sname); |
| 477 | + for (Alignment a : s.hitMap.get(sname)) |
| 478 | + { |
| 479 | + sNameCell.append(br); |
| 480 | + sNameCell.append(sname); |
474 | 481 |
|
475 | | - alignLengthCell.append(br); |
476 | | - alignLengthCell.append(a.alignLength); |
| 482 | + alignLengthCell.append(br); |
| 483 | + alignLengthCell.append(a.alignLength); |
477 | 484 |
|
478 | | - qLengthCell.append(br); |
479 | | - qLengthCell.append(a.qLength); |
| 485 | + qLengthCell.append(br); |
| 486 | + qLengthCell.append(a.qLength); |
480 | 487 |
|
481 | | - sLengthCell.append(br); |
482 | | - sLengthCell.append(a.sLength); |
| 488 | + sLengthCell.append(br); |
| 489 | + sLengthCell.append(a.sLength); |
483 | 490 |
|
484 | | - br = "<br>"; |
| 491 | + br = "<br>"; |
| 492 | + } |
485 | 493 | } |
486 | | - } |
487 | 494 |
|
488 | | - out.write("<td>" + sNameCell + "</td>"); |
489 | | - out.write("<td>" + alignLengthCell + "</td>"); |
490 | | - out.write("<td>" + qLengthCell + "</td>"); |
491 | | - out.write("<td>" + sLengthCell + "</td>"); |
| 495 | + out.write("<td>" + sNameCell + "</td>"); |
| 496 | + out.write("<td>" + alignLengthCell + "</td>"); |
| 497 | + out.write("<td>" + qLengthCell + "</td>"); |
| 498 | + out.write("<td>" + sLengthCell + "</td>"); |
492 | 499 |
|
493 | | - out.write("</tr>"); |
494 | | - } |
| 500 | + out.write("</tr>"); |
| 501 | + } |
495 | 502 |
|
496 | | - out.write("</table><br><br>"); |
497 | | - out.write("<hr>"); |
| 503 | + out.write("</table><br><br>"); |
| 504 | + out.write("<hr>"); |
498 | 505 |
|
499 | | - out.write("<b>BLAST Output:</b>"); |
500 | | - out.write("<pre>"); |
501 | | - new BLASTWrapper(_log).runBlastFormatter(results, BLAST_OUTPUT_FORMAT.flatQueryAnchoredWithIdentities, out); |
502 | | - out.write("</pre>"); |
| 506 | + out.write("<b>BLAST Output:</b>"); |
| 507 | + out.write("<pre>"); |
| 508 | + out.write(new BLASTWrapper(_log).runBlastFormatter(results, BLAST_OUTPUT_FORMAT.flatQueryAnchoredWithIdentities)); |
| 509 | + out.write("</pre>"); |
| 510 | + |
| 511 | + return out.toString(); |
| 512 | + } |
503 | 513 | } |
504 | 514 |
|
505 | 515 | private void appendHit(String qname, String sname, int qLen, int sLen, int alignLen) |
@@ -547,23 +557,23 @@ public boolean supportsHTML() |
547 | 557 | return _supportsHTML; |
548 | 558 | } |
549 | 559 |
|
550 | | - public void processResults(File results, Writer out) throws IOException, PipelineJobException |
| 560 | + public String processResults(File results) throws IOException, PipelineJobException |
551 | 561 | { |
552 | 562 | if (_processor == null) |
553 | 563 | { |
554 | | - new BLASTWrapper(_log).runBlastFormatter(results, this, out); |
| 564 | + return new BLASTWrapper(_log).runBlastFormatter(results, this); |
555 | 565 | } |
556 | 566 | else |
557 | 567 | { |
558 | | - _processor.processResults(results, out); |
| 568 | + return _processor.processResults(results); |
559 | 569 | } |
560 | 570 | } |
561 | 571 |
|
562 | 572 | } |
563 | 573 |
|
564 | 574 | public interface BlastResultProcessor |
565 | 575 | { |
566 | | - void processResults(File results, Writer out) throws IOException, PipelineJobException; |
| 576 | + String processResults(File results) throws IOException, PipelineJobException; |
567 | 577 | } |
568 | 578 | } |
569 | 579 |
|
0 commit comments