Ejemplo n.º 1
0
        public Digestion GetDigestion(String name)
        {
            DbDigestion digestion = GetDbDigestion(name);

            if (digestion == null)
            {
                return(null);
            }
            return(new Digestion(this, digestion));
        }
Ejemplo n.º 2
0
        public Digestion Digest(IProtease protease, String name, String description, ProgressMonitor progressMonitor)
        {
            DbOrganism       organism;
            DbDigestion      digestion;
            List <DbProtein> proteins;

            using (ISession session = ProteomeDb.OpenWriteSession())
            {
                organism = GetEntity(session);
                session.BeginTransaction();
                digestion = new DbDigestion
                {
                    Name               = name,
                    Description        = description,
                    Organism           = organism,
                    MaxMissedCleavages = protease.MaxMissedCleavages
                };
                session.Save(digestion);
                if (!progressMonitor.Invoke("Listing proteins", 0))
                {
                    return(null);
                }
                proteins = new List <DbProtein>(organism.Proteins);
                Dictionary <String, long> digestedPeptideIds
                    = new Dictionary <string, long>();
                const String sqlPeptide =
                    "INSERT INTO ProteomeDbDigestedPeptide (Digestion, MissedCleavages, Sequence, Version) VALUES(@Digestion,@MissedCleavages,@Sequence,1);select last_insert_rowid();";
                var commandPeptide = session.Connection.CreateCommand();
                commandPeptide.CommandText = sqlPeptide;
                commandPeptide.Parameters.Add(new SQLiteParameter("@Digestion"));
                commandPeptide.Parameters.Add(new SQLiteParameter("@MissedCleavages"));
                commandPeptide.Parameters.Add(new SQLiteParameter("@Sequence"));
                const String sqlPeptideProtein =
                    "INSERT INTO ProteomeDbDigestedPeptideProtein (StartIndex, Peptide, Protein, Version) VALUES(?,?,?,1);";
                var commandProtein = session.Connection.CreateCommand();
                commandProtein.CommandText = sqlPeptideProtein;
                commandProtein.Parameters.Add(new SQLiteParameter("@StartIndex"));
                commandProtein.Parameters.Add(new SQLiteParameter("@Peptide"));
                commandProtein.Parameters.Add(new SQLiteParameter("@Protein"));
                for (int i = 0; i < proteins.Count; i++)
                {
                    if (!progressMonitor.Invoke("Digesting " + proteins.Count
                                                + " proteins", 100 * i / proteins.Count))
                    {
                        return(null);
                    }
                    Protein protein = new Protein(this, proteins[i]);
                    foreach (DigestedPeptide digestedPeptide in protease.Digest(protein))
                    {
                        if (digestedPeptide.Sequence.Length > MAX_PEPTIDE_LENGTH)
                        {
                            continue;
                        }
                        long digestedPeptideId;
                        if (!digestedPeptideIds.TryGetValue(digestedPeptide.Sequence, out digestedPeptideId))
                        {
                            ((SQLiteParameter)commandPeptide.Parameters[0]).Value = digestion.Id;
                            ((SQLiteParameter)commandPeptide.Parameters[1]).Value = digestedPeptide.MissedCleavages;
                            ((SQLiteParameter)commandPeptide.Parameters[2]).Value = digestedPeptide.Sequence;
                            digestedPeptideId = Convert.ToInt64(commandPeptide.ExecuteScalar());
                            digestedPeptideIds.Add(digestedPeptide.Sequence, digestedPeptideId);
                        }
                        ((SQLiteParameter)commandProtein.Parameters[0]).Value = digestedPeptide.Index;
                        ((SQLiteParameter)commandProtein.Parameters[1]).Value = digestedPeptideId;
                        ((SQLiteParameter)commandProtein.Parameters[2]).Value = proteins[i].Id;
                        commandProtein.ExecuteNonQuery();
                    }
                }
                if (!progressMonitor.Invoke("Committing transaction", 99))
                {
                    return(null);
                }
                session.Transaction.Commit();
                progressMonitor.Invoke(
                    "Digested " + proteins.Count + " proteins into " + digestedPeptideIds.Count + " unique peptides",
                    100);
                return(new Digestion(this, digestion));
            }
        }
Ejemplo n.º 3
0
        public Digestion Digest(IProtease protease, ProgressMonitor progressMonitor)
        {
            using (ISession session = OpenWriteSession())
            {
                DbDigestion      dbDigestion       = GetDbDigestion(protease.Name);
                HashSet <string> existingSequences = new HashSet <string>();
                using (var transaction = session.BeginTransaction())
                {
                    if (dbDigestion != null)
                    {
                        if (dbDigestion.MaxSequenceLength >= MAX_SEQUENCE_LENGTH)
                        {
                            return(new Digestion(this, dbDigestion));
                        }
                        if (!progressMonitor.Invoke(Resources.ProteomeDb_Digest_Listing_existing_peptides, 0))
                        {
                            return(null);
                        }
                        IQuery query = session.CreateQuery("SELECT P.Sequence FROM "                                          // Not L10N
                                                           + typeof(DbDigestedPeptide) + " P WHERE P.Digestion = :Digestion") // Not L10N
                                       .SetParameter("Digestion", dbDigestion);                                               // Not L10N
                        List <String> listSequences = new List <string>();
                        query.List(listSequences);
                        existingSequences.UnionWith(listSequences);
                        dbDigestion.MaxSequenceLength = MAX_SEQUENCE_LENGTH;
                        session.Update(dbDigestion);
                    }
                    else
                    {
                        dbDigestion = new DbDigestion
                        {
                            Name = protease.Name,
                            MinSequenceLength = MIN_SEQUENCE_LENGTH,
                            MaxSequenceLength = MAX_SEQUENCE_LENGTH,
                        };
                        session.Save(dbDigestion);
                    }
                    if (!progressMonitor.Invoke(Resources.ProteomeDb_Digest_Listing_proteins, 0))
                    {
                        return(null);
                    }
                    List <DbProtein> proteins = new List <DbProtein>();
                    session.CreateCriteria(typeof(DbProtein)).List(proteins);
                    Dictionary <String, long> digestedPeptideIds
                        = new Dictionary <string, long>();
                    const String sqlPeptide =
                        "INSERT INTO ProteomeDbDigestedPeptide (Digestion, Sequence) VALUES(?,?);select last_insert_rowid();";     // Not L10N
                    using (var commandPeptide = session.Connection.CreateCommand())
                        using (var commandProtein = session.Connection.CreateCommand())
                        {
                            commandPeptide.CommandText = sqlPeptide;
                            commandPeptide.Parameters.Add(new SQLiteParameter());
                            commandPeptide.Parameters.Add(new SQLiteParameter());
                            const String sqlPeptideProtein =
                                "INSERT INTO ProteomeDbDigestedPeptideProtein (Peptide, Protein) VALUES(?,?);"; // Not L10N
                            commandProtein.CommandText = sqlPeptideProtein;
                            commandProtein.Parameters.Add(new SQLiteParameter());
                            commandProtein.Parameters.Add(new SQLiteParameter());
                            commandProtein.Parameters.Add(new SQLiteParameter());
                            for (int i = 0; i < proteins.Count; i++)
                            {
                                var proteinSequences = new HashSet <string>();
                                if (!progressMonitor.Invoke(string.Format(Resources.ProteomeDb_Digest_Digesting__0__proteins, proteins.Count), 100 * i / proteins.Count))
                                {
                                    return(null);
                                }
                                Protein protein = new Protein(ProteomeDbPath, proteins[i]);

                                foreach (DigestedPeptide digestedPeptide in protease.Digest(protein))
                                {
                                    if (digestedPeptide.Sequence.Length < dbDigestion.MinSequenceLength)
                                    {
                                        continue;
                                    }
                                    String truncatedSequence = digestedPeptide.Sequence.Substring(
                                        0, Math.Min(digestedPeptide.Sequence.Length, dbDigestion.MaxSequenceLength));
                                    if (existingSequences.Contains(truncatedSequence))
                                    {
                                        continue;
                                    }
                                    if (proteinSequences.Contains(truncatedSequence))
                                    {
                                        continue;
                                    }
                                    proteinSequences.Add(truncatedSequence);
                                    long digestedPeptideId;
                                    if (!digestedPeptideIds.TryGetValue(truncatedSequence, out digestedPeptideId))
                                    {
                                        ((SQLiteParameter)commandPeptide.Parameters[0]).Value = dbDigestion.Id;
                                        ((SQLiteParameter)commandPeptide.Parameters[1]).Value = truncatedSequence;
                                        digestedPeptideId = Convert.ToInt64(commandPeptide.ExecuteScalar());
                                        digestedPeptideIds.Add(truncatedSequence, digestedPeptideId);
                                    }
                                    ((SQLiteParameter)commandProtein.Parameters[0]).Value = digestedPeptideId;
                                    ((SQLiteParameter)commandProtein.Parameters[1]).Value = protein.Id;
                                    commandProtein.ExecuteNonQuery();
                                }
                            }
                        }
                    if (!progressMonitor.Invoke(Resources.ProteomeDb_AddFastaFile_Saving_changes, 99))
                    {
                        return(null);
                    }
                    transaction.Commit();

                    AnalyzeDb(session);
                    progressMonitor.Invoke(
                        string.Format(Resources.ProteomeDb_Digest_Digested__0__proteins_into__1__unique_peptides,
                                      proteins.Count, digestedPeptideIds.Count),
                        100);
                }
                return(new Digestion(this, dbDigestion));
            }
        }
Ejemplo n.º 4
0
        public Digestion Digest(IProtease protease, ProgressMonitor progressMonitor)
        {
            using (ISession session = OpenWriteSession())
            {
                DbDigestion dbDigestion = GetDbDigestion(protease.Name);
                HashSet<string> existingSequences = new HashSet<string>();
                using (var transaction = session.BeginTransaction())
                {
                    if (dbDigestion != null)
                    {
                        if (dbDigestion.MaxSequenceLength >= MAX_SEQUENCE_LENGTH)
                        {
                            return new Digestion(this, dbDigestion);
                        }
                        if (!progressMonitor.Invoke(Resources.ProteomeDb_Digest_Listing_existing_peptides, 0))
                        {
                            return null;
                        }
                        IQuery query = session.CreateQuery("SELECT P.Sequence FROM " // Not L10N
                                                           + typeof(DbDigestedPeptide) + " P WHERE P.Digestion = :Digestion") // Not L10N
                            .SetParameter("Digestion", dbDigestion); // Not L10N
                        List<String> listSequences = new List<string>();
                        query.List(listSequences);
                        existingSequences.UnionWith(listSequences);
                        dbDigestion.MaxSequenceLength = MAX_SEQUENCE_LENGTH;
                        session.Update(dbDigestion);
                    }
                    else
                    {
                        dbDigestion = new DbDigestion
                        {
                            Name = protease.Name,
                            MinSequenceLength = MIN_SEQUENCE_LENGTH,
                            MaxSequenceLength = MAX_SEQUENCE_LENGTH,
                        };
                        session.Save(dbDigestion);
                    }
                    if (!progressMonitor.Invoke(Resources.ProteomeDb_Digest_Listing_proteins, 0)) 
                    {
                        return null;
                    }
                    List<DbProtein> proteins = new List<DbProtein>();
                    session.CreateCriteria(typeof(DbProtein)).List(proteins);
                    Dictionary<String, long> digestedPeptideIds
                        = new Dictionary<string, long>();
                    const String sqlPeptide =
                            "INSERT INTO ProteomeDbDigestedPeptide (Digestion, Sequence) VALUES(?,?);select last_insert_rowid();"; // Not L10N
                    using (var commandPeptide = session.Connection.CreateCommand())
                    using (var commandProtein = session.Connection.CreateCommand())
                    {
                        commandPeptide.CommandText = sqlPeptide;
                        commandPeptide.Parameters.Add(new SQLiteParameter());
                        commandPeptide.Parameters.Add(new SQLiteParameter());
                        const String sqlPeptideProtein =
                            "INSERT INTO ProteomeDbDigestedPeptideProtein (Peptide, Protein) VALUES(?,?);"; // Not L10N
                        commandProtein.CommandText = sqlPeptideProtein;
                        commandProtein.Parameters.Add(new SQLiteParameter());
                        commandProtein.Parameters.Add(new SQLiteParameter());
                        commandProtein.Parameters.Add(new SQLiteParameter());
                        for (int i = 0; i < proteins.Count; i++)
                        {
                            var proteinSequences = new HashSet<string>();
                            if (!progressMonitor.Invoke(string.Format(Resources.ProteomeDb_Digest_Digesting__0__proteins,proteins.Count), 100 * i / proteins.Count))
                            {
                                return null;
                            }
                            Protein protein = new Protein(ProteomeDbPath, proteins[i]);

                            foreach (DigestedPeptide digestedPeptide in protease.Digest(protein))
                            {
                                if (digestedPeptide.Sequence.Length < dbDigestion.MinSequenceLength)
                                {
                                    continue;
                                }
                                String truncatedSequence = digestedPeptide.Sequence.Substring(
                                    0, Math.Min(digestedPeptide.Sequence.Length, dbDigestion.MaxSequenceLength));
                                if (existingSequences.Contains(truncatedSequence))
                                {
                                    continue;
                                }
                                if (proteinSequences.Contains(truncatedSequence))
                                {
                                    continue;
                                }
                                proteinSequences.Add(truncatedSequence);
                                long digestedPeptideId;
                                if (!digestedPeptideIds.TryGetValue(truncatedSequence, out digestedPeptideId))
                                {
                                    ((SQLiteParameter)commandPeptide.Parameters[0]).Value = dbDigestion.Id;
                                    ((SQLiteParameter)commandPeptide.Parameters[1]).Value = truncatedSequence;
                                    digestedPeptideId = Convert.ToInt64(commandPeptide.ExecuteScalar());
                                    digestedPeptideIds.Add(truncatedSequence, digestedPeptideId);
                                }
                                ((SQLiteParameter)commandProtein.Parameters[0]).Value = digestedPeptideId;
                                ((SQLiteParameter)commandProtein.Parameters[1]).Value = protein.Id;
                                commandProtein.ExecuteNonQuery();
                            }
                        }
                    }
                    if (!progressMonitor.Invoke(Resources.ProteomeDb_AddFastaFile_Saving_changes, 99))
                    {
                        return null;
                    }
                    transaction.Commit();

                    AnalyzeDb(session);
                    progressMonitor.Invoke(
                        string.Format(Resources.ProteomeDb_Digest_Digested__0__proteins_into__1__unique_peptides,
                                      proteins.Count, digestedPeptideIds.Count),
                        100);
                }
                return new Digestion(this, dbDigestion);
            }
        }
Ejemplo n.º 5
0
        public Digestion Digest(IProtease protease, int maxMissedCleavages, IProgressMonitor progressMonitor, ref IProgressStatus status, bool delayDbIndexing = false)
        {
            using (ISession session = OpenWriteSession())
            {
                DbDigestion      dbDigestion = GetDbDigestion(protease.Name, session);
                HashSet <string> existingSequences;  // TODO(bspratt) - the logic around this seems fishy, investigate.  Probably never actually been used.  Part of fix for issue #304, probably
                if (dbDigestion != null)
                {
                    if (dbDigestion.MaxSequenceLength >= MAX_SEQUENCE_LENGTH)
                    {
                        return(new Digestion(this, dbDigestion));
                    }
                    if (!UpdateProgressAndCheckForCancellation(progressMonitor, ref status, Resources.ProteomeDb_Digest_Listing_existing_peptides, 0))
                    {
                        return(null);
                    }
                    IQuery query = session.CreateQuery("SELECT P.Sequence FROM "                                          // Not L10N
                                                       + typeof(DbDigestedPeptide) + " P WHERE P.Digestion = :Digestion") // Not L10N
                                   .SetParameter("Digestion", dbDigestion);                                               // Not L10N
                    List <String> listSequences = new List <string>();
                    query.List(listSequences);
                    existingSequences             = new HashSet <string>(listSequences);
                    dbDigestion.MaxSequenceLength = MAX_SEQUENCE_LENGTH;
                }
                else
                {
                    dbDigestion = new DbDigestion
                    {
                        Name = protease.Name,
                        MinSequenceLength = MIN_SEQUENCE_LENGTH,
                        MaxSequenceLength = MAX_SEQUENCE_LENGTH,
                    };
                    existingSequences = new HashSet <string>();
                }
                if (!UpdateProgressAndCheckForCancellation(progressMonitor, ref status, Resources.ProteomeDb_Digest_Listing_proteins, 0))
                {
                    return(null);
                }
                var dbProteins = new List <DbProtein>();
                session.CreateCriteria(typeof(DbProtein)).List(dbProteins);

                // Digest the proteins
                var proteinCount = dbProteins.Count;
                if (proteinCount == 0)
                {
                    return(null);
                }

                var       proteinsList       = new Protein[proteinCount];
                var       truncatedSequences = new HashSet <string> [proteinCount]; // One hashset of sequences for each protein of interest
                const int N_DIGEST_THREADS   = 16;                                  // Arbitrary value - do a progress/canel check every nth protein
                string    message            = string.Format(Resources.ProteomeDb_Digest_Digesting__0__proteins, proteinCount);
                for (var i = 0; i < proteinCount; i += N_DIGEST_THREADS)
                {
                    var endRange = Math.Min(proteinCount, i + N_DIGEST_THREADS);
                    if (!UpdateProgressAndCheckForCancellation(progressMonitor, ref status, message, 50 * endRange / proteinCount))
                    {
                        return(null);
                    }
                    for (int ii = i; ii < endRange; ii++)
                    {
                        var protein = new Protein(ProteomeDbPath, dbProteins[ii]);
                        proteinsList[ii] = protein;
                    }
                    Parallel.For(i, endRange, ii =>
                    {
                        var proteinSequences   = new HashSet <string>(); // We only save the first dbDigestion.MaxSequenceLength characters of each peptide so collisions are likely
                        truncatedSequences[ii] = proteinSequences;       // One hashset of sequences for each protein of interest

                        foreach (var digestedPeptide in protease.DigestSequence(proteinsList[ii].Sequence, maxMissedCleavages, null))
                        {
                            if (digestedPeptide.Sequence.Length < dbDigestion.MinSequenceLength)
                            {
                                continue;
                            }
                            var truncatedSequence = digestedPeptide.Sequence.Substring(
                                0, Math.Min(digestedPeptide.Sequence.Length, dbDigestion.MaxSequenceLength));
                            if (!existingSequences.Contains(truncatedSequence))
                            {
                                proteinSequences.Add(truncatedSequence);
                            }
                        }
                    });
                }

                // Now write to db
                if (!UpdateProgressAndCheckForCancellation(progressMonitor, ref status, Resources.ProteomeDb_AddFastaFile_Saving_changes, 50))
                {
                    return(null);
                }
                bool committed = true;
                int  digestedPeptideIdsCount;
                try
                {
                    using (var transaction = session.BeginTransaction())
                    {
                        session.SaveOrUpdate(dbDigestion);

                        Dictionary <String, long> digestedPeptideIds
                            = new Dictionary <string, long>();
                        const String sqlPeptide =
                            "INSERT INTO ProteomeDbDigestedPeptide (Digestion, Sequence) VALUES(?,?);select last_insert_rowid();";     // Not L10N
                        using (var commandPeptide = session.Connection.CreateCommand())
                            using (var commandProtein = session.Connection.CreateCommand())
                            {
                                commandPeptide.CommandText = sqlPeptide;
                                commandPeptide.Parameters.Add(new SQLiteParameter());
                                commandPeptide.Parameters.Add(new SQLiteParameter());
                                const String sqlPeptideProtein =
                                    "INSERT INTO ProteomeDbDigestedPeptideProtein (Peptide, Protein) VALUES(?,?);"; // Not L10N
                                commandProtein.CommandText = sqlPeptideProtein;
                                commandProtein.Parameters.Add(new SQLiteParameter());
                                commandProtein.Parameters.Add(new SQLiteParameter());
                                commandProtein.Parameters.Add(new SQLiteParameter());
                                for (int i = 0; i < proteinCount; i++)
                                {
                                    var protein = proteinsList[i];
                                    if (!UpdateProgressAndCheckForCancellation(progressMonitor, ref status, message, 50 * (proteinCount + i) / proteinCount))
                                    {
                                        return(null);
                                    }
                                    foreach (var truncatedSequence in truncatedSequences[i])
                                    {
                                        long digestedPeptideId;
                                        if (!digestedPeptideIds.TryGetValue(truncatedSequence, out digestedPeptideId))
                                        {
                                            ((SQLiteParameter)commandPeptide.Parameters[0]).Value = dbDigestion.Id;
                                            ((SQLiteParameter)commandPeptide.Parameters[1]).Value = truncatedSequence;
                                            digestedPeptideId = Convert.ToInt64(commandPeptide.ExecuteScalar());
                                            digestedPeptideIds.Add(truncatedSequence, digestedPeptideId);
                                        }
                                        ((SQLiteParameter)commandProtein.Parameters[0]).Value = digestedPeptideId;
                                        ((SQLiteParameter)commandProtein.Parameters[1]).Value = protein.Id;
                                        commandProtein.ExecuteNonQuery();
                                    }
                                }
                            }
                        try
                        {
                            transaction.Commit();
                        }
                        catch (Exception)
                        {
                            committed = false;
                        }
                        digestedPeptideIdsCount = digestedPeptideIds.Count;
                    }
                }
                catch (Exception)
                {
                    if (!committed)
                    {
                        return(null); // Interrupted
                    }
                    else
                    {
                        throw;
                    }
                }
                if (committed && !delayDbIndexing)
                {
                    AnalyzeDb(session); // This runs asynchronously, and interferes with writes
                }
                if (committed)
                {
                    progressMonitor.UpdateProgress(new ProgressStatus(string.Format(Resources.ProteomeDb_Digest_Digested__0__proteins_into__1__unique_peptides, proteinCount, digestedPeptideIdsCount)).ChangePercentComplete(100));
                }
                return(committed ? new Digestion(this, dbDigestion) : null);
            }
        }