Exemplo n.º 1
0
        /// <summary>
        /// Save in one of .NET Bio supported formats like fasta or GenBank.
        /// </summary>
        /// <param name="path">Filename.</param>
        public void SaveAsBio(String path)
        {
            ISequenceFormatter formatter = SequenceFormatters.FindFormatterByFileName(path);

            formatter.Write(this.Sequence);
            formatter.Close();
        }
Exemplo n.º 2
0
        private void saveButton_Click(object sender, RoutedEventArgs e)
        {
            if (sequenceTextBox.Text != null)
            {
                InitializeSaveSequenceFileDialog();

                if (saveSequenceFileDialog.ShowDialog() == true)
                {
                    try
                    {
                        Fragment toSave = new Fragment();
                        toSave.Name = sequenceName;
                        ISequence seq = new Bio.Sequence(Alphabets.DNA, sequence);
                        toSave.Sequence = seq;
                        toSave.Length   = sequence.Length;
                        ISequenceFormatter formatter = SequenceFormatters.FindFormatterByFileName(saveSequenceFileDialog.FileName);
                        formatter.Write(seq);
                        formatter.Close();
                        //toSave.Construct.SaveAsBio(saveSequenceFileDialog.FileName);
                    }
                    catch (Exception ex)
                    {
                        MessageBoxResult result = ModernDialog.ShowMessage(ex.Message, "Exception", MessageBoxButton.OK);
                    }
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// This is invoked when the user clicks the Save menu item.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_transcribedSequence != null)
            {
                SaveFileDialog sfd = new SaveFileDialog()
                {
                    Title  = "Select Filename for Transcribed Sequence",
                    Filter = string.Join("|", SequenceFormatters.All
                                         .Select(sf => string.Format("{0}|{1}",
                                                                     sf.Name, sf.SupportedFileTypes.Replace(',', ';')))),
                    OverwritePrompt  = true,
                    RestoreDirectory = true
                };

                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    var formatter = SequenceFormatters.FindFormatterByFileName(sfd.FileName);
                    if (formatter != null)
                    {
                        formatter.Write(_transcribedSequence);
                        MessageBox.Show("Sequence has been saved to " + sfd.FileName, "Sequence Saved");
                        formatter.Close();
                    }
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// When implemented in a derived class, performs the execution of the activity.
        /// </summary>
        /// <param name="context">The execution context under which the activity executes.</param>
        protected override void Execute(CodeActivityContext context)
        {
            string             filename  = Filename.Get(context);
            ISequenceFormatter formatter = SequenceFormatters.FindFormatterByFileName(filename);

            if (formatter == null)
            {
                throw new ArgumentException("Could not determine formatter for " + filename);
            }

            if (LogOutput)
            {
                var tw = context.GetExtension <TextWriter>() ?? Console.Out;
                tw.WriteLine("Writing sequences to " + filename);
            }

            try
            {
                foreach (var s in Sequences.Get(context))
                {
                    formatter.Format(s);
                }
            }
            finally
            {
                formatter.Close();
            }
        }
Exemplo n.º 5
0
        public void FindGenBankFromatter()
        {
            string             dummyFileName = "dummy.gb";
            ISequenceFormatter formatter     = SequenceFormatters.FindFormatterByFile(dummyFileName);

            Assert.IsInstanceOf(typeof(GenBankFormatter), formatter);
        }
Exemplo n.º 6
0
        public void ReturnNoFormatter()
        {
            string             dummyFileName = "dummy.abc";
            ISequenceFormatter formatter     = SequenceFormatters.FindFormatterByFile(dummyFileName);

            Assert.AreEqual(formatter, null);
        }
Exemplo n.º 7
0
        /// <summary>
        /// convert input file to output file using the specified format conversion
        /// </summary>
        public void ConvertFile()
        {
            //make sure input file is valid
            if (!File.Exists(InputFile))
            {
                throw new Exception("Input file does not exist.");
            }

            //Finds a parser and opens the file
            var inputParser = SequenceParsers.FindParserByFileName(InputFile);

            if (inputParser == null)
            {
                throw new Exception("Input file not a valid file format to parse.");
            }

            IEnumerable <ISequence> sequences;

            //Finds a formatter and opens the file
            var outputFormatter = SequenceFormatters.FindFormatterByFileName(OutputFile);

            if (inputParser == null)
            {
                throw new Exception("Output file not a valid file format for conversion.");
            }

            sequences = inputParser.Parse();
            foreach (ISequence sequence in sequences)
            {
                outputFormatter.Write(sequence);
            }

            outputFormatter.Close();
            inputParser.Close();
        }
Exemplo n.º 8
0
        public void FindFastaFromater()
        {
            string             dummyFileName = "dummy.fasta";
            ISequenceFormatter formatter     = SequenceFormatters.FindFormatterByFile(dummyFileName);

            Assert.IsInstanceOf(typeof(FastaFormatter), formatter);

            dummyFileName = "dummy.fa";
            formatter     = SequenceFormatters.FindFormatterByFile(dummyFileName);
            Assert.IsInstanceOf(typeof(FastaFormatter), formatter);

            dummyFileName = "dummy.mpfa";
            formatter     = SequenceFormatters.FindFormatterByFile(dummyFileName);
            Assert.IsInstanceOf(typeof(FastaFormatter), formatter);

            dummyFileName = "dummy.fna";
            formatter     = SequenceFormatters.FindFormatterByFile(dummyFileName);
            Assert.IsInstanceOf(typeof(FastaFormatter), formatter);

            dummyFileName = "dummy.faa";
            formatter     = SequenceFormatters.FindFormatterByFile(dummyFileName);
            Assert.IsInstanceOf(typeof(FastaFormatter), formatter);

            dummyFileName = "dummy.fsa";
            formatter     = SequenceFormatters.FindFormatterByFile(dummyFileName);
            Assert.IsInstanceOf(typeof(FastaFormatter), formatter);

            dummyFileName = "dummy.fas";
            formatter     = SequenceFormatters.FindFormatterByFile(dummyFileName);
            Assert.IsInstanceOf(typeof(FastaFormatter), formatter);
        }
        static void Main(string[] args)
        {
            if (args.Length != 2)
            {
                Console.WriteLine("Need source and destination filenames.");
                return;
            }

            string sourceFilename = args[0];
            string destFilename   = args[1];

            ISequenceParser parser = SequenceParsers.FindParserByFileName(sourceFilename);

            if (parser == null)
            {
                parser = SequenceParsers.All.FirstOrDefault(
                    sp => sp.SupportedFileTypes.Contains(Path.GetExtension(sourceFilename)));
                if (parser == null)
                {
                    Console.WriteLine("Failed to locate parser for {0}", sourceFilename);
                    return;
                }
                parser.Open(sourceFilename);
            }

            ISequenceFormatter formatter = SequenceFormatters.FindFormatterByFileName(destFilename);

            if (formatter == null)
            {
                formatter = SequenceFormatters.All.FirstOrDefault(
                    sp => sp.SupportedFileTypes.Contains(Path.GetExtension(destFilename)));
                if (formatter == null)
                {
                    Console.WriteLine("Failed to locate formatter for {0}", destFilename);
                    return;
                }
                formatter.Open(destFilename);
            }

            foreach (var sequence in parser.Parse())
            {
                formatter.Write(sequence);
            }

            parser.Close();
            formatter.Close();
        }
Exemplo n.º 10
0
        /// <summary>
        /// Validates general Sequence Parser.
        /// </summary>
        /// <param name="nodeName">xml node name.</param>
        /// <param name="IsParser">IsParser is true if testcases is validating Parsers,
        /// false if formatter validation</param>
        void ValidateSequenceFileParser(string nodeName, bool IsParser)
        {
            // Gets the expected sequence from the Xml
            string[] filePaths = utilityObj.xmlUtil.GetTextValues(nodeName,
                                                                  Constants.FilePathsNode);
            string parserDescription = utilityObj.xmlUtil.GetTextValue(nodeName,
                                                                       Constants.DescriptionNode);
            string parserName = utilityObj.xmlUtil.GetTextValue(nodeName,
                                                                Constants.ParserNameNode);
            string fileTypes = utilityObj.xmlUtil.GetTextValue(nodeName,
                                                               Constants.FileTypesNode);

            // Get a default parser for the file types.
            for (int i = 0; i < filePaths.Length; i++)
            {
                if (IsParser)
                {
                    using (ISequenceParser parser = SequenceParsers.FindParserByFileName(filePaths[i]))
                    {
                        string description = parser.Description.Replace("\r", "").Replace("\n", "");
                        // Validate parser name, description and the file type supported by parser.
                        Assert.AreEqual(parserName, parser.Name);
                        Assert.AreEqual(parserDescription, description);
                        Assert.AreEqual(fileTypes, parser.SupportedFileTypes);
                    }
                }
                else
                {
                    using (ISequenceFormatter formatter =
                               SequenceFormatters.FindFormatterByFileName(filePaths[i]))
                    {
                        string description =
                            formatter.Description.Replace("\r", "").Replace("\n", "");
                        // Validate parser name, description and the file type supported by parser.
                        Assert.AreEqual(parserName, formatter.Name);
                        Assert.AreEqual(parserDescription, description);
                        Assert.AreEqual(fileTypes, formatter.SupportedFileTypes);
                    }
                }
            }

            Console.WriteLine(string.Format((IFormatProvider)null,
                                            "SequenceParser : Type of the parser is validated successfully"));
            ApplicationLog.WriteLine("Type of the parser is validated successfully");
        }
Exemplo n.º 11
0
        /// <summary>
        /// convert input file to output file using the specified format conversion
        /// </summary>
        public void ConvertFile()
        {
            //make sure input file is valid
            if (!File.Exists(this.InputFile))
            {
                throw new Exception("Input file does not exist.");
            }

            //Finds a parser and opens the file
            ISequenceParser inputParser = SequenceParsers.FindParserByFileName(this.InputFile);

            if (inputParser == null)
            {
                throw new Exception("Input file not a valid file format to parse.");
            }

            //Finds a formatter and opens the file
            ISequenceFormatter outputFormatter = SequenceFormatters.FindFormatterByFileName(this.OutputFile);

            if (outputFormatter == null)
            {
                throw new Exception("Output file not a valid file format for conversion.");
            }

            try
            {
                foreach (ISequence sequence in inputParser.Parse())
                {
                    outputFormatter.Format(sequence);
                }
            }
            catch
            {
                throw new OperationCanceledException(
                          string.Format(
                              "Unable to convert sequences from {0} to {1} - verify that the input sequences have the appropriate type of data to convert to the output formatter.",
                              inputParser.Name,
                              outputFormatter.Name));
            }
            finally
            {
                outputFormatter.Close();
                inputParser.Close();
            }
        }
Exemplo n.º 12
0
        public void ValidateSequenceFormatterFindFormatterByFileName()
        {
            IFormatter fastAObj = SequenceFormatters.FindFormatterByFileName("test.fasta");

            Assert.AreEqual(SequenceType.FastA.ToString(), fastAObj.Name);

            IFormatter gbkObj = SequenceFormatters.FindFormatterByFileName("test.gbk");

            Assert.AreEqual(SequenceType.GenBank.ToString(), gbkObj.Name);

            IFormatter fastQObj = SequenceFormatters.FindFormatterByFileName("test.fastq");

            Assert.AreEqual(SequenceType.FastQ.ToString(), fastQObj.Name);

            IFormatter gffObj = SequenceFormatters.FindFormatterByFileName("test.gff");

            Assert.AreEqual(SequenceType.GFF.ToString(), gffObj.Name);

            ApplicationLog.WriteLine("Sequence Formatter BVT : Successfully validated the FindFormatterByFileName() method");
        }
Exemplo n.º 13
0
        public void ValidateSequenceFormatterFindFormatterByFileName()
        {
            IFormatter fastAObj = SequenceFormatters.FindFormatterByFileName("test.fasta");

            Assert.AreEqual("FastA", fastAObj.Name);

            IFormatter gbkObj = SequenceFormatters.FindFormatterByFileName("test.gbk");

            Assert.AreEqual("GenBank", gbkObj.Name);

            IFormatter fastQObj = SequenceFormatters.FindFormatterByFileName("test.fastq");

            Assert.AreEqual("FastQ", fastQObj.Name);

            IFormatter gffObj = SequenceFormatters.FindFormatterByFileName("test.gff");

            Assert.AreEqual("GFF", gffObj.Name);

            ApplicationLog.WriteLine("Sequence Formatter BVT : Successfully validated the FindFormatterByFileName() method");
            Console.WriteLine("Sequence Formatter BVT : Successfully validated the FindFormatterByFileName() method");
        }
Exemplo n.º 14
0
        public static void StripNonAlphabets(string inputFile, string outputFile)
        {
            var parser       = SequenceParsers.FindParserByFileName(inputFile);
            var sequenceList = parser.Parse();
            var sequences    = Helper.ConvertIenumerableToList(sequenceList);

            var newSequences = new List <Sequence>();

            foreach (var sequence in sequences)
            {
                var sequenceChanged = false;
                var sequenceString  = Helper.ConvertSequenceToString(sequence);
                var newSequence     = "";
                foreach (var character in sequenceString.ToCharArray())
                {
                    if (char.IsLetter(character) || char.IsSeparator(character))
                    {
                        newSequence += character;
                    }
                    else
                    {
                        sequenceChanged = true;
                    }
                }

                // usuniêcie starszego alfabetu
                var seq = new Sequence(sequence.Alphabet, newSequence);
                newSequences.Add(seq);
            }

            // zapisanie sekwencji do pliku
            var formatter = SequenceFormatters.FindFormatterByFileName(outputFile);

            foreach (var sequence in newSequences)
            {
                formatter.Write(sequence);
            }

            formatter.Dispose();
        }
        private void save_Click(object sender, RoutedEventArgs e)
        {
            if (changeTextBox.Text != null)
            {
                InitializeSaveSequenceFileDialog();

                if (saveEditSequenceFileDialog.ShowDialog() == true)
                {
                    try
                    {
                        string             sequenceToSave = changeTextBox.Text;
                        ISequence          seq            = new Bio.Sequence(Alphabets.DNA, sequenceToSave);
                        ISequenceFormatter formatter      = SequenceFormatters.FindFormatterByFileName(saveEditSequenceFileDialog.FileName);
                        formatter.Write(seq);
                        formatter.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBoxResult result = ModernDialog.ShowMessage(ex.Message, "Exception", MessageBoxButton.OK);
                    }
                }
            }
        }
Exemplo n.º 16
0
        public void ValidateFindFormatterByFileName()
        {
            ISequenceFormatter formatter = SequenceFormatters.FindFormatterByFileName(Constants.InvalidFileName);

            ValidateFormatterExceptions(formatter);
        }
Exemplo n.º 17
0
        public static void ConcatenateSequences(string firstFile, string secondFile, string outputFile)
        {
            if (!SequenceParsers.IsFasta(firstFile))
            {
                Console.WriteLine("Nieprawidlowy format pierwszego pliku!");
                return;
            }

            if (!SequenceParsers.IsFasta(secondFile))
            {
                Console.WriteLine("Nieprawidlowy format drugiego pliku!");
                return;
            }

            var firstParser = SequenceParsers.FindParserByFileName(firstFile);

            firstParser.Alphabet = AmbiguousProteinAlphabet.Instance;
            var firstSequenceList  = firstParser.Parse();
            var firstFileSequences = Helper.ConvertIenumerableToList(firstSequenceList);

            // parsowanie drugiej listy
            var secondParser = SequenceParsers.FindParserByFileName(firstFile);

            secondParser.Alphabet = AmbiguousProteinAlphabet.Instance;
            var secondSequenceList  = secondParser.Parse();
            var secondFileSequences = Helper.ConvertIenumerableToList(secondSequenceList);

            // sprawdzenie czy wszystkie sekwencje wykorzystuj¹ ten sam alfabet
            IAlphabet alphabet         = DnaAlphabet.Instance;
            var       selectedAlphabet = false;

            foreach (var sequence in firstFileSequences)
            {
                if (!selectedAlphabet)
                {
                    alphabet         = sequence.Alphabet;
                    selectedAlphabet = true;
                }
                else if (sequence.Alphabet != alphabet)
                {
                    Console.WriteLine("Niedopasowane alfabety: " + alphabet.Name + ", " + sequence.Alphabet.Name);

                    return;
                }
            }

            foreach (var sequence in secondFileSequences)
            {
                if (!selectedAlphabet)
                {
                    alphabet         = sequence.Alphabet;
                    selectedAlphabet = true;
                }
                else if (sequence.Alphabet != alphabet)
                {
                    Console.WriteLine("Niedopasowane alfabety: " + alphabet.Name + ", " + sequence.Alphabet.Name);

                    return;
                }
            }

            Console.WriteLine("Uzywany alfabet: " + alphabet.Name);

            var firstListConcatenated  = "";
            var secondListConcatenated = "";

            foreach (var sequence in firstFileSequences)
            {
                firstListConcatenated += Helper.ConvertSequenceToString(sequence);
            }

            foreach (var sequence in secondFileSequences)
            {
                secondListConcatenated += Helper.ConvertSequenceToString(sequence);
            }

            var final = firstListConcatenated + secondListConcatenated;

            var concatenatedSequence = new Sequence(alphabet, final);

            concatenatedSequence.ID = "Concatenated sequence";

            var formatter = SequenceFormatters.FindFormatterByFileName(outputFile);

            formatter.Write(concatenatedSequence);
            formatter.Dispose();
        }
        public void ValidateFindFormatterByName()
        {
            ISequenceFormatter formatter = SequenceFormatters.FindFormatterByName(Constants.FastaTempFileName, Constants.InvalidFileName);

            this.ValidateFormatterExceptions(formatter);
        }