public HaploGrepSharp.NewSearchMethods.HaploTypeReport OutputAssembly(string fileNamePrefix)
 {
     if (SuccessfulAssembly)
     {
         FastAFormatter fa           = new FastAFormatter(fileNamePrefix + "BestGreedyAssembly.fna");
         StringBuilder  sb           = new StringBuilder(StaticResources.CRS_LENGTH);
         var            bestAssembly = GreedyPathAssembly;
         bestAssembly.FinalizeAndOrientToReference();
         Bio.Sequence s = new Bio.Sequence(bestAssembly.Sequence);
         s.ID = "GreedyAssembly - length=" + AssemblyLength.ToString();                 // + bestAssembly.FirstReferencePosition.Value.ToString() + " - " + GreedyPathAssembly.LastReferencePosition.Value.ToString();
         fa.Write(s);
         fa.Close();
         //Now report all differences as well
         StreamWriter  sw           = new StreamWriter(fileNamePrefix + "Report.txt");
         var           searcher     = new HaploGrepSharp.NewSearchMethods.HaplotypeSearcher();
         List <string> linesToWrite = new List <string> ();
         var           report       = searcher.GetHaplotypeReport(s, linesToWrite, fileNamePrefix);
         foreach (var l in linesToWrite)
         {
             sw.WriteLine(l);
         }
         sw.Close();
         return(report);
     }
     return(null);
 }
Exemple #2
0
        /// <summary>
        /// The execution method for the activity.
        /// </summary>
        /// <param name="executionContext">The execution context.</param>
        /// <returns>The execution status.</returns>
        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            FastAFormatter formatter = new FastAFormatter();

            formatter.Open(OutputFile);

            if ((Sequence == null) && (SequenceList != null))
            {
                foreach (ISequence sequence in SequenceList)
                {
                    formatter.Write(sequence);
                }
            }
            else if ((Sequence != null) && (SequenceList == null))
            {
                formatter.Write(Sequence);
            }
            else if ((Sequence != null) && (SequenceList != null))
            {
                foreach (ISequence sequence in SequenceList)
                {
                    formatter.Write(sequence);
                }

                formatter.Write(Sequence);
            }

            formatter.Close();
            return(ActivityExecutionStatus.Closed);
        }
Exemple #3
0
        /// <summary>
        /// Get the blast service request object with all the request parameter set
        /// </summary>
        /// <param name="sequence">Input sequece</param>
        /// <param name="parameters">Blast parameters</param>
        /// <returns>Blast service request object</returns>
        private static BlastSerivceRequest GetRequestParameter(
            ISequence sequence,
            BlastParameters parameters)
        {
            BlastSerivceRequest blastParameter = new BlastSerivceRequest();

            // Sets the format of output expected from Azure Blast service
            blastParameter.OptionM = OPTIONMVALUE;

            // Sets the name of Job owner
            blastParameter.Owner          = Resources.OWNERVALUE;
            blastParameter.ParitionNumber = PARTITIONVALUE;

            // Convert string to byte
            string        inputContent  = FastAFormatter.FormatString(sequence);
            ASCIIEncoding asciiEncoding = new ASCIIEncoding();

            blastParameter.InputContent = asciiEncoding.GetBytes(inputContent);

            // Other parameters
            // Set the Title of Job
            blastParameter.Title = sequence.ID;

            // Name of the database to be searched in
            blastParameter.DatabaseName = parameters.Settings[PARAMETERDATABASE];

            // Type of search program to be executed
            blastParameter.ProgramName = parameters.Settings[PARAMETERPROGRAM];

            return(blastParameter);
        }
Exemple #4
0
        /// <summary>
        /// Writes the contigs to the file.
        /// </summary>
        /// <param name="assembly">IDeNovoAssembly parameter is the result of running De Novo Assembly on a set of two or more sequences. </param>
        protected void writeContigs(PadenaAssembly assembly)
        {
            if (assembly.AssembledSequences.Count == 0)
            {
                Output.WriteLine(OutputLevel.Results, "\tNo sequences assembled.");
                return;
            }
            ensureContigNames(assembly.AssembledSequences);

            if (!string.IsNullOrEmpty(this.DiagnosticFilePrefix))
            {
                using (FastAFormatter formatter = new FastAFormatter(ContigFileName)) {
                    formatter.AutoFlush = true;
                    foreach (ISequence seq in assembly.AssembledSequences)
                    {
                        formatter.Write(seq);
                    }
                }
                Output.WriteLine(OutputLevel.Information, "\tWrote {0} sequences to {1}", assembly.AssembledSequences.Count, ContigFileName);
            }
            else
            {
                Output.WriteLine(OutputLevel.Information, "\tAssembled Sequence Results: {0} sequences", assembly.AssembledSequences.Count);
                using (FastAFormatter formatter = new FastAFormatter()) {
                    formatter.Open(new StreamWriter(Console.OpenStandardOutput()));
                    formatter.MaxSymbolsAllowedPerLine = decideOutputWidth();
                    formatter.AutoFlush = true;
                    foreach (ISequence seq in assembly.AssembledSequences)
                    {
                        formatter.Write(seq);
                    }
                }
            }
        }
Exemple #5
0
        /// <summary>
        /// It writes Contigs to the file.
        /// </summary>
        /// <param name="scaffolds">The list of scaffolds sequence.</param>
        private void WriteContigs(IList <ISequence> scaffolds)
        {
            if (scaffolds.Count == 0)
            {
                Output.WriteLine(OutputLevel.Information, "No Scaffolds generated.");
                return;
            }

            EnsureContigNames(scaffolds);

            if (!string.IsNullOrEmpty(this.OutputFile))
            {
                FastAFormatter formatter = new FastAFormatter {
                    AutoFlush = true
                };
                using (formatter.Open(this.OutputFile))
                {
                    formatter.Format(scaffolds);
                }
                Output.WriteLine(OutputLevel.Information, "Wrote {0} scaffolds to {1}", scaffolds.Count, this.OutputFile);
            }
            else
            {
                Output.WriteLine(OutputLevel.Information, "Scaffold Results: {0} sequences", scaffolds.Count);
                FastAFormatter formatter = new FastAFormatter {
                    MaxSymbolsAllowedPerLine = Math.Min(80, Console.WindowWidth - 2),
                    AutoFlush = true
                };
                formatter.Format(Console.OpenStandardOutput(), scaffolds);
            }
        }
Exemple #6
0
        public void FastAFormatterValidateWrite()
        {
            using (FastAFormatter formatter = new FastAFormatter(Constants.FastaTempFileName))
            {
                // Gets the actual sequence and the alphabet from the Xml
                string actualSequence = utilityObj.xmlUtil.GetTextValue(Constants.SimpleFastaNodeName,
                                                                        Constants.ExpectedSequenceNode);
                string alpName = utilityObj.xmlUtil.GetTextValue(Constants.SimpleFastaNodeName,
                                                                 Constants.AlphabetNameNode);
                // Logs information to the log file
                ApplicationLog.WriteLine(string.Format((IFormatProvider)null,
                                                       "FastA Formatter BVT: Validating with Sequence '{0}' and Alphabet '{1}'.",
                                                       actualSequence, alpName));
                Sequence seqOriginal = new Sequence(Utility.GetAlphabet(alpName),
                                                    actualSequence);
                seqOriginal.ID = "";
                Assert.IsNotNull(seqOriginal);
                // Use the formatter to write the original sequences to a temp file
                ApplicationLog.WriteLine(string.Format((IFormatProvider)null,
                                                       "FastA Formatter BVT: Creating the Temp file '{0}'.", Constants.FastaTempFileName));
                formatter.Write(seqOriginal);
                formatter.Close();
                IEnumerable <ISequence> seqsNew = null;

                // Read the new file, then compare the sequences
                using (FastAParser parser = new FastAParser(Constants.FastaTempFileName))
                {
                    parser.Alphabet = Alphabets.Protein;
                    seqsNew         = parser.Parse();
                    char[] seqString   = seqsNew.ElementAt(0).Select(a => (char)a).ToArray();
                    string newSequence = new string(seqString);
                    Assert.IsNotNull(seqsNew);

                    ApplicationLog.WriteLine(string.Format((IFormatProvider)null,
                                                           "FastA Formatter BVT: New Sequence is '{0}'.",
                                                           newSequence));

                    // Now compare the sequences.
                    int countNew = seqsNew.Count();
                    Assert.AreEqual(1, countNew);
                    ApplicationLog.WriteLine("The Number of sequences are matching.");
                    Assert.AreEqual(seqOriginal.ID, seqsNew.ElementAt(0).ID);
                    string orgSeq = new string(seqsNew.ElementAt(0).Select(a => (char)a).ToArray());

                    Assert.AreEqual(orgSeq, newSequence);
                    Console.WriteLine(string.Format((IFormatProvider)null,
                                                    "FastA Formatter BVT: The FASTA sequences '{0}' are matching with Format() method and is as expected.",
                                                    newSequence));

                    ApplicationLog.WriteLine(string.Format((IFormatProvider)null,
                                                           "FastA Formatter BVT: The FASTA sequences '{0}' are matching with Format() method.",
                                                           newSequence));
                }

                // Passed all the tests, delete the tmp file. If we failed an Assert,
                // the tmp file will still be there in case we need it for debugging.
                File.Delete(Constants.FastaTempFileName);
                ApplicationLog.WriteLine("Deleted the temp file created.");
            }
        }
Exemple #7
0
        public void FastAFormatterValidateGetDescription()
        {
            var    formatter = new FastAFormatter();
            string desc      = formatter.Description;

            Assert.IsNotNull(desc);
        }
Exemple #8
0
        /// <summary>
        /// Returns parser which supports the specified file.
        /// </summary>
        /// <param name="fileName">File name for which the parser is required.</param>
        /// <param name="formatterName">Name of the formatter to use.</param>
        /// <returns>If found returns the formatter as ISequenceFormatter else returns null.</returns>
        public static ISequenceFormatter FindFormatterByName(string fileName, string formatterName)
        {
            ISequenceFormatter formatter = null;

            if (!string.IsNullOrEmpty(fileName))
            {
                if (formatterName == Properties.Resource.FastAName)
                {
                    formatter = new FastAFormatter(fileName);
                }
                else if (formatterName == Properties.Resource.FastQName)
                {
                    formatter = new FastQFormatter(fileName);
                }
                else if (formatterName == Properties.Resource.GENBANK_NAME)
                {
                    formatter = new GenBankFormatter(fileName);
                }
                else if (formatterName == Properties.Resource.GFF_NAME)
                {
                    formatter = new GffFormatter(fileName);
                }
                else
                {
                    formatter = null;
                }
            }

            return(formatter);
        }
Exemple #9
0
        public void ValidateSeqFormatterProperties()
        {
            // Gets the expected sequence from the Xml
            string fastaFormatterName = this.utilityObj.xmlUtil.GetTextValue(Constants.FastAFileParserNode,
                                                                             Constants.ParserNameNode);
            string genBankFormatterName = this.utilityObj.xmlUtil.GetTextValue(Constants.GenBankFileParserNode,
                                                                               Constants.ParserNameNode);
            string gffFormatterName = this.utilityObj.xmlUtil.GetTextValue(Constants.GffFileParserNode,
                                                                           Constants.ParserNameNode);
            string fastQFormatterName = this.utilityObj.xmlUtil.GetTextValue(Constants.FastQFileParserNode,
                                                                             Constants.ParserNameNode);

            // Get SequenceFormatter class properties.
            FastAFormatter actualFastAFormatter = SequenceFormatters.Fasta;
            IReadOnlyList <ISequenceFormatter> allFormatters = SequenceFormatters.All;
            GenBankFormatter actualgenBankFormatterName      = SequenceFormatters.GenBank;
            FastQFormatter   actualFastQFormatterName        = SequenceFormatters.FastQ;
            GffFormatter     actualGffFormatterName          = SequenceFormatters.Gff;

            // Validate Sequence Formatter
            Assert.AreEqual(fastaFormatterName, actualFastAFormatter.Name);
            Assert.AreEqual(genBankFormatterName, actualgenBankFormatterName.Name);
            Assert.AreEqual(gffFormatterName, actualGffFormatterName.Name);
            Assert.AreEqual(fastQFormatterName, actualFastQFormatterName.Name);
            Assert.IsNotNull(allFormatters);
            ApplicationLog.WriteLine("Type of the parser is validated successfully");
        }
Exemple #10
0
        /// <summary>
        /// Returns formatter which supports the specified file.
        /// </summary>
        /// <param name="fileName">File name for which the formatter is required.</param>
        /// <returns>If found returns the formatter as ISequenceFormatter else returns null.</returns>
        public static ISequenceFormatter FindFormatterByFileName(string fileName)
        {
            ISequenceFormatter formatter = null;

            if (!string.IsNullOrEmpty(fileName))
            {
                if (IsFasta(fileName))
                {
                    formatter = new FastAFormatter(fileName);
                }
                else if (IsFastQ(fileName))
                {
                    formatter = new FastQFormatter(fileName);
                }
                else if (IsGenBank(fileName))
                {
                    formatter = new GenBankFormatter(fileName);
                }
                else if (fileName.EndsWith(Properties.Resource.GFF_FILEEXTENSION, StringComparison.InvariantCultureIgnoreCase))
                {
                    formatter = new GffFormatter(fileName);
                }
                else
                {
                    formatter = null;
                }
            }

            return(formatter);
        }
Exemple #11
0
 /// <summary>
 /// Writes the reads to StandardOutput.
 /// </summary>
 /// <param name="reads">Reads to write.</param>
 private static void WriteOutput(IEnumerable <ISequence> reads)
 {
     foreach (ISequence seq in reads)
     {
         Console.WriteLine(FastAFormatter.FormatString(seq));
     }
 }
Exemple #12
0
        /// <summary>
        /// Convert a list of ISequences to FASTA format and write to file. In order to reduce the amount of compute time required
        /// by BLAST, we limit the number of sequences being fed to BLAST.
        /// </summary>
        /// <param name="sequences">IEnumerable list of Sequence objects</param>
        /// <param name="output">Name of the output FASTA file</param>
        /// <param name="maxSequences">Optional maximum number of sequences to convert</param>
        /// <param name="overwrite">If true, any existing file with the same name will be overwritten. Otherwise, the file will not be overwritten and conversion will be skipped.</param>
        /// <returns>True if a Fasta file was written, false if it already exists</returns>
        public static bool ConvertToFASTA(IEnumerable <ISequence> sequences, string output, int maxSequences, bool overwrite = false)
        {
            // If conditions:
            // 1. File doesn't exist; OR
            // 2. File exists but is empty; OR
            // 3. File exists but overwrite flag is set.
            if (!File.Exists(output) || new FileInfo(output).Length == 0 || overwrite)
            {
                FastAFormatter fa = new FastAFormatter(output);

                int count = 0;
                foreach (var seqObj in sequences)
                {
                    fa.Write(seqObj);
                    ++count;

                    if (count >= maxSequences)
                    {
                        break;
                    }
                }

                fa.Close();
                return(true);
            }
            return(false);
        }
Exemple #13
0
        /// <summary>
        /// Write sequences to the file
        /// </summary>
        /// <param name="sequences"></param>
        private void WriteSequences(IEnumerable <ISequence> sequences)
        {
            if (!string.IsNullOrEmpty(this.OutputFile))
            {
                int count = 0;
                using (var formatter = new FastAFormatter(this.OutputFile))
                {
                    formatter.AutoFlush = true;
                    foreach (ISequence sequence in sequences)
                    {
                        count++;
                        formatter.Write(sequence);
                    }
                }
                Output.WriteLine(OutputLevel.Information, "Wrote {0} sequences to {1}.", count, this.OutputFile);
            }
            else
            {
                Output.WriteLine(OutputLevel.Information, "Results:");

                foreach (ISequence seq in sequences)
                {
                    Output.WriteLine(OutputLevel.Results, seq.ID);
                    Output.WriteLine(OutputLevel.Results, new string(seq.Select(a => (char)a).ToArray()));
                }
            }
        }
        /// <summary>
        /// Returns parser which supports the specified file.
        /// </summary>
        /// <param name="fileName">File name for which the parser is required.</param>
        /// <param name="formatterName">Name of the formatter to use.</param>
        /// <returns>If found returns the formatter as ISequenceFormatter else returns null.</returns>
        public static ISequenceFormatter FindFormatterByName(string fileName, string formatterName)
        {
            ISequenceFormatter formatter = null;

            if (!string.IsNullOrEmpty(fileName))
            {
                if (formatterName == Properties.Resource.FastAName)
                {
                    formatter = new FastAFormatter(fileName);
                }
                else if (formatterName == Properties.Resource.FastQName)
                {
                    formatter = new FastQFormatter(fileName);
                }
                else if (formatterName == Properties.Resource.GENBANK_NAME)
                {
                    formatter = new GenBankFormatter(fileName);
                }
                else
                {
                    // Do a search through the known formatters to pick up custom formatters added through add-in.
                    formatter = All.FirstOrDefault(p => p.Name == formatterName);
                    // If we found a match based on extension, then open the file - this
                    // matches the above behavior where a specific formatter was created for
                    // the passed filename - the formatter is opened automatically in the constructor.
                    if (formatter != null)
                    {
                        formatter.Open(fileName);
                    }
                }
            }

            return(formatter);
        }
Exemple #15
0
        /// <summary>
        ///     Validates general FastA Parser test cases which are further Formatted
        ///     with the xml node name specified.
        /// </summary>
        /// <param name="nodeName">xml node name.</param>
        private void ValidateParseFormatGeneralTestCases(string nodeName)
        {
            // Gets the expected sequence from the Xml
            string filePath = utilityObj.xmlUtil.GetTextValue(nodeName, Constants.FilePathNode);
            string alphabet = utilityObj.xmlUtil.GetTextValue(nodeName, Constants.AlphabetNameNode);

            Assert.IsTrue(File.Exists(filePath));
            string filepathTmp = Path.Combine(Path.GetTempPath(), "temp.fasta");

            // Ensure output is deleted
            if (File.Exists(filepathTmp))
            {
                File.Delete(filepathTmp);
            }

            List <ISequence> seqsOriginal;

            using (var parserObj = new FastAParser(filePath))
            {
                // Read the original file
                parserObj.Alphabet = Utility.GetAlphabet(alphabet);
                seqsOriginal       = parserObj.Parse().ToList();
                Assert.IsFalse(seqsOriginal.Count == 0);
            }

            // Write to a new file
            using (var formatter = new FastAFormatter(filepathTmp))
            {
                formatter.Write(seqsOriginal);
            }

            try
            {
                // Compare original with new file
                using (var parserObjNew = new FastAParser(filepathTmp))
                {
                    // Read the new file, then compare the sequences
                    parserObjNew.Alphabet = Utility.GetAlphabet(alphabet);
                    IEnumerable <ISequence> seqsNew = parserObjNew.Parse();
                    Assert.IsNotNull(seqsNew);

                    int count = 0;
                    foreach (ISequence newSequence in seqsNew)
                    {
                        string s1 = seqsOriginal[count].ConvertToString();
                        string s2 = newSequence.ConvertToString();
                        Assert.AreEqual(s1, s2);
                        count++;
                    }

                    Assert.AreEqual(count, seqsOriginal.Count, "Number of sequences is different.");
                }
            }
            finally
            {
                // Delete new file
                File.Delete(filepathTmp);
            }
        }
Exemple #16
0
        /// <summary>
        /// CreateJob() to get job id and control id and Submit job
        /// using input sequence with job id and control id
        /// </summary>
        /// <param name="sequence">input sequences</param>
        /// <param name="parameters">input params</param>
        /// <returns>result params with job id and control id</returns>
        public ServiceParameters SubmitRequest(IList <ISequence> sequence, ClustalWParameters parameters)
        {
            ServiceParameters result = new ServiceParameters();

            if (null == sequence)
            {
                throw new ArgumentNullException("Sequence");
            }

            if (null == parameters)
            {
                throw new ArgumentNullException("Parameters");
            }

            // ClusterOption = biosim cbsum1 cbsum2k8 cbsusrv05 cbsum2 or Auto
            string[] output = _baseClient.CreateJob(tAppId.P_CLUSTALW, "test_BioHPC_Job", "1", parameters.Values[ClustalWParameters.Email].ToString(),
                                                    string.Empty, parameters.Values[ClustalWParameters.ClusterOption].ToString());

            if (!output[0].Contains(ERROR))
            {
                result.JobId = output[1];
                result.Parameters.Add(CONTROLID, output[2]);

                AppInputData  inputData     = _baseClient.InitializeApplicationParams(tAppId.P_CLUSTALW, "test_BioHPC_Job");
                StringBuilder inputSequence = new StringBuilder();

                foreach (ISequence seq in sequence)
                {
                    inputSequence.AppendLine(FastAFormatter.FormatString(seq));
                }

                inputData.clustalw.inputsource = QuerySrcType.paste;
                inputData.clustalw.inputstring = inputSequence.ToString();
                inputData.clustalw.isDNA       = false;
                inputData.clustalw.action      = (ClwActions)Enum.Parse(typeof(ClwActions),
                                                                        parameters.Values[ClustalWParameters.ActionAlign].ToString());
                inputData.clustalw.email_notify = true;

                _baseClient.SubmitJob(result.JobId, result.Parameters[CONTROLID].ToString(), inputData);
                result.Parameters.Add(SUBMISSONRESULT, SUCCESS);

                // Only if the event is registered, invoke the thread
                if (null != RequestCompleted)
                {
                    // Start the BackGroundThread to check the status of job
                    _workerThread = new BackgroundWorker();
                    _workerThread.WorkerSupportsCancellation = true;
                    _workerThread.DoWork             += new DoWorkEventHandler(ProcessRequestThread);
                    _workerThread.RunWorkerCompleted += new RunWorkerCompletedEventHandler(CompletedRequestThread);
                    _workerThread.RunWorkerAsync(result);
                }
            }
            else
            {
                result.Parameters.Add(SUBMISSONRESULT, output[0]);
            }

            return(result);
        }
        /// <summary>
        /// Exports a given list of sequences to a file in FastA format
        /// </summary>
        /// <param name="sequences">List of Sequences to be exported.</param>
        /// <param name="filename">Target filename.</param>
        static void ExportFastA(ICollection <ISequence> sequences, string filename)
        {
            // A formatter to export the output
            FastAFormatter formatter = new FastAFormatter(filename);

            // Exports the sequences to a file
            formatter.Write(sequences);
        }
        /// <summary>
        /// Exports a given sequence to a file in FastA format
        /// </summary>
        /// <param name="sequence">Sequence to be exported.</param>
        /// <param name="filename">Target filename.</param>
        static void ExportFastA(ISequence sequence, string filename)
        {
            // A formatter to export the output
            FastAFormatter formatter = new FastAFormatter(filename);

            // Exports the sequence to a file
            formatter.Write(sequence);
        }
Exemple #19
0
        public void FastAFormatterValidateGetName()
        {
            var    formatter = new FastAFormatter();
            string name      = formatter.Name;

            Assert.IsNotNull(name);
            Assert.AreEqual(name, "FastA");
        }
Exemple #20
0
        public void FastAFormatterValidateWriteWithStream()
        {
            string actualSequence = string.Empty;

            using (var formatter = new FastAFormatter())
            {
                using (var writer = new StreamWriter(Constants.FastaTempFileName))
                {
                    formatter.Open(writer);

                    // Gets the actual sequence and the alphabet from the Xml
                    actualSequence = utilityObj.xmlUtil.GetTextValue(Constants.SimpleFastaNodeName,
                                                                     Constants.ExpectedSequenceNode);
                    string alpName = utilityObj.xmlUtil.GetTextValue(Constants.SimpleFastaNodeName,
                                                                     Constants.AlphabetNameNode);

                    // Logs information to the log file
                    ApplicationLog.WriteLine(string.Format(null,
                                                           "FastA Formatter BVT: Validating with Sequence '{0}' and Alphabet '{1}'.",
                                                           actualSequence, alpName));
                    var seqOriginal = new Sequence(Utility.GetAlphabet(alpName),
                                                   actualSequence);

                    seqOriginal.ID = "";
                    Assert.IsNotNull(seqOriginal);
                    // Use the formatter to write the original sequences to a stream.
                    ApplicationLog.WriteLine(string.Format(null,
                                                           "FastA Formatter BVT: Creating the Temp file '{0}'.",
                                                           Constants.FastaTempFileName));
                    formatter.Write(seqOriginal);
                    formatter.Close();
                }
                IEnumerable <ISequence> seq = null;

                using (var reader = new StreamReader(Constants.FastaTempFileName))
                {
                    // Read the new file, then compare the sequences
                    using (var parser = new FastAParser())
                    {
                        parser.Alphabet = Alphabets.Protein;
                        seq             = parser.Parse(reader);

                        //Create a list of sequences.
                        List <ISequence> seqsList = seq.ToList();
                        Assert.IsNotNull(seqsList);

                        var seqString = new string(seqsList[0].Select(a => (char)a).ToArray());
                        Assert.AreEqual(actualSequence, seqString);
                    }
                }

                // Passed all the tests, delete the tmp file. If we failed an Assert,
                // the tmp file will still be there in case we need it for debugging.
                File.Delete(Constants.FastaTempFileName);
                ApplicationLog.WriteLine("Deleted the temp file created.");
            }
        }
Exemple #21
0
 public void FastAFormatterValidateGetName()
 {
     using (FastAFormatter formatter = new FastAFormatter())
     {
         string name = formatter.Name;
         Assert.IsNotNull((object)name);
         Assert.AreEqual(name, "FastA");
     }
 }
Exemple #22
0
        private void ValidateFormatterGeneralTestCases(string nodeName)
        {
            // Gets the actual sequence and the alphabet from the Xml
            string expectedSequence  = utilityObj.xmlUtil.GetTextValue(nodeName, Constants.ExpectedSequenceNode);
            string formattedSequence = expectedSequence.Replace("\r", "").Replace("\n", "").Replace(" ", "");

            string alphabet = utilityObj.xmlUtil.GetTextValue(nodeName, Constants.AlphabetNameNode);

            // Logs information to the log file
            ApplicationLog.WriteLine(string.Format(null,
                                                   "FastA Formatter : Validating with Sequence '{0}' and Alphabet '{1}'.",
                                                   expectedSequence, alphabet));

            // Replacing all the empty characters, Paragraphs and null entries added
            // while formatting the xml.
            ISequence seqOriginal = new Sequence(Utility.GetAlphabet(alphabet), formattedSequence)
            {
                ID = "test"
            };

            Assert.IsNotNull(seqOriginal);

            // Write it to a file
            var formatter = new FastAFormatter();
            {
                // Use the formatter to write the original sequences to a temp file
                ApplicationLog.WriteLine(string.Format("FastA Formatter : Creating the Temp file '{0}'.",
                                                       Constants.FastaTempFileName));
                formatter.Format(seqOriginal, Constants.FastaTempFileName);
            }

            // Read the new file, then compare the sequences
            var parserObj = new FastAParser();

            {
                parserObj.Alphabet = Utility.GetAlphabet(alphabet);
                IEnumerable <ISequence> seqsNew = parserObj.Parse(Constants.FastaTempFileName);

                // Get a single sequence
                ISequence seqNew = seqsNew.FirstOrDefault();
                Assert.IsNotNull(seqNew);

                string newSequence = seqNew.ConvertToString();
                ApplicationLog.WriteLine(string.Format(null, "FastA Formatter : New Sequence is '{0}'.", newSequence));
                Assert.AreEqual(formattedSequence, newSequence);
                Assert.AreEqual(seqOriginal.ID, seqNew.ID);

                // Verify only one sequence exists.
                Assert.AreEqual(1, seqsNew.Count());
            }

            // Passed all the tests, delete the tmp file. If we failed an Assert,
            // the tmp file will still be there in case we need it for debugging.
            File.Delete(Constants.FastaTempFileName);
            ApplicationLog.WriteLine("Deleted the temp file created.");
        }
Exemple #23
0
 private void WriteSequences(IEnumerable <ISequence> sequences)
 {
     using (FastAFormatter ff = new FastAFormatter(this.OutputFile))
     {
         foreach (ISequence sequence in sequences)
         {
             ff.Write(sequence);
         }
     }
 }
Exemple #24
0
        /// <summary>
        /// Writes delta for query sequences.
        /// </summary>
        /// <param name="delta">The Deltas.</param>
        private void WriteDelta(
            IList<List<IEnumerable<DeltaAlignment>>> delta)
        {
            TextWriter textWriterConsoleOutSave = Console.Out;
            if (!string.IsNullOrEmpty(this.OutputFile))
            {
                FileStream fileStreamConsoleOut = new FileStream(this.OutputFile, FileMode.Create);
                StreamWriter streamWriterConsoleOut = new StreamWriter(fileStreamConsoleOut);
                Console.SetOut(streamWriterConsoleOut);
                streamWriterConsoleOut.AutoFlush = true;
            }

            foreach (var alignment in delta)
            {
                foreach (IEnumerable<DeltaAlignment> align in alignment)
                {
                    foreach (DeltaAlignment deltaAlignment in align)
                    {
                        Console.WriteLine(
                            ">" +
                            deltaAlignment.ReferenceSequenceId);
                        if (deltaAlignment.QuerySequence != null)
                        {
                            Console.WriteLine(FastAFormatter.FormatString(deltaAlignment.QuerySequence).Substring(1));
                        }
                        else
                        {
                            //To provide empty lines in place of query sequence id and query sequence
                            Console.WriteLine();
                            Console.WriteLine();
                        }
                        Console.WriteLine(
                            deltaAlignment.FirstSequenceStart + " " +
                            deltaAlignment.FirstSequenceEnd + " " +
                            deltaAlignment.SecondSequenceStart + " " +
                            deltaAlignment.SecondSequenceEnd + " " +
                            deltaAlignment.Errors + " " +
                            deltaAlignment.SimilarityErrors + " " +
                            deltaAlignment.NonAlphas);

                        foreach (long deltas in deltaAlignment.Deltas)
                        {
                            Console.WriteLine(deltas);
                        }

                        Console.WriteLine("*");
                    }
                    Console.WriteLine("--");
                }
            }

            Console.SetOut(textWriterConsoleOutSave);
        }
Exemple #25
0
 public void FastAFormatterInvalidateClose()
 {
     try
     {
         FastAFormatter formatter = new FastAFormatter();
         formatter.Close();
         Assert.Fail();
     }
     catch (InvalidOperationException ex)
     {
         ApplicationLog.Write("Fasta P2 : InvalidOperationException caught successfully. " + ex.Message);
     }
 }
Exemple #26
0
        public void FastAFormatterInvalidateFormatString()
        {
            ISequence iSeq = null;

            try
            {
                string formattedString = FastAFormatter.FormatString(iSeq);
                Assert.Fail();
            }
            catch (ArgumentNullException ex)
            {
                ApplicationLog.Write("Fasta P2 : ArgumentNullException caught successfully. " + ex.Message);
            }
        }
Exemple #27
0
        public void FastAFormatterValidateGetSupportedFileTypes()
        {
            var    formatter         = new FastAFormatter();
            string supportedFileType = formatter.SupportedFileTypes;

            Assert.IsNotNull(supportedFileType);
            Assert.IsTrue(supportedFileType.Contains(".fa"));
            Assert.IsTrue(supportedFileType.Contains(".mpfa"));
            Assert.IsTrue(supportedFileType.Contains(".fna"));
            Assert.IsTrue(supportedFileType.Contains(".faa"));
            Assert.IsTrue(supportedFileType.Contains(".fsa"));
            Assert.IsTrue(supportedFileType.Contains(".fas"));
            Assert.IsTrue(supportedFileType.Contains(".fasta"));
        }
Exemple #28
0
        /// <summary>
        /// Writes ambiguous reads that are filtered out to the specified file.
        /// </summary>
        /// <param name="ambiguousReads">Reads with ambiguous symbols.</param>
        /// <param name="ambiguousFilename">File to write.</param>
        private static void WriteAmbiguousReads(BlockingCollection <ISequence> ambiguousReads, string ambiguousFilename)
        {
            FastAFormatter formatter = new FastAFormatter(ambiguousFilename);

            while (!ambiguousReads.IsCompleted)
            {
                ISequence seq;
                if (ambiguousReads.TryTake(out seq, -1))
                {
                    formatter.Write(seq);
                    formatter.Flush();
                }
            }

            formatter.Close();
        }
Exemple #29
0
        static void OldMain(string[] args)
        {
            StreamWriter SW = new StreamWriter(HOME + "CountsByDate.csv");
            StreamReader SR = new StreamReader("FileLocations.csv");

            string[] lines = SR.ReadToEnd().Split('\n');
            lines = lines.Skip(1).ToArray();
            //  Parallel.ForEach(lines, line =>
            Console.WriteLine("Starting");
            foreach (string line in lines)
            {
                Console.WriteLine(line);

                try
                {
                    string[]       split     = line.Split(',');
                    string         fname     = split[0];
                    string         patientid = split[1];
                    string         date      = split[2];
                    var            mtReads   = MitoDataGrabber.OutputMitoReadsFromBamFile(fname);
                    FastAFormatter fao       = new FastAFormatter(HOME + patientid + ".fa");
                    long           count     = 0;
                    foreach (var seq in mtReads)
                    {
                        count++;
                        fao.Write(seq);
                    }
                    fao.Close();
                    FileInfo FI   = new FileInfo(fname);
                    string   size = FI.Length.ToString();
                    lock (SW)
                    {
                        SW.WriteLine(String.Join(",", patientid, count.ToString(), size, date));
                        Console.WriteLine(patientid + " has " + count.ToString() + " reads");
                    }
                    if (args.Length > 2)
                    {
                        break;
                    }
                }
                catch (Exception thrown)
                { Console.WriteLine(thrown.Message); }
            }
            //);
            SW.Close();
        }
        public static async void AssemblySequences(string fastqFileName)
        {
            var parser = new FastQParser();
            List <IQualitativeSequence> sequences = new List <IQualitativeSequence>();

            using (var fileStream = new FileStream(fastqFileName, FileMode.Open))
            {
                sequences = parser.Parse(fileStream).ToList();
            }
            OverlapDeNovoAssembler assembler = new OverlapDeNovoAssembler();
            IDeNovoAssembly        assembly  = assembler.Assemble(sequences);

            FastAFormatter outputFormatter = new FastAFormatter();

            outputFormatter.Open("assembled_sequences.fasta");
            outputFormatter.Format(assembly.AssembledSequences);
            outputFormatter.Close();
        }