Exemplo n.º 1
0
        private static object ReadDictionaryNonGeneric <T>(ReaderState reader)
        {
            var count  = reader.Read <int>();
            var result = CreateDictionaryResultObject <T>(count);

            switch (result)
            {
            case IDictionary d:
            {
                for (var i = 0; i < count; i++)
                {
                    var key   = Deserializer.ReadValue(reader);
                    var value = Deserializer.ReadValue(reader);
                    d.Add(key, value);
                }

                break;
            }

            default:
            {
                for (var i = 0; i < count; i++)
                {
                    Discarder.DiscardValue(reader);     // key
                    Discarder.DiscardValue(reader);     // value
                }

                break;
            }
            }

            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// This executes the discarding of reads on a background thread
        /// </summary>
        /// <param name="sender">BackgoundWorker instance</param>
        /// <param name="e">DoWorkEvent arguments</param>
        private void DoDiscardReads(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            if (worker != null)
            {
                try
                {
                    Discarder      myDiscarder = null;
                    FilterToolArgs args        = null;

                    if (e.Argument is DiscardByLengthArgs)
                    {
                        args        = e.Argument as DiscardByLengthArgs;
                        myDiscarder = ((DiscardByLengthArgs)args).discarder;

                        // If possible, verify that the discard length is not larger than the maximum read length.
                        MessageBoxResult tooLargeReadLength = TryConfirmLargeReadLength(args.InputInfo.Filename, (myDiscarder as DiscardByLength).MinLengthThreshold);
                        if (tooLargeReadLength == MessageBoxResult.Cancel)
                        {
                            e.Cancel = true;
                            return;
                        }
                    }
                    else if (e.Argument is DiscardByMeanQualityArgs)
                    {
                        args        = e.Argument as DiscardByMeanQualityArgs;
                        myDiscarder = (e.Argument as DiscardByMeanQualityArgs).discarder;
                    }
                    else
                    {
                        args        = e.Argument as DiscardByRegexArgs;
                        myDiscarder = (e.Argument as DiscardByRegexArgs).discarder;
                    }
                    myDiscarder.DiscardReads();

                    // Report results
                    string report = string.Format(Resource.DiscardReport, myDiscarder.DiscardCount, myDiscarder.Counted);

                    this.OpenInExplorer(args.OutputFilename);

                    e.Result = report.ToString();
                }
                catch (Exception ex)
                {
                    e.Cancel = true;
                    if (worker.CancellationPending)
                    {
                        return;
                    }
                    MessageBox.Show(ex.TargetSite + ": " + ex.Message);
                }
            }
        }
Exemplo n.º 3
0
            private static void Populate(object obj, ReaderState reader, IDictionary <string, IMemberSetterHandler <ReaderState> > setterHandlers)
            {
                while (Reader.ReadEnumerableType(reader) == EnumerableType.HasItem)
                {
                    var key = reader.ReadString();
                    if (!setterHandlers.TryGetValue(key, out var setter))
                    {
                        Discarder.DiscardValue(reader);
                        continue;
                    }

                    setter.Handle(reader, obj);
                }
            }
Exemplo n.º 4
0
        private static object ReadHEnumerableNonGeneric <T>(ReaderState reader)
        {
            var elementType = Reader.ReadSerializedType(reader);
            var result      = CreateResultObject <T>();

            if (result is IList l)
            {
                Deserializer.ReadValuesIntoList(reader, elementType, l);
            }
            else
            {
                Discarder.DiscardValues(reader, elementType);
            }

            return(result);
        }
Exemplo n.º 5
0
        private static object ReadEnumerableNonGeneric <T>(ReaderState reader)
        {
            var result = CreateResultObject <T>();

            if (result is IList l)
            {
                while (Reader.ReadEnumerableType(reader) == EnumerableType.HasItem)
                {
                    l.Add(Deserializer.ReadValue(reader));
                }
            }
            else
            {
                while (Reader.ReadEnumerableType(reader) == EnumerableType.HasItem)
                {
                    Discarder.DiscardValue(reader);
                }
            }

            return(result);
        }
Exemplo n.º 6
0
        public async Task ReadFile(StreamReader reader)
        {
            string        line;
            IGedcomReader objectReader = new Discarder();

            while ((line = reader.ReadLine()) != null)
            {
                PropertyLine pline = new PropertyLine(line);

                if (pline.Level == 0)
                {
                    // save previous object
                    await objectReader.Store(this.treeService).ConfigureAwait(false);

                    switch (pline.Value)
                    {
                    case "INDI":
                        objectReader = new IndividualReader(pline);
                        break;

                    case "FAM":
                        objectReader = new FamilyReader(pline);
                        break;

                    default:
                        // HEAD, TRLR, SOUR, NOTE, ...
                        objectReader = new Discarder();
                        break;
                    }
                }
                else
                {
                    objectReader.ProcessNextLine(pline);
                }
            }

            // and store the last object (although that would be an ignored TRLR in a real file)
            await objectReader.Store(this.treeService).ConfigureAwait(false);
        }
Exemplo n.º 7
0
        private static object ReadListNonGeneric <T>(ReaderState reader)
        {
            var count  = reader.Read <int>();
            var result = CreateResultObject <T>(count);

            if (result is IList l)
            {
                for (var i = 0; i < count; i++)
                {
                    l.Add(Deserializer.ReadValue(reader));
                }
            }
            else
            {
                for (var i = 0; i < count; i++)
                {
                    Discarder.DiscardValue(reader);
                }
            }

            return(result);
        }
Exemplo n.º 8
0
        static void Main(string[] args)
        {
            Console.Error.WriteLine(SplashString());
            CommandLineOptions myArgs = ProcessCommandLine(args);

            #region Discarding
            // Determine parser
            InputSubmission input = new InputSubmission(myArgs.FileList[0]);
            input.DetermineParserUtil();

            // Create a sequence formatter object
            ISequenceFormatter filteredFormatter;
            ISequenceFormatter discardedFormatter = null;

            // If the format is FASTA, then output will be FASTA.
            // Everything else (assuming quality scores are available)
            // will be outputted to FASTQ.
            if (input.Parser is FastAParser)
            {
                filteredFormatter = new FastAFormatter(myArgs.FileList[1]);

                if (myArgs.DiscardedFile != null)
                {
                    discardedFormatter = new FastAFormatter(myArgs.DiscardedFile);
                }
            }
            else
            {
                filteredFormatter = new FastQFormatter(myArgs.FileList[1]);

                if (myArgs.DiscardedFile != null)
                {
                    discardedFormatter = new FastQFormatter(myArgs.DiscardedFile);
                }
            }

            // Initialize a Trimmer object
            Discarder myDiscarder = null;

            // By now, we should have sanity checked the command line arguments. So we should be able to
            // figure out what mode is being used simply by checking the properties.
            if (myArgs.DiscardByLength > 0)
            {
                myDiscarder = new DiscardByLength(input.Parser, filteredFormatter, discardedFormatter, myArgs.DiscardByLength);
            }

            else if (myArgs.DiscardByQuality > 0)
            {
                if (!(input.Parser is FastQParser))
                {
                    Console.Error.WriteLine("Input file must be in FASTQ format.");
                    Environment.Exit(-1);
                }

                myDiscarder = new DiscardByMeanQuality(input.Parser, filteredFormatter, discardedFormatter, (byte)myArgs.DiscardByQuality);
            }

            else
            {
                // Should never reach this line.
                Console.Error.WriteLine("Invalid trim mode. Use '-l' or '-q'.");
                Environment.Exit(-1);
            }

            myDiscarder.DiscardReads();

            #endregion

            if (myArgs.Verbose)
            {
                Console.Error.WriteLine("Discarded {0}/{1} sequences.", myDiscarder.DiscardCount, myDiscarder.Counted);
                Console.Error.WriteLine("Non-discarded sequences saved in {0}.", Path.GetFullPath(myArgs.FileList[1]));
                if (myArgs.DiscardedFile != null)
                {
                    Console.Error.WriteLine("Discarded sequences saved in {0}.", Path.GetFullPath(myArgs.DiscardedFile));
                    discardedFormatter.Close();
                }
                Console.Error.WriteLine("Warning: Output may not be in the same order as the original input.");
            }
            input.Parser.Close();
            filteredFormatter.Close();
            if (discardedFormatter != null)
            {
                discardedFormatter.Close();
            }
        }