Ejemplo n.º 1
0
            public void Run(ProcessStartInfo psi, string stdin, IProgressMonitor progress, ref IProgressStatus status, TextWriter writer)
            {
                if (shouldCancel)
                {
                    status.Cancel();
                    progress.UpdateProgress(status = status.Cancel());
                    return;
                }

                if (!string.IsNullOrEmpty(stringToWriteToWriter))
                {
                    writer.WriteLine(stringToWriteToWriter);
                }
                status.ChangePercentComplete(100);
                progress.UpdateProgress(status);
            }
Ejemplo n.º 2
0
        private static bool UpdatePrecentComplete(IProgressMonitor progressMonitor, int percentComplete,
                                                  ref IProgressStatus progressStatus)
        {
            if (progressMonitor.IsCanceled)
            {
                progressMonitor.UpdateProgress(progressStatus.Cancel());
                return(false);
            }

            if (progressStatus.PercentComplete != percentComplete)
            {
                progressMonitor.UpdateProgress(progressStatus = progressStatus.ChangePercentComplete(percentComplete));
            }
            return(true);
        }
Ejemplo n.º 3
0
 public void Cancel()
 {
     local.Cancel();
 }
		bool DatabaseInfrastructureCheck (IProgressStatus monitor)
		{
			// Do some sanity check, to make sure the basic database infrastructure can be created
			
			bool hasChanges = false;
			
			try {
			
				if (!Directory.Exists (AddinCachePath)) {
					Directory.CreateDirectory (AddinCachePath);
					hasChanges = true;
				}
			
				if (!Directory.Exists (AddinFolderCachePath)) {
					Directory.CreateDirectory (AddinFolderCachePath);
					hasChanges = true;
				}
			
				// Make sure we can write in those folders

				Util.CheckWrittableFloder (AddinCachePath);
				Util.CheckWrittableFloder (AddinFolderCachePath);
				
				fatalDatabseError = false;
			}
			catch (Exception ex) {
				monitor.ReportError ("Add-in cache directory could not be created", ex);
				fatalDatabseError = true;
				monitor.Cancel ();
			}
			return hasChanges;
		}
Ejemplo n.º 5
0
        void RunScannerProcess(IProgressStatus monitor)
        {
            ISetupHandler setup = GetSetupHandler ();

            IProgressStatus scanMonitor = monitor;
            ArrayList pparams = new ArrayList ();

            bool retry = false;
            do {
                try {
                    if (monitor.LogLevel > 1)
                        monitor.Log ("Looking for addins");
                    setup.Scan (scanMonitor, registry, null, (string[]) pparams.ToArray (typeof(string)));
                    retry = false;
                }
                catch (Exception ex) {
                    ProcessFailedException pex = ex as ProcessFailedException;
                    if (pex != null) {
                        // Get the last logged operation.
                        if (pex.LastLog.StartsWith ("scan:")) {
                            // It crashed while scanning a file. Add the file to the ignore list and try again.
                            string file = pex.LastLog.Substring (5);
                            pparams.Add (file);
                            monitor.ReportWarning ("Could not scan file: " + file);
                            retry = true;
                            continue;
                        }
                    }
                    fatalDatabseError = true;
                    // If the process has crashed, try to do a new scan, this time using verbose log,
                    // to give the user more information about the origin of the crash.
                    if (pex != null && !retry) {
                        monitor.ReportError ("Add-in scan operation failed. The runtime may have encountered an error while trying to load an assembly.", null);
                        if (monitor.LogLevel <= 1) {
                            // Re-scan again using verbose log, to make it easy to find the origin of the error.
                            retry = true;
                            scanMonitor = new ConsoleProgressStatus (true);
                        }
                    } else
                        retry = false;

                    if (!retry) {
                        var pfex = ex as ProcessFailedException;
                        monitor.ReportError ("Add-in scan operation failed", pfex != null? pfex.InnerException : ex);
                        monitor.Cancel ();
                        return;
                    }
                }
            }
            while (retry);
        }
Ejemplo n.º 6
0
        private bool Load(ILoadMonitor loader, IProgressStatus status, bool cached)
        {
            try
            {
                int loadPercent = 100;

                if (!cached)
                {
                    // Building the cache will take 95% of the load time.
                    loadPercent = 5;

                    status = status.ChangeMessage(string.Format(Resources.XHunterLibrary_Load_Building_binary_cache_for__0__library, Path.GetFileName(FilePath)));
                    status = status.ChangePercentComplete(0);

                    loader.UpdateProgress(status);

                    if (!CreateCache(loader, status, 100 - loadPercent))
                    {
                        return(false);
                    }
                }

                status = status.ChangeMessage(string.Format(Resources.XHunterLibrary_Load_Loading__0__library, Path.GetFileName(FilePath)));
                loader.UpdateProgress(status);

                var valueCache = new ValueCache();
                var sm         = loader.StreamManager;
                using (Stream stream = sm.CreateStream(CachePath, FileMode.Open, true))
                {
                    // Read library header from the end of the cache
                    int countHeader = (int)LibHeaders.count * sizeof(int);
                    stream.Seek(-countHeader, SeekOrigin.End);

                    byte[] libHeader = new byte[countHeader];
                    ReadComplete(stream, libHeader, countHeader);

                    int version = GetInt32(libHeader, (int)LibHeaders.format_version);
                    if (version != FORMAT_VERSION_CACHE)
                    {
                        return(false);
                    }

                    int countRevisionBytes = GetInt32(libHeader, (int)LibHeaders.revision_byte_count);
                    int countIdBytes       = GetInt32(libHeader, (int)LibHeaders.id_byte_count);
                    stream.Seek(-countHeader - countRevisionBytes - countIdBytes, SeekOrigin.End);
                    Revision = ReadString(stream, countRevisionBytes);
                    Id       = ReadString(stream, countIdBytes);

                    int numSpectra = GetInt32(libHeader, (int)LibHeaders.num_spectra);

                    var libraryEntries = new XHunterSpectrumInfo[numSpectra];

                    // Seek to beginning of spectrum headers
                    long locationHeaders = BitConverter.ToInt64(libHeader,
                                                                ((int)LibHeaders.location_headers_lo) * sizeof(int));
                    stream.Seek(locationHeaders, SeekOrigin.Begin);

                    for (int i = 0; i < numSpectra; i++)
                    {
                        int percent = (100 - loadPercent) + (i * loadPercent / numSpectra);
                        if (status.PercentComplete != percent)
                        {
                            // Check for cancellation after each integer change in percent loaded.
                            if (loader.IsCanceled)
                            {
                                loader.UpdateProgress(status.Cancel());
                                return(false);
                            }

                            // If not cancelled, update progress.
                            loader.UpdateProgress(status = status.ChangePercentComplete(percent));
                        }

                        // Read spectrum header
                        LibKey key                = LibKey.Read(valueCache, stream);
                        long   location           = PrimitiveArrays.ReadOneValue <long>(stream);
                        float  processedIntensity = PrimitiveArrays.ReadOneValue <float>(stream);
                        int    numPeaks           = PrimitiveArrays.ReadOneValue <int>(stream);
                        float  expect             = PrimitiveArrays.ReadOneValue <float>(stream);
                        libraryEntries[i] = new XHunterSpectrumInfo(key, processedIntensity, expect, (short)numPeaks, location);
                    }

                    // Checksum = checksum.ChecksumValue;
                    SetLibraryEntries(libraryEntries);

                    loader.UpdateProgress(status.Complete());

                    // Create the stream from which the spectra will be read
                    CreateStream(loader);
                }

                return(true);
            }
            catch (InvalidDataException x)
            {
                if (!cached)
                {
                    loader.UpdateProgress(status.ChangeErrorException(x));
                }
                return(false);
            }
            catch (IOException x)
            {
                if (!cached)
                {
                    loader.UpdateProgress(status.ChangeErrorException(x));
                }
                return(false);
            }
            catch (Exception x)
            {
                if (!cached)
                {
                    x = new Exception(string.Format(Resources.XHunterLibrary_Load_Failed_loading_library__0__, FilePath), x);
                    loader.UpdateProgress(status.ChangeErrorException(x));
                }
                return(false);
            }
            finally
            {
                if (ReadStream != null)
                {
                    // Close the read stream to ensure we never leak it.
                    // This only costs on extra open, the first time the
                    // active document tries to read.
                    try { ReadStream.CloseStream(); }
                    catch (IOException) { }
                }
            }
        }
Ejemplo n.º 7
0
        // ReSharper restore UnusedMember.Local

        private bool CreateCache(ILoadMonitor loader, IProgressStatus status, int percent)
        {
            var sm = loader.StreamManager;

            BufferedStream stream = new BufferedStream(CreateStream(loader), 32 * 1024);

            int    version = 1;
            string id = string.Empty, revision = string.Empty;
            int    size = ReadSize(stream);
            int    i;

            if (size == 0)
            {
                version = 2;
                size    = ReadSize(stream);

                const int countLibHeader = 256 - 8;
                byte[]    libHeader      = new byte[countLibHeader];
                if (stream.Read(libHeader, 0, libHeader.Length) != libHeader.Length)
                {
                    throw new InvalidDataException(Resources.XHunterLibrary_CreateCache_Data_truncation_in_library_header_File_may_be_corrupted);
                }

                for (i = 0; i < libHeader.Length; i++)
                {
                    if (libHeader[i] == 0)
                    {
                        break;
                    }
                }

                string header = Encoding.UTF8.GetString(libHeader, 0, i);
                Match  match  = REGEX_HEADER.Match(header);
                if (match.Success)
                {
                    version  = int.Parse(match.Groups[1].Value);
                    id       = match.Groups[2].Value;
                    revision = match.Groups[3].Value;
                }
            }
            var setLibKeys     = new HashSet <LibKey>(size);
            var libraryEntries = new List <XHunterSpectrumInfo>(size);

            const int countHeader = ((int)SpectrumHeaders2.count) * sizeof(int);

            byte[] specHeader   = new byte[1024];
            byte[] specSequence = new byte[1024];
            i = 0;

            while (stream.Read(specHeader, 0, countHeader) == countHeader)
            {
                int percentComplete = (i++ *percent / size);
                if (status.PercentComplete != percentComplete)
                {
                    // Check for cancellation after each integer change in percent loaded.
                    if (loader.IsCanceled)
                    {
                        loader.UpdateProgress(status.Cancel());
                        return(false);
                    }

                    // If not cancelled, update progress.
                    loader.UpdateProgress(status = status.ChangePercentComplete(percentComplete));
                }

                int charge = (version == 1
                                  ? GetInt32(specHeader, ((int)SpectrumHeaders1.charge))
                                  : GetInt32(specHeader, ((int)SpectrumHeaders2.charge)));

                float i2 = (version == 1
                                ? GetSingle(specHeader, ((int)SpectrumHeaders1.i2))
                                : GetSingle(specHeader, ((int)SpectrumHeaders2.i2)));
                int seqLength = (version == 1
                                     ? GetInt32(specHeader, ((int)SpectrumHeaders1.seq_len))
                                     : GetInt32(specHeader, ((int)SpectrumHeaders2.seq_len)));

                float expect = (version == 1 ? 0.001f : GetSingle(specHeader, (int)SpectrumHeaders2.expect));


                // Read sequence information
                ReadComplete(stream, specSequence, seqLength);
                specSequence[seqLength] = 0;

                short numPeaks = (short)ReadSize(stream);

                // Save spectrum location
                long location = stream.Position;

                // Read over spectrum
                int countPeaks = (sizeof(byte) + sizeof(float)) * numPeaks;
                stream.Seek(countPeaks, SeekOrigin.Current); // Skip spectrum

                // Read modifications
                int    numMods          = ReadSize(stream);
                string modifiedSequence = Encoding.UTF8.GetString(specSequence, 0, seqLength);
                if (numMods > 0)
                {
                    StringBuilder sb = new StringBuilder();

                    ReadComplete(stream, specHeader, (4 + sizeof(double)) * numMods);
                    int    iLast    = 0;
                    double modTotal = 0;
                    for (int j = 0; j < numMods; j++)
                    {
                        int    iPos = GetInt32(specHeader, j * 3);
                        double mod  = BitConverter.ToDouble(specHeader, (j * 3 + 1) * 4);

                        // X! Hunter allows multiple modifications on the same
                        // residue.  So, they need to be totaled, and assigned to a
                        // single residue to allow them to match Skyline modification
                        // settings.
                        if (iPos > iLast)
                        {
                            if (modTotal != 0)
                            {
                                sb.Append(SequenceMassCalc.GetModDiffDescription(modTotal));
                            }
                            sb.Append(Encoding.UTF8.GetString(specSequence, iLast, iPos - iLast));

                            modTotal = 0;
                        }
                        modTotal += mod;
                        iLast     = iPos;
                    }
                    if (modTotal != 0)
                    {
                        sb.Append(SequenceMassCalc.GetModDiffDescription(modTotal));
                    }
                    sb.Append(Encoding.UTF8.GetString(specSequence, iLast, seqLength - iLast));
                    modifiedSequence = sb.ToString();
                }

                // Skip over homologs (list of protein IDs and start positions from a FASTA
                // file used to generate the library)
                int numHomologs = ReadSize(stream);
                for (int j = 0; j < numHomologs; j++)
                {
                    stream.Seek(ReadSize(stream) + 4, SeekOrigin.Current);
                }

                // These libraries should not have duplicates, but just in case.
                // Apparently, GPM libraries do contain redundancies, as we found
                // when a revision lost this test.
                var key = new LibKey(modifiedSequence, charge);
                if (setLibKeys.Add(key))
                {
                    libraryEntries.Add(new XHunterSpectrumInfo(key, i2, expect, numPeaks, location));
                }
            }

            libraryEntries = FilterInvalidLibraryEntries(ref status, libraryEntries);

            using (FileSaver fs = new FileSaver(CachePath, sm))
                using (Stream outStream = sm.CreateStream(fs.SafeName, FileMode.Create, true))
                {
                    foreach (var info in libraryEntries)
                    {
                        info.Key.Write(outStream);
                        PrimitiveArrays.WriteOneValue(outStream, info.Location);
                        PrimitiveArrays.WriteOneValue(outStream, info.ProcessedIntensity);
                        PrimitiveArrays.WriteOneValue(outStream, info.NumPeaks);
                        PrimitiveArrays.WriteOneValue(outStream, info.Expect);
                    }

                    byte[] revisionBytes = Encoding.UTF8.GetBytes(revision);
                    outStream.Write(revisionBytes, 0, revisionBytes.Length);
                    byte[] idBytes = Encoding.UTF8.GetBytes(id);
                    outStream.Write(idBytes, 0, idBytes.Length);
                    outStream.Write(BitConverter.GetBytes(revisionBytes.Length), 0, sizeof(int));
                    outStream.Write(BitConverter.GetBytes(idBytes.Length), 0, sizeof(int));
                    outStream.Write(BitConverter.GetBytes(FORMAT_VERSION_CACHE), 0, sizeof(int));
                    outStream.Write(BitConverter.GetBytes(libraryEntries.Count), 0, sizeof(int));
                    outStream.Write(BitConverter.GetBytes((long)0), 0, sizeof(long));

                    sm.Finish(outStream);
                    fs.Commit();
                    sm.SetCache(FilePath, CachePath);
                }

            loader.UpdateProgress(status.Complete());

            return(true);
        }
Ejemplo n.º 8
0
        public void Run(ProcessStartInfo psi, string stdin, IProgressMonitor progress, ref IProgressStatus status, TextWriter writer)
        {
            // Make sure required streams are redirected.
            psi.RedirectStandardOutput = true;
            psi.RedirectStandardError  = true;

            _messageLog.Clear();

            var proc = Process.Start(psi);

            if (proc == null)
            {
                throw new IOException(string.Format(@"Failure starting {0} command.", psi.FileName));
            }
            if (stdin != null)
            {
                try
                {
                    proc.StandardInput.Write(stdin);
                }
                finally
                {
                    proc.StandardInput.Close();
                }
            }

            var           reader      = new ProcessStreamReader(proc);
            StringBuilder sbError     = new StringBuilder();
            int           percentLast = 0;
            string        line;

            while ((line = reader.ReadLine(progress)) != null)
            {
                if (writer != null && !line.StartsWith(HideLinePrefix))
                {
                    writer.WriteLine(line);
                }

                if (progress == null || line.ToLowerInvariant().StartsWith(@"error"))
                {
                    sbError.AppendLine(line);
                }
                else // if (progress != null)
                {
                    if (progress.IsCanceled)
                    {
                        proc.Kill();
                        progress.UpdateProgress(status = status.Cancel());
                        return;
                    }

                    if (!string.IsNullOrEmpty(MessagePrefix) && line.StartsWith(MessagePrefix))
                    {
                        _messageLog.Add(line.Substring(MessagePrefix.Length));
                    }
                    else if (line.EndsWith(@"%"))
                    {
                        double   percent;
                        string[] parts       = line.Split(' ');
                        string   percentPart = parts[parts.Length - 1];
                        if (double.TryParse(percentPart.Substring(0, percentPart.Length - 1), out percent))
                        {
                            percentLast = (int)percent;
                            status      = status.ChangePercentComplete(percentLast);
                            if (percent >= 100 && status.SegmentCount > 0)
                            {
                                status = status.NextSegment();
                            }
                            progress.UpdateProgress(status);
                        }
                    }
                    else if (StatusPrefix == null || line.StartsWith(StatusPrefix))
                    {
                        // Remove prefix, if there is one.
                        if (StatusPrefix != null)
                        {
                            line = line.Substring(StatusPrefix.Length);
                        }

                        status = status.ChangeMessage(line);
                        progress.UpdateProgress(status);
                    }
                }
            }
            proc.WaitForExit();
            int exit = proc.ExitCode;

            if (exit != 0)
            {
                line = proc.StandardError.ReadLine();
                if (line != null)
                {
                    sbError.AppendLine(line);
                }
                if (sbError.Length == 0)
                {
                    sbError.AppendLine(@"Error occurred running process.");
                }
                string processPath = Path.GetDirectoryName(psi.FileName)?.Length == 0
                    ? Path.Combine(Environment.CurrentDirectory, psi.FileName)
                    : psi.FileName;
                // ReSharper disable LocalizableElement
                sbError.AppendFormat("\r\nCommand-line: {0} {1}\r\nWorking directory: {2}{3}", processPath,
                                     // ReSharper restore LocalizableElement
                                     string.Join(" ", proc.StartInfo.Arguments), psi.WorkingDirectory,
                                     stdin != null ? "\r\nStandard input:\r\n" + stdin : "");
                throw new IOException(sbError.ToString());
            }
            // Make to complete the status, if the process succeeded, but never
            // printed 100% to the console
            if (percentLast < 100)
            {
                status = status.ChangePercentComplete(100);
                if (status.SegmentCount > 0)
                {
                    status = status.NextSegment();
                }
                if (progress != null)
                {
                    progress.UpdateProgress(status);
                }
            }
        }
Ejemplo n.º 9
0
		public static void MonitorProcessStatus (IProgressStatus monitor, TextReader reader, StringCollection progessLog)
		{
			string line;
			string exceptionText = null;
			while ((line = reader.ReadLine ()) != null) {
				int i = line.IndexOf (':');
				if (i != -1) {
					string tag = line.Substring (0, i);
					string txt = line.Substring (i+1);
					bool wasTag = true;
					
					switch (tag) {
						case "process-ps-msg":
							monitor.SetMessage (Decode (txt));
							break;
						case "process-ps-progress":
							monitor.SetProgress (double.Parse (txt));
							break;
						case "process-ps-log":
							monitor.Log (Decode (txt));
							break;
						case "process-ps-warning":
							monitor.ReportWarning (Decode (txt));
							break;
						case "process-ps-exception":
							exceptionText = Decode (txt);
							if (exceptionText == string.Empty)
								exceptionText = null;
							break;
						case "process-ps-error":
							string err = Decode (txt);
							if (err == string.Empty) err = null;
							monitor.ReportError (err, exceptionText != null ? new Exception (exceptionText) : null);
							break;
						case "process-ps-cancel":
							monitor.Cancel ();
							break;
						case "process-ps-plog":
							progessLog.Add (Decode (txt));
							break;
						default:
							wasTag = false;
							break;
					}
					if (wasTag)
						continue;
				}
				Console.WriteLine (line);
			}
		}
Ejemplo n.º 10
0
        private bool Load(ILoadMonitor loader, IProgressStatus status, bool cached)
        {
            try
            {
                int loadPercent = 100;

                if (!cached)
                {
                    // Building the cache will take 95% of the load time.
                    loadPercent = 5;

                    status = status.ChangeMessage(string.Format(Resources.XHunterLibrary_Load_Building_binary_cache_for__0__library, Path.GetFileName(FilePath)));
                    status = status.ChangePercentComplete(0);

                    loader.UpdateProgress(status);

                    if (!CreateCache(loader, status, 100 - loadPercent))
                    {
                        return(false);
                    }
                }

                status = status.ChangeMessage(string.Format(Resources.XHunterLibrary_Load_Loading__0__library, Path.GetFileName(FilePath)));
                loader.UpdateProgress(status);

                var sm = loader.StreamManager;
                using (Stream stream = sm.CreateStream(CachePath, FileMode.Open, true))
                {
                    // Read library header from the end of the cache
                    int countHeader = (int)LibHeaders.count * sizeof(int);
                    stream.Seek(-countHeader, SeekOrigin.End);

                    byte[] libHeader = new byte[countHeader];
                    ReadComplete(stream, libHeader, countHeader);

                    int version = GetInt32(libHeader, (int)LibHeaders.format_version);
                    if (version != FORMAT_VERSION_CACHE)
                    {
                        return(false);
                    }

                    int countRevisionBytes = GetInt32(libHeader, (int)LibHeaders.revision_byte_count);
                    int countIdBytes       = GetInt32(libHeader, (int)LibHeaders.id_byte_count);
                    stream.Seek(-countHeader - countRevisionBytes - countIdBytes, SeekOrigin.End);
                    Revision = ReadString(stream, countRevisionBytes);
                    Id       = ReadString(stream, countIdBytes);

                    int numSpectra = GetInt32(libHeader, (int)LibHeaders.num_spectra);

                    var setSequences   = new Dictionary <LibSeqKey, bool>(numSpectra);
                    var libraryEntries = new XHunterSpectrumInfo[numSpectra];

                    // Seek to beginning of spectrum headers
                    long locationHeaders = BitConverter.ToInt64(libHeader,
                                                                ((int)LibHeaders.location_headers_lo) * sizeof(int));
                    stream.Seek(locationHeaders, SeekOrigin.Begin);

                    byte[] specSequence = new byte[1024];
                    byte[] specHeader   = new byte[1024];
                    countHeader = (int)SpectrumCacheHeader.count * 4;

                    for (int i = 0; i < numSpectra; i++)
                    {
                        int percent = (100 - loadPercent) + (i * loadPercent / numSpectra);
                        if (status.PercentComplete != percent)
                        {
                            // Check for cancellation after each integer change in percent loaded.
                            if (loader.IsCanceled)
                            {
                                loader.UpdateProgress(status.Cancel());
                                return(false);
                            }

                            // If not cancelled, update progress.
                            loader.UpdateProgress(status = status.ChangePercentComplete(percent));
                        }

                        // Read spectrum header
                        ReadComplete(stream, specHeader, countHeader);

                        int seqKeyHash   = GetInt32(specHeader, ((int)SpectrumCacheHeader.seq_key_hash));
                        int seqKeyLength = GetInt32(specHeader, ((int)SpectrumCacheHeader.seq_key_length));
                        int charge       = GetInt32(specHeader, ((int)SpectrumCacheHeader.charge));
                        if (charge == 0 || charge > TransitionGroup.MAX_PRECURSOR_CHARGE)
                        {
                            throw new InvalidDataException(Resources.XHunterLibrary_Load_Invalid_precursor_charge_found_File_may_be_corrupted);
                        }
                        float i2        = BitConverter.ToSingle(specHeader, ((int)SpectrumCacheHeader.i2) * 4);
                        long  location  = BitConverter.ToInt64(specHeader, ((int)SpectrumCacheHeader.location_lo) * 4);
                        int   numPeaks  = GetInt32(specHeader, ((int)SpectrumCacheHeader.num_peaks));
                        float expect    = BitConverter.ToSingle(specHeader, ((int)SpectrumCacheHeader.expect) * 4);
                        int   seqLength = GetInt32(specHeader, (int)SpectrumCacheHeader.seq_len);

                        // Read sequence information
                        ReadComplete(stream, specSequence, seqLength);

                        LibKey key = new LibKey(specSequence, 0, seqLength, charge);
                        libraryEntries[i] = new XHunterSpectrumInfo(key, i2, expect, (short)numPeaks, location);

                        if (seqKeyLength > 0)
                        {
                            LibSeqKey seqKey = new LibSeqKey(key, seqKeyHash, seqKeyLength);
                            setSequences.Add(seqKey, true);
                        }
                    }

                    // Checksum = checksum.ChecksumValue;
                    _libraryEntries = libraryEntries;
                    _setSequences   = setSequences;

                    loader.UpdateProgress(status.Complete());

                    // Create the stream from which the spectra will be read
                    CreateStream(loader);
                }

                return(true);
            }
            catch (InvalidDataException x)
            {
                if (!cached)
                {
                    loader.UpdateProgress(status.ChangeErrorException(x));
                }
                return(false);
            }
            catch (IOException x)
            {
                if (!cached)
                {
                    loader.UpdateProgress(status.ChangeErrorException(x));
                }
                return(false);
            }
            catch (Exception x)
            {
                if (!cached)
                {
                    x = new Exception(string.Format(Resources.XHunterLibrary_Load_Failed_loading_library__0__, FilePath), x);
                    loader.UpdateProgress(status.ChangeErrorException(x));
                }
                return(false);
            }
            finally
            {
                if (ReadStream != null)
                {
                    // Close the read stream to ensure we never leak it.
                    // This only costs on extra open, the first time the
                    // active document tries to read.
                    try { ReadStream.CloseStream(); }
                    catch (IOException) { }
                }
            }
        }
 public void Cancel()
 {
     status.Cancel();
 }
Ejemplo n.º 12
0
        // ReSharper restore LocalizableElement

        public static List <string> ConvertPilotFiles(IList <string> inputFiles, IProgressMonitor progress, IProgressStatus status)
        {
            string groupConverterExePath    = null;
            var    inputFilesPilotConverted = new List <string>();

            for (int index = 0; index < inputFiles.Count; index++)
            {
                string inputFile = inputFiles[index];
                if (!inputFile.EndsWith(BiblioSpecLiteBuilder.EXT_PILOT))
                {
                    inputFilesPilotConverted.Add(inputFile);
                    continue;
                }
                string outputFile = Path.ChangeExtension(inputFile, BiblioSpecLiteBuilder.EXT_PILOT_XML);
                // Avoid re-converting files that have already been converted
                if (File.Exists(outputFile))
                {
                    // Avoid duplication, in case the user accidentally adds both .group and .group.xml files
                    // for the same results
                    if (!inputFiles.Contains(outputFile))
                    {
                        inputFilesPilotConverted.Add(outputFile);
                    }
                    continue;
                }

                string message = string.Format(Resources.VendorIssueHelper_ConvertPilotFiles_Converting__0__to_xml, Path.GetFileName(inputFile));
                int    percent = index * 100 / inputFiles.Count;
                progress.UpdateProgress(status = status.ChangeMessage(message).ChangePercentComplete(percent));

                if (groupConverterExePath == null)
                {
                    var key = Registry.LocalMachine.OpenSubKey(KEY_PROTEIN_PILOT, false);
                    if (key != null)
                    {
                        string proteinPilotCommandWithArgs = (string)key.GetValue(string.Empty);

                        var proteinPilotCommandWithArgsSplit =
                            // ReSharper disable LocalizableElement
                            proteinPilotCommandWithArgs.Split(new[] { "\" \"" }, StringSplitOptions.RemoveEmptyEntries);     // Remove " "%1"
                        // ReSharper restore LocalizableElement
                        string path = Path.GetDirectoryName(proteinPilotCommandWithArgsSplit[0].Trim(new[] { '\\', '\"' })); // Remove preceding "
                        if (!string.IsNullOrEmpty(path))
                        {
                            var groupFileExtractorPath = Path.Combine(path, EXE_GROUP_FILE_EXTRACTOR);
                            if (File.Exists(groupFileExtractorPath))
                            {
                                groupConverterExePath = groupFileExtractorPath;
                            }
                            else
                            {
                                var group2XmlPath = Path.Combine(path, EXE_GROUP2_XML);
                                if (File.Exists(group2XmlPath))
                                {
                                    groupConverterExePath = group2XmlPath;
                                }
                                else
                                {
                                    string errorMessage = string.Format(Resources.VendorIssueHelper_ConvertPilotFiles_Unable_to_find__0__or__1__in_directory__2____Please_reinstall_ProteinPilot_software_to_be_able_to_handle__group_files_,
                                                                        EXE_GROUP_FILE_EXTRACTOR, EXE_GROUP2_XML, path);
                                    throw new IOException(errorMessage);
                                }
                            }
                        }
                    }

                    if (groupConverterExePath == null)
                    {
                        throw new IOException(Resources.VendorIssueHelper_ConvertPilotFiles_ProteinPilot_software__trial_or_full_version__must_be_installed_to_convert___group__files_to_compatible___group_xml__files_);
                    }
                }

                // run group2xml
                // ReSharper disable LocalizableElement
                var argv = new[]
                {
                    "XML",
                    "\"" + inputFile + "\"",
                    "\"" + outputFile + "\""
                };
                // ReSharper restore LocalizableElement

                var psi = new ProcessStartInfo(groupConverterExePath)
                {
                    CreateNoWindow  = true,
                    UseShellExecute = false,
                    // Common directory includes the directory separator
                    // ReSharper disable once ConstantNullCoalescingCondition
                    WorkingDirectory       = Path.GetDirectoryName(groupConverterExePath) ?? string.Empty,
                    Arguments              = string.Join(@" ", argv.ToArray()),
                    RedirectStandardError  = true,
                    RedirectStandardOutput = true,
                };

                var sbOut = new StringBuilder();
                var proc  = new Process {
                    StartInfo = psi
                };
                proc.Start();

                var    reader = new ProcessStreamReader(proc);
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    if (progress.IsCanceled)
                    {
                        proc.Kill();
                        throw new LoadCanceledException(status.Cancel());
                    }

                    sbOut.AppendLine(line);
                }

                while (!proc.WaitForExit(200))
                {
                    if (progress.IsCanceled)
                    {
                        proc.Kill();
                        return(inputFilesPilotConverted);
                    }
                }

                if (proc.ExitCode != 0)
                {
                    throw new IOException(TextUtil.LineSeparate(string.Format(Resources.VendorIssueHelper_ConvertPilotFiles_Failure_attempting_to_convert_file__0__to__group_xml_,
                                                                              inputFile), string.Empty, sbOut.ToString()));
                }

                inputFilesPilotConverted.Add(outputFile);
            }
            progress.UpdateProgress(status.ChangePercentComplete(100));
            return(inputFilesPilotConverted);
        }
Ejemplo n.º 13
0
        private static void ConvertBrukerToMzml(string filePathBruker,
                                                string outputPath, IProgressMonitor monitor, IProgressStatus status)
        {
            // We use CompassXport, if it is installed, to convert a Bruker raw file to mzML.  This solves two
            // issues: the Bruker reader can't be called on any thread other than the main thread, and there
            // is no 64-bit version of the reader.  So we start CompassXport in its own 32-bit process,
            // and use it to convert the raw data to mzML in a temporary file, which we read back afterwards.
            var    key             = Registry.LocalMachine.OpenSubKey(KEY_COMPASSXPORT, false);
            string compassXportExe = (key != null) ? (string)key.GetValue(string.Empty) : null;

            if (compassXportExe == null)
            {
                throw new IOException(Resources.VendorIssueHelper_ConvertBrukerToMzml_CompassXport_software_must_be_installed_to_import_Bruker_raw_data_files_);
            }

            // CompassXport arguments
            // ReSharper disable LocalizableElement
            var argv = new[]
            {
                "-a \"" + filePathBruker + "\"",                    // input file (directory)
                "-o \"" + outputPath + "\"",                        // output file (directory)
                "-mode 2",                                          // mode 2 (mzML)
                "-raw 0"                                            // export line spectra (profile data is HUGE and SLOW!)
            };
            // ReSharper restore LocalizableElement

            // Start CompassXport in its own process.
            var psi = new ProcessStartInfo(compassXportExe)
            {
                CreateNoWindow  = true,
                UseShellExecute = false,
                // Common directory includes the directory separator
                WorkingDirectory       = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? string.Empty,
                Arguments              = string.Join(@" ", argv),
                RedirectStandardError  = true,
                RedirectStandardOutput = true,
            };
            var proc = new Process {
                StartInfo = psi
            };

            proc.Start();

            // CompassXport starts by calculating a hash of the input file.  This takes a long time, and there is
            // no intermediate output during this time.  So we set the progress bar some fraction of the way and
            // let it sit there and animate while we wait for the start of spectra processing.
            const int hashPercent = 25; // percentage of import time allocated to calculating the input file hash

            int spectrumCount = 0;

            var    sbOut  = new StringBuilder();
            var    reader = new ProcessStreamReader(proc);
            string line;

            while ((line = reader.ReadLine()) != null)
            {
                if (monitor.IsCanceled)
                {
                    proc.Kill();
                    throw new LoadCanceledException(status.Cancel());
                }

                sbOut.AppendLine(line);
                line = line.Trim();

                // The main part of conversion starts with the hash calculation.
                if (line.StartsWith(@"Calculating hash"))
                {
                    status = status.ChangeMessage(Resources.VendorIssueHelper_ConvertBrukerToMzml_Calculating_hash_of_input_file)
                             .ChangePercentComplete(hashPercent);
                    monitor.UpdateProgress(status);
                    continue;
                }

                // Determine how many spectra will be converted so we can track progress.
                // ReSharper disable LocalizableElement
                var match = Regex.Match(line, @"Converting (\d+) spectra");
                // ReSharper restore LocalizableElement
                if (match.Success)
                {
                    spectrumCount = int.Parse(match.Groups[1].Value);
                    continue;
                }

                // Update progress as each spectra batch is converted.
                // ReSharper disable LocalizableElement
                match = Regex.Match(line, @"Spectrum \d+ - (\d+)");
                // ReSharper restore LocalizableElement
                if (match.Success)
                {
                    var spectrumEnd     = int.Parse(match.Groups[1].Value);
                    var percentComplete = hashPercent + (100 - hashPercent) * spectrumEnd / spectrumCount;
                    status = status.ChangeMessage(line).ChangePercentComplete(percentComplete);
                    monitor.UpdateProgress(status);
                }
            }

            while (!proc.WaitForExit(200))
            {
                if (monitor.IsCanceled)
                {
                    proc.Kill();
                    throw new LoadCanceledException(status.Cancel());
                }
            }

            if (proc.ExitCode != 0)
            {
                throw new IOException(TextUtil.LineSeparate(string.Format(Resources.VendorIssueHelper_ConvertBrukerToMzml_Failure_attempting_to_convert__0__to_mzML_using_CompassXport_,
                                                                          filePathBruker), string.Empty, sbOut.ToString()));
            }
        }
Ejemplo n.º 14
0
        public static void MonitorProcessStatus(IProgressStatus monitor, TextReader reader, StringCollection progessLog)
        {
            string line;
            string exceptionText = null;

            while ((line = reader.ReadLine()) != null)
            {
                int i = line.IndexOf(':');
                if (i != -1)
                {
                    string tag    = line.Substring(0, i);
                    string txt    = line.Substring(i + 1);
                    bool   wasTag = true;

                    switch (tag)
                    {
                    case "process-ps-msg":
                        monitor.SetMessage(Decode(txt));
                        break;

                    case "process-ps-progress":
                        monitor.SetProgress(double.Parse(txt));
                        break;

                    case "process-ps-log":
                        monitor.Log(Decode(txt));
                        break;

                    case "process-ps-warning":
                        monitor.ReportWarning(Decode(txt));
                        break;

                    case "process-ps-exception":
                        exceptionText = Decode(txt);
                        if (exceptionText == string.Empty)
                        {
                            exceptionText = null;
                        }
                        break;

                    case "process-ps-error":
                        string err = Decode(txt);
                        if (err == string.Empty)
                        {
                            err = null;
                        }
                        monitor.ReportError(err, exceptionText != null ? new Exception(exceptionText) : null);
                        break;

                    case "process-ps-cancel":
                        monitor.Cancel();
                        break;

                    case "process-ps-plog":
                        progessLog.Add(Decode(txt));
                        break;

                    default:
                        wasTag = false;
                        break;
                    }
                    if (wasTag)
                    {
                        continue;
                    }
                }
                Console.WriteLine(line);
            }
        }
Ejemplo n.º 15
0
        protected void Complete(Exception x)
        {
            lock (this)
            {
                ChromatogramCache result = null;
                try
                {
                    if (x == null && !_status.IsFinal)
                    {
                        long locationScanIds = 0, countBytesScanIds = 0;
                        if (_fs.Stream != null)
                        {
                            try
                            {
                                locationScanIds   = _fs.Stream.Position;
                                countBytesScanIds = _fsScans.Stream.Position;

                                ChromatogramCache.WriteStructs(_fs.Stream,
                                                               _fsScans.Stream,
                                                               _fsPeaks.Stream,
                                                               _fsScores.Stream,
                                                               _listCachedFiles,
                                                               _listGroups,
                                                               _listTransitions,
                                                               _listTextIdBytes,
                                                               _listScoreTypes,
                                                               _scoreCount,
                                                               _peakCount);

                                _loader.StreamManager.Finish(_fs.Stream);
                                _fs.Stream = null;
                                // Allow cancelation right up to the final commit
                                if (!_loader.IsCanceled)
                                {
                                    _fs.Commit(_destinationStream);
                                }
                                else
                                {
                                    _loader.UpdateProgress(_status = _status.Cancel());
                                }
                            }
                            catch (Exception xWrite)
                            {
                                throw new IOException(TextUtil.LineSeparate(string.Format(Resources.ChromCacheWriter_Complete_Failure_attempting_to_write_the_file__0_, _fs.RealName), xWrite.Message));
                            }
                        }

                        if (!_status.IsCanceled)
                        {
                            // Create stream identifier, but do not open.  The stream will be opened
                            // the first time the document uses it.
                            var readStream = _loader.StreamManager.CreatePooledStream(CachePath, false);

                            // DebugLog.Info("{0}. {1} - created", readStream.GlobalIndex, CachePath);

                            _fsPeaks.Stream.Seek(0, SeekOrigin.Begin);
                            _fsScores.Stream.Seek(0, SeekOrigin.Begin);
                            var rawData = new ChromatogramCache.RawData
                            {
                                FormatVersion       = ChromatogramCache.FORMAT_VERSION_CACHE,
                                ChromCacheFiles     = _listCachedFiles.ToArray(),
                                ChromatogramEntries = _listGroups.ToArray(),
                                ChromTransitions    = _listTransitions.ToArray(),
                                ChromatogramPeaks   = new BlockedArray <ChromPeak>(
                                    count => ChromPeak.ReadArray(_fsPeaks.FileStream, count), _peakCount,
                                    ChromPeak.SizeOf, ChromPeak.DEFAULT_BLOCK_SIZE),
                                ScoreTypes = _listScoreTypes.ToArray(),
                                Scores     = new BlockedArray <float>(
                                    count => PrimitiveArrays.Read <float>(_fsScores.FileStream, count), _scoreCount,
                                    sizeof(float), ChromatogramCache.DEFAULT_SCORES_BLOCK_SIZE),
                                TextIdBytes       = _listTextIdBytes.ToArray(),
                                LocationScanIds   = locationScanIds,
                                CountBytesScanIds = countBytesScanIds,
                            };
                            result  = new ChromatogramCache(CachePath, rawData, readStream);
                            _status = _status.Complete();
                            _loader.UpdateProgress(_status);
                        }
                    }
                }
                catch (Exception x2)
                {
                    x = x2;
                }
                finally
                {
                    Dispose();
                }

                try
                {
                    _completed(result, x == null ? _status : _status.ChangeErrorException(x));
                }
                catch (Exception x2)
                {
                    _completed(null, _status.ChangeErrorException(x2));
                }
            }
        }
Ejemplo n.º 16
0
 public void Cancel()
 {
     FlushLog();
     status.Cancel();
 }