File: EntrezController.m

package info (click to toggle)
biococoa 2.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,172 kB
  • sloc: objc: 21,348; makefile: 18; ansic: 4
file content (901 lines) | stat: -rw-r--r-- 30,256 bytes parent folder | download | duplicates (5)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
//
//  EntrezController.m
//  BioCocoa
//
//  Created by Alexander Griekspoor
//  Copyright (c) 2003-2009 The BioCocoa Project.
//  All rights reserved.
//
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions
//  are met:
//  1. Redistributions of source code must retain the above copyright
//  notice, this list of conditions and the following disclaimer.
//  2. Redistributions in binary form must reproduce the above copyright
//  notice, this list of conditions and the following disclaimer in the
//  documentation and/or other materials provided with the distribution.
//  3. The name of the author may not be used to endorse or promote products
//  derived from this software without specific prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
//  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
//  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
//  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
//  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
//  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
//  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
//  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
//  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
//  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


/* This (shared) window controller is all you need to search and fetch entrez. In comes 
with its own nib file and generates EntrezResult objects which are displayed in a 
tableview. We make use of NSConnection and NSURLDownload for asynchronous fetching of
results and sequences.
The flow of the program is in short:
- retrieve results for query, generates an xml file with IDs (using NCBI's eSearch eUtil).
- parse this file and retrieve summaries for each ID (using NCBI's eSummary's eUtil).
- parse xml file with summaries, create for each one a EntrezResult object and display 
  those in the Tableview.
- if the user selects one, it is fetched (using NCBI's eFetch eUtil).
- the response is parsed and forwarded to the delegate, this controller is cleaned-up 
  afterwards.
*/

#import "EntrezController.h"

// The delegate only has to implement one method to receive the fetched sequence: 
@protocol EntrezControllerDelegate <NSObject>
- (void)insertFetchedSequence: (NSString *)seq withName: (NSString *)title;
@end


@implementation EntrezController  

//===========================================================================
#pragma mark -
#pragma mark  Init & Dealloc
//===========================================================================

- (EntrezController *)init
{
    self = [super initWithWindowNibName: @"Entrez"];
    prefs = [NSUserDefaults standardUserDefaults];
    searchResults = [[NSMutableArray array] retain];
    
    return self;
    
}

- (void)awakeFromNib{    

    //TEXTVIEW SETUP
    [preview setFont: [NSFont fontWithName: @"Courier" size: 11.0]];

    //SEARCHFIELD SETUP
    [searchField setRecentsAutosaveName: @"RecentEntrezSearches"];
    [searchField setTarget:self];
    [searchField setDelegate:self];
    
    [self showPreview: self];
    
    // PROGRESS SETUP
    [progressTextField setStringValue: @""];
    [self cleanupDownload];
    
    // STATUS
    searchInProgress = NO;
    summaryFetchInProgress = NO;
    fetchInProgress  = NO;
    fetchedResults = 0;

    // DOWNLOAD SETUP
    receivedData = nil;
}

- (void)dealloc{
 
    if(receivedData != nil){
        [receivedData release];
        receivedData = nil;
    }
    
    if ( connection != nil ) {
        [connection release];
        connection = nil;
    }
    
    [webenv release];
    [querykey release];
    
    [searchResults release];
    
    [super dealloc];
}

-(void)setDelegate:(id) newDelegate {
    delegate = newDelegate;
}

-(id)delegate {
    return delegate;
}


//===========================================================================
#pragma mark -
#pragma mark  ACTIONS
//===========================================================================

- (IBAction)searchForQuery:(id)sender{
	if([[sender stringValue]isEqualToString:@""]){
		[self cleanupDownload];	
		[progressTextField setStringValue: @""];
		fetchedResults = 0;
		[searchResults removeAllObjects];
		[tv reloadData];
	}
    else if(searchInProgress || summaryFetchInProgress || fetchInProgress){
        // cancel
        [self cleanupDownload];
        // go again
        [self retrieveSearchResultsForQuery: [sender stringValue]];
        
    } else {
        [self retrieveSearchResultsForQuery: [sender stringValue]];
    }
}

- (IBAction)fetch:(id)sender{
    int row = [tv selectedRow];
        
    if (row == -1) {
        NSBeep();
    } else {
        [self fetchResult: [searchResults objectAtIndex: row]];
    }
        
}

- (IBAction)cancel:(id)sender{
    [self cleanupDownload];
    fetchedResults = 0;
    [progressTextField setStringValue: @""];

    [NSApp endSheet: [self window] returnCode: 1];
    [[self window] orderOut: self];
}


- (IBAction)showPreview:(id)sender{
    NSAttributedString* newStorage;
    NSMutableDictionary* attDict  = [NSMutableDictionary dictionary];

    int row = [tv selectedRow];
    
    NSMutableString *resultsstring = [NSMutableString stringWithCapacity: 1000];
    
    if (row == -1) {
        [resultsstring appendString: @"\nNo Record Selected"];
        NSMutableParagraphStyle *modifiedStyle = [[NSMutableParagraphStyle alloc] init];
        [modifiedStyle setAlignment: NSCenterTextAlignment]; 
        [attDict setObject: modifiedStyle forKey: NSParagraphStyleAttributeName];
        [attDict setObject: [NSFont systemFontOfSize: 16.0] forKey: NSFontAttributeName];
        [attDict setObject: [NSColor lightGrayColor] forKey: NSForegroundColorAttributeName];
        [modifiedStyle release]; 
        
        newStorage = [[NSAttributedString alloc] initWithString: resultsstring attributes:attDict];
        [fetchButton setEnabled: NO];
        
    } else {
               
        [resultsstring appendString:   [NSString stringWithFormat: @"%@ %@", [[searchResults objectAtIndex: row] extra], [[searchResults objectAtIndex: row] description]]];
        
        [attDict setObject: [NSFont fontWithName: @"Courier" size: 12] forKey: NSFontAttributeName];
        [attDict setObject: [NSColor blackColor] forKey: NSForegroundColorAttributeName];
        [attDict setObject: [NSColor whiteColor] forKey: NSBackgroundColorAttributeName];
        
        newStorage = [[NSAttributedString alloc] initWithString: resultsstring attributes:attDict];
        [fetchButton setEnabled: YES];
    }
    
    [[preview textStorage] setAttributedString: newStorage];  
    [newStorage release];   
}


//===========================================================================
#pragma mark -
#pragma mark  TABLEVIEW METHODS
//===========================================================================

- (int)numberOfRowsInTableView:(NSTableView *)theTableView{
    return [searchResults count];
}

- (id)tableView:(NSTableView *)theTableView objectValueForTableColumn:(NSTableColumn *)theColumn row:(int)rowIndex{
    
        if([[theColumn identifier]isEqualToString: @"accession"]){
            return [[searchResults objectAtIndex: rowIndex] accession];
            
        } else if([[theColumn identifier]isEqualToString: @"description"]){
            return [[searchResults objectAtIndex: rowIndex] description];
            
        } else if([[theColumn identifier]isEqualToString: @"species"]){
            return [[searchResults objectAtIndex: rowIndex] species];
        }
    
    return nil;
    
}

- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex{
        // TO COMPENSATE FOR ADJUSTED iTABLEVIEW
        if([(NSCell *)aCell type] == NSTextCellType){
            [aCell setTextColor: [NSColor blackColor]];
        }
}

- (void)tableViewSelectionDidChange:(NSNotification *)aNotification{
    [self showPreview: self];
}

- (void)tableView:(NSTableView*)tv didClickTableColumn:(NSTableColumn *)tableColumn{
    
}

- (void)clearIndicatorImages{
    
}

// ================================================================
#pragma mark -
#pragma mark  DOWNLOAD METHODS
// ================================================================

- (void)retrieveSearchResultsForQuery: (NSString *)q{   
    //NSString *field = nil;
    
    if([[searchField stringValue]isEqualToString: @""]){
        NSBeep();
        return;
    }
    
    // PREPARE
    [progress startAnimation: self];
    [progressTextField setStringValue: @"Contacting NCBI..."];
    
    searchInProgress = YES;
    
    /*
    // PREVIOUS SEARCH?  -> support for Search more using webenv, not implemented here...
    if(fetchedResults > 0 && webenv != nil){
    } else {
    */
	//NSLog(@"%@", q);

    NSMutableString *str = [NSMutableString stringWithString: [NSString stringWithFormat: @"http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=nucleotide&retmode=xml&usehistory=y&retmax=150&tool=EnzymeX&retstart=%d&term=", fetchedResults]];
   	NSMutableString *query = [NSMutableString stringWithString: q];

    // Replace spaces by +
    [query replaceOccurrencesOfString: @" " withString: @"+" options:  NSCaseInsensitiveSearch range: NSMakeRange(0, [query length])];
	[str appendString: query];

	/*
	// Add field, unused here, but would allow searches in specific categories only
	// Contains [] already? -> ALL, otherwise check title of menu
	if([query rangeOfString: @"["].location == NSNotFound){
		id eObject;
		NSEnumerator *e = [[searchMenu itemArray] objectEnumerator];
		while (eObject = [e nextObject]){
			if([eObject state] == NSOnState){
				field = [eObject title];
				break;
			}
		}
		
        if([field isEqualToString: @"All"]) field = nil;
        else if([field isEqualToString: @"Accession"])   field = @"[ACCN]";
        else if([field isEqualToString: @"Author(s)"])   field = @"[AUTH]";
        else if([field isEqualToString: @"Property"])    field = @"[PROP]";
        else if([field isEqualToString: @"Definition"])  field = @"[TITL]";
        else if([field isEqualToString: @"Feature"])     field = @"[FKEY]";
        else if([field isEqualToString: @"Gene"])        field = @"[GENE]";
        else if([field isEqualToString: @"Organism"])    field = @"[ORGN]";
        else if([field isEqualToString: @"Protein"])     field = @"[PROT]";
        else if([field isEqualToString: @"UID"])         field = @"[UID]";
        
        //NSLog(@"Field: %@", field);
		
	}
	
    if(field) [str appendString: field];
	*/
	
	
	// QUERY

    // Remove other strange characters
    // query = [NSMutableString stringWithString: (NSString *) CFURLCreateStringByAddingPercentEscapes (NULL, (CFStringRef) query, NULL, NULL, kCFStringEncodingMacRoman)];
     
	//NSLog(@"Search: %@", str);

         
    if ( ![self _canConnect] ) {
        [progressTextField setStringValue: @"Unable to contact NCBI. Please provide an internet connection."];
        [self NCBIconnectionError: @"not reachable"]; 
        return;
    }
    
    receivedData = [[NSMutableData alloc] init];
    
    NSURLRequest *theRequest = [NSURLRequest requestWithURL: [NSURL URLWithString: str]
                                                cachePolicy:  NSURLRequestReloadIgnoringCacheData
                                            timeoutInterval:  20.0];
    
    if ( !theRequest) {
        [progressTextField setStringValue: @"Unable to generate query. Check for any inapropriate characters in your query."];
        [self NCBIconnectionError: @"no request"]; 
        return;
    }
    
    connection = [NSURLConnection connectionWithRequest: theRequest delegate:self];
    
    if ( !connection ) {
        [progressTextField setStringValue: @"Unable to generate query. Check for any inapropriate characters in your query."];
        [self NCBIconnectionError: @"connection failure"];
        return;
    }
    
    [connection retain];
    
}


- (BOOL)parseSearchResults: (NSData *)results{
    SXMLTree *xmldata = nil;
    BOOL success = YES;
    searchcount = 0;
    
    NS_DURING
        xmldata = [[SXMLTree alloc] initFromData: results usingEncoding: NSASCIIStringEncoding];
        //NSLog(@"XML: %@", xmldata);
    NS_HANDLER
        //NSLog(@"Error initializing SXMLTree from output");
        success = NO;
    NS_ENDHANDLER
    
    if([xmldata findRoot] == nil) return NO;
    
    // First check for found items
    
    NS_DURING
        [self setWebenv:   [[xmldata childWithPath: @"/eSearchResult/WebEnv"]nodeText]];
        [self setQuerykey: [[xmldata childWithPath: @"/eSearchResult/QueryKey"]nodeText]];
            searchcount = [[[xmldata childWithPath: @"/eSearchResult/Count"]nodeText] intValue];
        
        //NSLog(@"%@", [[xmldata childWithPath: @"/eSearchResult/Count"]nodeText]);
        //NSLog(@"%@", [[xmldata childWithPath: @"/eSearchResult/WebEnv"]nodeText]);
        //NSLog(@"%@", [[xmldata childWithPath: @"/eSearchResult/QueryKey"]nodeText]);
    NS_HANDLER
        //NSLog(@"Error extracting webenv");
        success = NO;
    NS_ENDHANDLER
    
    [self cleanupDownload];
    return success;

}

- (void)retrieveSummaries{
    // PREPARE
    //[progress setIndeterminate: YES];
    [progress startAnimation: self];
    [progressTextField setStringValue: @"Receiving results from NCBI..."];
 
    summaryFetchInProgress = YES;
    
    NSString *query = [NSString stringWithFormat: @"http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=nucleotide&retmode=xml&retmax=150&retstart=%d&WebEnv=%@&query_key=%@",
        fetchedResults, [self webenv], [self querykey]];

    //NSLog(@"Retrieve: %@", query);
    
    if(!query || [query isEqualToString: @""]){
        NSBeep();
        [progressTextField setStringValue: @"Error while retrieving summaries. Please try again."];
        [self cleanupDownload];
        return;   
    }
    
    if ( ![self _canConnect] ) {
        [self NCBIconnectionError: @"not reachable"]; 
        [progressTextField setStringValue: @"Unable to contact NCBI. Please provide an internet connection."];
        return;
    }
    
    receivedData = [[NSMutableData alloc] init];
    
    NSURLRequest *theRequest = [NSURLRequest requestWithURL: [NSURL URLWithString: query]
                                                cachePolicy:  NSURLRequestReloadIgnoringCacheData
                                            timeoutInterval:  20.0];
    
    
    if ( !theRequest) {
        [progressTextField setStringValue: @"Unable to generate query. Check for any inapropriate characters in your query."];
        [self NCBIconnectionError: @"no request"]; 
        return;
    }
    
    connection = [NSURLConnection connectionWithRequest: theRequest delegate:self];
    
    if ( !connection ) {
        [progressTextField setStringValue: @"Unable to generate query. Check for any inapropriate characters in your query."];
        [self NCBIconnectionError: @"connection failure"];
        return;
    }
    
    [connection retain];
}


- (BOOL)parseSummaries: (NSData *)results{
    SXMLTree *xmldata = nil;
    SXMLTree *theXMLHitSet = nil;
    NSString *db_id = nil;
    NSString *accession = nil;
    NSString *extra = nil;
    NSString *description = nil;
    NSString *species = nil;

    BOOL success = YES;

    NS_DURING
        xmldata = [[SXMLTree alloc] initFromData: results usingEncoding: NSASCIIStringEncoding];
        //NSLog(@"XML: %@", xmldata);
    NS_HANDLER
        //NSLog(@"Error initializing SXMLTree from output");
        success = NO;
    NS_ENDHANDLER
    
    if([xmldata findRoot] == nil) return NO;
    
    // First check for found items
    
    // REMOVE PREVIOUS RESULTS
    [searchResults removeAllObjects];
    
     //TEST
     //Fetch important data from the XML entry
     //SXMLTree *theXMLOutput = [results childWithPath:@"/BlastOutput"];
     NS_DURING
     theXMLHitSet = [xmldata childWithPath: @"/eSummaryResult"];
     NS_HANDLER
     //NSLog(@"Error initializing SXMLTree from output");
     success = NO;
     NS_ENDHANDLER
     
     if(!success) return NO;        // DO THESE KIND OF CHECKS!
     
     int theHitCount  = [theXMLHitSet childCount];
     int i;
     for (i=0; i< theHitCount; i++) {
         NS_DURING
             SXMLTree *theXMLHit = [theXMLHitSet childAtIndex:i];
             db_id = [[theXMLHit childWithPath: @"Id"] nodeText];
             
             accession = [[theXMLHit childAtIndex: 1] nodeText];
           description = [[theXMLHit childAtIndex: 2] nodeText];
                 extra = [[theXMLHit childAtIndex: 3] nodeText];
               species = [[theXMLHit childAtIndex: 8] nodeText];
             
         NS_HANDLER
             //NSLog(@"Error initializing SXMLTree from output");
             success = NO;
         NS_ENDHANDLER
         
         if(db_id){
             EntrezResult *result = [[EntrezResult alloc] initWithID: [db_id intValue]];
             
             if(accession)  [result setAccession: accession];
             if(extra)      [result setExtra: extra];
             if(description)[result setDescription: description];
             if(species)    [result setSpecies: species];
             
             [searchResults addObject: result];
             [result release];
             
         } else success = NO;

     }
    
     [searchResults sortUsingSelector: @selector(sortResultsOnIdAscending:)];
     [tv reloadData];
     
     /*
     if(success && searchcount > fetchedResults + 50){
         fetchedResults += 50;
         //[searchButton setTitle: @"More"];
     } else {
         fetchedResults = 0;
         //[searchButton setTitle: @"Search"];
     }
     */
     
     [self showPreview: self];
     [self cleanupDownload];

     return success;

}

- (void)fetchResult: (EntrezResult *)result{
    // PREPARE
    [progress startAnimation: self];
    [progressTextField setStringValue: @"Retrieving record..."];
    
    fetchInProgress = YES;
    
    NSString *query = [NSString stringWithFormat: @"http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=nucleotide&id=%d&retmode=xml&rettype=fasta",
        [result db_id]];
    
    //NSLog(@"Retrieve: %@", query);
    
    if(!query || [query isEqualToString: @""]){
        NSBeep();
        [progressTextField setStringValue: @"Error while retrieving record. Please try again."];
        [self cleanupDownload];
        return;   
    }
    
    if ( ![self _canConnect] ) {
        [progressTextField setStringValue: @"Unable to contact NCBI. Please provide an internet connection."];
        [self NCBIconnectionError: @"not reachable"]; 
        return;
    }
    
    receivedData = [[NSMutableData alloc] init];
    
    NSURLRequest *theRequest = [NSURLRequest requestWithURL: [NSURL URLWithString: query]
                                                cachePolicy:  NSURLRequestReloadIgnoringCacheData
                                            timeoutInterval:  20.0];
    
    if ( !theRequest) {
        [progressTextField setStringValue: @"Unable to generate query. Check for any inapropriate characters in your query."];
        [self NCBIconnectionError: @"no request"]; 
        return;
    }
    
    connection = [NSURLConnection connectionWithRequest: theRequest delegate:self];
    
    if ( !connection ) {
        [progressTextField setStringValue: @"Unable to generate query. Check for any inapropriate characters in your query."];
        [self NCBIconnectionError: @"connection failure"];
        return;
    }
    
    [connection retain];
}

- (BOOL)parseFetch: (NSData *)results{
    SXMLTree *xmldata = nil;
    NSString *seq = nil;
    
    BOOL success = YES;
    
    NS_DURING
        xmldata = [[SXMLTree alloc] initFromData: results usingEncoding: NSASCIIStringEncoding];
        //NSLog(@"XML: %@", xmldata);
    NS_HANDLER
        //NSLog(@"Error initializing SXMLTree from output");
        success = NO;
    NS_ENDHANDLER
    
    if([xmldata findRoot] == nil) return NO;
    
    // First check for found items    
    if(!success) return NO;        // DO THESE KIND OF CHECKS!
    
    //Fetch important data from the XML entry
    NS_DURING
        seq = [[xmldata childWithPath: @"/TSeqSet/TSeq/TSeq_sequence"] nodeText];
    NS_HANDLER
        success = NO;
        //NSLog(@"Error extracting sequence");
    NS_ENDHANDLER
    
    if(seq){
        int row = [tv selectedRow];
        EntrezResult *res = [searchResults objectAtIndex: row];
        NSString *name = [NSString stringWithFormat: @"%@ %@", [res accession], [res description]];
        if([name length] > 50) name = [NSString stringWithFormat: @"%@...", [name substringWithRange: NSMakeRange(0, 50)]];
                 
		id <EntrezControllerDelegate> del = [self delegate];
		if ([del respondsToSelector:@selector(insertFetchedSequence:withName:)]){
			[del insertFetchedSequence: seq withName: name]; 
		}
		
    } else success = NO;
    
    [self cleanupDownload];
    
    [NSApp endSheet: [self window] returnCode: 1];
    [[self window] orderOut: self];
    
    return success;
}

- (void)cleanupDownload{
        
    [progress stopAnimation: self];
    //[progress setIndeterminate: NO];
    //[progress setDoubleValue: 0.0];
    //[progressTextField setStringValue: @""];
    
    searchInProgress = NO;
    summaryFetchInProgress = NO;
    fetchInProgress = NO;
    
    if(receivedData != nil){
        [receivedData release];
        receivedData = nil;
    }
    
    if ( connection != nil ) {
        [connection cancel];
        [connection release];
        connection = nil;
    }
    
    if ( response != nil ) {
        [response release];
         response = nil;
    }
    
    
}

- (void)reportDownloadFailureWithError: (NSString *)errorstring{
	
}

- (void) NCBIconnectionError: (id) anError {
    /*
	// Unused now, but in case we really want to use it it would be in line with the following 
	// (taken from John Timmer's 4Peaks Ensembl plugin implementation)
    // the argument will either come in as a string, or as the NSURLConnection's
    // NSError, which we reformat
    NSMutableDictionary *tempDict;
    
    
    if ( [anError isKindOfClass: [NSError class] ] ) {
        tempDict = [NSMutableDictionary dictionaryWithObject: [anError localizedDescription] forKey: @"title"];
        [tempDict setObject: 
            NSLocalizedString( @"There were problems loading a URL from ENSEMBL.  Please check whether the ENSEMBL website is working.", @"ENSEMBL URL Loading Error description" )
                     forKey: @"description"];
        theBLASTError = [[NSError errorWithDomain: @"4Peaks  Analysis error" code: 100 userInfo: tempDict] retain];
        
    }
    else {
        if ( [anError isEqualToString: @"not reachable"] ) {
            tempDict = [NSMutableDictionary dictionaryWithObject: NSLocalizedString( @"Network Unavailable", @"No Network Error title" ) forKey: @"title"];
            [tempDict setObject: 
                NSLocalizedString( @"This search requires an active network and there is no connection currently available.  Please try again when you have network access.", @"No Network Error description" )
                         forKey: @"description"];
            theBLASTError = [ [NSError errorWithDomain: @"4Peaks  Analysis error" code: 100 userInfo: tempDict] retain];
        }
        
        if ( [anError isEqualToString: @"connection failure"] ) {
            tempDict = [NSMutableDictionary dictionaryWithObject: NSLocalizedString( @"Network Timeout", @"Network Timeout Error title") forKey: @"title"];
            [tempDict setObject: 
                NSLocalizedString( @"The attempt to reach ENSEMBL has timed out.  Please ensure that you can connect to ENSEMBL and then try again.", @"Network Timeout Error description")
                         forKey: @"description"];
            theBLASTError = [ [NSError errorWithDomain: @"4Peaks  Analysis error" code: 102 userInfo: tempDict] retain];
        }
        
    */
}



// ================================================================
#pragma mark --- DOWNLOAD ACCESSORS
// ================================================================

- (NSURLResponse *)response
{
    return response;
}

- (void)setResponse:(NSURLResponse *)newResponse
{
    [newResponse retain];
    [response release];
    response = newResponse;
}

- (NSString *)webenv
{
	return webenv;
}

- (void)setWebenv:(NSString *)newWebenv
{
	[newWebenv retain];
	[webenv release];
	webenv = newWebenv;
}

- (NSString *)querykey
{
	return querykey;
}

- (void)setQuerykey:(NSString *)newQuerykey
{
	[newQuerykey retain];
	[querykey release];
	querykey = newQuerykey;
}



// ================================================================
#pragma mark --- CONNECTION DELEGATES
// ================================================================

- (void) connection: (NSURLConnection *)theConnection didReceiveResponse: (NSURLResponse *)theresponse {
    // NSLog(@"Response: %@", theresponse);
    
    // Apple says to clear the data in the case of a redirect
    // we pretty much trust Apple on this
    [receivedData setLength: 0];
    
    // retain the response to use later
    [self setResponse: theresponse];
        
    bytesReceived = 0;
    
    [progressTextField setStringValue: @"Connected to NCBI..."];
    
}

- (void) connection: (NSURLConnection *)connection didReceiveData: (NSData *)data {
    //NSLog(@"Bytes received - %d", [data length]);

    // append the new data to the receivedData
    [receivedData appendData:data];

    bytesReceived = bytesReceived + [data length];

	if(searchInProgress) [progressTextField setStringValue: [NSString stringWithFormat: @"Receiving results from NCBI... (%dKb)", bytesReceived/1024]];
	else if (summaryFetchInProgress) [progressTextField setStringValue: [NSString stringWithFormat: @"Receiving record from NCBI... (%dKb)", bytesReceived/1024]];

		

}

- (void)connection: (NSURLConnection *)connection didFailWithError: (NSError *)error {

    // forward the actual error to our error generation routine
    [self NCBIconnectionError: error];
    [progressTextField setStringValue: [NSString stringWithFormat: @"Error: %@", [error localizedDescription]]];

    [self cleanupDownload];
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    
    //NSLog(@"Received: %@", [NSString stringWithUTF8String: [receivedData bytes]]);
    
    if(searchInProgress){
        if([self parseSearchResults: receivedData]){
            [self cleanupDownload];
			[progressTextField setStringValue: @"Parsing received records..."];
            [self retrieveSummaries];
            
        } else {
            [self NCBIconnectionError: @"error parsing search results"];
            [progressTextField setStringValue: @"No results found."];
            [self cleanupDownload];
        }
    
    } else if (summaryFetchInProgress){
        
        if([self parseSummaries: receivedData]){
            [self cleanupDownload];
            [progressTextField setStringValue: [NSString stringWithFormat: @"%d records found.", searchcount]];
        } else {
            [self NCBIconnectionError: @"error parsing summaries"];
            [progressTextField setStringValue: @"No results found."];
            [self cleanupDownload];
        }
        
    } else if (fetchInProgress){
        if([self parseFetch: receivedData]){
            [self cleanupDownload];
            [progressTextField setStringValue: @""];
        } else {
            [self NCBIconnectionError: @"error parsing record"];
            [progressTextField setStringValue: @"Error parsing record."];
            [self cleanupDownload];
        }        
    }
}



//===========================================================================
#pragma mark -
#pragma mark  GENERAL METHODS
//===========================================================================

- (BOOL)parseOutput: (NSData *)output{
	// Debugging method to parse the output, disabled here.
    /*
    SXMLTree *xmldata, *theXMLHitSet;
    NS_DURING
        xmldata = [[SXMLTree alloc] initFromData: output usingEncoding:NSASCIIStringEncoding];
    NS_HANDLER
        NSLog(@"Error initializing SXMLTree from output");
    NS_ENDHANDLER
    
    if([xmldata findRoot] == nil) return NO;
    
    [self setResults: xmldata]; 
    [xmldata release];
    
    //TEST
    //Fetch important data from the XML entry
    //SXMLTree *theXMLOutput = [results childWithPath:@"/BlastOutput"];
    NS_DURING
        theXMLHitSet = [[self results] childWithPath: @"/BlastOutput/BlastOutput_iterations/Iteration/Iteration_hits"];
    NS_HANDLER
        NSLog(@"Error initializing SXMLTree from output");
    NS_ENDHANDLER
    
    int theHitCount  = [theXMLHitSet childCount];
    int i;
    for (i=0; i< theHitCount; i++) {
        NS_DURING
            SXMLTree *theXMLHit = [theXMLHitSet childAtIndex:i];
        NS_HANDLER
            NSLog(@"Error initializing SXMLTree from output");
        NS_ENDHANDLER
        //NSLog(@"%@", [[theXMLHit childWithPath: @"Hit_def"]nodeText]);
    }
    
    // runstatistics
    
    // results
    
    // querystatistics
    return [self resultsAvailable];
     */
    return YES;
}


- (BOOL)validateMenuItem:(NSMenuItem *)anItem{
    if([[anItem title] isEqualToString: @"Recent Searches"])
		return NO;
     else return YES;
}
		

/////////////////////////////////////////////////////
// a private method to determine network availability
/////////////////////////////////////////////////////
- (BOOL) _canConnect {
    Boolean result;
    SCNetworkConnectionFlags flags;
    assert(sizeof(SCNetworkConnectionFlags) == sizeof(int));
    result = NO;
    if ( SCNetworkCheckReachabilityByName([[NSString stringWithString:@"eutils.ncbi.nlm.nih.gov"] UTF8String], &flags) ) {
        result = (flags & kSCNetworkFlagsReachable);
    }
    return result;
}

@end