1 | package nl.tno.metagenomics |
---|
2 | |
---|
3 | import java.util.List; |
---|
4 | |
---|
5 | class StudyController { |
---|
6 | def synchronizationService |
---|
7 | def gscfService |
---|
8 | def fileService |
---|
9 | def trashService |
---|
10 | def fastaService |
---|
11 | def sampleExcelService |
---|
12 | |
---|
13 | def index = { |
---|
14 | // Clean the upload directory |
---|
15 | fileService.cleanDirectory(); |
---|
16 | |
---|
17 | // Clean the trash if needed |
---|
18 | trashService.cleanTrash(); |
---|
19 | |
---|
20 | // Filter studies for the ones the user is allowed to see |
---|
21 | def studies = Study.list(); |
---|
22 | [studies: studies.findAll { it.canRead( session.user ) }, |
---|
23 | gscfAddUrl: gscfService.urlAddStudy(), |
---|
24 | lastSynchronized: synchronizationService.lastFullSynchronization ] |
---|
25 | } |
---|
26 | |
---|
27 | |
---|
28 | /** |
---|
29 | * Exports data about one or more studies in fasta format |
---|
30 | */ |
---|
31 | def exportAsFasta = { |
---|
32 | def assaySamples = getAssaySamples( params ); |
---|
33 | def name |
---|
34 | |
---|
35 | if( assaySamples == null ) { |
---|
36 | return |
---|
37 | } else if( assaySamples*.assay*.study.unique().size() == 1 ) { |
---|
38 | name = "Study_" + assaySamples[0].assay.study.name?.replace( ' ', '_' ); |
---|
39 | } else { |
---|
40 | name = "studies"; |
---|
41 | } |
---|
42 | |
---|
43 | // Export the sequences and quality scores |
---|
44 | response.setHeader "Content-disposition", "attachment; filename=" + name.trim() + ".zip" |
---|
45 | try { |
---|
46 | fastaService.export( assaySamples.unique(), response.getOutputStream(), name ); |
---|
47 | response.outputStream.flush(); |
---|
48 | } catch( Exception e ) { |
---|
49 | log.error( "Exception occurred during export of sequences. Probably the user has cancelled the download." ); |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | /** |
---|
54 | * Export metadata of one or more studies in excel format |
---|
55 | */ |
---|
56 | def exportMetaData = { |
---|
57 | def assaySamples = getAssaySamples( params ); |
---|
58 | def name |
---|
59 | |
---|
60 | if( assaySamples == null ) { |
---|
61 | return |
---|
62 | } else if( assaySamples*.assay*.study.unique().size() == 1 ) { |
---|
63 | name = "Study_" + assaySamples[0].assay.study.name?.replace( ' ', '_' ); |
---|
64 | } else { |
---|
65 | name = "studies"; |
---|
66 | } |
---|
67 | |
---|
68 | // Export the metadata |
---|
69 | response.setHeader "Content-disposition", "attachment; filename=${name}.xls" |
---|
70 | try { |
---|
71 | // The export functionality needs a assaySample-tag list, but it |
---|
72 | // should be empty when only exporting metadata |
---|
73 | def tags = []; |
---|
74 | assaySamples.unique().each { assaySample -> |
---|
75 | tags << [assaySampleId: assaySample.id, sampleName: assaySample.sample.name, assayName: assaySample.assay.name, studyName: assaySample.assay.study.name, tag: "-"] |
---|
76 | } |
---|
77 | sampleExcelService.exportExcelSampleData( assaySamples.unique(), tags, response.getOutputStream() ); |
---|
78 | response.outputStream.flush(); |
---|
79 | } catch( Exception e ) { |
---|
80 | log.error( "Exception occurred during export of metadata. Probably the user has cancelled the download." ); |
---|
81 | e.printStackTrace(); |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | /** |
---|
86 | * Parse the given parameters and try to extract studies using ids and tokens |
---|
87 | * @param params |
---|
88 | * @return |
---|
89 | */ |
---|
90 | protected List getAssaySamples( params ) { |
---|
91 | def tokens = params.list( 'tokens' ); |
---|
92 | def ids = params.list( 'ids' ); |
---|
93 | def name; |
---|
94 | |
---|
95 | ids = ids.findAll { it.isLong() }.collect { Long.parseLong( it ) } |
---|
96 | |
---|
97 | if( !tokens && !ids ) { |
---|
98 | def message = "No assay tokens or ids given" |
---|
99 | flash.error = message |
---|
100 | redirect( action: "index" ); |
---|
101 | return; |
---|
102 | } |
---|
103 | |
---|
104 | def assaySamples = []; |
---|
105 | |
---|
106 | // Determine which assaySamples to export |
---|
107 | def study; |
---|
108 | tokens.each { token -> |
---|
109 | study = Study.findByStudyToken( token ); |
---|
110 | if( study && study.canRead( session.user ) ) |
---|
111 | assaySamples += study.assays*.assaySamples.flatten(); |
---|
112 | } |
---|
113 | ids.each { id -> |
---|
114 | study = Study.get( id ); |
---|
115 | if( study && study.canRead( session.user ) ) |
---|
116 | assaySamples += study.assays*.assaySamples.flatten(); |
---|
117 | } |
---|
118 | |
---|
119 | return assaySamples; |
---|
120 | } |
---|
121 | |
---|
122 | } |
---|