首先,大家好。我是C#的初学者,正在尝试做此作业。我的问题是,读取.pdb(蛋白质数据库)文件的特定部分并将该特定行拆分为数组或列表。然后我将其用于Forms App
所以.pdb文件索引看起来像这样;
标头防冻17-SEP-97 7MSI标题III型抗冻蛋白ISOFORM HPLC 12COMPND MOL_ID:1;化合物2分子:III型抗冻蛋白ISOFORM HPLC 12;来源MOL_ID:1;消息来源2 ORGANISM_SCIENTIFIC:美洲巨;;原子1 N MET A 0 18.112 24.345 32.146 1.00 51.10 N原子2 CA MET A 0 18.302 23.436 31.020 1.00 49.06 C原子3 C MET A 0 18.079 24.312 29.799 1.00 46.75 C原子4 O MET A 0 16.928 24.678 29.560 1.00 48.24 O原子5 CB MET A 0 17.257 22.311 31.008 1.00 48.14 C原子6 N ALA A 1 19.106 24.757 29.076 1.00 43.47 NHETATM 491 O OH A 101 23.505 19.335 23.451 1.00 35.56 OHETATM 492 O HOH A 102 19.193 19.013 25.418 1.00 12.73 OHETATM 493 O HOH A 103 7.781 12.538 12.927 1.00 80.11 O
....并继续这样
我只需要阅读以“ ATOM”关键字开头的行。然后,我想将它们的信息拆分为变量以及数组或列表。之后,我想将X坐标的最大值打印到标签。
例如;
原子1 N MET A 0 18.112 24.345 32.146 1.00 51.10 N
1代表原子序数
N代表原子名称
MET代表氨基酸名称
18.112代表X坐标等。
我要做什么
我使用了之前在这里提出的类似问题的代码,但无法将其实现到我的项目中。首先,我为变量创建了一个类[]
class Atom { public int atom_no; public string atom_name; public string amino_name; public char chain; public int amino_no; public float x_coordinate; public float y_coordinate; public float z_coordinate; public float ratio; public float temperature; }
对于主要阶层;注意:我应该提到变量之间没有单个空格。例如,在“ MET”和“ A”之间,存在额外的3或4个空格。我尝试在读取文件时将其删除,但是不知道是否可行。.
private void button1_Click(object sender, EventArgs e) { string filePath = @"path_of_file"; string stringToSearch = @"ATOM"; List<Atom> Atoms = new List<Atom>(); using (StreamReader sr = new StreamReader(filePath)) { string[] lines = File.ReadAllLines(filePath); foreach (string line in lines) { if (line.Contains(stringToSearch)) // i have tried to read the parts that starts with ATOM { while (sr.Peek() >= 0) //this while part is from the question asked before { string[] strArray; string line1 = sr.ReadLine(); // i've added theese 2 lines to remove the extra whitespaces var lineParts = line1.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); strArray = line1.Split(' '); Atom currentAtom = new Atom(); currentAtom.atom_no = int.Parse(strArray[0]); currentAtom.atom_name = strArray[1]; currentAtom.amino_name = strArray[2]; currentAtom.chain = char.Parse(strArray[3]); currentAtom.amino_no = int.Parse(strArray[4]); currentAtom.x_coordinate = float.Parse(strArray[5]); currentAtom.y_coordinate = float.Parse(strArray[6]); currentAtom.z_coordinate = float.Parse(strArray[7]); currentAtom.ratio = float.Parse(strArray[8]); currentAtom.temperature = float.Parse(strArray[9]); Atoms.Add(currentAtom); } } } } listBox1.DataSource = Atoms; listBox1.ValueMember = "atom_no"; listBox1.DisplayMember = "atom_name"; }
我尚未将要打印X坐标最大值的部分添加到标签。我现在正在使用列表框进行测试。因此,当我运行代码并按下按钮时,在
currentAtom.atom_no = int.Parse(strArray[0]);
行上显示“输入字符串的格式不正确”错误。
[我知道我的代码看起来一团糟,如果我偷了你的时间,对不起。如果你们能帮助我为我的作业做这个表格应用程序,我将不胜感激。如果没有,仍然感谢您阅读。祝您健康愉快。.>
首先,大家好。我是C#的初学者,正在尝试做此作业。我的问题是,读取.pdb(蛋白质数据库)文件的特定部分,并将该特定行拆分为...
一种方法是只使用File.ReadAllLines
来读取文件,然后过滤掉没有StartWith
stringToSearch
文本的任何行(使用System.Linq
方法Where
),最后使用Atom
方法从每一行中选择一个新的Split
(并删除空条目),最后使用ToList
将它们全部返回:
List<Atom> Atoms = File.ReadAllLines(filePath) // Read all the lines
.Where(line => line.StartsWith(stringToSearch)) // Filter on our string
.Select(line =>
{
// Split the line on the space character into an array
var strArray = line.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
// Return a new Atom for each line based on the array contents
return strArray.Length < 10 // Ensure we have at least ten elements in the array
? null // If we don't have 10, return 'null'
: new Atom // Otherwise return a new atom from the array
{
atom_no = int.Parse(strArray[0]),
atom_name = strArray[1],
amino_name = strArray[2],
chain = char.Parse(strArray[3]),
amino_no = int.Parse(strArray[4]),
x_coordinate = float.Parse(strArray[5]),
y_coordinate = float.Parse(strArray[6]),
z_coordinate = float.Parse(strArray[7]),
ratio = float.Parse(strArray[8]),
temperature = float.Parse(strArray[9])
};
})
.ToList(); // Return the items as a list