-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
210 lines (186 loc) · 11.3 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;
namespace Download_PubMED
{
class Program
{
//EInfo, Get Databases
//EGQuery, Search all databases and return database/count
//ESearch, Search a database and return a list of UIDs
//ESummary, Turn UID into Document Summary
//EFetch, Turn UID into formatted data records
//EFetch, see http://www.ncbi.nlm.nih.gov/books/NBK25499/table/chapter4.chapter4_table1/?report=objectonly
//ELink, List links, and more
//ELink, see http://www.ncbi.nlm.nih.gov/books/NBK25499/#chapter4.ELink
//EPost, Append UIDs to a list of UIDs
//ESpell, Suggests ?improved queries? and spelling fixes.
static void Main(string[] args)
{
//Current Plan
//Get Article IDs from PMC
//Get Journal name, File Name, and Date
//Sort by Journal Name
//Extract tars
//Keep only the Journals & Articles we care about.
//
//Processing ?? Co-Word Occurance ??
//Stop Words = 1:5000 checking every 500,000 words.
string myQuery = "((\"hiv\"[MeSH Terms] OR \"hiv\"[All Fields]) OR (\"acquired immunodeficiency syndrome\"[MeSH Terms] OR (\"acquired\"[All Fields] AND \"immunodeficiency\"[All Fields] AND \"syndrome\"[All Fields]) OR \"acquired immunodeficiency syndrome\"[All Fields] OR \"aids\"[All Fields])) AND hasabstract[text]";
string linkageFileName = "Links.txt";
string articleFileName = "Articles.csv";
//myQuery = "( \"hiv\"[MeSH Terms] OR \"hiv\"[All Fields]) OR (\"acquired immunodeficiency syndrome\"[MeSH Terms] OR (\"acquired\"[All Fields] AND \"immunodeficiency\"[All Fields] AND \"syndrome\"[All Fields]) OR \"acquired immunodeficiency syndrome\"[All Fields] OR \"aids\"[All Fields])";
eUtils.eUtilsServiceSoapClient utilServ = new eUtils.eUtilsServiceSoapClient();
string database = null;
//database = "PMC";
eUtils.eSearchResult searchResult = GetSearchResults(database, myQuery, utilServ);
TextWriter LinksOutputFile = new StreamWriter(linkageFileName);
TextWriter ArticleOutputFile = new StreamWriter(articleFileName);
ArticleOutputFile.WriteLine("PMID,Date,Title,Abstract,MeSH,Citation,Grant");
int Total = int.Parse(searchResult.Count);
while (searchResult != null)
{
if (int.Parse(searchResult.RetStart) > 1000)
break;
foreach (string paper in searchResult.IdList)
{
#region Article Links
eUtils.eLinkResult linkResults = GetLinkResults(paper, utilServ);
//if (linkResults.LinkSet.Length != 0)
//{
LinksOutputFile.Write(paper);
foreach (var a in linkResults.LinkSet)
foreach (var b in a.LinkSetDb)
foreach (var c in b.Link)
if (c.Id.Value != paper && b.DbTo.ToLower() == "pubmed")
LinksOutputFile.Write(" " + c.Id.Value);
LinksOutputFile.WriteLine();
//}
//else
//{
// //No References!?!?
// //Should probably remove from Dataset.
// throw new ArgumentNullException("linkResults", "PubMED ID: " + paper + " has no references, which is improbable to impossible. Throwing Error");
//}
#endregion
#region Article Title, Abstract, and Date
eFetch_pubmed.eUtilsServiceSoapClient fetchServ = new eFetch_pubmed.eUtilsServiceSoapClient();
eFetch_pubmed.eFetchRequest fetchRequest = new eFetch_pubmed.eFetchRequest();
fetchRequest.id = paper;
//fetchRequest.id = string.Join(",", Results);
eFetch_pubmed.eFetchResult fetchResult = fetchServ.run_eFetch(fetchRequest);
for (int i = 0; i < fetchResult.PubmedArticleSet.Length; i++)
{
eFetch_pubmed.PubmedArticleType art = null;
//eFetch_pubmed.PubmedBookArticleType book = null;
if (fetchResult.PubmedArticleSet[i] is eFetch_pubmed.PubmedArticleType)
{
art = (eFetch_pubmed.PubmedArticleType)fetchResult.PubmedArticleSet[i];
if (art.PubmedData.History == null)
{
art.PubmedData.History = new eFetch_pubmed.PubMedPubDateType[1];
art.PubmedData.History[0] = new eFetch_pubmed.PubMedPubDateType();
art.PubmedData.History[0].Year = "N/A";
art.PubmedData.History[0].Month = string.Empty;
art.PubmedData.History[0].Day = string.Empty;
}
if(art.MedlineCitation.Article.ArticleTitle != null)
ArticleOutputFile.Write(string.Join(",",
art.MedlineCitation.PMID.Value,
art.PubmedData.History[0].Year + " " + art.PubmedData.History[0].Month + " " + art.PubmedData.History[0].Day,
//art.MedlineCitation.Article.ArticleDate != null ? art.MedlineCitation.Article.ArticleDate[0].Year + " " + art.MedlineCitation.Article.ArticleDate[0].Month + " " + art.MedlineCitation.Article.ArticleDate[0].Day : "N/A",
'"' + art.MedlineCitation.Article.ArticleTitle.Value.Replace("\"", "''") + '"',
'"' + art.MedlineCitation.Article.Abstract.AbstractText[0].Value.Replace("\"", "''") + '"',
art.MedlineCitation.MeshHeadingList != null ? '"' + string.Join(" ", art.MedlineCitation.MeshHeadingList.Select(o => o.DescriptorName.Value)) + '"' : "N/A",
art.MedlineCitation.CitationSubset != null ? string.Join(" ", art.MedlineCitation.CitationSubset) : "N/A",
(art.MedlineCitation.Article.GrantList != null && art.MedlineCitation.Article.GrantList.Grant != null) ?
string.Join(" ", art.MedlineCitation.Article.GrantList.Grant.Select(o => o.Agency)) : "N/A"));
else if(art.MedlineCitation.Article.Journal.Title != null)
ArticleOutputFile.Write(string.Join(",",
art.MedlineCitation.PMID.Value,
art.PubmedData.History[0].Year + " " + art.PubmedData.History[0].Month + " " + art.PubmedData.History[0].Day,
'"' + art.MedlineCitation.Article.Journal.Title.Replace("\"", "''") + '"',
'"' + art.MedlineCitation.Article.Abstract.AbstractText[0].Value.Replace("\"", "''") + '"',
art.MedlineCitation.MeshHeadingList != null ? '"' + string.Join(" ", art.MedlineCitation.MeshHeadingList.Select(o => o.DescriptorName.Value)) + '"' : "N/A",
art.MedlineCitation.CitationSubset != null ? string.Join(" ", art.MedlineCitation.CitationSubset) : "N/A",
(art.MedlineCitation.Article.GrantList != null && art.MedlineCitation.Article.GrantList.Grant != null) ?
string.Join(" ", art.MedlineCitation.Article.GrantList.Grant.Select(o => o.Agency)) : "N/A"));
ArticleOutputFile.WriteLine();
//foreach (var v in art.MedlineCitation.Article.Abstract.AbstractText)
//Console.WriteLine(v.Value);
//foreach(string a in art.MedlineCitation.CitationSubset)
//Console.WriteLine(a);
}
else if (fetchResult.PubmedArticleSet[i] is eFetch_pubmed.PubmedBookArticleType) { }
else if (fetchResult.PubmedArticleSet[i] is eFetch_pubmed.PubmedBookDataType) { }
else if (fetchResult.PubmedArticleSet[i] is eFetch_pubmed.PubmedDataType) { }
else if (fetchResult.PubmedArticleSet[i] is eFetch_pubmed.PubMedPubDateType) { }
else if (fetchResult.PubmedArticleSet[i] is eFetch_pubmed.PubMedPubDateTypePubStatus) { }
}
#endregion
}
searchResult = GetSearchResults(database, myQuery, utilServ, searchResult);
}
LinksOutputFile.Flush();
LinksOutputFile.Close();
LinksOutputFile.Dispose();
ArticleOutputFile.Flush();
ArticleOutputFile.Close();
ArticleOutputFile.Dispose();
}
private static eUtils.eLinkResult GetLinkResults(string id, eUtils.eUtilsServiceSoapClient serv)
{
eUtils.eLinkRequest req = new eUtils.eLinkRequest();
req.id = new string[]{id};
//req.db = "pubmed";
return serv.run_eLink(req);
}
private static eUtils.eSearchResult GetSearchResults(string db, string myQuery, eUtils.eUtilsServiceSoapClient serv, eUtils.eSearchResult searchResult = null)
{
eUtils.eSearchRequest req = new eUtils.eSearchRequest();
//req.db = "pmc";
req.term = HttpUtility.UrlEncode(myQuery);
//req.RetMax = "100000";
req.RetMax = "1000";
req.db = db;
req.usehistory = "y";
if (searchResult != null)
{
int current = (int.Parse(searchResult.RetStart) + int.Parse(searchResult.RetMax));
if (current >= int.Parse(searchResult.Count))
return null;
req.RetStart = current.ToString();
req.WebEnv = searchResult.WebEnv;
}
eUtils.eSearchResult res = serv.run_eSearch(req);
req.WebEnv = res.WebEnv;
req.QueryKey = res.QueryKey;
return res;
}
}
}
//string theQuery = myQuery;
//eUtils.eGqueryRequest theRequest = new eUtils.eGqueryRequest()
// {
// term = theQuery
// };
//eUtils.Result theResult = serv.run_eGquery(theRequest);
//for (int i = 0; i < theResult.eGQueryResult.ResultItem.Length; i++)
//{
// Result +=
// " " + theResult.eGQueryResult.ResultItem[i].DbName +
// ": " + theResult.eGQueryResult.ResultItem[i].Count + "\r\n";
//}
//eUtils.eInfoResult res = serv.run_eInfo(new eUtils.eInfoRequest());
//for (int i = 0; i < res.DbList.Items.Length; i++)
//{
// Result += res.DbList.Items[i] + "\r\n";
//}
//}
//catch (Exception eee)
//{
// Result = eee.ToString();
//}