Esempio n. 1
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="type">type of fragment</param>
 /// <param name="messageSeqNumber">message sequence number</param>
 /// <param name="isAckRequired">is ack required</param>
 /// <param name="fragmentSeqNumber">fragment sequence number</param>
 public FragmentHeader(FragmentType type, byte messageSeqNumber, bool isAckRequired, byte fragmentSeqNumber)
 {
     _type              = type;
     _messageSeqNumber  = messageSeqNumber;
     _isAckRequired     = isAckRequired;
     _fragmentSeqNumber = fragmentSeqNumber;
 }
        public void TestStateAddWithFragmentWithArguments()
        {
            var    fragmentType = new FragmentType("fields", "state_type");
            TypeQL typeQL       = new TypeQL(
                new Fragments(
                    new Fragment(
                        new QueryType(fragmentType,
                                      new Fields(new Field("id"), new Field("name"))
                                      )
                        )
                    ),
                new QueryType(
                    "state_add",
                    new Fields(
                        new Field(fragmentType)
                        ),
                    new Arguments(
                        new Argument("input",
                                     new State {
                Id = 0, Name = "MINAS GERAIS", Cities = new List <City>()
            }
                                     )
                        )
                    )
                );
            var text = typeQL.ToBodyJson();
            IExecutionResult result = RequestExecutor.Execute(text);
            var json = result.ToJson();

            Assert.IsNull(result.Errors);
        }
Esempio n. 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FragmentToken"/> class.
 /// </summary>
 /// <param name="type">
 /// The type of fragment.
 /// </param>
 /// <param name="name">
 /// The name of the fragment if it has one.
 /// </param>
 /// <param name="content">
 /// The content of the fragment if it has any.
 /// </param>
 public FragmentToken(FragmentType type, string name, object content)
 {
     this.Type          = type;
     this.Name          = name;
     this.Content       = content;
     this.ContentString = content as string ?? string.Empty;
 }
        public void TestStateWithFragment()
        {
            FragmentType fragmentType = new FragmentType("fields", "state_type");
            TypeQL       typeQL       = new TypeQL(
                new Fragments(
                    new Fragment(
                        new QueryType(fragmentType,
                                      new Fields(new Field("id"), new Field("name"))
                                      )
                        )
                    ),
                new QueryType(
                    "states",
                    new Fields(
                        new Field(fragmentType)
                        )
                    )
                );
            var text = typeQL.ToBodyJson();
            //var text0 = typeQL.ToStringJson();
            IExecutionResult result = RequestExecutor.Execute(text);
            var json = result.ToJson();

            Assert.IsNull(result.Errors);
        }
        public void TestCarAddWithFragmentWithVariables()
        {
            Car car = new Car
            {
                Active   = true,
                Purchase = DateTime.Parse("02/01/1999"),
                Time     = TimeSpan.Parse("13:14:55"),
                Title    = "Car One",
                Value    = 10000M,
                Id       = 0
            };
            var fragmentType = new FragmentType("fields", "car_type");
            var fragments    = new Fragments(new Fragment(new QueryType(fragmentType,
                                                                        new Fields(new Field("id"), new Field("title"), new Field("active")))));
            var arguments  = new Arguments(new Argument(new Parameter("input")));
            var variables  = new Variables("getCar", new Variable <object>("input", car, "car_input"));
            var queryTypes = new QueryType("car_add", new Fields(new Field(fragmentType)), arguments);

            TypeQL typeQL = new TypeQL(variables, fragments, queryTypes);

            var text = typeQL.ToBodyJson();
            //var textComplete = typeQL.ToStringJson();
            var varDic = typeQL.Variables.ToDictionary();
            IExecutionResult result = RequestExecutor.Execute(text, varDic);
            var json = result.ToJson();

            Assert.IsNull(result.Errors);
        }
Esempio n. 6
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="type">type of fragment</param>
 /// <param name="messageSeqNumber">message sequence number</param>
 /// <param name="isAckRequired">is ack required</param>
 /// <param name="fragmentSeqNumber">fragment sequence number</param>
 public FragmentHeader(FragmentType type, byte messageSeqNumber, bool isAckRequired, byte fragmentSeqNumber)
 {
     _type = type;
     _messageSeqNumber = messageSeqNumber;
     _isAckRequired = isAckRequired;
     _fragmentSeqNumber = fragmentSeqNumber;
 }
Esempio n. 7
0
 public TabFragPackage(FragmentType fragType)
 {
     Icon          = GetTabIconFromString(fragType.ToString());
     Title         = fragType.ToString();
     RootUrl       = Https.URLs.GetUrlStringFromPref(fragType.ToString());
     WebViewClient = GetWebViewClientByType(FragmentType.None, fragType.ToString());
 }
 private MessageFragment(String text, TimeSpanWrapper timeSpanWrapper, OpponentData opponent, FragmentType type)
 {
     this.text = text;
     this.timeSpanWrapper = timeSpanWrapper;
     this.opponent = opponent;
     this.type = type;
 }
Esempio n. 9
0
 public ProfileTextPositionParams(bool expected, int position, string message, FragmentType fragmentType, string newProfileText)
 {
     Expected       = expected;
     Position       = position;
     Message        = message;
     FragmentType   = fragmentType;
     NewProfileText = newProfileText;
 }
 public AScoreParameters(FragmentType f, double tol)
 {
     staticMods  = new List <Mod.Modification>();
     terminiMods = new List <Mod.TerminiModification>();
     //      dynamMods = new List<Mod.DynamicModification>();
     fragmentType          = f;
     fragmentMassTolerance = tol;
 }
Esempio n. 11
0
 protected GenFragmentParams(GenDataDef genDataDef, Fragment fragment, FragmentType fragmentType)
 {
     Contract.Requires(fragment != null && fragment.GetType().Name == fragmentType.ToString());
     Contract.Ensures(Fragment != null && Fragment.GetType().Name == FragmentType.ToString());
     _fragment    = fragment;
     GenDataDef   = genDataDef;
     FragmentType = fragmentType;
 }
Esempio n. 12
0
 public Fragment(int Start, int End, int Line, string Text, FragmentType Type)
 {
     this.Start = Start;
     this.End   = End;
     this.Line  = Line;
     this.Text  = Text;
     this.Type  = Type;
 }
Esempio n. 13
0
 static IRule <Fragment> ThenCreateFragment(this IRule <int> rule, FragmentType type) => rule.Map(x =>
 {
     var fragment = new Fragment
     {
         FragmentType = type
     };
     fragment.Trivia.StartPosition = x;
     return(fragment);
 });
Esempio n. 14
0
    public void GeneratorFragment(FragmentType type)
    {
        Vector3 dir =Vector3.Normalize(new Vector3(Random.Range(-1f,1f),0,Random.Range(-1f,1f)));
        Vector3 generatorPos = transform.position+dir*radius;
        generatorPos.y =Random.Range(0.25f,0.5f);

        GameObject newObj = GameObject.Instantiate(FragmentPrefab)as GameObject;
        newObj.transform.position = generatorPos;
    }
 public AScoreParameters(List <Mod.Modification> stat, List <Mod.TerminiModification> term,
                         List <Mod.DynamicModification> dynam, FragmentType f, double tol)
 {
     staticMods            = stat;
     terminiMods           = term;
     dynamMods             = dynam;
     fragmentType          = f;
     fragmentMassTolerance = tol;
 }
Esempio n. 16
0
        private void CheckFragment(FragmentType fragmentType, FragmentBody fragmentBody)
        {
            Contract.Ensures(Fragment != null);
            if (FragmentExists)
            {
                return;
            }

            var fragmentName = fragmentBody.FragmentName(fragmentType);

            switch (fragmentType)
            {
            case FragmentType.Profile:
                Fragment = fragmentBody.AddProfile();
                break;

            case FragmentType.Text:
                Fragment = fragmentBody.AddText(fragmentName);
                break;

            case FragmentType.Placeholder:
                Fragment = fragmentBody.AddPlaceholder(fragmentName);
                break;

            case FragmentType.Segment:
                var segmentParams = ((GenSegmentParams)this);
                Fragment = fragmentBody.AddSegment(segmentParams.ClassName, segmentParams.Cardinality.ToString());
                break;

            case FragmentType.Annotation:
                Fragment = fragmentBody.AddAnnotation();
                break;

            case FragmentType.Block:
                Fragment = fragmentBody.AddBlock();
                break;

            case FragmentType.Lookup:
                Fragment = fragmentBody.AddLookup();
                break;

            case FragmentType.Condition:
                Fragment = fragmentBody.AddCondition();
                break;

            case FragmentType.Function:
                Fragment = fragmentBody.AddFunction();
                break;

            case FragmentType.TextBlock:
                Fragment = fragmentBody.AddTextBlock();
                break;

            default:
                throw new ArgumentOutOfRangeException("fragmentType");
            }
        }
Esempio n. 17
0
        public PresentationContextInputStream(DicomConnection connection, FragmentType fragmentType)
        {
            _connection = connection;

            _fragmentType = fragmentType;

            StartReadFragment(isFirstFragment: true);

            PresentationContextID = _fragmentHeader.PresentationContextID;
        }
Esempio n. 18
0
 public SingleFragment AddFragmentAtScreenPos(Vector3 viewPos, FragmentType type)
 {
     viewPos.z = 10;
     Vector3 wordPos = Camera.main.ViewportToWorldPoint (viewPos);
     wordPos = GetPosInRange (wordPos);
     SingleFragment fragment = AddNewFragment(wordPos, type);
     fragment.SetRandomMoveActive(false);
     fragment.IsBeginnerGuideFragment = true;
     return fragment;
 }
Esempio n. 19
0
 public SingleFragment AddNewFragment(Vector3 pos,FragmentType type,bool showEffect = false)
 {
     //        Debug.Log("AddFragment");
     SingleFragment newFragment = (GameObject.Instantiate(FragmentPrefab) as GameObject).GetComponent<SingleFragment>();
     newFragment.transform.position = pos;
     newFragment.Init(type, showEffect);
     newFragment.transform.parent = Root.transform;
     mFragmentGroup.Add(newFragment);
     return newFragment;
 }
Esempio n. 20
0
    public static IFragment GetFragment(FragmentType type, int id)
    {
        switch (type)
        {
        case FragmentType.Guard: return(GetGuardFrag(id));

        case FragmentType.Demon: return(GetDemonFrag(id));
        }
        throw new System.Exception("no but try get it");
    }
Esempio n. 21
0
    public void Init(FragmentType type,bool isShowEffect = false)
    {
        mFragmentType = type;

        transform.localScale *= (int)type;

        mFragmentStatus = FragmentStatus.Stay;

        SetRandomMoveActive(true);
    }
Esempio n. 22
0
 public GenFragmentLabel(Fragment fragment)
 {
     Contract.Ensures(Fragment == fragment);
     Contract.Ensures(Fragment.GetType().Name == FragmentType.ToString());
     Fragment = fragment;
     //FragmentType fragmentType;
     //Contract.Assert(Enum.TryParse(Fragment.GetType().Name, out fragmentType), "Fragment type invalid: " + Fragment.GetType().Name);
     //FragmentType = fragmentType;
     FragmentType = fragment.FragmentType;
 }
        public void TestFragmentType()
        {
            IFragmentType fragmentType = new FragmentType("fields", "state_type");

            Assert.IsInstanceOfType(fragmentType.GetType(), typeof(IFragmentType).GetType());
            Assert.IsInstanceOfType(fragmentType.GetType(), typeof(FragmentType).GetType());
            Assert.AreEqual(fragmentType.FragmentName, "...fields");
            Assert.AreEqual(fragmentType.FragmentNameAndType, "fragment fields on state_type");
            Assert.AreEqual(fragmentType.Name, "fields");
            Assert.AreEqual(fragmentType.NameType, "state_type");
        }
Esempio n. 24
0
 public AttachFragmentCommand(
     IPowerPlant powerPlant,
     FragmentType lateInstantiationFragmentType,
     string lateInstantiationFragmentName,
     int lateInstantiationFragmentPressureAffection)
     : base(powerPlant)
 {
     this.LateInstantiationFragmentType = lateInstantiationFragmentType;
     this.LateInstantiationFragmentName = lateInstantiationFragmentName;
     this.LateInstantiationFragmentPressureAffection = lateInstantiationFragmentPressureAffection;
 }
Esempio n. 25
0
        public string GetSource(FragmentType mediaType, long bitrate)
        {
            ISMElement ismElement = GetISMElement(mediaType, bitrate);

            if (ismElement != null)
            {
                return(ismElement.Source);
            }

            return(null);
        }
Esempio n. 26
0
        public ISMElement GetISMElement(FragmentType fragmentType, long bitrate)
        {
            foreach (ISMElement ismElement in _ismElements)
            {
                if (ismElement.FragmentType == fragmentType && ismElement.Bitrate == bitrate)
                {
                    return(ismElement);
                }
            }

            return(null);
        }
Esempio n. 27
0
        public GenFragmentParams SetFragmentType(FragmentType fragmentType)
        {
            Contract.Requires(Container != null || Fragment != null);
            Contract.Ensures(Fragment != null && Fragment.GetType().Name == FragmentType.ToString());
            FragmentType = fragmentType;
            if (FragmentExists)
            {
                return(this);
            }
            var fragmentBody = IsPrimary ? Container.CheckBody() : Container.CheckSecondaryBody();

            CheckFragment(fragmentType, fragmentBody);
            return(this);
        }
        public IFragment CreateFragment(FragmentType fragmentType, string name, int pressureAffection)
        {
            IFragment fragment = null;
            switch (fragmentType)
            {
                case FragmentType.Cooling:
                    fragment = new CoolingFragment(name, pressureAffection);
                    break;
                case FragmentType.Nuclear:
                    fragment = new NuclearFragment(name, pressureAffection);
                    break;
            }

            return fragment;
        }
Esempio n. 29
0
        public Fragment(string name, FragmentType type, List <Instruction> instructions)
        {
            this.Name         = name;
            this.Type         = type;
            this.Instructions = new ReadOnlyCollection <Instruction>(instructions);
            this.Labels       = new Dictionary <string, int>();

            for (int i = 0; i < instructions.Count; i++)
            {
                if (instructions[i].Label == null)
                {
                    continue;
                }
                this.Labels[instructions[i].Label.Name] = i;
            }
        }
Esempio n. 30
0
 public void AddComponent(string name, FragmentType type, string parent)
 {
     TreeNode newGuy = new TreeNode(name);
       newGuy.Name = name;
       // TODO: Change icon based on FragmentType
       if (!string.IsNullOrEmpty(parent))
       {
     foreach (TreeNode t in treeFragments.Nodes)
     {
       if (t.Name == parent)
     t.Nodes.Add(newGuy);
     }
       }
       else
     treeFragments.Nodes.Add(newGuy);
 }
Esempio n. 31
0
        public Fragment CalculateFragment(FragmentType fragmentIonType, int fragmentNumber)
        {
            switch (fragmentIonType)
            {
            case FragmentType.b:
            case FragmentType.c:
                return(new Fragment(fragmentIonType, fragmentNumber, cumulativeNTerminalMass[fragmentNumber] + ION_CAPS[fragmentIonType].Mass));

            case FragmentType.y:
            case FragmentType.zdot:
                return(new Fragment(fragmentIonType, fragmentNumber, cumulativeCTerminalMass[fragmentNumber] + ION_CAPS[fragmentIonType].Mass));

            default:
                return(null);
            }
        }
Esempio n. 32
0
        /// <summary>
        /// Convert UInt16 into a FragmentHeader
        /// </summary>
        /// <param name="value">encode header value</param>
        /// <returns>a new FragmentHeade</returns>
        static public FragmentHeader Decode(UInt16 value)
        {
            ushort       mask  = 0x0001; // 1 bit for type
            byte         btype = (byte)((value >> (byte)FragmentHeaderField.Type) & mask);
            FragmentType type  = (FragmentType)btype;

            mask = 0x0001 << (byte)FragmentHeaderField.RequireAck;
            bool isAckRequired = ((value & mask) > 0);

            mask = 0x00FF; // 8 bits for MessageNumber
            byte messageNumber = (byte)((value >> (byte)FragmentHeaderField.MessageNumber) & mask);

            mask = 0x003F; // 6 bits for FragmentSeqNumber
            byte fragmentSeqNumber = (byte)((value >> (byte)FragmentHeaderField.FragmentSeqNumber) & mask);

            return(new FragmentHeader(type, messageNumber, isAckRequired, fragmentSeqNumber));
        }
Esempio n. 33
0
        /// <summary>
        /// Method to get fragment type from xml
        /// </summary>
        /// <param name="fragmentType">xmlnode with fragment type info</param>
        /// <returns>the type of fragmentation</returns>
        private static FragmentType GetFragmentType(XmlNode fragmentType)
        {
            FragmentType f = FragmentType.CID;

            if (Regex.IsMatch(fragmentType.InnerText, "CID"))
            {
                f = FragmentType.CID;
            }
            else if (Regex.IsMatch(fragmentType.InnerText, "ETD"))
            {
                f = FragmentType.ETD;
            }
            else if (Regex.IsMatch(fragmentType.InnerText, "HCD"))
            {
                f = FragmentType.HCD;
            }
            return(f);
        }
Esempio n. 34
0
 public override int GetHashCode()
 {
     unchecked
     {
         int hash = 13;
         hash = (hash * 7) + FragmentType.GetHashCode();
         if (Value != null)
         {
             hash = (hash * 7) + Value.GetHashCode();
         }
         if (Attributes != null)
         {
             hash = (hash * 7) + Attributes.GetHashCode();
         }
         hash = (hash * 7) + IsSelfClosing.GetHashCode();
         return(hash);
     }
 }
 //IEnumerator Start()
 //{
 //    yield return new WaitForSeconds(1);
 //    SetRandomFly();
 //}
 public void Init(FragmentType type)
 {
     mType = type;
     switch (type)
     {
         case FragmentType.Big:
             mSPrite.spriteName = "Fragment_Big";
             break;
         case FragmentType.Medium:
             mSPrite.spriteName = "Fragment_Medium";
             break;
         case FragmentType.Small:
             mSPrite.spriteName = "Fragment_Small";
             break;
     }
     //mSPrite.transform.localScale *= (int)type;
     float scale = 0.5f+((int)type)*0.5f;
     mSPrite.transform.localScale*= scale;
 }
Esempio n. 36
0
        public static object GetWebViewClientByType(FragmentType fragtype = FragmentType.None, string type = null)
        {
            if (fragtype != FragmentType.None)
            {
                type = fragtype.ToString();
            }
            switch (type)
            {
            case "Home": return(new Home());

            case "Subs": return(new Subs());

            case "Feed": return(new Feed());

            case "MyChannel": return(new MyChannel());

            case "Settings": return(new Settings());
            }
            return(new Subs());
        }
Esempio n. 37
0
    public void SetMyFragments(int num,FragmentType type)
    {
        mBottleObjGroup.ApplyAllItem(C=>GameObject.Destroy(C.gameObject));
        mBottleObjGroup.Clear();
        for(int i = 0;i<num;i++)
        {
            GameObject newFrag = NGUITools.AddChild(gameObject, BottleFlyEffectPrefab);
            BottleFragRandomFly randomFly = newFrag.AddComponent<BottleFragRandomFly>();
            float scale = 0.5f+((int)type)*0.5f;
            newFrag.transform.localScale*= scale/2;

            int randomX = Random.Range(20, 30);
            int randomY = Random.Range(20, 30);
            Vector3 targetPos = new Vector3(Random.Range(-1f, 1f) > 0 ? randomX : -randomX, Random.Range(-1f, 1f) > 0 ? randomY : -randomY, 0);
            randomFly.transform.localPosition = targetPos;
            randomFly.SetFlyPosRange(20,30);
            randomFly.mSPrite.depth = 3;
            randomFly.SetActive(true);
            mBottleObjGroup.Add(newFrag);
        }
    }
        public void TestStateAddWithFragmentWithVariables()
        {
            var state = new State {
                Id = 0, Name = "MINAS GERAIS", Cities = new List <City>()
            };
            var fragmentType = new FragmentType("fields", "state_type");
            var fragments    = new Fragments(new Fragment(new QueryType(fragmentType, new Fields(new Field("id"), new Field("name")))));
            var arguments    = new Arguments(new Argument(new Parameter("input")));
            var variables    = new Variables("getState", new Variable <object>("input", state, "state_input"));
            var queryTypes   = new QueryType("state_add", new Fields(new Field(fragmentType)), arguments);

            TypeQL typeQL = new TypeQL(variables, fragments, queryTypes);

            var text = typeQL.ToBodyJson();
            //var textComplete = typeQL.ToStringJson();
            var varDic = typeQL.Variables.ToDictionary();
            IExecutionResult result = RequestExecutor.Execute(text, varDic);
            var json = result.ToJson();

            Assert.IsNull(result.Errors);
        }
Esempio n. 39
0
 public ModelComponent(string _name, string _parent)
 {
     name = _name;
       type = FragmentType.MODEL;
       parent = _parent;
 }
Esempio n. 40
0
 public ModelComponent(string _name, FragmentType _type, string _parent)
 {
     name = _name;
       type = _type;
       parent = _parent;
 }
 protected BaseFragment(string name, FragmentType fragmentType, int pressureAffection)
 {
     this.Name = name;
     this.FragmentType = fragmentType;
     this.PressureAffection = pressureAffection;
 }
Esempio n. 42
0
 public void AddComponent(string name, FragmentType type)
 {
     AddComponent(name, type, "");
 }
Esempio n. 43
0
        public ISMElement GetISMElement(FragmentType fragmentType, long bitrate)
        {
            foreach (ISMElement ismElement in ismElements) {
            if (ismElement.FragmentType == fragmentType && ismElement.Bitrate == bitrate) {
              return (ismElement);
            }
              }

              return (null);
        }
Esempio n. 44
0
 public Fragment(FragmentType type, string value)
 {
     Type  = type;
     Value = value;
 }
Esempio n. 45
0
 public Fragment(FragmentType fragmentType, int number, double mass)
 {
     this.fragmentType = fragmentType;
     this.number = number;
     this.mass = mass;
 }
 public Fragment(string text, FragmentType type)
     : this(text, type, -1)
 {
 }
 private Fragment(string text, FragmentType type, int indent)
 {
     _text = text;
     _type = type;
     _indent = indent;
 }
 public void Write(FragmentType type)
 {
     Write(type, null);
 }
 public void Write(FragmentType type, string text)
 {
     _fragments.Add(new Fragment(text, type));
 }
Esempio n. 50
0
 public Fragment CalculateFragment(FragmentType fragmentIonType, int fragmentNumber)
 {
     switch(fragmentIonType)
     {
         case FragmentType.b:
         case FragmentType.c:
             return new Fragment(fragmentIonType, fragmentNumber, cumulativeNTerminalMass[fragmentNumber] + ION_CAPS[fragmentIonType].Mass);
         case FragmentType.y:
         case FragmentType.zdot:
             return new Fragment(fragmentIonType, fragmentNumber, cumulativeCTerminalMass[fragmentNumber] + ION_CAPS[fragmentIonType].Mass);
         default:
             return null;
     }
 }
Esempio n. 51
0
        public string GetSource(FragmentType mediaType, long bitrate)
        {
            ISMElement ismElement = GetISMElement(mediaType, bitrate);

              if (ismElement != null) {
            return (ismElement.Source);
              }

              return (null);
        }
Esempio n. 52
0
 public ModelComponent(string _name, FragmentType _type)
 {
     name = _name;
       type = _type;
       parent = "";
 }
Esempio n. 53
0
        /// <summary>
        ///   Sends a fragment.
        /// </summary>
        private Task SendFragment(Message message, byte[] fragment, FragmentType type)
        {
            // create a task
              return Task.Run(() => {
            // check the listening state
            if (_listening) {
              // error
              Log.Singleton.LogError("Can't send a fragment while listening");

              // delete message
              lock (message) {
            MessageCenter.Singleton.Messages.Remove(BitConverter.ToUInt16(message.ID, 0));
              }

              // cancel
              return;
            }

            // append CRC checksum
            CRC.GenerateChecksum(ref fragment);

            // need to lock to prevent multiple tasks trying to send simultaneously
            lock (_clientMutex) {
              // check if we want to lose some fragments
              if (Options.LoseFragments) {
            // get a random value
            var random = new Random();
            if ((ushort) random.Next() % 100 < 30) {
              // 30% of fragments will be lost

              // log that
              Log.Singleton.LogMessage($"Purposefully losing a fragment of type <{type}>");

              // exit
              return;
            }
              }

              // check if we want to corrupt some fragments
              if (Options.SendCorrupt) {
            // get a random value
            var random = new Random();
            if ((ushort) random.Next() % 100 < 40) {
              // 40% of fragments will be corrupted

              // log that
              Log.Singleton.LogMessage($"Purposefully corrupting a fragment of type <{type}>");

              // change random bytes
              for (int i = 0, imax = Math.Abs(random.Next() + 2) % 16; i < imax; ++i) {
                fragment[random.Next() % fragment.Length] = (byte) random.Next();
              }
            }
              }

              // create a client
              _client = new UdpClient();

              // bind it
              try {
            _client.Client.Bind(new IPEndPoint(IPAddress.Any, Options.Port));
              }
              catch (SocketException) {
            // nope, can't bind to that port
            Log.Singleton.LogError($"Failed to bind the sending socket to the port <{Options.Port}>");

            // remove client
            _client = null;

            // remove message
            lock (message) {
              MessageCenter.Singleton.Messages.Remove(BitConverter.ToUInt16(message.ID, 0));
            }

            // stop
            return;
              }

              // log
              lock (message) {
            Log.Singleton.LogMessage(
              $"Sending a <{type}> fragment for message <{message.ID[0].ToString("00")}{message.ID[1].ToString("00")}> to <{message.RemoteEndPoint}>");
              }

              // send the fragment
              lock (message) {
            _client.Send(fragment, fragment.Length, message.RemoteEndPoint);
              }

              // close and remove client
              _client.Close();
              _client = null;
            }
              });
        }
Esempio n. 54
0
 public ModelComponent(string _name)
 {
     name = _name;
       type = FragmentType.MODEL;
       parent = "";
 }
Esempio n. 55
0
 protected Fragment(FragmentType fragmentType, string fragmentName, bool undefined)
 {
     _fragmentType = fragmentType;
       _fragmentName = fragmentName;
       _undefined = undefined;
 }
Esempio n. 56
0
    /// <summary>
    /// Finds the next delimiter from the starting index.
    /// </summary>
    static Fragment ParseFragment(string format, int startIndex, out int fragmentEndIndex)
    {
        bool         foundEscapedDelimiter = false;
        FragmentType type     = FragmentType.Literal;
        int          numChars = format.Length;

        for (int i = startIndex; i < numChars; i++)
        {
            char currChar     = format[i];
            bool isOpenBrace  = currChar == OpeningDelimiter;
            bool isCloseBrace = isOpenBrace ? false : currChar == ClosingDelimiter;
            if (!isOpenBrace && !isCloseBrace)
            {
                continue;
            }
            else if (i < (numChars - 1) && format[i + 1] == currChar)
            {    //{{ or }}
                i++;
                foundEscapedDelimiter = true;
            }
            else if (isOpenBrace)
            {
                if (i == startIndex)
                {
                    type = FragmentType.FormatItem;
                }
                else
                {
                    if (type == FragmentType.FormatItem)
                    {
                        throw new FormatException("Two consequtive unescaped { format item openers were found.  Either close the first or escape any literals with another {.");
                    }
                    //curr character is the opening of a new format item.  so we close this literal out
                    string literal = format.Substring(startIndex, i - startIndex);
                    if (foundEscapedDelimiter)
                    {
                        literal = ReplaceEscapes(literal);
                    }
                    fragmentEndIndex = i - 1;
                    return(new Fragment(FragmentType.Literal, literal));
                }
            }
            else
            {    //close bracket
                if (i == startIndex || type == FragmentType.Literal)
                {
                    throw new FormatException("A } closing brace existed without an opening { brace.");
                }
                string formatItem = format.Substring(startIndex + 1, i - startIndex - 1);
                if (foundEscapedDelimiter)
                {
                    formatItem = ReplaceEscapes(formatItem);    //a format item with a { or } in its name is crazy but it could be done
                }
                fragmentEndIndex = i;
                return(new Fragment(FragmentType.FormatItem, formatItem));
            }
        }
        if (type == FragmentType.FormatItem)
        {
            throw new FormatException("A format item was opened with { but was never closed.");
        }
        fragmentEndIndex = numChars - 1;
        string literalValue = format.Substring(startIndex);

        if (foundEscapedDelimiter)
        {
            literalValue = ReplaceEscapes(literalValue);
        }
        return(new Fragment(FragmentType.Literal, literalValue));
    }
Esempio n. 57
0
public void Phosphinate()
{
    StreamWriter log = null;
            IXRawfile2 raw = null;
            StreamReader csv = null;
            StreamWriter non_phospho_output = null;
            StreamWriter localized_phospho_output = null;
            StreamWriter unlocalized_phospho_output = null;
            StreamWriter motifX = null;

            try
            {
                onStarting(new EventArgs());

                onUpdateProgress(new ProgressEventArgs(0));

                StringBuilder fixed_modifications_sb = new StringBuilder();
                foreach (Modification modification in fixedModifications)
                {
                    fixed_modifications_sb.Append(modification.Name + ", ");
                }
                if (fixed_modifications_sb.Length > 0)
                {
                    fixed_modifications_sb = fixed_modifications_sb.Remove(fixed_modifications_sb.Length - 2, 2);
                }
                string fixed_modifications = fixed_modifications_sb.ToString();

                if (!Directory.Exists(outputFolder))
                {
                    Directory.CreateDirectory(outputFolder);
                }

                log = new StreamWriter(Path.Combine(outputFolder, "Phosphinator_log.txt"));
                log.AutoFlush = true;

                log.WriteLine("Phosphinator PARAMETERS");
                log.WriteLine("Fixed Modifications: " + fixed_modifications);
                log.WriteLine("Fragment Intensity Threshold: " + intensityThreshold.ToString() + " (" + intensityThresholdType.ToString() + ')');
                log.WriteLine("Fragment m/z Tolerance (Th): " + mzTolerance.ToString());
                log.WriteLine("Ambiguity Score Threshold: " + ambiguityScoreThreshold.ToString());
                log.WriteLine("Eliminate Precursor Interference: " + eliminatePrecursorInterference.ToString());
                if (eliminatePrecursorInterference)
                {
                    log.WriteLine("Precursor Interference Threshold: " + precursorInterferenceThreshold.ToString());
                }
                if (motifXOutput)
                {
                    log.WriteLine("Motif-X Fasta Protein Database Filepath: " + motifXFastaProteinDatabaseFilepath);
                    log.WriteLine("Motif-X Window Size: " + motifXWindowSize.ToString());
                }
                log.WriteLine();

                ProteinSiteCounter identified_sites_by_protein = new ProteinSiteCounter();
                ProteinSiteCounter localized_sites_by_protein = new ProteinSiteCounter();
                ProteinSiteCounter unlocalized_sites_by_protein = new ProteinSiteCounter();

                Dictionary<string, Dictionary<KeyValuePair<int, string>, List<string>>> localized =
                    new Dictionary<string, Dictionary<KeyValuePair<int, string>, List<string>>>();
                Dictionary<string, Dictionary<KeyValuePair<int, string>, List<string>>> unlocalized =
                    new Dictionary<string, Dictionary<KeyValuePair<int, string>, List<string>>>();

                non_phospho_output = new StreamWriter(Path.Combine(outputFolder, "non_phospho.csv"));
                localized_phospho_output = new StreamWriter(Path.Combine(outputFolder, "localized_phospho.csv"));
                unlocalized_phospho_output = new StreamWriter(Path.Combine(outputFolder, "unlocalized_phospho.csv"));

                ProteinDictionary proteins = null;
                Dictionary<string, int> motifs = null;
                if (motifXOutput)
                {
                    proteins = new ProteinDictionary(motifXFastaProteinDatabaseFilepath);
                    motifs = new Dictionary<string, int>();
                    motifX = new StreamWriter(Path.Combine(outputFolder, "motif-x.txt"));
                }

                raw = (IXRawfile2) new MSFileReader_XRawfile();

                string header_line = null;
                string[] headers = null;
                bool quant = false;

                foreach (string csv_filepath in csvFilepaths)
                {
                    onStartingFile(new FilepathEventArgs(csv_filepath));

                    csv = new StreamReader(csv_filepath);

                    using (CsvReader reader = new CsvReader(csv, true))
                    {
                        headers = reader.GetFieldHeaders();

                        header_line = string.Join(",", headers);
                        quant = headers.Contains("Channels Detected");
                        string[] lineData = new string[headers.Length];
                        //header_line = csv.ReadLine();
                        //quant = header_line.Contains("TQ_");

                        non_phospho_output.WriteLine(header_line);
                        localized_phospho_output.WriteLine(header_line +
                                                           ", Number of Theoretical Fragments, Identified Phosphoisoform, Identified Phosphoisoform Number of Matching Fragments, Best Phosphoisoforms, Best Phosphoisoform, Best Phosphoisoform Number of Matching Fragments, Second-Best Phosphoisoform, Second-Best Phosphoisoform Number of Matching Fragments, Identified Phosphoisoform Correct?, Preliminary Localization of All Phosphorylations?, Peptide Phosphorylation Sites, Probability of Spurious Fragment Match, Number of Theoretical Site-Determining Fragment Ions, Number of Matching Site-Determining Fragment Ions, Matching Site-Determining Fragment Ions, Probability Values, Ambiguity Scores, Phosphorylation Sites Localized?, All Phosphorylation Sites Localized?");
                        unlocalized_phospho_output.WriteLine(header_line +
                                                             ", Number of Theoretical Fragments, Identified Phosphoisoform, Identified Phosphoisoform Number of Matching Fragments, Best Phosphoisoforms, Best Phosphoisoform, Best Phosphoisoform Number of Matching Fragments, Second-Best Phosphoisoform, Second-Best Phosphoisoform Number of Matching Fragments, Identified Phosphoisoform Correct?, Preliminary Localization of All Phosphorylations?, Peptide Phosphorylation Sites, Probability of Spurious Fragment Match, Number of Theoretical Site-Determining Fragment Ions, Number of Matching Site-Determining Fragment Ions, Matching Site-Determining Fragment Ions, Probability Values, Ambiguity Scores, Phosphorylation Sites Localized?, All Phosphorylation Sites Localized?");

                        while (reader.ReadNextRecord())
                        {
                            //string line = csv.ReadLine();

                            //string[] fields = Regex.Split(line,
                            //    @",(?!(?<=(?:^|,)\s*\x22(?:[^\x22]|\x22\x22|\\\x22)*,)(?:[^\x22]|\x22\x22|\\\x22)*\x22\s*(?:,|$))");
                            //    // crazy regex to parse CSV with internal double quotes from http://regexlib.com/REDetails.aspx?regexp_id=621

                            string sequence = reader["Peptide"];

                            string dynamic_modifications = reader["Mods"];
                            if (!dynamic_modifications.Contains("phosphorylation"))
                            {
                                //non_phospho_output.WriteLine(line);
                            }
                            else
                            {
                                Peptide identified_phosphopeptide = new Peptide(sequence, fixedModifications,
                                    dynamic_modifications);

                                int start_residue = int.Parse(reader["Start"]);
                                int stop_residue = int.Parse(reader["Stop"]);
                                string protein_description = reader["Defline"].Trim('"');

                                StringBuilder sb = new StringBuilder();
                                reader.CopyCurrentRecordTo(lineData);
                                foreach (string datum in lineData)
                                {
                                    if (datum.Contains(','))
                                        sb.Append("\"" + datum + "\"");
                                    else
                                        sb.Append(datum);
                                    sb.Append(',');
                                }
                                sb.Remove(sb.Length - 1, 1);
                                string line = sb.ToString();
                                if (!identified_sites_by_protein.ContainsKey(protein_description))
                                {
                                    identified_sites_by_protein.Add(protein_description, new Dictionary<string, int>());
                                }
                                foreach (KeyValuePair<int, string> kvp in identified_phosphopeptide.DynamicModifications)
                                {
                                    if (kvp.Value.Contains("phosphorylation"))
                                    {
                                        string site = sequence[kvp.Key - 1] + (start_residue + kvp.Key).ToString();

                                        if (!identified_sites_by_protein[protein_description].ContainsKey(site))
                                        {
                                            identified_sites_by_protein[protein_description].Add(site, 0);
                                        }
                                        identified_sites_by_protein[protein_description][site]++;
                                    }
                                }

                                int scan_number = int.Parse(reader["Spectrum number"]);
                                string filenameID = reader["Filename/id"];
                                FragmentType[] fragment_types = null;
                                if (filenameID.Contains(".ETD.") || filenameID.Contains(".ECD."))
                                {
                                    fragment_types = new FragmentType[] {FragmentType.c, FragmentType.zdot};
                                }
                                else
                                {
                                    fragment_types = new FragmentType[] {FragmentType.b, FragmentType.y};
                                }
                                string raw_filename = filenameID.Substring(0, filenameID.IndexOf('.')) + ".raw";
                                int charge = int.Parse(reader["Charge"]);

                                string current_raw_filename = null;
                                raw.GetFileName(ref current_raw_filename);

                                if (current_raw_filename == null ||
                                    !raw_filename.Equals(Path.GetFileName(current_raw_filename),
                                        StringComparison.InvariantCultureIgnoreCase))
                                {
                                    raw.Close();
                                    string[] raw_filepaths = null;
                                    if (!string.IsNullOrEmpty(rawFolder) && Directory.Exists(rawFolder))
                                    {
                                        raw_filepaths = Directory.GetFiles(rawFolder, raw_filename,
                                            SearchOption.AllDirectories);
                                    }
                                    else
                                    {
                                        raw_filepaths = Directory.GetFiles(Path.GetDirectoryName(csv_filepath),
                                            raw_filename, SearchOption.AllDirectories);
                                    }
                                    if (raw_filepaths.Length == 0)
                                    {
                                        throw new FileNotFoundException("No corresponding .raw file found for " +
                                                                        csv_filepath);
                                    }
                                    if (raw_filepaths.Length > 1)
                                    {
                                        throw new Exception("Multiple corresponding .raw files found for " +
                                                            csv_filepath);
                                    }
                                    raw.Open(raw_filepaths[0]);
                                    raw.SetCurrentController(0, 1);
                                }

                                string scan_filter = null;
                                raw.GetFilterForScanNum(scan_number, ref scan_filter);

                                string low_mz_scan_filter = scan_filter.Substring(scan_filter.IndexOf('[') + 1);
                                double low_mz =
                                    double.Parse(low_mz_scan_filter.Substring(0, low_mz_scan_filter.IndexOf('-')));
                                string high_mz_scan_filter = scan_filter.Substring(scan_filter.LastIndexOf('-') + 1);
                                double high_mz =
                                    double.Parse(high_mz_scan_filter.Substring(0, high_mz_scan_filter.IndexOf(']')));

                                double[,] spectrum = null;
                                if (scan_filter.Contains("FTMS"))
                                {
                                    object labels_obj = null;
                                    object flags_obj = null;
                                    raw.GetLabelData(ref labels_obj, ref flags_obj, ref scan_number);
                                    spectrum = (double[,]) labels_obj;
                                }
                                else
                                {
                                    double centroid_width = double.NaN;
                                    object spectrum_obj = null;
                                    object flags = null;
                                    int size = -1;
                                    raw.GetMassListFromScanNum(ref scan_number, null, 0, -1, 0, 1, ref centroid_width,
                                        ref spectrum_obj, ref flags, ref size);
                                    spectrum = (double[,]) spectrum_obj;
                                }

                                double base_peak_mz = double.NaN;
                                double base_peak_intensity = double.NaN;
                                for (int i = spectrum.GetLowerBound(1); i <= spectrum.GetUpperBound(1); i++)
                                {
                                    if (double.IsNaN(base_peak_mz) ||
                                        spectrum[(int) RawLabelDataColumn.Intensity, i] > base_peak_intensity)
                                    {
                                        base_peak_mz = spectrum[(int) RawLabelDataColumn.MZ, i];
                                        base_peak_intensity = spectrum[(int) RawLabelDataColumn.Intensity, i];
                                    }
                                }

                                double intensity_threshold = intensityThreshold;
                                if (intensityThresholdType == IntensityThresholdType.Relative)
                                {
                                    intensity_threshold = (intensityThreshold/100.0)*base_peak_intensity;
                                }

                                double[] parameters = new double[4];
                                if (!scan_filter.Contains("FTMS") &&
                                    intensityThresholdType == IntensityThresholdType.SignalToNoiseRatio)
                                {
                                    List<double> relative_intensities = new List<double>();
                                    for (int i = spectrum.GetLowerBound(1); i <= spectrum.GetUpperBound(1); i++)
                                    {
                                        relative_intensities.Add(spectrum[(int) RawLabelDataColumn.Intensity, i]/
                                                                 base_peak_intensity);
                                    }

                                    double bin_width = 0.001;
                                    int bins = 101;
                                    double[][] relative_intensity_histogram = new double[2][];
                                    relative_intensity_histogram[0] = new double[bins];
                                    relative_intensity_histogram[1] = new double[bins];
                                    for (int i = relative_intensity_histogram[0].GetLowerBound(0);
                                        i <= relative_intensity_histogram[0].GetUpperBound(0);
                                        i++)
                                    {
                                        relative_intensity_histogram[0][i] = i*bin_width;
                                    }
                                    foreach (double relative_intensity in relative_intensities)
                                    {
                                        int bin_number = (int) Math.Floor(relative_intensity/bin_width);
                                        if (bin_number < bins)
                                        {
                                            relative_intensity_histogram[1][bin_number]++;
                                        }
                                    }

                                    parameters[0] = 0.0;
                                    parameters[1] = 100.0;
                                    parameters[2] = 0.0;
                                    parameters[3] = 0.001;

                                    double[] weights = new double[relative_intensity_histogram[1].Length];
                                    for (int i = weights.GetLowerBound(0); i <= weights.GetUpperBound(0); i++)
                                    {
                                        weights[i] = 1.0;
                                    }
                                    LMA lma = new LMA(new GaussianFunctionWithPartials(), parameters,
                                        relative_intensity_histogram, weights, new DotNetMatrix.GeneralMatrix(4, 4),
                                        0.001, 5000);
                                    lma.Fit();
                                }

                                List<Peptide> peptides = GetAlternativePhosphoisoformPeptides(
                                    identified_phosphopeptide, fixedModifications);
                                List<PhosphopeptideStatistics> all_phosphopeptide_stats =
                                    new List<PhosphopeptideStatistics>(peptides.Count);
                                PhosphopeptideStatistics identified_phosphoisoform = null;
                                List<double> ms2_mz_peaks = new List<double>(spectrum.GetLength(1));
                                for (int i = spectrum.GetLowerBound(1); i <= spectrum.GetUpperBound(1); i++)
                                {
                                    double signal_to_noise = scan_filter.Contains("FTMS")
                                        ? (spectrum[(int) RawLabelDataColumn.Intensity, i] -
                                           spectrum[(int) RawLabelDataColumn.NoiseBaseline, i])/
                                          spectrum[(int) RawLabelDataColumn.NoiseLevel, i]
                                        : ((spectrum[(int) RawLabelDataColumn.Intensity, i]/base_peak_intensity) -
                                           parameters[2])/parameters[3];
                                    if ((intensityThresholdType == IntensityThresholdType.SignalToNoiseRatio
                                         && signal_to_noise >= intensity_threshold)
                                        || (intensityThresholdType != IntensityThresholdType.SignalToNoiseRatio
                                            && spectrum[(int) RawLabelDataColumn.Intensity, i] >= intensity_threshold))
                                    {
                                        ms2_mz_peaks.Add(spectrum[(int) RawLabelDataColumn.MZ, i]);
                                    }
                                }

                                double mz_range = high_mz - low_mz;

                                Dictionary<double, bool> searched_fragment_mzs = new Dictionary<double, bool>();
                                foreach (Peptide peptide in peptides)
                                {
                                    PhosphopeptideStatistics phosphopeptide_stats = new PhosphopeptideStatistics(peptide);
                                    if (peptide.Sequence == identified_phosphopeptide.Sequence)
                                    {
                                        identified_phosphoisoform = phosphopeptide_stats;
                                    }

                                    FragmentDictionary fragments = peptide.CalculateFragments(fragment_types);
                                    foreach (KeyValuePair<string, Fragment> kvp in fragments)
                                    {
                                        phosphopeptide_stats.Fragments.Add(kvp.Key, new Dictionary<int, bool>());

                                        for (int fragment_charge = 1;
                                            fragment_charge <= (charge >= 3 ? 2 : 1);
                                            fragment_charge++)
                                        {
                                            if (fragment_charge > 1 &&
                                                fragment_charge >
                                                (double) kvp.Value.Number/peptide.Sequence.Length*charge)
                                            {
                                                break;
                                            }

                                            double mz = MZFromMassAndCharge(kvp.Value.Mass, fragment_charge);

                                            if (mz < low_mz || mz > high_mz)
                                            {
                                                continue;
                                            }

                                            if (!searched_fragment_mzs.ContainsKey(mz))
                                            {
                                                bool found = false;

                                                foreach (double ms2_mz_peak in ms2_mz_peaks)
                                                {
                                                    if (Math.Abs(ms2_mz_peak - mz) <= mzTolerance)
                                                    {
                                                        found = true;
                                                        break;
                                                    }
                                                    else if (ms2_mz_peak > mz + mzTolerance)
                                                    {
                                                        break;
                                                    }
                                                }

                                                searched_fragment_mzs.Add(mz, found);
                                            }

                                            phosphopeptide_stats.Fragments[kvp.Key].Add(fragment_charge,
                                                searched_fragment_mzs[mz]);
                                        }
                                    }

                                    all_phosphopeptide_stats.Add(phosphopeptide_stats);
                                }

                                all_phosphopeptide_stats.Sort(ComparePhosphopeptidesByDescendingMatchingFragments);

                                PhosphopeptideStatistics best_phosphoisoform = all_phosphopeptide_stats[0];
                                PhosphopeptideStatistics second_best_phosphoisoform = all_phosphopeptide_stats.Count > 1
                                    ? all_phosphopeptide_stats[1]
                                    : null;

                                List<string> best_sequences = new List<string>();
                                foreach (PhosphopeptideStatistics phosphopeptide_stats in all_phosphopeptide_stats)
                                {
                                    if (phosphopeptide_stats.NumberOfMatchingFragments ==
                                        best_phosphoisoform.NumberOfMatchingFragments)
                                    {
                                        best_sequences.Add(phosphopeptide_stats.Peptide.Sequence);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }

                                bool preliminary_localization = second_best_phosphoisoform == null
                                                                ||
                                                                best_phosphoisoform.NumberOfMatchingFragments >
                                                                second_best_phosphoisoform.NumberOfMatchingFragments;

                                bool all_sites_localized = preliminary_localization;

                                Dictionary<string, bool> peptide_sites = new Dictionary<string, bool>();
                                Dictionary<string, bool> protein_sites = new Dictionary<string, bool>();
                                string best_sequence = best_phosphoisoform.Peptide.Sequence;
                                for (int i = 0; i < best_sequence.Length; i++)
                                {
                                    if (char.IsLower(best_sequence[i]))
                                    {
                                        if (preliminary_localization && second_best_phosphoisoform == null)
                                        {
                                            peptide_sites.Add(best_sequence[i] + (i + 1).ToString(), true);
                                            protein_sites.Add(best_sequence[i] + (start_residue + i).ToString(), true);

                                            if (!localized_sites_by_protein.ContainsKey(protein_description))
                                            {
                                                localized_sites_by_protein.Add(protein_description,
                                                    new Dictionary<string, int>());
                                            }
                                            string site = best_sequence[i] + (start_residue + i).ToString();
                                            if (!localized_sites_by_protein[protein_description].ContainsKey(site))
                                            {
                                                localized_sites_by_protein[protein_description].Add(site, 0);
                                            }
                                            localized_sites_by_protein[protein_description][site]++;

                                            if (motifXOutput)
                                            {
                                                ExtractMotifs(motifs, proteins, protein_description, best_sequence,
                                                    start_residue, i);
                                            }
                                        }
                                        else
                                        {
                                            peptide_sites.Add(best_sequence[i] + (i + 1).ToString(), false);
                                            protein_sites.Add(best_sequence[i] + (start_residue + i).ToString(), false);
                                        }
                                    }
                                }

                                double probability_of_success = double.NaN;
                                List<string> theoretical_site_determining_fragment_ions = new List<string>();
                                List<string> matching_site_determining_fragment_ions = new List<string>();
                                List<string> left_site_determining_fragments = new List<string>();
                                List<string> right_site_determining_fragments = new List<string>();
                                List<string> site_determining_fragments = new List<string>();
                                List<string> p_values = new List<string>();
                                List<string> a_scores = new List<string>();
                                List<string> sites_localized = new List<string>();

                                if (preliminary_localization && second_best_phosphoisoform != null)
                                {
                                    probability_of_success = (ms2_mz_peaks.Count*2*mzTolerance)/mz_range;

                                    for (int i = 0; i < best_sequence.Length; i++)
                                    {
                                        if (char.IsLower(best_sequence[i]))
                                        {
                                            int first_phosphorylatable_residue = i - 1;
                                            while (first_phosphorylatable_residue >= 0)
                                            {
                                                if (best_sequence[first_phosphorylatable_residue] == 'S'
                                                    || best_sequence[first_phosphorylatable_residue] == 'T'
                                                    || best_sequence[first_phosphorylatable_residue] == 'Y')
                                                {
                                                    break;
                                                }
                                                else
                                                {
                                                    first_phosphorylatable_residue--;
                                                }
                                            }

                                            int? num_left_theoretical_site_determining_fragment_ions = null;
                                            int? num_left_matching_site_determining_fragment_ions = null;
                                            double left_p_value = double.NaN;
                                            double left_a_score = double.NaN;
                                            if (first_phosphorylatable_residue >= 0)
                                            {
                                                num_left_theoretical_site_determining_fragment_ions = 0;
                                                num_left_matching_site_determining_fragment_ions = 0;

                                                for (int j = first_phosphorylatable_residue + 1; j <= i; j++)
                                                {
                                                    string n_terminal_fragment = fragment_types[0].ToString() +
                                                                                 j.ToString();
                                                    if (best_phosphoisoform.Fragments.ContainsKey(n_terminal_fragment))
                                                    {
                                                        foreach (
                                                            KeyValuePair<int, bool> kvp in
                                                                best_phosphoisoform.Fragments[n_terminal_fragment])
                                                        {
                                                            num_left_theoretical_site_determining_fragment_ions++;
                                                            if (kvp.Value)
                                                            {
                                                                num_left_matching_site_determining_fragment_ions++;
                                                                string n_terminal_fragment_string =
                                                                    n_terminal_fragment + "(+" + kvp.Key.ToString() +
                                                                    ')';
                                                                if (
                                                                    !left_site_determining_fragments.Contains(
                                                                        n_terminal_fragment_string))
                                                                {
                                                                    left_site_determining_fragments.Add(
                                                                        n_terminal_fragment_string);
                                                                }
                                                            }
                                                        }
                                                    }

                                                    string c_terminal_fragment = fragment_types[1].ToString() +
                                                                                 (best_sequence.Length - j).ToString();
                                                    if (best_phosphoisoform.Fragments.ContainsKey(c_terminal_fragment))
                                                    {
                                                        foreach (
                                                            KeyValuePair<int, bool> kvp in
                                                                best_phosphoisoform.Fragments[c_terminal_fragment])
                                                        {
                                                            num_left_theoretical_site_determining_fragment_ions++;
                                                            if (kvp.Value)
                                                            {
                                                                num_left_matching_site_determining_fragment_ions++;
                                                                string c_terminal_fragment_string =
                                                                    c_terminal_fragment + "(+" + kvp.Key.ToString() +
                                                                    ')';
                                                                if (
                                                                    !left_site_determining_fragments.Contains(
                                                                        c_terminal_fragment_string))
                                                                {
                                                                    left_site_determining_fragments.Add(
                                                                        c_terminal_fragment_string);
                                                                }
                                                            }
                                                        }
                                                    }
                                                }

                                                left_p_value =
                                                    alglib.binomialdistr.binomialcdistribution(
                                                        num_left_matching_site_determining_fragment_ions.Value - 1,
                                                        num_left_theoretical_site_determining_fragment_ions.Value,
                                                        probability_of_success);
                                                left_a_score = -10*Math.Log10(left_p_value);
                                            }

                                            int last_phosphorylatable_residue = i + 1;
                                            while (last_phosphorylatable_residue < best_sequence.Length)
                                            {
                                                if (best_sequence[last_phosphorylatable_residue] == 'S'
                                                    || best_sequence[last_phosphorylatable_residue] == 'T'
                                                    || best_sequence[last_phosphorylatable_residue] == 'Y')
                                                {
                                                    break;
                                                }
                                                else
                                                {
                                                    last_phosphorylatable_residue++;
                                                }
                                            }

                                            int? num_right_theoretical_site_determining_fragment_ions = null;
                                            int? num_right_matching_site_determining_fragment_ions = null;
                                            double right_p_value = double.NaN;
                                            double right_a_score = double.NaN;
                                            if (last_phosphorylatable_residue < best_sequence.Length)
                                            {
                                                num_right_theoretical_site_determining_fragment_ions = 0;
                                                num_right_matching_site_determining_fragment_ions = 0;

                                                for (int j = last_phosphorylatable_residue; j > i; j--)
                                                {
                                                    string n_terminal_fragment = fragment_types[0].ToString() +
                                                                                 j.ToString();
                                                    if (best_phosphoisoform.Fragments.ContainsKey(n_terminal_fragment))
                                                    {
                                                        foreach (
                                                            KeyValuePair<int, bool> kvp in
                                                                best_phosphoisoform.Fragments[n_terminal_fragment])
                                                        {
                                                            num_right_theoretical_site_determining_fragment_ions++;
                                                            if (kvp.Value)
                                                            {
                                                                num_right_matching_site_determining_fragment_ions++;
                                                                string n_terminal_fragment_string =
                                                                    n_terminal_fragment + "(+" + kvp.Key.ToString() +
                                                                    ')';
                                                                if (
                                                                    !right_site_determining_fragments.Contains(
                                                                        n_terminal_fragment_string))
                                                                {
                                                                    right_site_determining_fragments.Add(
                                                                        n_terminal_fragment_string);
                                                                }
                                                            }
                                                        }
                                                    }

                                                    string c_terminal_fragment = fragment_types[1].ToString() +
                                                                                 (best_sequence.Length - j).ToString();
                                                    if (best_phosphoisoform.Fragments.ContainsKey(c_terminal_fragment))
                                                    {
                                                        foreach (
                                                            KeyValuePair<int, bool> kvp in
                                                                best_phosphoisoform.Fragments[c_terminal_fragment])
                                                        {
                                                            num_right_theoretical_site_determining_fragment_ions++;
                                                            if (kvp.Value)
                                                            {
                                                                num_right_matching_site_determining_fragment_ions++;
                                                                string c_terminal_fragment_string =
                                                                    c_terminal_fragment + "(+" + kvp.Key.ToString() +
                                                                    ')';
                                                                if (
                                                                    !right_site_determining_fragments.Contains(
                                                                        c_terminal_fragment_string))
                                                                {
                                                                    right_site_determining_fragments.Add(
                                                                        c_terminal_fragment_string);
                                                                }
                                                            }
                                                        }
                                                    }
                                                }

                                                right_p_value =
                                                    alglib.binomialdistr.binomialcdistribution(
                                                        num_right_matching_site_determining_fragment_ions.Value - 1,
                                                        num_right_theoretical_site_determining_fragment_ions.Value,
                                                        probability_of_success);
                                                right_a_score = -10*Math.Log10(right_p_value);
                                            }

                                            theoretical_site_determining_fragment_ions.Add(
                                                (num_left_theoretical_site_determining_fragment_ions.HasValue
                                                    ? num_left_theoretical_site_determining_fragment_ions.ToString()
                                                    : "n/a") + " | " +
                                                (num_right_theoretical_site_determining_fragment_ions.HasValue
                                                    ? num_right_theoretical_site_determining_fragment_ions.ToString()
                                                    : "n/a"));
                                            matching_site_determining_fragment_ions.Add(
                                                (num_left_matching_site_determining_fragment_ions.HasValue
                                                    ? num_left_matching_site_determining_fragment_ions.ToString()
                                                    : "n/a") + " | " +
                                                (num_right_matching_site_determining_fragment_ions.HasValue
                                                    ? num_right_matching_site_determining_fragment_ions.ToString()
                                                    : "n/a"));
                                            site_determining_fragments.Add((left_site_determining_fragments.Count > 0
                                                ? string.Join(",", left_site_determining_fragments.ToArray())
                                                : "n/a") + " | " +
                                                                           (right_site_determining_fragments.Count > 0
                                                                               ? string.Join(",",
                                                                                   right_site_determining_fragments
                                                                                       .ToArray())
                                                                               : "n/a"));
                                            p_values.Add((double.IsNaN(left_p_value) ? "n/a" : left_p_value.ToString()) +
                                                         " | " +
                                                         (double.IsNaN(right_p_value) ? "n/a" : right_p_value.ToString()));
                                            a_scores.Add((double.IsNaN(left_a_score) ? "n/a" : left_a_score.ToString()) +
                                                         " | " +
                                                         (double.IsNaN(right_a_score) ? "n/a" : right_a_score.ToString()));
                                            bool site_localized = (double.IsNaN(left_a_score) ||
                                                                   left_a_score >= ambiguityScoreThreshold) &&
                                                                  (double.IsNaN(right_a_score) ||
                                                                   right_a_score >= ambiguityScoreThreshold);
                                            sites_localized.Add(site_localized.ToString().ToUpper());
                                            if (site_localized)
                                            {
                                                peptide_sites[best_sequence[i] + (i + 1).ToString()] = true;
                                                protein_sites[best_sequence[i] + (start_residue + i).ToString()] = true;

                                                if (!localized_sites_by_protein.ContainsKey(protein_description))
                                                {
                                                    localized_sites_by_protein.Add(protein_description,
                                                        new Dictionary<string, int>());
                                                }
                                                string site = best_sequence[i] + (start_residue + i).ToString();
                                                if (!localized_sites_by_protein[protein_description].ContainsKey(site))
                                                {
                                                    localized_sites_by_protein[protein_description].Add(site, 0);
                                                }
                                                localized_sites_by_protein[protein_description][site]++;

                                                if (motifXOutput)
                                                {
                                                    ExtractMotifs(motifs, proteins, protein_description, best_sequence,
                                                        start_residue, i);
                                                }
                                            }
                                            if (!site_localized)
                                            {
                                                all_sites_localized = false;
                                            }
                                        }
                                    }
                                }

                                int phosphorylations = 0;
                                foreach (
                                    string dynamic_modification in
                                        best_phosphoisoform.Peptide.DynamicModifications.Values)
                                {
                                    if (dynamic_modification.Contains("phosphorylation"))
                                    {
                                        phosphorylations++;
                                    }
                                }
                                string isoform = null;
                                if (all_sites_localized)
                                {
                                    foreach (KeyValuePair<string, bool> kvp in protein_sites)
                                    {
                                        isoform += kvp.Key + ',';
                                    }
                                    isoform = isoform.Substring(0, isoform.Length - 1);
                                    KeyValuePair<int, string> isoform_kvp =
                                        new KeyValuePair<int, string>(phosphorylations, isoform);

                                    if (!localized.ContainsKey(protein_description))
                                    {
                                        localized.Add(protein_description,
                                            new Dictionary<KeyValuePair<int, string>, List<string>>());
                                    }
                                    if (!localized[protein_description].ContainsKey(isoform_kvp))
                                    {
                                        localized[protein_description].Add(isoform_kvp, new List<string>());
                                    }
                                    localized[protein_description][isoform_kvp].Add(line);
                                }
                                else
                                {
                                    if (preliminary_localization)
                                    {
                                        foreach (KeyValuePair<string, bool> kvp in protein_sites)
                                        {
                                            isoform += kvp.Key;
                                            if (!kvp.Value)
                                            {
                                                isoform += '?';
                                            }
                                            isoform += ',';
                                        }
                                    }
                                    else
                                    {
                                        for (int i = 0; i < best_sequence.Length; i++)
                                        {
                                            bool phospho = false;
                                            for (int j = 0; j < all_phosphopeptide_stats.Count; j++)
                                            {
                                                if (all_phosphopeptide_stats[j].NumberOfMatchingFragments <
                                                    best_phosphoisoform.NumberOfMatchingFragments)
                                                {
                                                    break;
                                                }

                                                if (char.IsLower(all_phosphopeptide_stats[j].Peptide.Sequence[i]))
                                                {
                                                    phospho = true;
                                                }
                                            }
                                            if (phospho)
                                            {
                                                isoform += char.ToLower(best_sequence[i]) +
                                                           (start_residue + i).ToString() + "?,";
                                            }
                                        }
                                    }
                                    isoform = isoform.Substring(0, isoform.Length - 1);
                                    KeyValuePair<int, string> isoform_kvp =
                                        new KeyValuePair<int, string>(phosphorylations, isoform);

                                    if (!unlocalized.ContainsKey(protein_description))
                                    {
                                        unlocalized.Add(protein_description,
                                            new Dictionary<KeyValuePair<int, string>, List<string>>());
                                    }
                                    if (!unlocalized[protein_description].ContainsKey(isoform_kvp))
                                    {
                                        unlocalized[protein_description].Add(isoform_kvp, new List<string>());
                                    }
                                    unlocalized[protein_description][isoform_kvp].Add(line);
                                }

                                StreamWriter output = all_sites_localized
                                    ? localized_phospho_output
                                    : unlocalized_phospho_output;

                                output.Write(line + ',');

                                output.Write(identified_phosphoisoform.NumberOfTotalFragments.ToString() + ',');

                                output.Write(identified_phosphoisoform.Peptide.Sequence + ',');
                                output.Write(identified_phosphoisoform.NumberOfMatchingFragments.ToString() + ',');

                                for (int s = 0; s < best_sequences.Count; s++)
                                {
                                    output.Write(best_sequences[s]);
                                    if (s < best_sequences.Count - 1)
                                    {
                                        output.Write('/');
                                    }
                                }
                                output.Write(',');

                                output.Write(best_phosphoisoform.Peptide.Sequence + ',');
                                output.Write(best_phosphoisoform.NumberOfMatchingFragments.ToString() + ',');

                                if (second_best_phosphoisoform != null)
                                {
                                    output.Write(second_best_phosphoisoform.Peptide.Sequence + ',');
                                    output.Write(second_best_phosphoisoform.NumberOfMatchingFragments.ToString() + ',');
                                }
                                else
                                {
                                    output.Write("n/a,n/a,");
                                }

                                output.Write(
                                    (identified_phosphoisoform.NumberOfMatchingFragments ==
                                     best_phosphoisoform.NumberOfMatchingFragments).ToString() + ',');

                                output.Write(preliminary_localization.ToString() + ',');

                                string[] peptide_sites_array = new string[peptide_sites.Count];
                                peptide_sites.Keys.CopyTo(peptide_sites_array, 0);
                                string peptide_sites_array_string = string.Join("; ", peptide_sites_array);
                                AppendFieldToCsv(peptide_sites_array_string, output);
                                output.Write((!double.IsNaN(probability_of_success)
                                    ? probability_of_success.ToString()
                                    : string.Empty) + ',');
                                string theoretical_site_determining_fragment_ions_string = string.Join("; ",
                                    theoretical_site_determining_fragment_ions.ToArray());
                                AppendFieldToCsv(theoretical_site_determining_fragment_ions_string, output);
                                string matching_site_determining_fragment_ions_string = string.Join("; ",
                                    matching_site_determining_fragment_ions.ToArray());
                                AppendFieldToCsv(matching_site_determining_fragment_ions_string, output);
                                string site_determining_fragments_string = string.Join("; ",
                                    site_determining_fragments.ToArray());
                                AppendFieldToCsv(site_determining_fragments_string, output);
                                string p_values_string = string.Join("; ", p_values.ToArray());
                                AppendFieldToCsv(p_values_string, output);
                                string a_scores_string = string.Join("; ", a_scores.ToArray());
                                AppendFieldToCsv(a_scores_string, output);
                                string sites_localized_string = string.Join("; ", sites_localized.ToArray());
                                AppendFieldToCsv(sites_localized_string, output);

                                output.Write(all_sites_localized.ToString().ToUpper());

                                output.WriteLine();
                            }

                            double progress = (double) csv.BaseStream.Position/csv.BaseStream.Length;
                            onUpdateProgress(new ProgressEventArgs((int) Math.Round(progress*100.0)));
                        }

                    }
                    csv.Close();

                    onFinishedFile(new EventArgs());
                }

                raw.Close();

                non_phospho_output.Close();
                localized_phospho_output.Close();
                unlocalized_phospho_output.Close();

                log.WriteLine("Identified Phosphoproteins: " + identified_sites_by_protein.Proteins.ToString());
                log.WriteLine("Identified Phosphosites: " + identified_sites_by_protein.Sites.ToString());

                log.WriteLine();

                log.WriteLine("Localized Phosphoproteins: " + localized_sites_by_protein.Proteins.ToString());
                log.WriteLine("Localized Phosphosites: " + localized_sites_by_protein.Sites.ToString());

                log.WriteLine();

                int localized_phosphoisoforms = 0;
                foreach (KeyValuePair<string, Dictionary<KeyValuePair<int, string>, List<string>>> kvp in localized)
                {
                    foreach (KeyValuePair<KeyValuePair<int, string>, List<string>> kvp2 in kvp.Value)
                    {
                        localized_phosphoisoforms++;
                    }
                }

                int unlocalized_phosphoisoforms = 0;
                foreach (KeyValuePair<string, Dictionary<KeyValuePair<int, string>, List<string>>> kvp in unlocalized)
                {
                    foreach (KeyValuePair<KeyValuePair<int, string>, List<string>> kvp2 in kvp.Value)
                    {
                        unlocalized_phosphoisoforms++;
                    }
                }

                log.WriteLine("Localized Phosphoisoforms: " + localized_phosphoisoforms.ToString());
                log.WriteLine("Unlocalized Phosphoisoforms: " + unlocalized_phosphoisoforms.ToString());

                log.Close();

                using (StreamWriter protein_sites = new StreamWriter(Path.Combine(outputFolder, "localized_protein_phosphosites.csv")))
                {
                    protein_sites.WriteLine("Protein Description, Number of Localized Phosphosites");
                    protein_sites.WriteLine(", Localized Phosphosite");

                    foreach (KeyValuePair<string, Dictionary<string, int>> kvp in localized_sites_by_protein)
                    {
                        protein_sites.WriteLine((kvp.Key.Contains(",") ? '"' + kvp.Key + '"' : kvp.Key) + ',' + kvp.Value.Count.ToString());
                        foreach (KeyValuePair<string, int> kvp2 in kvp.Value)
                        {
                            protein_sites.WriteLine(',' + kvp2.Key);
                        }
                    }
                }

                using (StreamWriter full_localized_output = new StreamWriter(Path.Combine(outputFolder, "full_localized_phosphoisoforms.csv")))
                {
                    //int interference_index = -1;
                    int first_quant_index = -1;
                    int last_quant_index = -1;
                    if (!quant)
                    {
                        full_localized_output.Write("Protein Description, Phosphoisoform, Phosphoisoform Sites, PSMs Identified, Peptides Identified");
                    }
                    else
                    {
                        full_localized_output.Write("Protein Description, Phosphoisoform, Phosphoisoform Sites, PSMs Identified, PSMs Quantified, Peptides Identified, Peptides Quantified,");
                        for (int i = 0; i < headers.Length; i++)
                        {
                            if (headers[i].EndsWith("NL)"))
                            {
                                if (first_quant_index < 0)
                                {
                                    first_quant_index = i;
                                }
                            }
                            if (first_quant_index >= 0)
                                full_localized_output.Write(' ' + headers[i] + ',');
                            if (headers[i].Equals("Channels Detected"))
                            {
                                last_quant_index = i;
                            }
                        }
                        full_localized_output.Write(" Phosphoisoform Quantified?");
                    }
                    full_localized_output.WriteLine();
                    full_localized_output.WriteLine(", " + header_line);

                    using (StreamWriter reduced_localized_output = new StreamWriter(Path.Combine(outputFolder, "reduced_localized_phosphoisoforms.csv")))
                    {
                        if (!quant)
                        {
                            reduced_localized_output.Write("Protein Description, Phosphoisoform, Phosphoisoform Sites,Peptides , PSMs Identified, Peptides Identified");
                        }
                        else
                        {
                            reduced_localized_output.Write("Protein Description, Phosphoisoform, Phosphoisoform Sites,Peptides , PSMs Identified, PSMs Quantified, Peptides Identified, Peptides Quantified,");
                            for (int i = first_quant_index; i <= last_quant_index; i++)
                            {
                                reduced_localized_output.Write(' ' + headers[i] + ',');
                            }
                            reduced_localized_output.Write(" Phosphoisoform Quantified?");
                        }
                        reduced_localized_output.WriteLine();

                        foreach (KeyValuePair<string, Dictionary<KeyValuePair<int, string>, List<string>>> kvp in localized)
                        {
                            foreach (KeyValuePair<KeyValuePair<int, string>, List<string>> kvp2 in kvp.Value)
                            {
                                full_localized_output.Write((kvp.Key.Contains(",") ? '"' + kvp.Key + '"' : kvp.Key) + ',');
                                reduced_localized_output.Write((kvp.Key.Contains(",") ? '"' + kvp.Key + '"' : kvp.Key) + ',');

                                full_localized_output.Write((kvp2.Key.Value.Contains(",") ? '"' + kvp2.Key.Value + '"' : kvp2.Key.Value) + ',');
                                reduced_localized_output.Write((kvp2.Key.Value.Contains(",") ? '"' + kvp2.Key.Value + '"' : kvp2.Key.Value) + ',');

                                full_localized_output.Write(kvp2.Key.Key.ToString() + ',');
                                reduced_localized_output.Write(kvp2.Key.Key.ToString() + ',');

                                double[] isoform_quantitation = new double[last_quant_index - first_quant_index + 1];
                                isoform_quantitation = Array.ConvertAll<double, double>(isoform_quantitation, SET_DOUBLE_VALUE_TO_NAN);
                                int spectra_identified = 0;
                                int spectra_quantified = 0;
                                Dictionary<string, int> unique_peptides_identified = new Dictionary<string, int>();
                                Dictionary<string, int> unique_peptides_quantified = new Dictionary<string, int>();
                                StringBuilder peptides = new StringBuilder();
                                foreach (string line in kvp2.Value)
                                {
                                    string[] fields = Regex.Split(line, @",(?!(?<=(?:^|,)\s*\x22(?:[^\x22]|\x22\x22|\\\x22)*,)(?:[^\x22]|\x22\x22|\\\x22)*\x22\s*(?:,|$))"); // crazy regex to parse CSV with internal double quotes from http://regexlib.com/REDetails.aspx?regexp_id=621
                                    spectra_identified++;
                                    string peptide_sequence = fields[2];
                                    peptides.Append(peptide_sequence + " ");
                                    if (!unique_peptides_identified.ContainsKey(peptide_sequence))
                                    {
                                        unique_peptides_identified.Add(peptide_sequence, 0);
                                    }
                                    unique_peptides_identified[peptide_sequence]++;
                                    if (quant)
                                    {

                                        spectra_quantified++;
                                        if (double.IsNaN(isoform_quantitation[0]))
                                        {
                                            isoform_quantitation = Array.ConvertAll<double, double>(isoform_quantitation, SET_DOUBLE_VALUE_TO_ZERO);
                                        }
                                        if (!unique_peptides_quantified.ContainsKey(peptide_sequence))
                                        {
                                            unique_peptides_quantified.Add(peptide_sequence, 0);
                                        }
                                        unique_peptides_quantified[peptide_sequence]++;
                                        for (int i = first_quant_index; i <= last_quant_index; i++)
                                        {
                                            double val = 0;
                                            double.TryParse(fields[i], out val);
                                            isoform_quantitation[i - first_quant_index] += val;
                                        }

                                    }
                                }

                                full_localized_output.Write(peptides.ToString() + ',');
                                reduced_localized_output.Write(peptides.ToString() + ',');

                                full_localized_output.Write(spectra_identified.ToString() + ',');
                                reduced_localized_output.Write(spectra_identified.ToString() + ',');
                                if (quant)
                                {
                                    full_localized_output.Write(spectra_quantified.ToString() + ',');
                                    reduced_localized_output.Write(spectra_quantified.ToString() + ',');
                                }
                                full_localized_output.Write(unique_peptides_identified.Count.ToString() + ',');
                                reduced_localized_output.Write(unique_peptides_identified.Count.ToString() + ',');
                                if (quant)
                                {
                                    full_localized_output.Write(unique_peptides_quantified.Count.ToString() + ',');
                                    reduced_localized_output.Write(unique_peptides_quantified.Count.ToString() + ',');
                                    for (int i = isoform_quantitation.GetLowerBound(0); i <= isoform_quantitation.GetUpperBound(0); i++)
                                    {
                                        full_localized_output.Write(isoform_quantitation[i].ToString() + ',');
                                        reduced_localized_output.Write(isoform_quantitation[i].ToString() + ',');
                                    }
                                    full_localized_output.Write((spectra_quantified > 0).ToString());
                                    reduced_localized_output.Write((spectra_quantified > 0).ToString());
                                }
                                full_localized_output.WriteLine();
                                reduced_localized_output.WriteLine();

                                foreach (string line in kvp2.Value)
                                {
                                    full_localized_output.WriteLine(',' + line);
                                }
                            }
                        }
                    }
                }

                using (StreamWriter full_unlocalized_output = new StreamWriter(Path.Combine(outputFolder, "full_unlocalized_phosphoisoforms.csv")))
                {
                    int first_quant_index = -1;
                    int last_quant_index = -1;
                    if (!quant)
                    {
                        full_unlocalized_output.Write("Protein Description, Phosphoisoform, Phosphoisoform Sites,Peptides, PSMs Identified, Peptides Identified");
                    }
                    else
                    {
                        full_unlocalized_output.Write("Protein Description, Phosphoisoform, Phosphoisoform Sites,Peptides, PSMs Identified, PSMs Quantified, Peptides Identified, Peptides Quantified,");
                        for (int i = 0; i < headers.Length; i++)
                        {
                            if (headers[i].EndsWith("NL)"))
                            {
                                if (first_quant_index < 0)
                                {
                                    first_quant_index = i;
                                }
                            }
                            if (first_quant_index >= 0)
                                full_unlocalized_output.Write(' ' + headers[i] + ',');
                            if (headers[i].Equals("Channels Detected"))
                            {
                                last_quant_index = i;
                            }
                        }
                        full_unlocalized_output.Write(" Phosphoisoform Quantified?");
                    }
                    full_unlocalized_output.WriteLine();
                    full_unlocalized_output.WriteLine(", " + header_line);

                    using (StreamWriter reduced_unlocalized_output = new StreamWriter(Path.Combine(outputFolder, "reduced_unlocalized_phosphoisoforms.csv")))
                    {
                        if (!quant)
                        {
                            reduced_unlocalized_output.Write("Protein Description, Phosphoisoform, Phosphoisoform Sites,Peptides, PSMs Identified, Peptides Identified");
                        }
                        else
                        {
                            reduced_unlocalized_output.Write("Protein Description, Phosphoisoform, Phosphoisoform Sites,Peptides, PSMs Identified, PSMs Quantified, Peptides Identified, Peptides Quantified,");
                            for (int i = first_quant_index; i <= last_quant_index; i++)
                            {
                                reduced_unlocalized_output.Write(' ' + headers[i] + ',');
                            }
                            reduced_unlocalized_output.Write(" Phosphoisoform Quantified?");
                        }
                        reduced_unlocalized_output.WriteLine();

                        foreach (KeyValuePair<string, Dictionary<KeyValuePair<int, string>, List<string>>> kvp in unlocalized)
                        {
                            foreach (KeyValuePair<KeyValuePair<int, string>, List<string>> kvp2 in kvp.Value)
                            {
                                full_unlocalized_output.Write((kvp.Key.Contains(",") ? '"' + kvp.Key + '"' : kvp.Key) + ',');
                                reduced_unlocalized_output.Write((kvp.Key.Contains(",") ? '"' + kvp.Key + '"' : kvp.Key) + ',');

                                full_unlocalized_output.Write((kvp2.Key.Value.Contains(",") ? '"' + kvp2.Key.Value + '"' : kvp2.Key.Value) + ',');
                                reduced_unlocalized_output.Write((kvp2.Key.Value.Contains(",") ? '"' + kvp2.Key.Value + '"' : kvp2.Key.Value) + ',');

                                full_unlocalized_output.Write(kvp2.Key.Key.ToString() + ',');
                                reduced_unlocalized_output.Write(kvp2.Key.Key.ToString() + ',');

                                double[] isoform_quantitation = new double[last_quant_index - first_quant_index + 1];
                                isoform_quantitation = Array.ConvertAll<double, double>(isoform_quantitation, SET_DOUBLE_VALUE_TO_NAN);
                                int spectra_identified = 0;
                                int spectra_quantified = 0;
                                Dictionary<string, int> unique_peptides_identified = new Dictionary<string, int>();
                                Dictionary<string, int> unique_peptides_quantified = new Dictionary<string, int>();
                                StringBuilder peptides = new StringBuilder();
                                foreach (string line in kvp2.Value)
                                {
                                    string[] fields = Regex.Split(line, @",(?!(?<=(?:^|,)\s*\x22(?:[^\x22]|\x22\x22|\\\x22)*,)(?:[^\x22]|\x22\x22|\\\x22)*\x22\s*(?:,|$))"); // crazy regex to parse CSV with internal double quotes from http://regexlib.com/REDetails.aspx?regexp_id=621
                                    spectra_identified++;
                                    string peptide_sequence = fields[2];
                                    peptides.Append(peptide_sequence + " ");
                                    if (!unique_peptides_identified.ContainsKey(peptide_sequence))
                                    {
                                        unique_peptides_identified.Add(peptide_sequence, 0);
                                    }
                                    unique_peptides_identified[peptide_sequence]++;
                                    if (quant)
                                    {
                                        spectra_quantified++;
                                        if (double.IsNaN(isoform_quantitation[0]))
                                        {
                                            isoform_quantitation = Array.ConvertAll<double, double>(isoform_quantitation, SET_DOUBLE_VALUE_TO_ZERO);
                                        }
                                        if (!unique_peptides_quantified.ContainsKey(peptide_sequence))
                                        {
                                            unique_peptides_quantified.Add(peptide_sequence, 0);
                                        }
                                        unique_peptides_quantified[peptide_sequence]++;
                                        for (int i = first_quant_index; i <= last_quant_index; i++)
                                        {
                                            double val = 0;
                                            double.TryParse(fields[i], out val);
                                            isoform_quantitation[i - first_quant_index] += val;
                                        }

                                    }
                                }

                                full_unlocalized_output.Write(peptides.ToString() + ',');
                                reduced_unlocalized_output.Write(peptides.ToString() + ',');

                                full_unlocalized_output.Write(spectra_identified.ToString() + ',');
                                reduced_unlocalized_output.Write(spectra_identified.ToString() + ',');
                                if (quant)
                                {
                                    full_unlocalized_output.Write(spectra_quantified.ToString() + ',');
                                    reduced_unlocalized_output.Write(spectra_quantified.ToString() + ',');
                                }
                                full_unlocalized_output.Write(unique_peptides_identified.Count.ToString() + ',');
                                reduced_unlocalized_output.Write(unique_peptides_identified.Count.ToString() + ',');
                                if (quant)
                                {
                                    full_unlocalized_output.Write(unique_peptides_quantified.Count.ToString() + ',');
                                    reduced_unlocalized_output.Write(unique_peptides_quantified.Count.ToString() + ',');
                                    for (int i = isoform_quantitation.GetLowerBound(0); i <= isoform_quantitation.GetUpperBound(0); i++)
                                    {
                                        full_unlocalized_output.Write(isoform_quantitation[i].ToString() + ',');
                                        reduced_unlocalized_output.Write(isoform_quantitation[i].ToString() + ',');
                                    }
                                    full_unlocalized_output.Write((spectra_quantified > 0).ToString());
                                    reduced_unlocalized_output.Write((spectra_quantified > 0).ToString());
                                }
                                full_unlocalized_output.WriteLine();
                                reduced_unlocalized_output.WriteLine();

                                foreach (string line in kvp2.Value)
                                {
                                    full_unlocalized_output.WriteLine(',' + line);
                                }
                            }
                        }
                    }
                }

                if (motifXOutput)
                {
                    foreach (string motif in motifs.Keys)
                    {
                        motifX.WriteLine(motif);
                    }
                    motifX.Close();

                    using (StreamWriter motif_fasta = new StreamWriter(Path.Combine(outputFolder, "motif-x.fasta")))
                    {
                        foreach (KeyValuePair<string, string> kvp in proteins)
                        {
                            if (!kvp.Key.Contains("DECOY") && !kvp.Key.Contains("REVERSED"))
                            {
                                motif_fasta.WriteLine('>' + kvp.Key);
                                motif_fasta.WriteLine(kvp.Value);
                            }
                        }
                    }
                }

                onFinished(new EventArgs());
                //}
                //catch(Exception ex)
                //{
                //    onThrowException(new ExceptionEventArgs(ex));
                //}
                //finally
                //{
                if (log != null)
                {
                    log.Close();
                }
                if (raw != null)
                {
                    raw.Close();
                }
                if (csv != null)
                {
                    csv.Close();
                }
                if (non_phospho_output != null)
                {
                    non_phospho_output.Close();
                }
                if (localized_phospho_output != null)
                {
                    localized_phospho_output.Close();
                }
                if (unlocalized_phospho_output != null)
                {
                    unlocalized_phospho_output.Close();
                }
                if (motifX != null)
                {
                    motifX.Close();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message + " " + e.StackTrace);
            }
}
			public Fragment(FragmentType type, string value)
			{
				Type = type;
				Value = value;
			}
Esempio n. 59
0
 private MessageFragment(String text, OpponentData opponent, FragmentType type)
 {
     this.text = text;
     this.opponent = opponent;
     this.type = type;
 }