Skip to content

Commit

Permalink
299 (#1622)
Browse files Browse the repository at this point in the history
* fix weird crash where some MS2s have no precursors (#1618)

* fix weird crash where some MS2s have no precursors

* whoops

* fix metadraw pdf export crash (#1619)

* fix weird crash where some MS2s have no precursors

* whoops

* fix PDF export crash in installed version of metamorpheus
  • Loading branch information
rmillikin authored Apr 4, 2019
1 parent a350ba7 commit 60e29f6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
22 changes: 18 additions & 4 deletions GUI/MetaDraw.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -541,8 +541,14 @@ private void PDFButton_Click(object sender, RoutedEventArgs e)
}

string filePath = Path.Combine(Path.GetDirectoryName(tsvResultsFilePath), "MetaDrawExport", psm.Ms2ScanNumber + "_" + myString + ".pdf");
string dir = Path.GetDirectoryName(filePath);

DrawPdfAnnotatedBaseSequence(psm, canvas); // captures the annotation for the pdf
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}

DrawPdfAnnotatedBaseSequence(psm, canvas, filePath); // captures the annotation for the pdf
mainViewModel.DrawPeptideSpectralMatchPdf(msDataScanToDraw, psm, filePath, numberOfScansToExport > 1);
}

Expand All @@ -554,7 +560,7 @@ private void PDFButton_Click(object sender, RoutedEventArgs e)
}
}

private void DrawPdfAnnotatedBaseSequence(PsmFromTsv psm, Canvas canvas)
private void DrawPdfAnnotatedBaseSequence(PsmFromTsv psm, Canvas canvas, string path)
{
if (psm.CrossType == null)
{
Expand All @@ -570,9 +576,17 @@ private void DrawPdfAnnotatedBaseSequence(PsmFromTsv psm, Canvas canvas)
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(renderBitmap));

using (FileStream file = File.Create("annotation.png"))
string tempPath = Path.Combine(Path.GetDirectoryName(tsvResultsFilePath), "MetaDrawExport", "annotation.png");
try
{
using (FileStream file = File.Create(tempPath))
{
encoder.Save(file);
}
}
catch (UnauthorizedAccessException)
{
encoder.Save(file);
MessageBox.Show("EXCEPTION 1");
}
}

Expand Down
19 changes: 8 additions & 11 deletions GUI/MetaDraw/PsmAnnotationViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,22 +257,19 @@ public void DrawPeptideSpectralMatchPdf(MsDataScan msDataScan, PsmFromTsv psm, s
var properties = psm.GetType().GetProperties();
var pdfModel = DrawPdf(msDataScan, properties, psm, redraw);

string dir = Path.GetDirectoryName(fileName);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}

string tempPath = Path.Combine(Path.GetDirectoryName(fileName), "sequence.pdf");
string baseSeqTempPath = Path.Combine(Path.GetDirectoryName(fileName), "annotation.png");

// exports plot to pdf
using (var stream = File.Create("sequence.pdf"))
using (var stream = File.Create(tempPath))
{
PdfExporter pdf = new PdfExporter { Width = 800, Height = 500 };
pdf.Export(pdfModel, stream);
}

// adds base seq annotation to pdf
using (Stream inputPdfStream = new FileStream("sequence.pdf", FileMode.Open, FileAccess.Read, FileShare.Read))
using (Stream inputImageStream = new FileStream("annotation.png", FileMode.Open, FileAccess.Read, FileShare.Read))
using (Stream inputPdfStream = new FileStream(tempPath, FileMode.Open, FileAccess.Read, FileShare.Read))
using (Stream inputImageStream = new FileStream(baseSeqTempPath, FileMode.Open, FileAccess.Read, FileShare.Read))
using (Stream outputPdfStream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
{
var reader = new PdfReader(inputPdfStream);
Expand All @@ -287,8 +284,8 @@ public void DrawPeptideSpectralMatchPdf(MsDataScan msDataScan, PsmFromTsv psm, s
stamper.Close();
}

File.Delete("sequence.pdf");
File.Delete("annotation.png");
File.Delete(tempPath);
File.Delete(baseSeqTempPath);
}

private void AnnotatePeak(PlotModel model, LineSeries[] allIons, MsDataScan msDataScan, MatchedFragmentIon matchedIon, double[] spectrumIntensities, PsmFromTsv psmToDraw,
Expand Down
2 changes: 1 addition & 1 deletion TaskLayer/MetaMorpheusTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public static IEnumerable<Ms2ScanWithSpecificMass> GetMs2Scans(MsDataFile myMSDa
});

var childScanNumbers = new HashSet<int>(scansWithPrecursors.SelectMany(p => p.SelectMany(v => v.ChildScans.Select(x => x.OneBasedScanNumber))));
var parentScans = scansWithPrecursors.Where(p => !childScanNumbers.Contains(p.First().OneBasedScanNumber)).SelectMany(v => v);
var parentScans = scansWithPrecursors.Where(p => p.Any() && !childScanNumbers.Contains(p.First().OneBasedScanNumber)).SelectMany(v => v);

// XCorr pre-processing for low-res data. this is here because the parent/child scans may have different
// resolutions, so this pre-processing must take place after the parent/child scans have been determined
Expand Down

0 comments on commit 60e29f6

Please sign in to comment.