Ejemplo n.º 1
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.º 2
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) { }
                }
            }
        }
Ejemplo n.º 3
0
        public override Uri  SendZipFile(Server server, string folderPath, string zipFilePath, IProgressMonitor progressMonitor)
        {
            _progressMonitor = progressMonitor;
            _progressStatus  = new ProgressStatus(String.Empty);
            var zipFileName = Path.GetFileName(zipFilePath) ?? string.Empty;

            // Upload zip file to pipeline folder.
            using (_webClient = new NonStreamBufferingWebClient())
            {
                _webClient.UploadProgressChanged += webClient_UploadProgressChanged;
                _webClient.UploadFileCompleted   += webClient_UploadFileCompleted;

                _webClient.Headers.Add(HttpRequestHeader.Authorization, server.AuthHeader);
                var     webDav         = PanoramaUtil.Call(server.URI, "pipeline", folderPath, "getPipelineContainer", true); // Not L10N
                var     webDavInfo     = _webClient.UploadString(webDav, PanoramaUtil.FORM_POST, string.Empty);
                JObject jsonWebDavInfo = JObject.Parse(webDavInfo);

                string webDavUrl = (string)jsonWebDavInfo["webDavURL"]; // Not L10N

                // Upload Url minus the name of the zip file.
                var baseUploadUri = new Uri(server.URI, webDavUrl);
                // Use Uri.EscapeDataString instead of Uri.EscapleUriString.
                // The latter will not escape characters such as '+' or '#'
                var escapedZipFileName = Uri.EscapeDataString(zipFileName);
                var tmpUploadUri       = new Uri(baseUploadUri, escapedZipFileName + ".part"); // Not L10N
                var uploadUri          = new Uri(baseUploadUri, escapedZipFileName);

                lock (this)
                {
                    // Write to a temp file first. This will be renamed after a successful upload or deleted if the upload is canceled.
                    // Add a "Temporary" header so that LabKey marks this as a temporary file.
                    // https://www.labkey.org/issues/home/Developer/issues/details.view?issueId=19220
                    _webClient.Headers.Add("Temporary", "T");                     // Not L10N
                    _webClient.UploadFileAsync(tmpUploadUri, "PUT", zipFilePath); // Not L10N

                    // Wait for the upload to complete
                    Monitor.Wait(this);
                }

                if (progressMonitor.IsCanceled)
                {
                    // Delete the temporary file on the server
                    progressMonitor.UpdateProgress(
                        _progressStatus =
                            _progressStatus.ChangeMessage(
                                Resources.WebPanoramaPublishClient_SendZipFile_Deleting_temporary_file_on_server));
                    DeleteTempZipFile(tmpUploadUri, server.AuthHeader);
                    return(null);
                }
                // Make sure the temporary file was uploaded to the server
                ConfirmFileOnServer(tmpUploadUri, server.AuthHeader);

                // Rename the temporary file
                _progressStatus = _progressStatus.ChangeMessage(
                    Resources.WebPanoramaPublishClient_SendZipFile_Renaming_temporary_file_on_server);
                progressMonitor.UpdateProgress(_progressStatus);

                RenameTempZipFile(tmpUploadUri, uploadUri, server.AuthHeader);

                _progressStatus = _progressStatus.ChangeMessage(Resources.WebPanoramaPublishClient_SendZipFile_Waiting_for_data_import_completion___);
                progressMonitor.UpdateProgress(_progressStatus = _progressStatus.ChangePercentComplete(-1));

                // Data must be completely uploaded before we can import.
                Uri importUrl = PanoramaUtil.Call(server.URI, "targetedms", folderPath, "skylineDocUploadApi"); // Not L10N

                // Need to tell server which uploaded file to import.
                var dataImportInformation = new NameValueCollection
                {
                    // For now, we only have one root that user can upload to
                    { "path", "./" },                                                                                      // Not L10N
                    { "file", zipFileName }                                                                                // Not L10N
                };
                byte[] responseBytes  = _webClient.UploadValues(importUrl, PanoramaUtil.FORM_POST, dataImportInformation); // Not L10N
                string response       = Encoding.UTF8.GetString(responseBytes);
                JToken importResponse = JObject.Parse(response);

                // ID to check import status.
                var details   = importResponse["UploadedJobDetails"];                                                 // Not L10N
                int rowId     = (int)details[0]["RowId"];                                                             // Not L10N
                Uri statusUri = PanoramaUtil.Call(server.URI, "query", folderPath, "selectRows",                      // Not L10N
                                                  "query.queryName=job&schemaName=pipeline&query.rowId~eq=" + rowId); // Not L10N
                bool complete = false;
                // Wait for import to finish before returning.
                Uri result = null;
                while (!complete)
                {
                    if (progressMonitor.IsCanceled)
                    {
                        return(null);
                    }

                    string statusResponse  = _webClient.UploadString(statusUri, PanoramaUtil.FORM_POST, string.Empty);
                    JToken jStatusResponse = JObject.Parse(statusResponse);
                    JToken rows            = jStatusResponse["rows"];                            // Not L10N
                    var    row             = rows.FirstOrDefault(r => (int)r["RowId"] == rowId); // Not L10N
                    if (row == null)
                    {
                        continue;
                    }

                    string status = (string)row["Status"];                                 // Not L10N
                    result = new Uri(server.URI, (string)row["_labkeyurl_Description"]);   // Not L10N
                    if (string.Equals(status, "ERROR"))                                    // Not L10N
                    {
                        var jobUrl = new Uri(server.URI, (string)row["_labkeyurl_RowId"]); // Not L10N
                        var e      = new PanoramaImportErrorException(server.URI, jobUrl);
                        progressMonitor.UpdateProgress(
                            _progressStatus = _progressStatus.ChangeErrorException(e));
                        throw e;
                    }

                    complete = string.Equals(status, "COMPLETE"); // Not L10N
                }

                progressMonitor.UpdateProgress(_progressStatus.Complete());
                return(result);
            }
        }
Ejemplo n.º 4
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));
                }
            }
        }