Exemplo n.º 1
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.º 2
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.º 3
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.º 4
0
        public static void ConvertFromOneFormatToAnother(string inputFileName, string outputFileName, ISequenceFormatter targetFormatter)
        {
            var parser       = SequenceParsers.FindParserByFileName(inputFileName);
            var sequenceList = parser.Parse();
            var sequences    = Helper.ConvertIenumerableToList(sequenceList);

            targetFormatter.Open(outputFileName);

            foreach (var sequence in sequences)
            {
                targetFormatter.Write(sequence);
            }

            targetFormatter.Close();
            targetFormatter.Dispose();
        }
        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.º 6
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();
            }
        }
        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.º 8
0
        protected void butBLAST_Click(object sender, EventArgs e)
        {
            string baseDir  = ConfigurationManager.AppSettings["BLASTbase"];
            string exomeRef = ConfigurationManager.AppSettings["RefName"];
            string file     = baseDir + @"temp\test" + DateTime.Now.ToString("yyyyMMddhhmmss");
            string ls       = Environment.NewLine.Normalize();

            if (!txtBlast.Text.StartsWith(">"))
            {
                txtBlast.Text = @">No definition line" + ls + txtBlast.Text;
            }
            using (MemoryStream mstrm = new MemoryStream(Encoding.UTF8.GetBytes(txtBlast.Text))) {
                ISequenceParser    parser    = SequenceParsers.Fasta;
                ISequenceFormatter formatter = SequenceFormatters.Fasta;
                try {
                    Sequence seq = (Sequence)parser.ParseOne(mstrm);
                    formatter.Format(seq, file + ".in");
                    txtBlast.Text     = @">" + seq.ID + ls + seq.ConvertToString(0);
                    ErrorMessage.Text = "";
                } catch (Exception ex) {
                    ErrorMessage.Text = ex.Message;
                    txtRes.Text       = "";
                    return;
                }
                parser.Close();
                formatter.Close();
            }
            Session["blast"] = txtBlast.Text;
            using (Process blast = new Process()) {
                blast.StartInfo.FileName        = baseDir + @"bin\blastn.exe";
                blast.StartInfo.UseShellExecute = false;
                blast.StartInfo.Arguments       = @"-task blastn -db " + baseDir + @"db\" + exomeRef + " -evalue 0.1 -outfmt 11 -query " + file + ".in -out " + file + ".asn -max_target_seqs 5";
                blast.Start();
                blast.WaitForExit();
            }
            if (File.Exists(file + ".asn"))
            {
                using (Process form = new Process()) {
                    form.StartInfo.FileName        = baseDir + @"bin\blast_formatter.exe";
                    form.StartInfo.UseShellExecute = false;
                    form.StartInfo.Arguments       = @"-archive " + file + ".asn" + @" -outfmt 5 -out " + file + ".xml";
                    form.Start();
                    form.WaitForExit();
                    form.StartInfo.Arguments = @"-archive " + file + ".asn" + @" -outfmt 0 -out " + file + ".txt";
                    form.Start();
                    form.WaitForExit();
                }
                using (StreamReader sr = new StreamReader(file + ".xml")) {
                    IBlastParser       blastParser = new BlastXmlParser();
                    List <BlastResult> blastres    = blastParser.Parse(sr.BaseStream).ToList();
                    ChartArea          area        = chartBlast.ChartAreas.FindByName("ChartArea1");
                    if (blastres.Count > 0)
                    {
                        BlastXmlMetadata meta = blastres[0].Metadata;
                        chartBlast.Titles.FindByName("blastTitle").Text = meta.QueryDefinition;
                        area.AxisY.Maximum = Math.Floor(meta.QueryLength + 5.0);
                        Series series = chartBlast.Series["Series1"];
                        series.Points.Clear();
                        int             i      = 0;
                        List <BlastHit> blasts = new List <BlastHit>();
                        chartBlast.Height = 30 * blastres[0].Records[0].Hits.Count + 50;
                        foreach (Hit hit in blastres[0].Records[0].Hits)
                        {
                            string contig = hit.Def;
                            for (int j = 0; j < hit.Hsps.Count; j++)
                            {
                                Hsp hsp = hit.Hsps[j];
                                int k   = series.Points.AddXY(i, hsp.QueryStart, hsp.QueryEnd);
                                if (j < 1)
                                {
                                    series.Points[k].Label = contig;
                                }
                                BlastHit bhit = new BlastHit();
                                bhit.seqID    = Convert.ToInt64(hit.Accession);
                                bhit.Contig   = contig;
                                bhit.Descr    = hit.Def;
                                bhit.Score    = hsp.BitScore.ToString("N2");
                                bhit.Evalue   = hsp.EValue.ToString("E2");
                                bhit.HitStart = hsp.HitStart.ToString();
                                bhit.HitEnd   = hsp.HitEnd.ToString();
                                bhit.Align    = hsp.AlignmentLength.ToString();
                                bhit.Frame    = hsp.QueryFrame > 0?"+":"-";
                                bhit.Frame   += @"/";
                                bhit.Frame   += hsp.HitFrame > 0 ? "+" : "-";
                                blasts.Add(bhit);
                            }
                            i++;
                        }
                        gridBLAST.DataSource = blasts;
                    }
                    else
                    {
                        gridBLAST.DataSource = null;
                        chartBlast.Height    = 1;
                    }
                    gridBLAST.DataBind();
                }
                using (StreamReader sr = new StreamReader(file + ".txt", Encoding.UTF8, true)) {
                    txtRes.Text = sr.ReadToEnd();
                }
            }
            else
            {
                txtRes.Text = "Nothing found.";
            }
            string[] tmpFiles = Directory.GetFiles(baseDir + @"temp\");
            foreach (string filePath in tmpFiles)
            {
                if (File.GetLastWriteTime(filePath).CompareTo(DateTime.Now.AddDays(-28)) < 0)
                {
                    File.Delete(filePath);
                }
            }
        }
Exemplo n.º 9
0
        public static void ConvertFromOneFormatToAnother(string inputFileName, string outputFileName, ISequenceFormatter targetFormatter)
        {
            var parser = SequenceParsers.FindParserByFileName(inputFileName);
            var sequenceList = parser.Parse();
            var sequences = Helper.ConvertIenumerableToList(sequenceList);

            targetFormatter.Open(outputFileName);

            foreach (var sequence in sequences)
            {
                targetFormatter.Write(sequence);
            }

            targetFormatter.Close();
            targetFormatter.Dispose();
        }