public static IChain ChainFromFileOrCode(string file, PdbLoadOptions loadOptions = PdbLoadOptions.Default, char?chainId = null) { IEnumerable <AtomRecord> records = GetPdbRecords(file).Select(record => record as AtomRecord).Where(record => record as HetatmRecord == null); IChain chain = ChainFromRecords(records, loadOptions, chainId); return(chain); }
public static IChain ChainFromText(string text, PdbLoadOptions loadOptions = PdbLoadOptions.Default, char?chainId = null) { using (TextReader stream = new StringReader(text)) { IEnumerable <AtomRecord> records = LoadRecords(stream).Where(record => record is AtomRecord).Cast <AtomRecord>(); IChain chain = ChainFromRecords(records, loadOptions, chainId); return(chain); } }
public static Structure AssemblyFromFileOrCode(string file, PdbLoadOptions loadOptions = PdbLoadOptions.Default) { Structure protein = new Structure(); IEnumerable <AtomRecord> records = GetPdbRecords(file, false).Select(r => r as AtomRecord).Where(r => r != null).Where(r => r as HetatmRecord == null); List <char> letters = records.Select(record => record.ChainId).Distinct().ToList(); foreach (char chainId in records.Select(record => record.ChainId).Distinct()) { IEnumerable <AtomRecord> chainRecords = records.Where(record => record.ChainId == chainId); IChain chain = ChainFromRecords(chainRecords); protein.Add(chain); } return(protein); }
public static Structure AssemblyFromText(string text, PdbLoadOptions loadOptions = PdbLoadOptions.Default) { using (TextReader stream = new StringReader(text)) { IEnumerable <AtomRecord> records = LoadRecords(stream).Where(record => record is AtomRecord).Cast <AtomRecord>(); List <char> letters = records.Select(record => record.ChainId).Distinct().ToList(); Structure protein = new Structure(); foreach (char chainId in letters) { IEnumerable <AtomRecord> chainRecords = records.Where(record => record.ChainId == chainId); IChain chain = ChainFromRecords(chainRecords); protein.Add(chain); } if (protein.Count == 0) { return(null); } return(protein); } }
public static IChain ChainFromRecords(IEnumerable <AtomRecord> records, PdbLoadOptions loadOptions = PdbLoadOptions.Default, char?chainId = null) { bool wellFormatted = (loadOptions) == PdbLoadOptions.WellFormatted; bool requireNCAC = (loadOptions & PdbLoadOptions.RequireNCAC) != 0; bool requireNonNCAC = (loadOptions & PdbLoadOptions.RequireNonNCAC) != 0; bool missingAtomsNaN = (loadOptions & PdbLoadOptions.MissingAtomsNaN) != 0; IEnumerable <AtomSource> allAtoms = records.Where(record => record != null).Select(record => record.ToAtom()); IEnumerable <IGrouping <char, AtomSource> > chains = allAtoms.GroupBy(atom => atom.ChainIndex); foreach (IGrouping <char, AtomSource> chain in chains) { bool chainFailed = false; char actualChainId = chain.Key; if (chainId != null && chainId != actualChainId) { continue; } Chain peptideQuick = new Chain(); List <IGrouping <int, AtomSource> > pdbResidues = chain.GroupBy(atom => atom.ResidueIndex).OrderBy(group => group.Key).ToList(); foreach (IGrouping <int, AtomSource> pdbResidue in pdbResidues) { bool nTerminus = pdbResidue == pdbResidues.First(); bool cTerminus = pdbResidue == pdbResidues.Last(); AtomSource N = pdbResidue.FirstOrDefault(atom => atom.Name == "N"); AtomSource CA = pdbResidue.FirstOrDefault(atom => atom.Name == "CA"); AtomSource C = pdbResidue.FirstOrDefault(atom => atom.Name == "C"); // Get the name and create the residue string name3 = pdbResidue.First().ResidueName; if (name3 == "HIS") // TODO: Is this the best way of doing this? { if (pdbResidue.FirstOrDefault(atom => atom.Name == "HD1" || atom.Name == "1HD") != null) { name3 = "HID"; } else { name3 = "HIE"; } } // Default to non-disulfide and then replace with disulfides when CYH are nearby with nearby SG atoms if (name3 == "CYS") { name3 = "CYH"; } if (!AaTable.IsResidueNameKnown(name3)) { // TODO: Handle ions in some way if ((new string[] { "ZN", "HOH", "H20", "AU", "CA", "MG", "SO4" }.Contains(name3))) { continue; } Console.WriteLine("Failing on unknown residue " + name3); chainFailed = true; break; } IAa residueQuick = new Aa(name3, nTerminus, cTerminus); // Check for existence of atoms if (N == null || CA == null || C == null) { if (requireNCAC) { // TODO: Tracer output chainFailed = true; break; } } else if (float.IsNaN(N.XYZ.Length()) || float.IsNaN(CA.XYZ.Length()) || float.IsNaN(C.XYZ.Length())) { if (requireNCAC) { // TODO: Tracer output chainFailed = true; break; } } else { residueQuick.AlignToNCAC(N.XYZ, CA.XYZ, C.XYZ); } if (missingAtomsNaN) { // Only explicitly PDB-defined atoms have real coordinates for (int i = 0; i < residueQuick.Count; i++) { residueQuick[i].Xyz = new Vector3(float.NaN, float.NaN, float.NaN); } } // Method 1 //foreach(AtomQuick atomQuick in residueQuick) //{ // AtomSource atomSource = pdbResidue.FirstOrDefault(atomPdb => atomPdb.Name == atomQuick.Name); // // Some PDBs are formatted with hydrogens like 2HD1 instead of HD12 // AtomSource alternateAtomSource = null; // if(atomQuick.Name.StartsWith("H") && Char.IsNumber(atomQuick.Name.Last())) { // string alternateName = atomQuick.Name.Last() + atomQuick.Name.Substring(0, atomQuick.Name.Length - 1); // alternateAtomSource = pdbResidue.FirstOrDefault(atomPdb => atomPdb.Name == alternateName); // } // if(atomSource != null) // { // atomQuick.XYZ = atomSource.XYZ; // } // else if (alternateAtomSource != null) // { // atomQuick.XYZ = alternateAtomSource.XYZ; // } // else if (!allowMissingAtoms) // { // // TODO ... return null? // continue; // } //} // Method 2 - explicit atom name alternatives // First pass: initialize atom locations based on exact name-matched PDB records List <IAtom> initializedAtoms = new List <IAtom>(); List <AtomSource> usedSourceAtoms = new List <AtomSource>(); List <IAtom> uninitializedAtoms = new List <IAtom>(residueQuick); foreach (IAtom atomQuick in residueQuick) { AtomSource atomSource = pdbResidue.FirstOrDefault(atomPdb => atomPdb.Name == atomQuick.Name); if (atomSource != null) { atomQuick.Xyz = atomSource.XYZ; initializedAtoms.Add(atomQuick); uninitializedAtoms.Remove(atomQuick); usedSourceAtoms.Add(atomSource); } } // Second pass: try to initialize any remaining atoms from PDB records corresponding to known alternative atom names. Multiple // passes are made to try to resolve the names, moving from more to less preferred. int alternateAtomNamesMaxCount = alternateAtomNames_.Values.Select(value => value.Count).Max(); for (int nameIndex = 0; nameIndex < alternateAtomNamesMaxCount; nameIndex++) { foreach (Atom atomQuick in residueQuick) { if (initializedAtoms.Contains(atomQuick)) { continue; } List <string> alternativeAtomNames = null; if (!alternateAtomNames_.TryGetValue(atomQuick.Name, out alternativeAtomNames)) { continue; } if (alternativeAtomNames.Count <= nameIndex) { continue; } IEnumerable <AtomSource> atomSources = pdbResidue.Where(atomPdb => alternativeAtomNames[nameIndex] == atomPdb.Name); foreach (AtomSource atomSource in atomSources) { if (usedSourceAtoms.Contains(atomSource)) { continue; } atomQuick.Xyz = atomSource.XYZ; initializedAtoms.Add(atomQuick); uninitializedAtoms.Remove(atomQuick); usedSourceAtoms.Add(atomSource); } } } if (initializedAtoms.Count != residueQuick.Count) { if (!requireNCAC && !requireNonNCAC) { continue; } IEnumerable <IAtom> uninitializedNCAC = uninitializedAtoms.Where(atom => atom.Name == "N" || atom.Name == "CA" || atom.Name == "C").Where(atom => float.IsNaN(atom.Xyz.Length())); IEnumerable <IAtom> uninitializedNonNCAC = uninitializedAtoms.Where(atom => atom.Name != "N" && atom.Name != "CA" && atom.Name != "C").Where(atom => float.IsNaN(atom.Xyz.Length())); if (requireNCAC && uninitializedNCAC.Count() > 0) { chainFailed = true; break; } if (requireNonNCAC && uninitializedNonNCAC.Count() > 0) { chainFailed = true; break; } //// TODO: Create trace output and move this there //Console.WriteLine("Failed to initialize residue " + residueQuick.Name); //foreach (AtomQuick atom in uninitializedAtoms) //{ // Console.WriteLine(atom.Name); //} //chainFailed = true; //break; } peptideQuick.Add(residueQuick); } if (!chainFailed && peptideQuick.Count > 0) { AddDisulfides(peptideQuick); return(peptideQuick); } } return(null); }