/// <summary> /// This method loads new sequences from a file. /// </summary> private void OnLoadFile() { string filterString = "All Supported Formats|" + string.Join(";", SequenceParsers.All.Select(parser => parser.SupportedFileTypes.Replace(',', ';').Replace(".", "*."))) + "|" + string.Join("|", SequenceParsers.All.Select(parser => string.Format("{0}|{1}", parser.Name, parser.SupportedFileTypes.Replace(',', ';').Replace(".", "*.")))); OpenFileDialog openFileDialog = new OpenFileDialog { CheckFileExists = true, Filter = filterString }; // Prompt the user for the filename if (openFileDialog.ShowDialog() == true) { // See if we can auto-locate the parser ISequenceParser parser = SequenceParsers.FindParserByFileName(openFileDialog.FileName); if (parser == null) { // Use the extension string fileExtension = Path.GetExtension(openFileDialog.FileName); parser = SequenceParsers.All.FirstOrDefault(sp => sp.SupportedFileTypes.Contains(fileExtension)); } // Cannot parse this file. if (parser == null) { MessageBox.Show(string.Format("Cannot locate a sequence parser for {0}", openFileDialog.FileName), "Cannot Parse File"); return; } // Parse the file - open it read-only as we will not be writing the sequences back out. try { foreach (var sequence in parser.Parse()) { LoadedSequences.Add(new SequenceViewModel(this, sequence)); } } catch (Exception ex) { ShowError("Cannot Parse File", "Failed to open " + openFileDialog.FileName, ex); } finally { parser.Close(); } } }
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(); }
/// <summary> /// This method loads a single sequence file. FASTA is the preferred format. /// </summary> /// <param name="sequenceFilename"></param> /// <param name="molNames"></param> /// <param name="distinct"></param> /// <returns></returns> public static List <ISequence> LoadSequenceFile(string sequenceFilename, string[] molNames, bool distinct = true) { if (string.IsNullOrWhiteSpace(sequenceFilename)) { throw new ArgumentOutOfRangeException(nameof(sequenceFilename)); } if (!File.Exists(sequenceFilename)) { throw new FileNotFoundException(sequenceFilename); } List <ISequence> sequences = null; ISequenceParser sequenceParser = null; try { sequenceParser = SequenceParsers.FindParserByFileName(sequenceFilename); } catch (DirectoryNotFoundException directoryNotFoundException) { // just forward exception for now throw new DirectoryNotFoundException(directoryNotFoundException.Message, directoryNotFoundException.InnerException); } if (sequenceParser != null) { sequences = sequenceParser.Parse().ToList(); sequenceParser.Close(); if (distinct) { sequences = sequences.Distinct().ToList(); } } if (sequences != null && sequences.Count > 0 && molNames != null && molNames.Length > 0) { sequences = sequences.Where(a => molNames.Contains(SequenceIdSplit.SequenceIdToPdbIdAndChainId(a.ID).Mol)).ToList(); } return(sequences); }
/// <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(); } }
/// <summary> /// Parses a sequence given a file name. Uses built in mechanisms to detect the /// appropriate parser based on the file name. /// </summary> /// <param name="fileName">The path of the file to be parsed for a sequence</param> public void ParseSequence(string fileName) { ISequenceParser parser = SequenceParsers.FindParserByFileName(fileName); if (parser == null) { throw new ArgumentException("Could not find an appropriate parser for " + fileName); } // Get the first sequence from the file SequenceToSplit = parser.Parse().FirstOrDefault(); parser.Close(); if (SequenceToSplit == null) { throw new ArgumentException("Unable to parse a sequence from file " + fileName); } }
/// <summary> /// Parses a sequence given a file name. Uses built in mechanisms to detect the /// appropriate parser based on the file name. /// </summary> /// <param name="fileName">The path of the file to be parsed for a sequence</param> internal void ParseSequence(string fileName) { ISequenceParser parser = SequenceParsers.FindParserByFileName(fileName); if (parser == null) { throw new ArgumentException("Could not find an appropriate parser for " + fileName); } IEnumerable <ISequence> sequences = parser.Parse(); if (sequences == null) { throw new ArgumentException("Unable to parse a sequence from file " + fileName); } SequenceToSplit = sequences.ElementAt(0); parser.Close(); }
public void openProject(String file) { Sequence sequence = null; parser = SequenceParsers.GenBank; parser.Open(file); sequence = (Sequence)parser.Parse().ToList()[0]; parser.Close(); Fragment project = new Fragment(file, "project", sequence); GenBankMetadata meta = sequence.Metadata["GenBank"] as GenBankMetadata; FragmentDict = new Dictionary <string, Fragment>(); foreach (var feat in meta.Features.MiscFeatures) { String subseq = project.GetString().Substring(feat.Location.LocationStart - 1, feat.Location.LocationEnd - feat.Location.LocationStart + 1); FragmentDict.Add(feat.StandardName, new Fragment(file, feat.StandardName, new Sequence(Alphabets.DNA, subseq))); } }
/// <summary> /// sequenceParser method /// method for parsing sequences from file /// return tuple(list of codons, number of cds's) /// </summary> /// <param name="file"></param> public static Tuple <List <string>, int> sequenceParser(string file) { parser = SequenceParsers.FindParserByFileName(file); List <ISequence> sequences = new List <ISequence>(); List <string> list = new List <string>(); // temp variables string seqTemp; // parsing sequence try { using (parser) { sequences = parser.Parse().ToList(); foreach (ISequence seq in sequences) { // getString method initialization seqTemp = getString(seq); // adding codon substrings for (int i = 0; i < seqTemp.Length - 2; i += 3) { list.Add(seqTemp.Substring(i, 3).ToUpper()); } } } parser.Close(); } catch (System.IO.FileFormatException) { string message = "Something went wrong. Probably you tried to use an improper file. Try again. \nFor more information about using Codon Context Ranking check the \"How to use\" page."; ModernDialog.ShowMessage(message.ToString(), "Warning", MessageBoxButton.OK); } return(new Tuple <List <string>, int>(list, sequences.Count)); }
public void openProject(String file) { Sequence sequence = null; parser = SequenceParsers.GenBank; parser.Open(file); sequence = (Sequence)parser.Parse().ToList()[0]; parser.Close(); Fragment project = new Fragment(file, "project", sequence); GenBankMetadata meta = sequence.Metadata["GenBank"] as GenBankMetadata; FragmentDict = new Dictionary<string, Fragment>(); foreach (var feat in meta.Features.MiscFeatures) { String subseq = project.GetString().Substring(feat.Location.LocationStart-1, feat.Location.LocationEnd - feat.Location.LocationStart + 1); FragmentDict.Add(feat.StandardName, new Fragment(file, feat.StandardName, new Sequence(Alphabets.AmbiguousDNA, subseq))); } }
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); } } }