Exemplo n.º 1
0
        public static async Task <LinkMap> InsertOrMergeEntityAsync(CloudTable table, LinkMap entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }
            try
            {
                // Create the InsertOrReplace table operation
                TableOperation insertOrMergeOperation = TableOperation.InsertOrMerge(entity);

                // Execute the operation.
                TableResult result = await table.ExecuteAsync(insertOrMergeOperation);

                LinkMap insertedLinkMap = result.Result as LinkMap;

                // Get the request units consumed by the current operation. RequestCharge of a TableResult is only applied to Azure Cosmos DB
                if (result.RequestCharge.HasValue)
                {
                    Console.WriteLine("Request Charge of InsertOrMerge Operation: " + result.RequestCharge);
                }

                return(insertedLinkMap);
            }
            catch (StorageException e)
            {
                Console.WriteLine(e.Message);
                Console.ReadLine();
                throw;
            }
        }
Exemplo n.º 2
0
        public void testParseNs()
        {
            Route x = LinkMap.Parse("/cm/Index");

            Assert.AreEqual("Admin", x.ns);
            Assert.AreEqual("TestComment", x.controller);
            Assert.AreEqual("Index", x.action);

            x = LinkMap.Parse("/cm/List");
            Assert.AreEqual("Admin", x.ns);
            Assert.AreEqual("TestComment", x.controller);
            Assert.AreEqual("List", x.action);

            x = LinkMap.Parse("/cm/Show/88");
            Assert.AreEqual("Admin", x.ns);
            Assert.AreEqual("TestComment", x.controller);
            Assert.AreEqual("Show", x.action);
            Assert.AreEqual(88, x.id);

            // 未映射
            x = LinkMap.Parse("/cms/List");
            Assert.IsNull(x);

            x = LinkMap.Parse("/c/List");
            Assert.IsNull(x);
        }
Exemplo n.º 3
0
        public override void Process(ProcessContext context)
        {
            MvcContext ctx = context.ctx;

            MvcEventPublisher.Instance.BeginUrlRewrite(ctx);
            if (ctx.utils.isSkipCurrentProcessor())
            {
                return;
            }

            foreach (KeyValuePair <String, String> kv in LinkMap.GetShortUrlMap())
            {
                if (strUtil.IsNullOrEmpty(kv.Value))
                {
                    continue;
                }

                String sPath = strUtil.Join("", kv.Key);
                sPath = strUtil.Append(sPath, MvcConfig.Instance.UrlExt);

                if (ctx.url.PathAndQueryWithouApp.Equals(sPath))
                {
                    if (kv.Value.StartsWith("http:"))
                    {
                        ctx.web.Redirect(kv.Value);
                        ctx.utils.end();
                    }
                    else
                    {
                        String newUrl = strUtil.Join(ctx.url.SiteAndAppPath, kv.Value);
                        ctx.setUrl(newUrl);
                    }
                }
            }
        }
Exemplo n.º 4
0
        public void testSameMap()
        {
            LinkMap.SetLinkToLow(false);

            Assert.AreEqual("/blog/Index.aspx", LinkMap.To(new Blog.BlogController().Index));
            Assert.AreEqual("/blog/List.aspx", LinkMap.To(new Blog.BlogController().List));
            Assert.AreEqual("/blog/Show/88.aspx", LinkMap.To(new Blog.BlogController().Show, 88));

            Route x = LinkMap.Parse("/blog/Index");

            Assert.AreEqual("Blog", x.ns);
            Assert.AreEqual("Blog", x.controller);
            Assert.AreEqual("Index", x.action);

            // 正常url
            x = LinkMap.Parse("/Blog/Blog/Index");
            Assert.IsNull(x);

            x = LinkMap.Parse("/space/zhang/Blog1/Blog/Index");
            Assert.IsNull(x);

            LinkMap.SetLinkToLow(true);

            x = LinkMap.Parse("/space/zhang/blog1/blog/index");
            Assert.IsNull(x);
        }
Exemplo n.º 5
0
        public static async Task DeleteEntityAsync(CloudTable table, LinkMap deleteEntity)
        {
            try
            {
                if (deleteEntity == null)
                {
                    throw new ArgumentNullException("deleteEntity");
                }

                TableOperation deleteOperation = TableOperation.Delete(deleteEntity);
                TableResult    result          = await table.ExecuteAsync(deleteOperation);

                // Get the request units consumed by the current operation. RequestCharge of a TableResult is only applied to Azure CosmoS DB
                if (result.RequestCharge.HasValue)
                {
                    Console.WriteLine("Request Charge of Delete Operation: " + result.RequestCharge);
                }
            }
            catch (StorageException e)
            {
                Console.WriteLine(e.Message);
                Console.ReadLine();
                throw;
            }
        }
Exemplo n.º 6
0
        public void testParseFull()
        {
            int appId = 5;

            Route x = LinkMap.Parse("/space/zhang/post5/Index");

            Assert.AreEqual("TestPost", x.controller);
            Assert.AreEqual("Index", x.action);
            Assert.AreEqual("zhang", x.owner);
            Assert.AreEqual("user", x.ownerType);
            Assert.AreEqual(appId, x.appId);


            x = LinkMap.Parse("/space/zhang/category8");
            Assert.AreEqual("TestPost", x.controller);
            Assert.AreEqual("List", x.action);
            Assert.AreEqual("zhang", x.owner);
            Assert.AreEqual("user", x.ownerType);
            Assert.AreEqual(8, x.appId);


            // 未映射
            x = LinkMap.Parse("/space/zhang/computer8/99");
            Assert.IsNull(x);
        }
Exemplo n.º 7
0
        public void testParseNs2()
        {
            Route x = LinkMap.Parse("/u/Index");

            Assert.AreEqual("Admin.Users", x.ns);
            Assert.AreEqual("TestUser", x.controller);
            Assert.AreEqual("Index", x.action);

            x = LinkMap.Parse("/u/List");
            Assert.AreEqual("Admin.Users", x.ns);
            Assert.AreEqual("TestUser", x.controller);
            Assert.AreEqual("List", x.action);

            x = LinkMap.Parse("/u/Show/88");
            Assert.AreEqual("Admin.Users", x.ns);
            Assert.AreEqual("TestUser", x.controller);
            Assert.AreEqual("Show", x.action);
            Assert.AreEqual(88, x.id);

            x = LinkMap.Parse("/addr/99");
            Assert.AreEqual("Admin.Users", x.ns);
            Assert.AreEqual("TestUser", x.controller);
            Assert.AreEqual("Address", x.action);
            Assert.AreEqual(99, x.id);
        }
Exemplo n.º 8
0
        public static async Task <LinkMap> RetrieveEntityUsingPointQueryAsync(CloudTable table, string partitionKey, string rowKey)
        {
            try
            {
                TableOperation retrieveOperation = TableOperation.Retrieve <LinkMap>(partitionKey, rowKey);
                TableResult    result            = await table.ExecuteAsync(retrieveOperation);

                LinkMap linkMap = result.Result as LinkMap;
                if (linkMap != null)
                {
                    Console.WriteLine("\t{0}\t{1}\t{2}", linkMap.PartitionKey, linkMap.RowKey, linkMap.LongLink);
                }

                // Get the request units consumed by the current operation. RequestCharge of a TableResult is only applied to Azure CosmoS DB
                if (result.RequestCharge.HasValue)
                {
                    Console.WriteLine("Request Charge of Retrieve Operation: " + result.RequestCharge);
                }

                return(linkMap);
            }
            catch (StorageException e)
            {
                Console.WriteLine(e.Message);
                Console.ReadLine();
                throw;
            }
        }
Exemplo n.º 9
0
        public static async Task <IActionResult> InsertLink(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "addLink")] HttpRequest req,
            ILogger log)
        {
            string shortLink = req.Query["shortLink"];
            string longLink  = req.Query["longLink"];

            string  requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data        = JsonConvert.DeserializeObject(requestBody);

            shortLink = shortLink ?? data?.shortLink;
            longLink  = longLink ?? data?.longLink;

            if (shortLink == null || longLink == null)
            {
                return(new BadRequestObjectResult("Wrong Input"));
            }

            LinkMap linkMap = new LinkMap(shortLink)
            {
                LongLink = longLink
            };

            string tableName = "link";

            // Create or reference an existing table
            CloudTable table = await Common.CreateOrGetCloudTableAsync(tableName);

            // Demonstrate how to insert the entity
            Console.WriteLine("Insert an Entity.");
            linkMap = await Common.InsertOrMergeEntityAsync(table, linkMap);

            return(new OkObjectResult(linkMap.LongLink));
        }
Exemplo n.º 10
0
        public override bool Run()
        {
            if (CodeInfo is null || CodePath is null)
            {
                Status.WriteLine(Severity.Error,
                                 "Application error. Must choose the code repo against which to run this report.");
            }

            try
            {
                Contract.Requires(Directory.Exists(CodePath));
                Contract.Requires(Directory.Exists(Path.Combine(CodePath, ".git")));
            }
            catch
            {
                MessageBox.Show("Invalid code directory.");
                return(false);
            }

            LinkMap.Clear();

            var helper = new RepoHelper();

            try
            {
                using (var docRepo = helper.GetRepository(DocPath))
                    using (var codeRepo = helper.GetRepository(CodePath))
                    {
                        FindRelevantCodeLinks(docRepo, codeRepo);
                        BackfillCommitDates(LinkMap.DocFileIndex.Keys, docRepo, helper);
                        BackfillCommitDates(LinkMap.CodeFileIndex.Keys, codeRepo, helper);
                    }

                if (LinkMap.IsEmpty)
                {
                    Status.WriteLine(Severity.Warning, "No code links found in this repo.");
                    return(false);
                }
                else
                {
                    Status.WriteLine(Severity.Information, "Found links to " +
                                     $"{LinkMap.CodeFileIndex.Count} code files, linked to from " +
                                     $"{LinkMap.DocFileIndex.Count} doc files.");

                    SaveDialog.Title = "Choose where to save the code link report:";
                    var result = SaveDialog.TrySave((writer) => { WriteReport(writer); });
                    Status.WriteLine(result.Sev, result.Message);
                    if (result.Reason == DialogResult.OK)
                    {
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                Status.WriteLine(Severity.Error, $"{ex.GetType().Name}: {ex.Message}");
            }
            return(false);
        }
Exemplo n.º 11
0
        public MapCompanyRowControl(int index)
            : base(By.ClassName("map-popup__main__right__btn"))

        {
            Name  = new LinkMap(By.XPath(string.Format("//li[{0}]/a/span[1]/img[2]", index + 1)));
            Price = new LinkMap(By.XPath(string.Format("//li[{0}]/a/span[2]", index + 1)));
            Date  = new LinkMap(By.XPath(string.Format("//li[{0}]/a/span[3]", index + 1)));
        }
Exemplo n.º 12
0
        public void Init()
        {
            LinkMap.Add("post", "wojilu.Test.Web.Mvc.TestPostController");            // 映射controller
            LinkMap.Add("category", "wojilu.Test.Web.Mvc.TestPostController.List");   // 映射action
            LinkMap.Add("product", "wojilu.Test.Web.Mvc.TestPostController.Product"); // 映射action_id
            LinkMap.Add("cm", "wojilu.Test.Web.Mvc.Admin.TestCommentController");
            LinkMap.Add("u", "wojilu.Test.Web.Mvc.Admin.Users.TestUserController");
            LinkMap.Add("addr", "wojilu.Test.Web.Mvc.Admin.Users.TestUserController.Address");

            LinkMap.Add("blog", "wojilu.Test.Web.Mvc.Blog.BlogController");
        }
Exemplo n.º 13
0
        public MapControl(By locator, HtmlControl container = null)
            : base(locator, container)
        {
            CityName = new TextInput(By.Name("ddelivery_city"));
//            City = new SelectMap(By.ClassName("delivery-place"));
            City           = new SelectMap(By.XPath("//div[@id='ddelivery']/div/div/div/div/input"));
            TakeHere       = new LinkMap(By.XPath("//div/div[2]/div[4]/div[5]/a"));
            ImageLocator   = new LinkMap(By.CssSelector("ymaps.ymaps-image"));
            ImageZoom      = new LinkMap(By.ClassName("ymaps-b-zoom__sprite"));
            HasFittingRoom = new LinkMap(By.XPath("//div[3]/div[3]/div[2]/p[2]/a[2]"));
            Scroll         = new LinkMap(By.XPath("//div[1]/div/div[1]/span"));
        }
Exemplo n.º 14
0
        public void testLinkFullStr()
        {
            LinkMap.SetLinkToLow(false);

            User u = new User {
                Id = 3, Url = "zhang"
            };
            int appId = 5;

            Assert.AreEqual("/space/zhang/post5/Index.aspx", LinkMap.To(u, "TestPost", "Index", appId));
            Assert.AreEqual("/space/zhang/category5.aspx", LinkMap.To(u, "TestPost", "List", appId));
            Assert.AreEqual("/space/zhang/post5/Show/88.aspx", LinkMap.To(u, "TestPost", "Show", 88, appId));
            Assert.AreEqual("/space/zhang/product5/99.aspx", LinkMap.To(u, "TestPost", "Product", 99, appId));
        }
Exemplo n.º 15
0
        /// <summary>
        /// url 必须没有后缀名
        /// </summary>
        /// <param name="cleanUrl">必须没有后缀名</param>
        /// <returns></returns>
        public static Route RecognizePath(String cleanUrl)
        {
            if (strUtil.IsNullOrEmpty(cleanUrl) || cleanUrl.ToLower().Equals("default"))
            {
                cleanUrl = "default";
            }

            logger.Info("RecognizePath begin, clearnUrl=" + cleanUrl);

            Route x = LinkMap.Parse(cleanUrl);

            if (x != null)
            {
                return(x);
            }

            if (cleanUrl.StartsWith("/"))
            {
                cleanUrl = strUtil.TrimStart(cleanUrl, "/");
            }

            String[] arrPathRow = cleanUrl.Split(Separator);

            foreach (RouteSetting route in RouteTable.GetRoutes())
            {
                if (!isMatched(route, arrPathRow))
                {
                    continue;
                }


                RouteParseInfo routeInfo = new RouteParseInfo(MemberPath.getOwnerInfo(arrPathRow), arrPathRow);
                processOwnerNamespaceAppId(routeInfo, route);

                Route result = getResult(route, routeInfo);

                if (result == null)
                {
                    continue;
                }

                setRouteInfo(result, routeInfo, cleanUrl);

                return(result);
            }

            return(null);
        }
Exemplo n.º 16
0
        public void testNsLink()
        {
            LinkMap.SetLinkToLow(false);

            Assert.AreEqual("/cm/Index.aspx", LinkMap.To(new Admin.TestCommentController().Index));
            Assert.AreEqual("/cm/List.aspx", LinkMap.To(new Admin.TestCommentController().List));
            Assert.AreEqual("/cm/Show/88.aspx", LinkMap.To(new Admin.TestCommentController().Show, 88));
            Assert.AreEqual("/cm/Product/99.aspx", LinkMap.To(new Admin.TestCommentController().Product, 99));

            LinkMap.SetLinkToLow(true);

            Assert.AreEqual("/cm/index.aspx", LinkMap.To(new Admin.TestCommentController().Index));
            Assert.AreEqual("/cm/list.aspx", LinkMap.To(new Admin.TestCommentController().List));
            Assert.AreEqual("/cm/show/88.aspx", LinkMap.To(new Admin.TestCommentController().Show, 88));
            Assert.AreEqual("/cm/product/99.aspx", LinkMap.To(new Admin.TestCommentController().Product, 99));
        }
Exemplo n.º 17
0
        public void testNsLink2()
        {
            LinkMap.SetLinkToLow(false);

            Assert.AreEqual("/u/Index.aspx", LinkMap.To(new Admin.Users.TestUserController().Index));
            Assert.AreEqual("/u/List.aspx", LinkMap.To(new Admin.Users.TestUserController().List));
            Assert.AreEqual("/u/Show/88.aspx", LinkMap.To(new Admin.Users.TestUserController().Show, 88));
            Assert.AreEqual("/addr/99.aspx", LinkMap.To(new Admin.Users.TestUserController().Address, 99));

            LinkMap.SetLinkToLow(true);

            Assert.AreEqual("/u/index.aspx", LinkMap.To(new Admin.Users.TestUserController().Index));
            Assert.AreEqual("/u/list.aspx", LinkMap.To(new Admin.Users.TestUserController().List));
            Assert.AreEqual("/u/show/88.aspx", LinkMap.To(new Admin.Users.TestUserController().Show, 88));
            Assert.AreEqual("/addr/99.aspx", LinkMap.To(new Admin.Users.TestUserController().Address, 99));
        }
Exemplo n.º 18
0
        public void testLinkStr()
        {
            LinkMap.SetLinkToLow(false);

            Assert.AreEqual("/post/Index.aspx", LinkMap.To("TestPost", "Index"));

            Assert.AreEqual("/category.aspx", LinkMap.To("TestPost", "List"));
            Assert.AreEqual("/post/Show/88.aspx", LinkMap.To("TestPost", "Show", 88));
            Assert.AreEqual("/product/99.aspx", LinkMap.To("TestPost", "Product", 99));

            // 未映射的Link
            Assert.AreEqual(null, LinkMap.To("TestArticle", "Index"));
            Assert.AreEqual(null, LinkMap.To("TestArticle", "List"));
            Assert.AreEqual(null, LinkMap.To("TestArticle", "Show", 88));
            Assert.AreEqual(null, LinkMap.To("TestArticle", "Product", 99));
        }
Exemplo n.º 19
0
        public static async Task <IActionResult> RedirectLink(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "{shortLink}")] HttpRequest req,
            string shortLink,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed an insert link request.");
            log.LogInformation("shortLink " + shortLink);
            string tableName = "link";

            // Create or reference an existing table
            CloudTable table = await Common.CreateOrGetCloudTableAsync(tableName);

            LinkMap linkMap = await Common.RetrieveEntityUsingPointQueryAsync(table, "Link", shortLink);

            if (linkMap == null)
            {
                return(new RedirectResult("http://error"));
            }
            return(new RedirectResult("http://" + linkMap.LongLink));
        }
Exemplo n.º 20
0
        public void testLinkFull()
        {
            LinkMap.SetLinkToLow(false);

            User u = new User {
                Id = 3, Url = "zhang"
            };
            int appId = 5;

            Assert.AreEqual("/space/zhang/post5/Index.aspx", LinkMap.To(u, new TestPostController().Index, appId));
            Assert.AreEqual("/space/zhang/category5.aspx", LinkMap.To(u, new TestPostController().List, appId));
            Assert.AreEqual("/space/zhang/post5/Show/88.aspx", LinkMap.To(u, new TestPostController().Show, 88, appId));
            Assert.AreEqual("/space/zhang/product5/99.aspx", LinkMap.To(u, new TestPostController().Product, 99, appId));

            appId = 0;
            Assert.AreEqual("/space/zhang/post/Index.aspx", LinkMap.To(u, new TestPostController().Index, appId));
            Assert.AreEqual("/space/zhang/category.aspx", LinkMap.To(u, new TestPostController().List, appId));
            Assert.AreEqual("/space/zhang/post/Show/88.aspx", LinkMap.To(u, new TestPostController().Show, 88, appId));
            Assert.AreEqual("/space/zhang/product/99.aspx", LinkMap.To(u, new TestPostController().Product, 99, appId));

            LinkMap.SetLinkToLow(true);

            appId = 5;
            Assert.AreEqual("/space/zhang/post5/index.aspx", LinkMap.To(u, new TestPostController().Index, appId));
            Assert.AreEqual("/space/zhang/category5.aspx", LinkMap.To(u, new TestPostController().List, appId));
            Assert.AreEqual("/space/zhang/post5/show/88.aspx", LinkMap.To(u, new TestPostController().Show, 88, appId));
            Assert.AreEqual("/space/zhang/product5/99.aspx", LinkMap.To(u, new TestPostController().Product, 99, appId));


            Assert.AreEqual("/post5/index.aspx", LinkMap.To(null, new TestPostController().Index, appId));
            Assert.AreEqual("/category5.aspx", LinkMap.To(null, new TestPostController().List, appId));
            Assert.AreEqual("/post5/show/88.aspx", LinkMap.To(null, new TestPostController().Show, 88, appId));
            Assert.AreEqual("/product5/99.aspx", LinkMap.To(null, new TestPostController().Product, 99, appId));

            // 未映射的Link
            Assert.AreEqual(null, LinkMap.To(u, new TestArticleController().Index, appId));
            Assert.AreEqual(null, LinkMap.To(u, new TestArticleController().List, appId));
            Assert.AreEqual(null, LinkMap.To(u, new TestArticleController().Show, 88, appId));
            Assert.AreEqual(null, LinkMap.To(u, new TestArticleController().Product, 99, appId));
        }
Exemplo n.º 21
0
        public void testLink()
        {
            LinkMap.SetLinkToLow(false);

            Assert.AreEqual("/post/Index.aspx", LinkMap.To(new TestPostController().Index));
            Assert.AreEqual("/category.aspx", LinkMap.To(new TestPostController().List));
            Assert.AreEqual("/post/Show/88.aspx", LinkMap.To(new TestPostController().Show, 88));
            Assert.AreEqual("/product/99.aspx", LinkMap.To(new TestPostController().Product, 99));

            LinkMap.SetLinkToLow(true);

            Assert.AreEqual("/post/index.aspx", LinkMap.To(new TestPostController().Index));
            Assert.AreEqual("/category.aspx", LinkMap.To(new TestPostController().List));
            Assert.AreEqual("/post/show/88.aspx", LinkMap.To(new TestPostController().Show, 88));
            Assert.AreEqual("/product/99.aspx", LinkMap.To(new TestPostController().Product, 99));

            // 未映射的Link
            Assert.AreEqual(null, LinkMap.To(new TestArticleController().Index));
            Assert.AreEqual(null, LinkMap.To(new TestArticleController().List));
            Assert.AreEqual(null, LinkMap.To(new TestArticleController().Show, 88));
            Assert.AreEqual(null, LinkMap.To(new TestArticleController().Product, 99));
        }
Exemplo n.º 22
0
        static void fun6()
        {
            UnsafeSynchronizer unsafeSynchronizer  = new UnsafeSynchronizer();
            LinkMap <int, UnsafeSynchronizer> nscd = new LinkMap <int, UnsafeSynchronizer>();

            for (int i = 0; i < 3; i++)
            {
                myk mmm = new myk();
                mmm.p = i;
                Thread t = new Thread((mmm) =>
                {
                    var m = mmm as myk;
                    //var w = o as ThreadWorkItem;
                    //if (w == null) return;
                    UnsafeSynchronizer uns = new UnsafeSynchronizer();

                    nscd.put(m.p, uns);
                    Console.WriteLine("我是线程:" + Thread.CurrentThread.Name + ":群里无聊吗?");
                    Thread.Sleep(300);
                    uns.acquire();


                    Console.WriteLine("我是线程:" + Thread.CurrentThread.Name + "退出了群聊");
                    Thread.Sleep(5000);
                    unsafeSynchronizer.release();
                });
                t.Name = "Hello,i 'am Thread: " + i;

                t.Start(mmm);
            }
            Thread.Sleep(5000);
            for (int i = 0; i < 3; i++)
            {
                nscd.get(i).release();
            }
            unsafeSynchronizer.acquire();
            Console.WriteLine("我是线程:" + Thread.CurrentThread.Name + "群主解散了群聊");
            //threads[0].Suspend();
        }
Exemplo n.º 23
0
 private async void Button_Click_1(object sender, RoutedEventArgs e)
 {
     try
     {
         uiX = Convert.ToInt32(txtbX.Text);
         uiY = Convert.ToInt32(txtbY.Text);
         if (uiX * uiY < 130 && 5 < uiX && uiX < 15 && 5 < uiY && uiY < 15)
         {
             uixImg  = uiX * uiY / 4;
             linkmap = new LinkMap(uiX, uiY, uixImg);
             LoadMapGrid();
         }
         else
         {
             MessageDialog msd = new MessageDialog("要求宽和高必须大于5并且小于15并且难度别太高");
             await msd.ShowAsync();
         }
     }
     catch
     {
         txtbX.Text = "错误";
     }
 }
Exemplo n.º 24
0
        private void FindRelevantCodeLinks(Repository docRepo, Repository codeRepo)
        {
            var dir      = Path.Combine(DocPath, ArticlesRoot1);
            var files    = Directory.GetFiles(dir, "*.md", SearchOption.AllDirectories);
            var pattern1 = "[!code-";
            var pattern2 = $"](~/../{CodeInfo.PathToRoot}/";

            foreach (var file in files)
            {
                var relDocPath = file.Substring(DocPath.Length + 1);
                var docFile    = new CodeLinkMap.FileData
                {
                    BranchName  = docRepo.Head.FriendlyName,
                    RelFilePath = relDocPath.Trim().Replace('\\', '/'),
                };

                var lines = File.ReadAllLines(file).ToList();
                for (var lineNum = 0; lineNum < lines.Count; lineNum++)
                {
                    // The array of lines is 0-based, but "line numbers" are 1-based.
                    var line = lines[lineNum];
                    if (line.StartsWith(pattern1, StringComparison.InvariantCultureIgnoreCase))
                    {
                        var pos = line.IndexOf(pattern2, StringComparison.InvariantCultureIgnoreCase);
                        if (pos < 0)
                        {
                            Status.WriteLine(Severity.Information,
                                             $"Ignoring link in {relDocPath,-64} line {lineNum + 1,3}:  {line}");
                            continue;
                        }

                        var start = pos + pattern2.Length;
                        var end   = line.IndexOf(")]", start);
                        if (end < 0)
                        {
                            Status.WriteLine(Severity.Warning,
                                             $"Poorly formed code link in {relDocPath} at line {lineNum + 1}.");
                            continue;
                        }

                        pos = line.IndexOf('?', start);
                        string relCodePath, queryParameters;
                        if (pos < 0)
                        {
                            relCodePath     = line.Substring(start, end - start);
                            queryParameters = null;
                        }
                        else
                        {
                            relCodePath = line.Substring(start, pos - start);
                            pos++;
                            queryParameters = line.Substring(pos, end - pos);
                        }

                        var codeFile = new CodeLinkMap.FileData
                        {
                            BranchName  = codeRepo.Head.FriendlyName,
                            RelFilePath = relCodePath.Trim(),
                        };

                        LinkMap.Add(docFile, codeFile, lineNum + 1, queryParameters);
                    }
                }
            }
        }
Exemplo n.º 25
0
 public CompoundField(FieldType type) : base(null, type)
 {
     _individualFields = new LinkMap <string, Field>();
 }
Exemplo n.º 26
0
        /// <summary>
        /// run clustering, Rescue PSMs, update idpDB
        /// </summary>
        private void RescuePSMsByClustering()
        {
            DateTime startTime = DateTime.Now;
            reportProgressDelegate reportProgress = new reportProgressDelegate(setProgress);
            reportStatusDelegate reportStatus = new reportStatusDelegate(setStatus);

            string database = session.Connection.GetDataSource();
            logFile = Path.ChangeExtension(database, ".log.txt");

            string config = string.Format("Parameters:\r\n" +
                                          "PrecursorMZTol: {0} \r\n" +
                                          "FragmentMZTol: {1} \r\n" +
                                          "Similarity Threshold >= {2} \r\n" +
                                          "Rank <= {3} \r\n" +
                                          "Cluster Size >= {4} \r\n" +
                                          "Search Scores: {5}{6}{7};{8}{9}{10};{11}{12}{13} \r\n\r\n",
                                          precursorMzTolerance,
                                          fragmentMzTolerance,
                                          similarityThreshold,
                                          maxRank,
                                          minClusterSize,
                                          searchScore1Name, searchScore1Order, searchScore1Threshold,
                                          searchScore2Name, searchScore2Order, searchScore2Threshold,
                                          searchScore3Name, searchScore3Order, searchScore3Threshold);
            reportStatus(config);

            //if (writeLog)
            //    File.WriteAllText(logFile, config);

            /*
             * back up original idpDB
             */
            if (backupDB)
            {
                string dbBackupFile = Path.ChangeExtension(database, ".backup.idpDB");
                reportStatus(string.Format("Backing up idpDB to {0} ... ", dbBackupFile));
                reportProgress(-1, "Backing up idpDB");
                File.Copy(database, dbBackupFile, true);
                reportStatus(reportSecondsElapsed((DateTime.Now - startTime).TotalSeconds));
            }

            //reportStatus("Dropping filters... \r\n");
            // basicDataFilter.DropFilters(session);  //// this will drop all filtered tables and rename unfiltered tables
            //basicDataFilter.ApplyBasicFilters(session);

            reportStatus("Querying spectra...");
            reportProgress(-1, "Querying spectra...");
            IList<object[]> queryRows;
            lock (session)
                //// SQL query to retrieve spectrum info for unfiltered psm, filter query results by rank1 search score
                //                queryRows = session.CreateSQLQuery(@"SELECT s.Id, source.Name, NativeID, PrecursorMZ
                //                                                        FROM Spectrum s
                //                                                        JOIN SpectrumSource source ON s.Source = source.Id
                //                                                        JOIN UnfilteredPeptideSpectrumMatch psm ON s.Id = psm.Spectrum AND psm.Rank = 1
                //                                                        JOIN PeptideSpectrumMatchScore psmScore ON psm.Id = psmScore.PsmId
                //                                                        JOIN PeptideSpectrumMatchScoreName scoreName ON psmScore.ScoreNameId=scoreName.Id
                //                                                        WHERE (scoreName.Name = " + "'" + searchScore1Name + "'" + " AND psmScore.Value " + searchScore1Order + searchScore1Threshold.ToString() + ") OR (scoreName.Name = " + "'" + searchScore2Name + "'" + " AND psmScore.Value " + searchScore2Order + searchScore2Threshold.ToString() + ") OR (scoreName.Name = " + "'" + searchScore3Name + "'" + " AND psmScore.Value " + searchScore3Order + searchScore3Threshold.ToString() + ")" +
                //                                                        " GROUP BY s.Id"
                //                                                    ).List<object[]>();

                //// SQL query to retrieve spectrum info for unfiltered psm that map to identified peptide, filter by search score 
                queryRows = session.CreateSQLQuery(@"SELECT s.Id, source.Name, NativeID, PrecursorMZ
                                                        FROM UnfilteredSpectrum s
                                                        JOIN SpectrumSource source ON s.Source = source.Id
                                                        JOIN UnfilteredPeptideSpectrumMatch psm ON s.Id = psm.Spectrum
                                                        JOIN Peptide p ON p.Id = psm.Peptide
                                                        JOIN PeptideSpectrumMatchScore psmScore ON psm.Id = psmScore.PsmId
                                                        JOIN PeptideSpectrumMatchScoreName scoreName ON psmScore.ScoreNameId=scoreName.Id
                                                        WHERE (scoreName.Name = " + "'" + searchScore1Name + "'" + " AND psmScore.Value " + searchScore1Order + searchScore1Threshold.ToString() + ") OR (scoreName.Name = " + "'" + searchScore2Name + "'" + " AND psmScore.Value " + searchScore2Order + searchScore2Threshold.ToString() + ") OR (scoreName.Name = " + "'" + searchScore3Name + "'" + " AND psmScore.Value " + searchScore3Order + searchScore3Threshold.ToString() + ")" +
                                                                       " GROUP BY s.Id"
                                                                   ).List<object[]>();
            var foundSpectraList = session.CreateSQLQuery(@"SELECT distinct spectrum FROM PeptideSpectrumMatch").List<object>();
            var foundSpectra = new HashSet<long>();
            {
                long tempLong;
                foreach (var item in foundSpectraList)
                    if (long.TryParse(item.ToString(), out tempLong))
                        foundSpectra.Add(tempLong);
            }

            var spectrumRows = queryRows.Select(o => new SpectrumRow(o)).OrderBy(o => o.SourceName).ToList();
            ////converted IOrderedEnumerable to List, the former one may end up with multiple enumeration, each invokes constructor, resulting a fresh set of object

            /*
             * extract peaks for each spectrum, spectrumRows was sorted by SourceName
            */
            string currentSourceName = null;
            string currentSourcePath = null;
            msdata.MSData msd = null;
            int spectrumRowsCount = spectrumRows.Count();
            //Set<long> processedSpectrumIDs = new Set<long>();

            reportStatus(reportSecondsElapsed((DateTime.Now - startTime).TotalSeconds));
            reportStatus(string.Format("Extracting peaks for {0} spectra ... ", spectrumRowsCount));
            lock (owner)
                for (int i = 0; i < spectrumRowsCount; ++i)
                {
                    if (_bgWorkerClustering.CancellationPending)
                    {
                        _bgWorkerCancelled = true;
                        return;
                    }

                    var row = spectrumRows.ElementAt(i);

                    reportProgress((int)(((double)(i + 1) / (double)spectrumRowsCount) * 100), string.Format("Extracting peaks ({0}/{1}) from {2}", i + 1, spectrumRowsCount, row.SourceName));

                    //if (processedSpectrumIDs.Contains(row.SpectrumId))
                    //    break;
                    if (row.SourceName != currentSourceName)
                    {
                        currentSourceName = row.SourceName;
                        currentSourcePath = IDPickerForm.LocateSpectrumSource(currentSourceName, session.Connection.GetDataSource());
                        if (msd != null)
                            msd.Dispose();
                        msd = new pwiz.CLI.msdata.MSDataFile(currentSourcePath);

                        SpectrumListFactory.wrap(msd, "threshold count 100 most-intense"); //only keep the top 100 peaks
                        //SpectrumListFactory.wrap(msd, "threshold bpi-relative .5 most-intense"); //keep all peaks that are at least 50% of the intensity of the base peak
                        //SpectrumListFactory.wrap(msd, "threshold tic-cutoff .95 most-intense"); //keep all peaks that count for 95% TIC
                        //threshold <count|count-after-ties|absolute|bpi-relative|tic-relative|tic-cutoff> <threshold> <most-intense|least-intense> [int_set(MS levels)]
                    }

                    var spectrumList = msd.run.spectrumList;
                    var pwizSpectrum = spectrumList.spectrum(spectrumList.find(row.SpectrumNativeID), true); //may create indexoutofrange error if no spectrum nativeID                   
                    row.OriginalMZs = pwizSpectrum.getMZArray().data; //getMZArray().data returns IList<double>
                    row.OriginalIntensities = pwizSpectrum.getIntensityArray().data;
                    //processedSpectrumIDs.Add(row.SpectrumId);

                }

            /* 
             * re-sort spectrumRows by precursorMZ
             * walk through each spectrum. compare similarity to all other spectra within the precursorMZTolerance 
             * (e.g. compare 1 to 2,3,4, then 2 to 3,4,5, then 3 to 4,5 etc), 
             * if above similarityThreshold, add link edge to BOTH spectra
             * merge all connected spectra to a cluster             
            */
            reportStatus(reportSecondsElapsed((DateTime.Now - startTime).TotalSeconds));
            reportStatus("Computing similarities... ");
            var spectrumRowsOrderByPrecursorMZ = (from randomVar in spectrumRows orderby randomVar.PrecursorMZ select randomVar).ToList();
            LinkMap linkMap = new LinkMap(); //// spectrum Id as key, directly linked spectra as value
            double similarityScore = 0;
            lock (owner)
                for (int i = 0; i < spectrumRowsCount; ++i)
                {
                    if (_bgWorkerClustering.CancellationPending)
                    {
                        _bgWorkerCancelled = true;
                        return;
                    }

                    var row = spectrumRowsOrderByPrecursorMZ.ElementAt(i);

                    reportProgress((int)(((double)(i + 1) / (double)spectrumRowsCount) * 100), "Computing similarities");
                    for (int j = i + 1; j < spectrumRowsCount; ++j)
                    {
                        var nextRow = spectrumRowsOrderByPrecursorMZ.ElementAt(j);

                        if (Math.Abs(row.PrecursorMZ - nextRow.PrecursorMZ) > precursorMzTolerance)
                        {
                            break;
                        }
                        else
                        {
                            ////compare pairwise similarity, link spectra passing threshold to both spectrum
                            Peaks rowPeakList = new Peaks(row.OriginalMZs, row.OriginalIntensities);
                            Peaks nextRowPeakList = new Peaks(nextRow.OriginalMZs, nextRow.OriginalIntensities);
                            //// converting peak intensities to sqrt here is 5-fold slower than doing this in DotProductCompareTo function
                            //Peaks rowPeakList = new Peaks(row.OriginalMZs, row.OriginalIntensities.Select(o => Math.Sqrt(o)).ToList());
                            //Peaks nextRowPeakList = new Peaks(nextRow.OriginalMZs, nextRow.OriginalIntensities.Select(o => Math.Sqrt(o)).ToList());
                            similarityScore = ClusteringAnalysis.DotProductCompareTo(rowPeakList, nextRowPeakList, fragmentMzTolerance);
                            //reportStatus("similarity between " + row.SpectrumNativeID + " and " + nextRow.SpectrumNativeID + " is " + similarityScore.ToString() + "\r\n");
                            if (similarityScore >= similarityThreshold)
                            {
                                linkMap[(long)row.SpectrumId].Add((long)nextRow.SpectrumId);
                                linkMap[(long)nextRow.SpectrumId].Add((long)row.SpectrumId); //// if a -> b, then b -> a  
                            }
                        }
                    }
                }
            reportStatus(reportSecondsElapsed((DateTime.Now - startTime).TotalSeconds));

            reportStatus("Clustering spectra... ");
            reportProgress(-1, "Clustering spectra");
            linkMap.GetMergedLinkList();
            reportStatus(reportSecondsElapsed((DateTime.Now - startTime).TotalSeconds));

            //// print clustered spectra
            //foreach (var cluster in linkMap.MergedLinkList)
            //{
            //    reportStatus("Number of spectra in cluster: " + cluster.Count().ToString() + "\r\n");
            //    foreach (var sID in cluster)
            //    {
            //        var nativeID = (from o in spectrumRows where o.SpectrumId == sID select o.SpectrumNativeID).First();
            //        reportStatus(nativeID.ToString() + "\t");
            //    }
            //    reportStatus("\r\n");
            //}

            ////free some memory
            queryRows.Clear();
            queryRows = null;
            msd.Dispose();
            msd = null;
            spectrumRows.Clear();
            spectrumRows = null;
            spectrumRowsOrderByPrecursorMZ.Clear();
            spectrumRowsOrderByPrecursorMZ = null;

            /* 
             * Go through each cluster, rescue PSMs if spectra in the same cluster were identified as the same peptide (id)
             */
            List<Set<long>> clusterSetList = (from o in linkMap.MergedLinkList where o.Count >= minClusterSize select o).ToList();    //// each element in the list is a set of clustered spectrum Ids, select sets with at least minClusterSize element           
            int clusterSetListCount = clusterSetList.Count();
            var allSpectrumIDs = (from o in clusterSetList from j in o select j).ToList();
            reportStatus(string.Format("Number of clusters: {0} \r\n", clusterSetListCount));
            reportStatus(string.Format("Number of spectra clustered: {0}/{1} ({2:0.0%}) \r\n", allSpectrumIDs.Count, spectrumRowsCount, (double)allSpectrumIDs.Count / spectrumRowsCount));

            IList<object> identPSMQueryRows;
            lock (session)
                identPSMQueryRows = session.CreateSQLQuery(@"SELECT psm.Id FROM PeptideSpectrumMatch psm").List<object>();

            var identPSMIdSet = new Set<long>(identPSMQueryRows.Select(o => (long)o));
            reportStatus(string.Format("Number of PSMs identified: {0} \r\n", identPSMIdSet.Count));

            //// create a temp table to store clustered spectrum IDs
            session.CreateSQLQuery(@"DROP TABLE IF EXISTS TempSpecIds;
                                     CREATE TEMP TABLE TempSpecIds (Id INTEGER PRIMARY KEY)
                                    ").ExecuteUpdate();

            var insertTempSpecIdscmd = session.Connection.CreateCommand();
            insertTempSpecIdscmd.CommandText = "INSERT INTO TempSpecIds VALUES (?)";
            var insertTempSpecIdsParameters = new List<System.Data.IDbDataParameter>();
            for (int i = 0; i < 1; ++i)
            {
                insertTempSpecIdsParameters.Add(insertTempSpecIdscmd.CreateParameter());
                insertTempSpecIdscmd.Parameters.Add(insertTempSpecIdsParameters[i]);
            }
            insertTempSpecIdscmd.Prepare();
            foreach (var id in allSpectrumIDs)
            {
                insertTempSpecIdsParameters[0].Value = id;
                insertTempSpecIdscmd.ExecuteNonQuery();
            }


            IList<object> allPsmIdQueryRows;
            lock (session)
                //// SQL query to retrieve all psm id for clustered spectra with score above a threshold
                allPsmIdQueryRows = session.CreateSQLQuery(@"SELECT GROUP_CONCAT(psm.Id)
                                                        FROM TempSpecIds
                                                        JOIN UnfilteredPeptideSpectrumMatch psm ON TempSpecIds.Id = psm.Spectrum
                                                        JOIN PeptideSpectrumMatchScore psmScore ON psm.Id = psmScore.PsmId
                                                        JOIN PeptideSpectrumMatchScoreName scoreName ON psmScore.ScoreNameId=scoreName.Id
                                                        WHERE psm.Rank <= " + maxRank.ToString() + " AND ((scoreName.Name = " + "'" + searchScore1Name + "'" + " AND psmScore.Value " + searchScore1Order + searchScore1Threshold.ToString() + ") OR (scoreName.Name = " + "'" + searchScore2Name + "'" + " AND psmScore.Value " + searchScore2Order + searchScore2Threshold.ToString() + ") OR (scoreName.Name = " + "'" + searchScore3Name + "'" + " AND psmScore.Value " + searchScore3Order + searchScore3Threshold.ToString() + "))" +
                                                        " GROUP BY TempSpecIds.Id, psm.Charge"
                                                    ).List<object>();

            var allPsmIdsRows = allPsmIdQueryRows.Select(o => new PsmIdRow(o)).ToList();

            Set<long> allPsmIds = new Set<long>();
            foreach (var row in allPsmIdsRows)
            {
                allPsmIds.Union(row.PsmIds);
            }

            session.CreateSQLQuery(@"DROP TABLE IF EXISTS TempSpecIds").ExecuteUpdate();

            reportStatus("Querying PSMs...");
            reportProgress(-1, "Querying PSMs");
            IList<object[]> allClusterQueryRows;

            //// create a temp table to store psm IDs
            session.CreateSQLQuery(@"DROP TABLE IF EXISTS TempPsmIds;
                                     CREATE TEMP TABLE TempPsmIds (Id INTEGER PRIMARY KEY)
                                    ").ExecuteUpdate();

            var cmd = session.Connection.CreateCommand();
            cmd.CommandText = "INSERT INTO TempPsmIds VALUES (?)";
            var parameters = new List<System.Data.IDbDataParameter>();
            for (int i = 0; i < 1; ++i)
            {
                parameters.Add(cmd.CreateParameter());
                cmd.Parameters.Add(parameters[i]);
            }
            cmd.Prepare();
            foreach (var id in allPsmIds)
            {
                parameters[0].Value = id;
                cmd.ExecuteNonQuery();
            }

            //// qurey string for revison 286, no DecoySequence in Peptide table
            //            string queryCmd = @"SELECT psm.Id as psmId, s.Id, source.Name, s.NativeID, psm.Rank, psm.Charge, psmScore.Value, IFNULL(GROUP_CONCAT(DISTINCT pm.Offset || ':' || mod.MonoMassDelta),''),
            //                                    (SELECT SUBSTR(pro.Sequence, pi.Offset+1, pi.Length)
            //                                                                FROM PeptideInstance pi
            //                                                                JOIN ProteinData pro ON pi.Protein=pro.Id
            //                                                                WHERE pi.Protein=pro.Id AND
            //                                                                  pi.Id=(SELECT MIN(pi2.Id)
            //                                                                         FROM PeptideInstance pi2
            //                                                                         WHERE psm.Peptide=pi2.Peptide))
            //                                    FROM TempIDs tempIDs
            //                                    JOIN Spectrum s ON s.Id = tempIDs.Id
            //                                    JOIN SpectrumSource source ON s.Source = source.Id
            //                                    JOIN PeptideSpectrumMatch psm ON s.Id = psm.Spectrum
            //                                    LEFT JOIN PeptideModification pm ON psm.Id = pm.PeptideSpectrumMatch
            //                                    LEFT JOIN Modification mod ON pm.Modification = mod.Id
            //                                    JOIN PeptideSpectrumMatchScore psmScore ON psm.Id = psmScore.PsmId
            //                                    JOIN PeptideSpectrumMatchScoreName scoreName ON psmScore.ScoreNameId=scoreName.Id
            //                                    WHERE scoreName.Name = " + "'" + searchScoreName + "'" + " AND psm.Rank <= 5" +
            //                                " GROUP BY psm.Id";
            //AND s.Id IN ( " + String.Join(",", allSpectrumIDs.Select(o => o.ToString()).ToArray()) + " ) " +

            //// query string for revison 288, added DecoySequence in Peptide table
            //            string queryCmd = @"SELECT psm.Id as psmId, s.Id, source.Name, s.NativeID, psm.Rank, psm.Charge, psmScore.Value, IFNULL(GROUP_CONCAT(DISTINCT pm.Offset || ':' || mod.MonoMassDelta),''),
            //                                    (SELECT IFNULL(SUBSTR(pro.Sequence, pi.Offset+1, pi.Length), (SELECT DecoySequence FROM Peptide p WHERE p.Id = pi.Peptide))
            //                                            FROM PeptideInstance pi
            //                                            LEFT JOIN ProteinData pro ON pi.Protein=pro.Id
            //                                            WHERE pi.Id=(SELECT pi2.Id FROM PeptideInstance pi2 WHERE pi2.Peptide=psm.Peptide LIMIT 1))
            //                                    FROM TempIDs tempIDs
            //                                    JOIN Spectrum s ON s.Id = tempIDs.Id
            //                                    JOIN SpectrumSource source ON s.Source = source.Id
            //                                    JOIN PeptideSpectrumMatch psm ON s.Id = psm.Spectrum
            //                                    LEFT JOIN PeptideModification pm ON psm.Id = pm.PeptideSpectrumMatch
            //                                    LEFT JOIN Modification mod ON pm.Modification = mod.Id
            //                                    JOIN PeptideSpectrumMatchScore psmScore ON psm.Id = psmScore.PsmId
            //                                    JOIN PeptideSpectrumMatchScoreName scoreName ON psmScore.ScoreNameId=scoreName.Id
            //                                    WHERE scoreName.Name = " + "'" + searchScoreName + "'" + " AND psm.Rank <= 5" +
            //                                " GROUP BY psm.Id";

            ////query string for revision 291, retrive by PSM Ids
            //            string queryCmd = @"SELECT psm.Id as psmId, psm.Peptide,s.Id, source.Name, s.NativeID, psm.Charge, IFNULL(GROUP_CONCAT(DISTINCT pm.Offset || ':' || mod.MonoMassDelta),''),
            //                                    (SELECT IFNULL(SUBSTR(pd.Sequence, pi.Offset+1, pi.Length), (SELECT DecoySequence FROM UnfilteredPeptide p WHERE p.Id = pi.Peptide))),
            //                                    GROUP_CONCAT(pro.Accession),psm.QValue, psm.Rank, psmScore.Value, analysis.Id
            //                                    FROM TempPsmIds tempPsmIds
            //                                    JOIN UnfilteredPeptideSpectrumMatch psm ON psm.Id = tempPsmIds.Id 
            //                                    JOIN Analysis analysis ON psm.Analysis = analysis.Id
            //                                    JOIN Spectrum s ON s.Id = psm.Spectrum
            //                                    JOIN SpectrumSource source ON s.Source = source.Id
            //                                    JOIN UnfilteredPeptideInstance pi ON psm.Peptide = pi.Peptide
            //                                    JOIN UnfilteredProtein pro ON pi.Protein = pro.Id
            //                                    LEFT JOIN ProteinData pd ON pi.Protein=pd.Id
            //                                    LEFT JOIN PeptideModification pm ON psm.Id = pm.PeptideSpectrumMatch
            //                                    LEFT JOIN Modification mod ON pm.Modification = mod.Id
            //                                    LEFT JOIN PeptideSpectrumMatchScore psmScore ON psm.Id = psmScore.PsmId
            //                                    LEFT JOIN PeptideSpectrumMatchScoreName scoreName ON psmScore.ScoreNameId=scoreName.Id
            //                                    WHERE scoreName.Name = " + "'" + searchScore1Name + "'" +
            //                                    " GROUP BY psm.Id";

            // query for r291, fix no seq for some peptides shared by target and decoy proteins, query seq for target and decoy proteins separately then union
            string queryCmd = @"SELECT psm.Id as psmId, psm.Peptide,s.Id, source.Name, s.NativeID, psm.Charge, 
                                        IFNULL(GROUP_CONCAT(DISTINCT pm.Offset || ':' || mod.MonoMassDelta),''),
                                        IFNULL(IFNULL(SUBSTR(pd.Sequence, pi.Offset+1, pi.Length),(SELECT DecoySequence FROM UnfilteredPeptide p WHERE p.Id = pi.Peptide)),
                                                (SELECT SUBSTR(pd.Sequence, pi.Offset+1, pi.Length)
                                                FROM UnfilteredPeptideInstance pi 
                                                JOIN UnfilteredProtein pro ON pi.Protein = pro.Id AND pro.IsDecoy = 0
                                                LEFT JOIN ProteinData pd ON pi.Protein=pd.Id
                                                WHERE psm.Peptide = pi.Peptide
                                                UNION
                                                SELECT p.DecoySequence
                                                FROM UnfilteredPeptide p
                                                JOIN UnfilteredPeptideInstance pi ON p.Id = pi.Peptide
                                                JOIN UnfilteredProtein pro ON pi.Protein = pro.Id AND pro.IsDecoy = 1
                                                WHERE psm.Peptide = pi.Peptide AND p.DecoySequence is not null)),
                                        GROUP_CONCAT(pro.Accession),
                                        psm.QValue, psm.Rank, psmScore.Value, psm.Analysis
                                        FROM TempPsmIds tempPsmIds
                                        JOIN UnfilteredPeptideSpectrumMatch psm ON psm.Id = tempPsmIds.Id 
                                        JOIN UnfilteredSpectrum s ON s.Id = psm.Spectrum
                                        JOIN SpectrumSource source ON s.Source = source.Id
                                        JOIN UnfilteredPeptideInstance pi ON psm.Peptide = pi.Peptide
                                        JOIN UnfilteredProtein pro ON pi.Protein = pro.Id
                                        LEFT JOIN ProteinData pd ON pi.Protein=pd.Id
                                        LEFT JOIN PeptideModification pm ON psm.Id = pm.PeptideSpectrumMatch
                                        LEFT JOIN Modification mod ON pm.Modification = mod.Id
                                        LEFT JOIN PeptideSpectrumMatchScore psmScore ON psm.Id = psmScore.PsmId
                                        LEFT JOIN PeptideSpectrumMatchScoreName scoreName ON psmScore.ScoreNameId=scoreName.Id
                                        WHERE scoreName.Name in ( " + "'" + searchScore1Name + "','" + searchScore2Name + "','" + searchScore3Name + "')" +
                                        " GROUP BY psm.Id";

            lock (session)
                allClusterQueryRows = session.CreateSQLQuery(queryCmd).List<object[]>();
            var allClusterSpectrumRows = allClusterQueryRows.Select(o => new ClusterSpectrumRow(o)).ToList();

            session.CreateSQLQuery(@"DROP TABLE IF EXISTS TempPsmIds").ExecuteUpdate();
            reportStatus(reportSecondsElapsed((DateTime.Now - startTime).TotalSeconds));
            reportStatus(string.Format("Number of PSMs retrieved: {0} \r\n", allClusterSpectrumRows.Count));

            reportStatus("Rescuing PSMs... ");
            if (writeLog)
            {
                string logHeader = string.Join("\t", new string[] { "SourceName", "NativeID", "Charge", "RescuedSequence", "Protein", "ScoreName", "SearchScore", "BAScore", "QValue", "Rank", "Rank1Sequence", "Rank1Protein", "Rank1SearchScore", "Rank1BAScore", "Rank1Qvalue", "\r\n" });
                File.WriteAllText(logFile, logHeader);
            }

            Dictionary<long, UpdateValues> updateDict = new Dictionary<long, UpdateValues>();  ////key: Id in unfiltered psm table, value: reassigned Qvalue and reassinged Rank
            Set<long> rescuedDistinctSpectraIds = new Set<long>();

            //// SQL query to retrieve anlaysis Id and search score order in QonvertSettings table
            IList<object[]> qonvertSettingsQueryRows;
            lock (session)
                qonvertSettingsQueryRows = session.CreateSQLQuery("SELECT Id, ScoreInfoByName FROM QonverterSettings").List<object[]>();
            var qonvertSettingRows = qonvertSettingsQueryRows.Select(o => new qonvertSettingRows(o)).ToList();
            Dictionary<long, string> analysisScoreOrder = new Dictionary<long, string>();
            Dictionary<long, string> analysisScoreName = new Dictionary<long, string>();
            foreach (var qonvertSettingRow in qonvertSettingRows)
            {
                analysisScoreOrder.Add(qonvertSettingRow.Id, qonvertSettingRow.ScoreOrder);
                analysisScoreName.Add(qonvertSettingRow.Id, qonvertSettingRow.ScoreName);
            }

            ////walk through each cluster to rescue PSMs
            for (int i = 0; i < clusterSetListCount; ++i)
            {
                var clusterSet = clusterSetList.ElementAt(i);

                if (_bgWorkerClustering.CancellationPending)
                {
                    _bgWorkerCancelled = true;
                    return;
                }

                //reportStatus("Clustering set: " + String.Join(",",clusterSet.Select(j => j.ToString()).ToArray()) + "\r\n");
                reportProgress((int)(((double)(i + 1) / (double)clusterSetListCount) * 100), "Rescuing PSMs");
                var clusterSpectrumRows = (from o in allClusterSpectrumRows where clusterSet.Contains(o.SpectrumId) select o).ToList();
                //Map<long, Set<long>> peptideIdDict = new Map<long, Set<long>>(); //key: peptide id, value: psm ids
                //Set<long> unprocessedPSMIds = new Set<long>();
                Set<string> unprocessedSpecChargeAnalysisSet = new Set<string>();  //spectrumId.charge.analysis

                var pepSeqDict = new PepDictionary();  //key: modified peptide sequence, value: spectrumId.charge.analysis, score
                //var peptideIdDict = new PepDictionary(); //key: peptide ID, value: PSM Ids and scores

                foreach (var row in clusterSpectrumRows)
                {
                    //peptideIdDict.Add(row.PeptideId,row.PSMId, row.SearchScore);
                    //peptideIdDict[row.PeptideId].Add(row.PSMId);
                    pepSeqDict.Add(row.ModifiedSequence, row.SpectrumId, row.Charge, row.Analysis, row.SearchScore, row.PSMId);
                    //unprocessedPSMIds.Add(row.PSMId);
                    //unprocessedSpectrumCharge.Add(row.SpectrumId.ToString() + "." + row.Charge.ToString());
                    unprocessedSpecChargeAnalysisSet.Add(row.SpectrumId.ToString() + "." + row.Charge.ToString() + "." + row.Analysis.ToString());
                }


                pepSeqDict.ComputeBayesianAverage(analysisScoreOrder); //replace score from sum of search scores to Bayesian Average

                var sortedPepSeqDictKeys = from k in pepSeqDict.Keys orderby pepSeqDict[k].FinalScore descending, pepSeqDict[k].PsmIdSpecDict.Count() descending select k; // sort by score, if tied, second sort by # of linked psms

                foreach (var pepSeq in sortedPepSeqDictKeys)
                {
                    if (unprocessedSpecChargeAnalysisSet.Count == 0)
                        break;

                    if (pepSeqDict[pepSeq].PsmIdSpecDict.Keys.Any(pId => identPSMIdSet.Contains(pId))) ////at least one psm identified as this peptide in this cluster
                    {
                        foreach (var psmId in pepSeqDict[pepSeq].PsmIdSpecDict.Keys)
                        {
                            var row = (from o in clusterSpectrumRows where o.PSMId == psmId select o).First();
                            string spec = row.SpectrumId.ToString() + "." + row.Charge.ToString() + "." + row.Analysis.ToString();
                            if (unprocessedSpecChargeAnalysisSet.Contains(spec))
                            {
                                if (identPSMIdSet.Contains(psmId) || foundSpectra.Contains(row.SpectrumId))
                                {
                                    //// not process ident PSMs
                                    unprocessedSpecChargeAnalysisSet.Remove(spec);
                                }
                                else
                                {
                                    updateDict.Add(psmId, new UpdateValues(-1, 1)); //// update Qvalue = -1, Rank =1
                                    ++rescuedPSMsCount;
                                    rescuedDistinctSpectraIds.Add(row.SpectrumId);
                                    unprocessedSpecChargeAnalysisSet.Remove(spec);

                                    if (writeLog)
                                    {
                                        string originalRank1Seq = "";
                                        string originalRank1Protein = "";
                                        string originalRank1Score = "";
                                        string originalRank1BAScore = "";
                                        string originalRank1Qvalue = "";

                                        if (row.Rank != 1)
                                        {
                                            var originalRank1Rows = (from o in clusterSpectrumRows where o.SpectrumId == row.SpectrumId && o.Rank == 1 && o.Charge == row.Charge && o.Analysis == row.Analysis select new { o.ModifiedSequence, o.Protein, o.SearchScore, o.QValue }).ToList(); ////may exist more than one rank1 hits
                                            foreach (var originalRank1Row in originalRank1Rows)
                                            {
                                                originalRank1Seq += originalRank1Row.ModifiedSequence + ";";
                                                originalRank1Protein += originalRank1Row.Protein + ";";
                                                originalRank1Score += originalRank1Row.SearchScore.ToString("0.0000") + ";";
                                                originalRank1BAScore += pepSeqDict.ContainsKey(originalRank1Row.ModifiedSequence) ? pepSeqDict[originalRank1Row.ModifiedSequence].FinalScore.ToString("0.0000") + ";" : "";
                                                originalRank1Qvalue += originalRank1Row.QValue.ToString("0.0000") + ";";
                                            }
                                        }
                                        string logLine = string.Join("\t", new string[] { row.SourceName, row.SpectrumNativeID, row.Charge.ToString(), row.ModifiedSequence, row.Protein, analysisScoreName[row.Analysis], row.SearchScore.ToString("0.0000"), pepSeqDict[pepSeq].FinalScore.ToString("0.0000"), row.QValue.ToString("0.0000"), row.Rank.ToString(), originalRank1Seq, originalRank1Protein, originalRank1Score, originalRank1BAScore, originalRank1Qvalue });
                                        using (StreamWriter sw = File.AppendText(logFile))
                                        {
                                            sw.WriteLine(logLine);
                                        }
                                    }
                                }
                            }
                        }
                    }
                } //// end of foreach (var pepSeq in sortedPepSeqDictKeys)

            } //// end of for (int i = 0; i < clusterSetListCount; ++i)
            reportStatus(string.Format("{0} seconds elapsed\r\n", (DateTime.Now - startTime).TotalSeconds));

            /*
             *update unfiltered psm table in idpDB
            */
            if (rescuedPSMsCount == 0)
                return;

            reportStatus("Updating idpDB... ");

            session.Transaction.Begin();
            //basicDataFilter.DropFilters(session);  // tables were dropped before querying
            var updateCmd = session.Connection.CreateCommand();
            updateCmd.CommandText = "UPDATE UnfilteredPeptideSpectrumMatch SET QValue = ?, Rank = ? WHERE Id = ?";
            var updateParameters = new List<System.Data.IDbDataParameter>();
            for (int i = 0; i < 3; ++i)
            {
                updateParameters.Add(updateCmd.CreateParameter());
                updateCmd.Parameters.Add(updateParameters[i]);
            }
            updateCmd.Prepare();
            int updateCount = 0;
            int allUpdateCount = updateDict.Count;
            foreach (KeyValuePair<long, UpdateValues> pair in updateDict)
            {
                updateParameters[0].Value = pair.Value.ReassignedQvalue;   //// Qvalue
                updateParameters[1].Value = pair.Value.ReassignedRank;   //// Rank
                updateParameters[2].Value = pair.Key;    //// psm id
                updateCmd.ExecuteNonQuery();
                reportProgress((int)(((double)(updateCount + 1) / (double)allUpdateCount) * 100), "Updating idpDB");
                ++updateCount;
            }
            session.Transaction.Commit();

            //basicDataFilter.ApplyBasicFilters(session);
            reportStatus(reportSecondsElapsed((DateTime.Now - startTime).TotalSeconds));
            reportStatus(string.Format("Rescued {0} PSMs for {1} distinct spectra\r\n", rescuedPSMsCount, rescuedDistinctSpectraIds.Count));
            reportProgress(0, "Ready");
            /*
             * not recompute q values, reload idpDB, implemented in _bgWorkerClustering_RunWorkerCompleted
            */

        } //// end of RescuePSMsByClustering
        public static List <ILink> CalculatePath(Node from, Node to, LinkMap linkMap)
        {
            // The set of nodes already evaluated.
            var closedSet = new List <Node>();
            // The set of currently discovered nodes that are not evaluated yet.
            // Initially, only the start node is known.
            var openSet = new List <Node> {
                from
            };
            // For each node, which node it can most efficiently be reached from.
            // If a node can be reached from many nodes, cameFrom will eventually contain the
            // most efficient previous step.
            var cameFrom = new Dictionary <Node, ILink>();
            var gScore   = new Dictionary <Node, float>();

            gScore[from] = 0.0f;
            // For each node, the total cost of getting from the start node to the goal
            // by passing by that node. That value is partly known, partly heuristic.
            var fScore = new Dictionary <Node, float>();

            fScore[from] = from.Position.Distance(to.Position);
            var current = from;

            while (!openSet.IsEmpty())
            {
                // the node in openSet having the lowest fScore[] value
                current = openSet.MinBy(node => fScore.GetOrPut(node, float.PositiveInfinity));
                if (current == to)
                {
                    return(Reconstruct(cameFrom, current));
                }
                openSet.Remove(current);
                closedSet.Add(current);
                var linkPoint = linkMap.GetLinkPoint(current);
                foreach (var link in linkPoint.Links)
                {
                    var neighbor = link.To;
                    if (closedSet.Contains(neighbor))
                    {
                        // Ignore the neighbor which is already evaluated.
                        continue;
                    }

                    var currentGScore   = gScore.GetOrPut(current, float.PositiveInfinity);
                    var neightborGScore = gScore.GetOrPut(neighbor, float.PositiveInfinity);
                    var linkDistance    = current.Distance(neighbor);
                    // The distance from start to a neighbor
                    var tentativeGScore = currentGScore + linkDistance;
                    if (!openSet.Contains(neighbor))
                    {
                        openSet.Add(neighbor);
                    }
                    else if (tentativeGScore >= neightborGScore)
                    {
                        // This is not a better path.
                        continue;
                    }
                    // This path is the best until now. Record it!
                    cameFrom[neighbor] = link;
                    gScore[neighbor]   = tentativeGScore;
                    fScore[neighbor]   = gScore[neighbor] + neighbor.Distance(to);
                }
            }
            //Failure
            Debug.LogWarning("Couldn't find a path from " + from + " to " + to);
            return(Reconstruct(cameFrom, current));
        }
Exemplo n.º 28
0
 public QueryCriteria()
 {
     _projections = new LinkMap <string, IEvaluable>();
     _limit       = long.MinValue;
 }
Exemplo n.º 29
0
 public void Clear()
 {
     LinkMap.Clear();
 }
Exemplo n.º 30
0
        public void testParse()
        {
            LinkMap.SetLinkToLow(false);

            Route x = LinkMap.Parse("/post8/Index");

            Assert.AreEqual("TestPost", x.controller);
            Assert.AreEqual("Index", x.action);
            Assert.AreEqual(8, x.appId);

            x = LinkMap.Parse("/category8");
            Assert.AreEqual("TestPost", x.controller);
            Assert.AreEqual("List", x.action);
            Assert.AreEqual(8, x.appId);

            x = LinkMap.Parse("/post8/Show/88");
            Assert.AreEqual("TestPost", x.controller);
            Assert.AreEqual("Show", x.action);
            Assert.AreEqual(88, x.id);
            Assert.AreEqual(8, x.appId);

            x = LinkMap.Parse("/product8/99");
            Assert.AreEqual("TestPost", x.controller);
            Assert.AreEqual("Product", x.action);
            Assert.AreEqual(99, x.id);
            Assert.AreEqual(8, x.appId);

            // 未映射
            x = LinkMap.Parse("/computer8/99");
            Assert.IsNull(x);

            x = LinkMap.Parse("/categoryList");
            Assert.IsNull(x);

            x = LinkMap.Parse("/cat");
            Assert.IsNull(x);


            LinkMap.SetLinkToLow(true);

            x = LinkMap.Parse("/post/index");
            Assert.AreEqual("TestPost", x.controller);
            Assert.AreEqual("index", x.action);   // action由path指定
            x = LinkMap.Parse("/post/Index");
            Assert.AreEqual("TestPost", x.controller);
            Assert.AreEqual("Index", x.action);   // action由path指定

            x = LinkMap.Parse("/category");
            Assert.AreEqual("TestPost", x.controller);
            Assert.AreEqual("List", x.action);
            x = LinkMap.Parse("/Category");
            Assert.AreEqual("TestPost", x.controller);
            Assert.AreEqual("List", x.action);

            x = LinkMap.Parse("/post/show/88");
            Assert.AreEqual("TestPost", x.controller);
            Assert.AreEqual("show", x.action);   // action由path指定
            Assert.AreEqual(88, x.id);
            x = LinkMap.Parse("/post/Show/88");
            Assert.AreEqual("TestPost", x.controller);
            Assert.AreEqual("Show", x.action);   // action由path指定
            Assert.AreEqual(88, x.id);

            x = LinkMap.Parse("/product/99");
            Assert.AreEqual("TestPost", x.controller);
            Assert.AreEqual("Product", x.action);
            Assert.AreEqual(99, x.id);
            x = LinkMap.Parse("/Product/99");
            Assert.AreEqual("TestPost", x.controller);
            Assert.AreEqual("Product", x.action);
            Assert.AreEqual(99, x.id);
        }