/// <summary> /// Utility method invokes the method, logs the attempt, and any exception. /// </summary> private static bool tryInvokeMethod( object target, MethodInfo method, object[] args, ISequence <object> traceStack) { traceStack.Add($"Trying method '{method}' for: '{target.GetType().GetFriendlyFullName()}'."); try { method.Invoke(target, args); return(true); } catch (Exception exception) { traceStack.Add(exception); return(false); } }
/// <summary> /// Checks if 'node' can be added to 'path' without causing a loop. /// If yes, adds node to path and returns true. If not, returns false. /// </summary> /// <param name="contigPath">List of graph nodes corresponding to contig path</param> /// <param name="contigSequence">Sequence of contig being assembled</param> /// <param name="nextNode">Next node on the path to be addded</param> /// <param name="isForwardDirection">Boolean indicating direction</param> /// <param name="isSameOrientation">Boolean indicating orientation</param> /// <returns>Boolean indicating if path was updated successfully</returns> private bool CheckAndAddNode( List <DeBruijnNode> contigPath, ISequence contigSequence, DeBruijnNode nextNode, bool isForwardDirection, bool isSameOrientation) { if (contigPath.Contains(nextNode)) { // there is a loop in this link // Return false indicating no update has been made return(false); } else { // Add node to contig list contigPath.Add(nextNode); // Update contig sequence with sequence from next node ISequence nextSequence = isSameOrientation ? _graph.GetNodeSequence(nextNode) : _graph.GetNodeSequence(nextNode).ReverseComplement; if (isForwardDirection) { contigSequence.Add(nextSequence.Last()); } else { contigSequence.Insert(0, nextSequence.First()); } return(true); } }
/// <summary> /// Utility method invokes the constructor, logs the attempt, and any exception. /// </summary> private static bool tryInvokeConstructor( ConstructorInfo constructor, object[] args, out object newInstance, ISequence <object> traceStack) { traceStack.Add( $"Trying constructor '{constructor}'" + $" for: '{constructor.ReflectedType?.GetFriendlyFullName()}'."); try { newInstance = constructor.Invoke(args); Debug.Assert(constructor.ReflectedType != null, "constructor.ReflectedType != null"); return(constructor.ReflectedType.IsInstanceOfType(newInstance)); } catch (Exception exception) { traceStack.Add(exception); newInstance = null; return(false); } }
/// <summary> /// Utility method returns all Public and NonPublic constructors, /// sorted with the most parameters first. /// </summary> private static IEnumerable <ConstructorInfo> findConstructors( Type type, bool?requireAttributes, IReadOnlyCollection <Type> attributeTypes, ISequence <object> traceStack) { List <ConstructorInfo> constructors = type.GetConstructors( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) .ToList(); if (constructors.Count == 0) { traceStack.Add($"No constructors found for '{type.GetFriendlyFullName()}'."); } if (requireAttributes == false) { constructors.Sort(ServiceConstructorMethods.attributeParameterCountSort <ConstructorInfo>(attributeTypes)); return(constructors); } if ((requireAttributes == true) || (constructors.FindIndex( ServiceConstructorMethods.hasAttributePredicate <ConstructorInfo>(attributeTypes)) >= 0)) { constructors.RemoveAll(ServiceConstructorMethods.hasAttributePredicate <ConstructorInfo>(attributeTypes, true)); } if (constructors.Count == 0) { traceStack.Add( $"No selected constructors found for '{type.GetFriendlyFullName()}'" + $" --- {attributeTypes.ToStringCollection(256, t => t.GetFriendlyFullName())}."); } constructors.Sort( ServiceConstructorMethods .attributeParameterCountSort <ConstructorInfo>(attributeTypes)); return(constructors); }
/// <summary> /// Utility method returns all Public and NonPublic instance methods /// with the <paramref name="attributeType"/>, /// sorted with the most parameters first. /// </summary> private static IEnumerable <MethodInfo> findMethods( object target, Type attributeType, ISequence <object> traceStack) { MethodInfo[] methods = target.GetType() .GetMethods( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) .Where(method => method.GetCustomAttribute(attributeType) != null) .ToArray(); Array.Sort(methods, ServiceConstructorMethods.parameterCountSort); if (methods.Length == 0) { traceStack.Add( $"No methods found with '{attributeType.GetFriendlyFullName()}'" + $" on '{target.GetType().GetFriendlyFullName()}'."); } return(methods); }
public void TestVirtualSequence() { VirtualSequence virtualSeq = new VirtualSequence(Alphabets.DNA); string id = Guid.NewGuid().ToString(string.Empty); virtualSeq.ID = id; Assert.AreEqual(virtualSeq.Alphabet, Alphabets.DNA); Assert.AreEqual(virtualSeq.Count, 0); Assert.AreEqual(virtualSeq.ID, id); // By default Displayid should show value of id. Assert.AreEqual(virtualSeq.DisplayID, virtualSeq.ID); virtualSeq.DisplayID = "VirtualSeq1"; Assert.AreEqual(virtualSeq.DisplayID, "VirtualSeq1"); Assert.AreNotEqual(virtualSeq.DisplayID, virtualSeq.ID); Assert.AreEqual(virtualSeq.IsReadOnly, true); Assert.AreEqual(virtualSeq.IndexOf(Alphabets.DNA.A), -1); foreach (Nucleotide nucleotide in Alphabets.DNA) { Assert.AreEqual(virtualSeq.IndexOf(nucleotide), -1); Assert.IsFalse(virtualSeq.Contains(nucleotide)); } // Verify the above test using ISequence interface. ISequence seq = virtualSeq as ISequence; Assert.AreEqual(seq.Alphabet, Alphabets.DNA); Assert.AreEqual(seq.Count, 0); Assert.AreEqual(seq.ID, id); Assert.AreNotEqual(seq.DisplayID, virtualSeq.ID); Assert.AreEqual(seq.DisplayID, "VirtualSeq1"); Assert.AreEqual(seq.IsReadOnly, true); foreach (Nucleotide nucleotide in Alphabets.DNA) { Assert.AreEqual(seq.IndexOf(nucleotide), -1); Assert.IsFalse(seq.Contains(nucleotide)); } #region Test not supported members try { seq.Add(Alphabets.DNA.A); Assert.Fail(); } catch (NotSupportedException) { Assert.AreEqual(seq.Count, 0); } try { seq.Clear(); Assert.Fail(); } catch (NotSupportedException) { Assert.AreEqual(seq.Count, 0); } ISequence tmpSeq = null; try { tmpSeq = seq.Complement; Assert.Fail(); } catch (NotSupportedException) { Assert.IsNull(tmpSeq); } try { ISequenceItem[] seqItems = new ISequenceItem[10]; seq.CopyTo(seqItems, 0); Assert.Fail(); } catch (NotSupportedException) { } try { seq.Insert(0, 'A'); Assert.Fail(); } catch (NotSupportedException) { Assert.AreEqual(seq.Count, 0); } try { seq.InsertRange(0, "ACG"); Assert.Fail(); } catch (NotSupportedException) { Assert.AreEqual(seq.Count, 0); } try { tmpSeq = seq.Range(0, 1); Assert.Fail(); } catch (NotSupportedException) { Assert.IsNull(tmpSeq); } try { seq.Remove(Alphabets.DNA.A); Assert.Fail(); } catch (NotSupportedException) { Assert.IsNull(tmpSeq); } try { seq.RemoveAt(0); Assert.Fail(); } catch (NotSupportedException) { Assert.AreEqual(seq.Count, 0); } try { seq.RemoveRange(0, 1); Assert.Fail(); } catch (NotSupportedException) { Assert.AreEqual(seq.Count, 0); } try { seq.Replace(0, Alphabets.DNA.A.Symbol); Assert.Fail(); } catch (NotSupportedException) { Assert.AreEqual(seq.IndexOf(Alphabets.DNA.A), -1); } try { seq.ReplaceRange(0, "GA"); Assert.Fail(); } catch (NotSupportedException) { Assert.AreEqual(seq.Count, 0); } try { tmpSeq = seq.Reverse; Assert.Fail(); } catch (NotSupportedException) { Assert.IsNull(tmpSeq); } try { tmpSeq = seq.ReverseComplement; Assert.Fail(); } catch (NotSupportedException) { Assert.IsNull(tmpSeq); } string sequence = string.Empty; try { sequence = seq.ToString(); Assert.Fail(); } catch (NotSupportedException) { Assert.IsTrue(string.IsNullOrEmpty(sequence)); } ISequenceItem seqItem = null; try { seqItem = seq[0]; Assert.Fail(); } catch (NotSupportedException) { Assert.IsNull(seqItem); } #endregion Test not supported members }