summaryrefslogtreecommitdiff
path: root/Code/refine_tissue.py
blob: 8bc111c5c9131128fd21c34835d5b19729b0f211 (plain)
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
# Usage: python refine_tissue.py > ../Data/information/experiment.and.tissue.2.txt
#        Set cmd =
#
# Purpose: for each RNA-seq in experiment.and.tissue.1.txt, add a column suggested.tissue as its tissue annotation.
#
# 2 June 2017, slcu, hui
# Last modified 19 June 2017, slcu, hui

import os, sys, operator
import string


def get_singular_form(w):
    d = {'seedlings':'seedling', 'roots':'root', 'leaves':'leaf', 'flowers':'flower', 'floral':'flower', 'shoots':'shoot', 'apices':'apex', 'stems':'stem', 'seeds':'seed', 'petals':'petals', 'sepals':'sepal', 'embryos':'embryo', 'embryonic':'embryo', 'cotyledons':'cotyledon', 'hairs':'hair', 'meristems':'meristem', 'epidermal':'epidermis', 'apical':'apex', 'buds':'bud', 'vacuoles':'vacuole', 'vacuolar':'vacuole', 'tips':'tip', 'pollens':'pollen', 'hypocotyls':'hypocotyl', 'tubes':'tube', 'stomatal':'stomata', 'ovule':'ovules', 'pistils':'pistil', 'anthers':'anther', 'carpels':'carpel', 'pedicle':'pedicel', 'vascular':'vasculum', 'whole plant':'seedling', 'inflorescence':'flower.inflorescence', 'inflorescences':'flower.inflorescence', 'whole seedling':'seedling', 'whole rosette':'leaf.rosette', 'whole aerial seedling':'seedling.aerial', 'vegatative shoot apical meristem':'shoot.apical.meristem', 'inflorescence containing stage 8 and younger flowers':'flower.inflorescence', 'plant roots':'root', 'entire vegetative rosette':'leaf.rosette', 'fungal-colonized plant roots':'root.fungal.colonized', 'rosettes - 5 leaves stage':'leaf.rosette', '2-week old seedlings without roots':'seedling.no.roots', 'immature inflorescence':'inflorescence.immature', 'rosette leaves':'leaf.rosette', 'plant seedling':'seedling', 'entire aerial part':'aerial.tissue', '14-d-old entire seedlings':'seedling', 'rosette leaf':'leaf.rosette', 'whole seedlings':'seedling', 'etiolated 5d-old seedlings':'seedling.etiolated', 'root structure':'root', 'mature leaves':'leaf.mature', 'root tip':'root.tip', '10d-old seedling':'seedling','primary root tip':'root.tip',
         'epidermis including guard cells':'epidermis',
         'root tip tissue':'root.tip',
         'anther stage 4-7':'flower.anther',
         'anther':'flower.anther',
         'embryo':'seed.embryo',
         'etiolated seedlings':'seedling.etiolated',
         '21 days-old seedlings':'seedling',
         'aerial tissue':'aerial.tissue',
         'endosperm':'seed.endosperm',
         'whole seed':'seed',
         'pistils pollinated for 8 hours':'flower.pistil.pollinated',
         'primary root':'root',
         'whole floral bud':'flower.bud',
         'whole seedling root':'seedling.root',
         'whole root':'seedling.root',
         'whole plants':'seedling',
         'aerial shoots':'shoot',
         'flower bud':'flower.bud',
         'aerial seedling':'seedling.aerial',
         'anthers at stage 4-7':'flower.anther',
         'carpels (collected manually from 15 developing inflorescences)':'flower.carpel',
         'ath_shoot_meristem_1':'shoot.meristem',
         'ath_whole_plant_1':'seedling',
         'ath_whole_plant_2':'seedling',
         'whole seeds':'seed',
         '3-day-old root':'root',
         'unopened flower buds':'flower.bud',
         'first true leaf':'leaf',
         '3-day-old root':'root',
         '7 dag seedlings':'seedling',
         'facs-sorted protoplasts from aerial tissue of 10-day old seedlings':'seedling.protoplasts',
         'root tip':'root.tip',
         'inflorescences and siliques':'inflorescences.and.siliques',
         'Epidermis including guard cells epidermis including guard cells':'leaf.stomata.epidermis',
         'base stem':'stem',
         'siliques':'silique',
         'whole organism':'seedling',
         'seedling shoot':'seedling.shoot',
         'aerial tissue':'aerial.tissue',
         '10-day-old seedlings and inflorescences from 25-day-old plants':'seedling.and.inflorescence',
         'shoot apical meristem':'shoot.apical.meristem',
         'expanded mature leaves from 28 day old plants':'leaf',
         'aerial tissues of 15 day seedlings': 'aerial.tissue',
         'whole parts':'seedling',
         'aerial organs':'aerial.tissue',
         'lower stem':'stem',
         'upper stem':'stem',
         'rosette':'leaf.rosette',
         'root and shoot':'root.and.shoot',
         'cell culture':'cell.culture',
         'aerial part':'aerial.tissue',
         'aerial':'aerial.tissue',
         'whole plantlet without root':'seedling',
         'sorted endodermis (facs)':'endodermis.facs-sorted',
         'whole root':'root',
         'siluge without seeds':'seed',
         'first internode':'stem',
         'rosettes':'leaf.rosette',
         'hypocotyl':'seedling.hypocotyl',
         'somatic embryo':'seed.embryo'
    }
    if w in d:
        return d[w]
    return w

def remove_parenthese(s):
    if '(' in s:
        return s[:s.find('(')]
    return s



def make_singular(lst):
    result = []
    # map plural to singular
    d = {'roots':'root', 'shoots':'shoot',
         'leaves':'leaf', 'flowers':'flower',
         'anthers':'anther', 'hairs':'hair',
         'seedlings':'seedling', 'apices':'apex',
         'buds':'bud', 'siliques':'silique',
         'rosettes':'rosette', 'meristems':'meristem',
         'sepals':'sepal', 'petals':'petal',
         'inflorescences':'inflorescence', 'carpels':'carpel',
         'seeds':'seed', 'pistils':'pistil',
         'stamens':'stamen', 'ovules':'ovule',
         'tissues':'tissue', 'ovaries':'ovary',
         'veins':'vein', 'nodes':'node',
         'internodes':'internode', 'fibres':'fibre',
         'hypocotyls':'hypocotyl', 'cotyledons':'cotyledon',
         'plants':'plant', 'embryos':'embryo'}
    
    for x in lst:
        if x in d:
            result.append(d[x])
        else:
            result.append(x)
    return result

def map_tissue(s):
    ''' given a string s, if all words in a key of d are in s, then the corresponding value is a likely tissue. '''
    d = {
        'hypocotyl':'seedling.hypocotyl',
        'hypocotyl seedling':'seedling.hypocotyl',
        'leaf':'leaf',
        'leaf petiole':'leaf.petiole',
        'petiole':'leaf.petiole',        
        'leaf blade':'leaf.blade',
        'leaf first true':'leaf',
        'leaf stomata':'leaf.stomata',
        'stomata':'leaf.stomata',
        'chlorophyll':'leaf.chlorophyll',
        'vein':'leaf.vein',
        'leaf vein':'leaf.vein',
        'leaf lamina':'leaf.lamina',                
        'leaf rosette':'leaf rosette',
        'rosette':'leaf.rosette',
        'rosette leaf':'leaf.rosette',
        'shoot':'shoot',
        'aerial shoot':'aerial.shoot',
        'shoot apex':'shoot.apex',
        'shoot tip':'shoot.apex',
        'flower':'flower',
        'flower petal':'flower.petal',
        'flower sepal':'flower.sepal',
        'flower stamen':'flower.stamen',    
        'flower anther':'flower.anther',
        'flower carpel':'flower.carpel',
        'flower pistil':'flower.pistil',
        'flower inflorescence':'flower.inflorescence',
        'stigma':'flower.stigma',
        'filament':'flower.filament',
        'style':'flower.style',        
        'anther':'flower.anther',    
        'petal':'flower.petal',
        'sepal':'flower.sepal',
        'stamen':'flower.stamen',    
        'carpel':'flower.carpel',
        'pistil':'flower.pistil',
        'ovary':'flower.ovary',
        'pedicel':'flower.pedicel',
        'ovule':'flower.ovule',        
        'inflorescence':'flower.inflorescence',    
        'seed':'seed',
        'epicotyl':'seed.epicotyl',
        'radicle':'seed.radicle',        
        'embryo':'seed.embryo',
        'endosperm':'seed.endosperm',
        'endodermis':'endodermis',        
        'stem':'stem',
        'pith':'pith',
        'protoxylem':'protoxylem',
        'xylem':'xylem',
        'phloem':'phloem',
        'sclerenchyma':'sclerenchyma',
        'bast fibre':'bast.fibre',
        'cortex':'cortex',
        'parenchyma':'parenchyma',
        'mesophyll':'leaf.mesophyll',
        'shoot apical meristem':'meristem.shoot.apical',
        'root apical meristem':'.meristem.root.apical',
        'apical meristem':'meristem.apical',
        'floral meristem':'meristem.floral',
        'inflorescence meristem':'meristem.inflorescence',        
        'meristem':'meristem',
        'meristem shoot':'meristem.shoot',        
        'cotyledon':'cotyledon',
        'apical':'apical',
        'basal':'basal',
        'root':'root',
        'root apex':'root.apex',
        'root tip':'root.tip',
        'root primary tip':'root.primary.tip',        
        'root cap':'root.cap',
        'root lateral':'root.lateral',
        'root primary':'root.primary',
        'root hair':'root.hairs',
        'bud':'bud',
        'bud axillary':'bud.axillary',
        'bud lateral':'bud.axillary',
        'bud apical':'bud.apical',
        'bud floral':'bud.flower',
        'bud flower':'bud.flower',
        'bud meristem':'bud.meristem',        
        'internode':'stem.internode',
        'node':'stem.node',
        'vascular':'vasculum',
        'epidermis':'epidermis',
        'seedling':'seedling',
        'plant':'seedling',        
        'whole plant':'seedling',
        'whole':'seedling',
        'whole parts':'seedling',
        'whole root':'root',        
        'seedling root':'seedling.root',
        'seedling shoot':'seedling.shoot',
        'seedling etiolated':'seedling.etiolated',
        'aerial':'aerial',
        'aerial tissue':'aerial.tissue',
        'aerial seedling':'seedling.aerial',
        'silique':'silique',
        'unknown':'unknown',
        'siluge':'seed',
        'bundle sheath':'leaf'
    }
    result = [] # a list of tuples, (tissue, word count)
    s = s.lower()
    slst = s.split()
    slst2 = make_singular(slst)
    for k in d: # search each key in d
        klst = k.split()
        count = 0
        exact_count = 0
        for x in klst:
            count += slst2.count(x)
            if x in slst2:
                exact_count += 1
        if count >= len(klst) and exact_count == len(klst):
            result.append((d[k], count))
    if result == []:
        return 'unknown'
    else:
        sresult = sorted(result, key=operator.itemgetter(1), reverse=True)
        return sresult[0][0]


def repeat_words(s):
    ''' s in the form of meristem(2) '''
    s = s.strip()
    index = s.find('(')
    if index < 0:
        return s
    index2 = s.find(')')
    word = s[:index]
    n = s[(index+1):index2]
    n = int(n)
    return ' '.join(n*[word])

def get_words(s):
    '''  s in the form meristem(2);leaf(2);bud(1) or shoot.meristem '''
    lst = s.split(';')
    result = []
    for x in lst:
        index = x.find('(')
        if index >= 0:
            t = repeat_words(x)
            result.append(t)            
        else:
            t = x
            if '.' in t:
                for y in t.split('.'):
                    result.append(y)
    return ' '.join(result)

def remove_punctuation(s):
    return s.replace('_', ' ')

# main

if os.path.exists('../Data/temp/experiment.and.tissue.1.txt'):
    cmd = 'cut -f 1-4 ../Data/temp/experiment.and.tissue.1.txt > ../Data/temp/a.txt' # generated by python assign_tissue.py
    os.system(cmd)
else:
    print('Run python assign_tissue.py > ../Data/temp/experiment.and.tissue.1.txt first.')
    sys.exit()
    
f = open('../Data/temp/a.txt')
print('run.id\tinferred.tissue\tbiosample.tissue\tbiosample.id\tsuggested.tissue')
for line in f:
    line = line.strip()
    lst = line.split('\t')
    
    if lst[2] != 'part_unknown' and lst[2] != '.':
        s = lst[2].lower()
        s = map_tissue(remove_punctuation(s))
    # elif lst[2] == '.':
    #     s = lst[1]
    #     s = remove_parenthese(s)
    else:
        s = lst[1]
        if not ';' in s:
            s = remove_parenthese(s)
        else:
            s = get_words(s)

        s = map_tissue(remove_punctuation(s))

    print(line + '\t' + s)
f.close()