summaryrefslogtreecommitdiff
path: root/Code/html_network.py
blob: 3237c5574e7c9e98c730d51bcdd1d116bd2bcc70 (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
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
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
# Usage: python html_network.py -f edges.txt -r parameter_for_buildRmatrix.txt -c parameter_for_buildCmatrix.txt -n parameter_for_net.txt
# Purpose: make a summary.html plus its associated files (stored in folder edges) given an edge file (edges.txt).  These files will be served as static files online.  The total volumn of these static files can be quite large, as we get one file for each edge.
#
#          This program is used in update_network.py.
#
# Created on 26 Feb 2017, SLCU, Hui
# Last modified 24 Mar 2017, SLCU, Hui
# Last modified 21 Apr 2017, SLCU, Hui [w2ui for regulatee and regulator tables]
# Last modified 19 Jun 2017, SLCU, Hui [changed text_to_dict to fit the updated RNA_SEQ_INFO_DATABASE]
# Last modified 29 Jun 2017, SLCU, Hui [added key 'sample_id' in text_to_dict]
# Last reviewed 01 Fen 2019, Hui [code review]

import sys, os
import networkx as nx # Run this command on MacOS: export PYTHONPATH="/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
import numpy as np
from optparse import OptionParser
from itertools import islice
import operator
from datetime import datetime
import collections, re, glob
from geneid2name import make_gene_name_AGI_map_dict
from param4net import make_global_param_dict

## Global variables
REGENERATE_ALL_EDGE_FILES  = 'YES'
INDEX_PAGE                 = '../Webapp/static/summary.html' # change
DIR_NAME                   = '../Webapp/static/edges'        # change

RNA_SEQ_INFO_DATABASE   = '../Data/information/rnaseq_info_database.txt'
RNA_SEQ_INFO_DATABASE_JSON   = '../Data/information/rnaseq_info_database.json'
RNA_SEQ_INFO_HTML_PAGE  = 'rnaseqinfo.html'

GENE_ID_TO_GENE_NAME    = '../Data/information/AGI-to-gene-names_v2.txt'
CHIP_SEQ_INFO_HTML_PAGE = 'chipseqinfo.html'

RAKE_STOPLIST_FILE      = '../Data/information/SmartStoplist.txt'

JSON_DIR                = '../Data/history/expr/json'   # move this directory to the same place as this file html_network.py, for gene expression scatterplot
JSON_DIR2               = '../Data/history/bind/json2'  # for displaying binding plots
C3_DIR                  = './depend/c3'
W2UI_DIR                = './depend/w2ui'
C3_FILES                = ['c3.min.css', 'c3.min.js', 'd3.min.js', 'scatterplot.js', 'barchart.js'] # for displaying scatterplots and binding strength
W2UI_FILES              = ['jquery.min.for.w2ui.js', 'w2ui.min.js', 'w2ui.min.css']
ALPHA                   = 0.6 # weight indicating the importance of number of RNA-seq experiments
## function definitions

### RAKE rapid automatic keyphrase extraction (NOT USED).  Skip it and jump to my function.

def is_number(s):
    try:
        float(s) if '.' in s else int(s)
        return True
    except ValueError:
        return False


def load_stop_words(stop_word_file):
    """
    Utility function to load stop words from a file and return as a list of words
    @param stop_word_file Path and file name of a file containing stop words.
    @return list A list of stop words.
    """
    stop_words = []
    for line in open(stop_word_file):
        if line.strip()[0:1] != "#":
            for word in line.split():  # in case more than one per line
                stop_words.append(word)
    return stop_words


def separate_words(text, min_word_return_size):
    """
    Utility function to return a list of all words that are have a length greater than a specified number of characters.
    @param text The text that must be split in to words.
    @param min_word_return_size The minimum no of characters a word must have to be included.
    """
    splitter = re.compile('[^a-zA-Z0-9_\\+\\-/]')
    words = []
    for single_word in splitter.split(text):
        current_word = single_word.strip().lower()
        #leave numbers in phrase, but don't count as words, since they tend to invalidate scores of their phrases
        if len(current_word) > min_word_return_size and current_word != '' and not is_number(current_word):
            words.append(current_word)
    return words


def split_sentences(text):
    """
    Utility function to return a list of sentences.
    @param text The text that must be split in to sentences.
    """
    sentence_delimiters = re.compile(u'[.!?,;:\t\\\\"\\(\\)\\\'\u2019\u2013]|\\s\\-\\s')
    sentences = sentence_delimiters.split(text)
    return sentences


def build_stop_word_regex(stop_word_file_path):
    stop_word_list = load_stop_words(stop_word_file_path)
    stop_word_regex_list = []
    for word in stop_word_list:
        word_regex = r'\b' + word + r'(?![\w-])'  # added look ahead for hyphen
        stop_word_regex_list.append(word_regex)
    stop_word_pattern = re.compile('|'.join(stop_word_regex_list), re.IGNORECASE)
    return stop_word_pattern


def generate_candidate_keywords(sentence_list, stopword_pattern):
    phrase_list = []
    for s in sentence_list:
        tmp = re.sub(stopword_pattern, '|', s.strip())
        phrases = tmp.split("|")
        for phrase in phrases:
            phrase = phrase.strip().lower()
            if phrase != "":
                phrase_list.append(phrase)
    return phrase_list


def calculate_word_scores(phraseList):
    word_frequency = {}
    word_degree = {}
    for phrase in phraseList:
        word_list = separate_words(phrase, 0)
        word_list_length = len(word_list)
        word_list_degree = word_list_length - 1
        #if word_list_degree > 3: word_list_degree = 3 #exp.
        for word in word_list:
            word_frequency.setdefault(word, 0)
            word_frequency[word] += 1
            word_degree.setdefault(word, 0)
            word_degree[word] += word_list_degree  #orig.
            #word_degree[word] += 1/(word_list_length*1.0) #exp.
    for item in word_frequency:
        word_degree[item] = word_degree[item] + word_frequency[item]

    # Calculate Word scores = deg(w)/frew(w)
    word_score = {}
    for item in word_frequency:
        word_score.setdefault(item, 0)
        word_score[item] = word_degree[item] / (word_frequency[item] * 1.0)  #orig.
    #word_score[item] = word_frequency[item]/(word_degree[item] * 1.0) #exp.
    return word_score


def generate_candidate_keyword_scores(phrase_list, word_score):
    keyword_candidates = {}
    for phrase in phrase_list:
        keyword_candidates.setdefault(phrase, 0)
        word_list = separate_words(phrase, 0)
        candidate_score = 0
        for word in word_list:
            candidate_score += word_score[word]
        keyword_candidates[phrase] = candidate_score
    return keyword_candidates


class Rake(object):
    def __init__(self, stop_words_path):
        self.stop_words_path = stop_words_path
        self.__stop_words_pattern = build_stop_word_regex(stop_words_path)

    def run(self, text):
        sentence_list = split_sentences(text)

        phrase_list = generate_candidate_keywords(sentence_list, self.__stop_words_pattern)

        word_scores = calculate_word_scores(phrase_list)

        keyword_candidates = generate_candidate_keyword_scores(phrase_list, word_scores)

        sorted_keywords = sorted(keyword_candidates.iteritems(), key=operator.itemgetter(1), reverse=True)
        return sorted_keywords


### my functions

def get_id(s):
    lst = s.split(' ')
    return lst[0]

def get_name(s, agi2name_dict):
    s = s.strip()
    if s == '':
        return '???'
    if s in agi2name_dict:
        name = agi2name_dict[s]
        lst = name.split(';')
        return lst[0]
    else:
        return s

def show_path(G, lst, options):
    s = ''
    n = len(lst)
    count = 0
    for i in range(n-1):
        u = lst[i]
        v = lst[i+1]
        e = G.get_edge_data(u, v)
        padding = ''
        if e['weight'] > 0:
            s += padding + '%s\t(%s,%2.2f)\t-> ' % (u, e['color'], e['weight']) + ('[%s]\n' % (e['condition']) if options.cond==True else '\n')
        else:
            s += padding + '%s\t(%s,%2.2f)\t|| ' % (u, e['color'], e['weight']) + ('[%s]\n' % (e['condition']) if options.cond==True else '\n')
        count += 4
    print(s + v)
    print('')


def k_shortest_paths(G, source, target, k, weight=None):
    return list(islice(nx.shortest_simple_paths(G, source, target, weight=weight), k))

def not_bad_line(s):
    if s.strip() == '':
        return False
    if 'WARNING' in s:
        return False
    if 'number' in s:
        return False
    if 'Need' in s:
        return False
    if 'Error' in s:
        return False
    if 'Too' in s:
        return False
    if not s.startswith('AT'): # need modification for other organisms
        return False
    return True

def build_network_from_file(fname):
    ''' build the network from the big edge file, edges.txt. '''
    MG = nx.MultiDiGraph(max_rsubset_size=1400) # maximum size of conditionR list

    max_rsize = 0
    
    f = open(fname)
    cond_list = []
    for line in f:
        line = line.strip()
        if not_bad_line(line):
            lst = line.split('\t')
            g1 = lst[0].split()[0] # target gene id
            g2 = lst[1].split()[0] # source gene id
            MG.add_node(g1)
            MG.add_node(g2)
            edge_type = lst[3] # all or mix

            condR_lst = []
            condC_lst = []
            model_fit_measure = '?'
            if len(lst) > 6:
                condR = lst[4]
                condR_lst = lst[4].split()
                condC = lst[5]
                condC_lst = lst[5].split()
                model_fit_measure = lst[6]
                if model_fit_measure == '.' and edge_type == 'mix':
                    model_fit_measure = '-1000.0'  # RNA-seq samples were selected using post.translation.3.  Search '-1000.0' in QUICKSTART.html for more detail.
                if '=' in model_fit_measure: # in early days, the log likelihood field looks like loglik=-1234.2
                    model_fit_measure = model_fit_measure.split('=')[1] # remove 'loglik='

            size_condR = len(condR_lst)
            if size_condR > max_rsize:
                max_rsize = size_condR

            create_date = '20161201' # default 2016-12-01
            if len(lst) > 7: # has date information, date information is the 8th column
                create_date = lst[7]

            metric = float(lst[8])    # appended by update_network.py
            tissue_or_method = lst[9] # appended by update_network.py

            score = float(lst[2]) # strength of various kinds of relationship.
            
            # Not sure why I distinguished 'all' and 'mix', as the add_edge statements are the same.
            if edge_type == 'all':
                if score > 0:
                    MG.add_edge(g2, g1, action='>', weight=score, metric=metric, conditionR=condR_lst, conditionC=condC_lst, rmse=model_fit_measure, edge_date=create_date, subset=tissue_or_method)
                elif score < 0:
                    MG.add_edge(g2, g1, action='X', weight=score, metric=metric, conditionR=condR_lst, conditionC=condC_lst, rmse=model_fit_measure, edge_date=create_date, subset=tissue_or_method)
            if edge_type == 'mix':
                if score > 0:
                    MG.add_edge(g2, g1, action='>', weight=score, metric=metric, conditionR=condR_lst, conditionC=condC_lst, rmse=model_fit_measure, edge_date=create_date, subset=tissue_or_method)
                elif score < 0:
                    MG.add_edge(g2, g1, action='X', weight=score, metric=metric, conditionR=condR_lst, conditionC=condC_lst, rmse=model_fit_measure, edge_date=create_date, subset=tissue_or_method)

    f.close()

    MG.graph['max_rsubset_size'] = max_rsize

    return MG


def get_value(s, delimit):
    ''' Get the value after the first delimit. '''
    lst = s.split(delimit, 1) # split by the first delimit
    return lst[1].strip()

def text_to_dict(fname, ignore_first_line=True):
    ''' fname is RNA_SEQ_INFO_DATABASE (see above).  '''
    if not os.path.exists(fname):
        print('html_network.py: you must provide %s. See parse_ena_xml.py on how to make it.' % (fname))
        sys.exit()

    d = {}
    f = open(fname)
    lines = f.readlines()
    if ignore_first_line == True:
        lines = lines[1:]
    f.close()
    for line in lines:
        line = line.strip()
        lst = line.split('\t')
        run_id = lst[0]
        d[run_id] = {}  # run_id is ENA/SRA run id
        d[run_id]['experiment_id'] = lst[2]
        if len(lst) < 5:
            continue
        d[run_id]['project_id'] = lst[4]
        d[run_id]['sample_id'] = lst[1].split('...')[0]
        d[run_id]['description'] = '\t'.join(lst[5:])
    return d

def get_true_run_id(s):
    s = s[2:] # s looks like R0SRR1548701XX, so 2 is the position of 'S'.
    index = s.find('X')
    if index >= 0:  # we don't need X
        return s[0:index]
    return s

def make_rna_seq_info_dict(fname):
    db_dict = text_to_dict(RNA_SEQ_INFO_DATABASE)
    f = open(fname)
    d = {}
    for line in f:
        line = line.strip()
        if line.startswith('@'):
            run_id = line[1:] # run_id is sth like R0SRR1548701XX
            run_id2 = get_true_run_id(run_id)
            if run_id2 in db_dict:
                d[run_id] = db_dict[run_id2]
            else:
                d[run_id] = {'project_id':'#', 'experiment_id':'#', 'sample_id':'#', 'description':'NA'}
            
    f.close()
    return d


def make_rna_seq_info_html_page(fname, d):
    f = open(fname, 'w')
    f.write('<html><head><style> body {font-family:\"HelveticaNeue-Light\", \"Helvetica Neue Light\", \"Helvetica neue\"} table {table-layout: fixed; width: 800px;}</style></head><body>')
    for k in sorted(d.keys()):
        run_link = 'http://www.ebi.ac.uk/ena/data/view/%s' % (get_true_run_id(k))
        s = '<p><a href=\"%s\" name=\'%s\'>%s</a></p>' % (run_link, k, k)
        d2 = d[k]
        s += '<table>'
        project_link    = 'http://www.ebi.ac.uk/ena/data/view/%s' % (d2['project_id'])
        experiment_link = 'http://www.ebi.ac.uk/ena/data/view/%s' % (d2['experiment_id'])
        biosample_link  = 'http://www.ebi.ac.uk/biosamples/samples/%s' % (d2['sample_id'])
        description     = d2['description']
        s += '<tr> <td><b>%s</b></td> <td><a href=\"%s\">%s</a> / <a href=\"%s\">%s</a> / <a href=\"%s\">%s</a></td>  </tr>' % ('External links', project_link, d2['project_id'], experiment_link, d2['experiment_id'], biosample_link, d2['sample_id'])
        s += '<tr> <td><b>%s</b></td> <td>%s</td> </tr>' % ('Description', description)
        s += '</table><br>\n'
        f.write(s)
    f.write('</body></html>')
    f.close()

def make_chip_seq_info_dict(fname):
    '''  See QUICKSTART.html#parameter-for-buildcmatrix '''
    f = open(fname)
    d = {}
    for line in f:
        line = line.strip()
        if line.startswith('@'):
            experiment_id = line[1:]
            d[experiment_id] = {}
        if line.startswith('PROTEIN_ID'):
            d[experiment_id]['PROTEIN_ID'] = get_value(line, ':')
        if line.startswith('PROTEIN_NAME'):
            d[experiment_id]['PROTEIN_NAME'] = get_value(line, ':')
        if line.startswith('DATA_NAME'):
            d[experiment_id]['DATA_NAME'] = get_value(line, ':')
        if line.startswith('DESCRIPTION'):
            d[experiment_id]['DESCRIPTION'] = get_value(line, ':')
        if line.startswith('LOCATION'):
            d[experiment_id]['LOCATION'] = get_value(line, ':')
        if line.startswith('NOTE'):
            d[experiment_id]['NOTE'] = get_value(line, ':')

    f.close()
    return d


def make_chip_seq_info_html_page(fname, d):
    f = open(fname, 'w')
    f.write('<html><head><style> body {font-family:\"HelveticaNeue-Light\", \"Helvetica Neue Light\", \"Helvetica neue\"} table {table-layout: fixed; width: 800px;}</style></head><body>')
    for k in sorted(d.keys()):
        s = '<p><a name=\'%s\'>%s</a></p>' % (k, k)
        d2 = d[k]
        s += '<table>'
        for k2 in sorted(d2.keys()):
            s += '<tr> <td>%s</td> <td>%s</td> </tr>' % (k2, d2[k2])
        s += '</table><br>\n'
        f.write(s)
    f.write('</body></html>')
    f.close()


def make_link_string_for_cond(s, type):
    ''' s is a string of RNA-seq IDs or ChIP IDs. '''
    lst = s.split()
    result = ''
    for x in lst:
        if type == 'rnaseq':
            path = '%s#%s' % (RNA_SEQ_INFO_HTML_PAGE, x)
        else:
            path = '%s#%s' % (CHIP_SEQ_INFO_HTML_PAGE, x)
        result += '<a href=\'%s\'>%s</a> ' % (path, x)
    return result


def get_chip_signal(s, d):
    ''' extract signal information, and return the words ordered by frequency '''    
    lst = s.split()
    result = ''
    for x in lst:
        desc = d[x]['DESCRIPTION']
        lst2 = desc.split('\t')
        for y in lst2:
            if y.startswith('SIGNAL='):
                result += ';' + y[7:] # 7 means after the '=' in 'SIGNAL='
                break
    return word_freq(result)


def get_chip_phenotype(s, d):
    ''' extract phenotype information, and return the words ordered by frequency '''
    lst = s.split()
    result = ''
    for x in lst:
        desc = d[x]['DESCRIPTION']
        lst2 = desc.split('\t')
        for y in lst2:
            if y.startswith('PHENOTYPE='):
                result += ';' + y[10:]  # 10 means after the '=' in 'PHENOTYPE='
                break
    return word_freq(result)


def word_freq(s): # for ChIP-seq data
    ''' s is string.  return a string of words ordered by frequency '''
    if s == '':
        return ''
    
    lst = s.split(';')
    d = {}
    for x in lst:
        lst2 = x.split()
        for y in lst2:
            #k = y.lower()
            k = y
            k = k.strip(',')
            k = k.strip('.')
            k = k.strip(')')
            k = k.strip('(')            
            if not k.lower() in ['at', 'in', 'to', 'with', ',', '.', ':', '-']: # exclude these words
                if not k in d:
                    d[k] = 1
                else:
                    d[k] += 1

    sorted_tuples =  sorted(d.items(), key=operator.itemgetter(1), reverse=True)
    first_items = [x[0] for x in sorted_tuples]
    return ' '.join(first_items)


def word_freq2(lst): # for RNA-seq data
    ''' s is string.  return a string of words ordered by frequency '''

    if lst == []:
        return ''
    
    d = {}
    for x in lst: # each description
        lst2 = x.split()
        for y in lst2: # each word
            k = y
            k = k.strip(',') # remove superfluous charaters, if any
            k = k.strip('.')
            k = k.strip(')')
            k = k.strip('(')
            k = k.strip(';')            
            if not k.startswith('SRR') and not k.startswith('ERR') and not k.startswith('DRR') and not k.isdigit() and not ':' in k and len(k) > 1 and not k.lower() in ['just', 'library', 'libraries',  'dna', 'nextseq', 'nextseq500', 'sequencing', 'end', 'al;', 'which', 'analyse', 'analyze', 'analyzer', 'whole-genome', 'thus', 'plant', 'plants', 'future', 'such', 'not', 'alone', 'most', 'within', 'into', 'but', 'between', 'we', 'is', 'or', 'also', 'was', 'can', 'be', 'use', 'kit', 'used', 'et', 'al', 'by', 'this', 'the', 'their', 'at', 'in', 'to', 'on', 'with', ',', '.', ':', '-', 'rna-seq', 'rnaseq', 'of', 'hiseq', 'hiseq2000', 'illumina', 'arabidopsis', 'thaliana', 'from', '<br><br>[title]', '<br><br>[description]', 'using', 'were', 'are', 'and', 'under', 'a', 'an', 'one', 'two', 'three', 'as', 'for', 'after', 'none', 'mapping', 'na', 'whole', 'chip-seq', 'paired']: # exclude these strings
                if not k in d:
                    d[k] = 1
                else:
                    d[k] += 1

    sorted_tuples =  sorted(d.items(), key=operator.itemgetter(1), reverse=True)
    first_items = [x[0] + ' (' + str(x[1]) + ')' for x in sorted_tuples]
    return '<br>'.join(first_items)


def word_freq3(lst): # for RNA-seq data, bag-of-words model
    ''' similar to word_freq2, but may be faster  '''
    if lst == []:
        return ''
    
    bow = [collections.Counter(re.findall(r'\w+', s)) for s in lst] # bag of words
    d = sum(bow, collections.Counter()) # frequency of each word
    sorted_tuples = d.most_common(len(d))
    exclude_lst = ['basis', 'requires', 'population', 'resolution', 'via', 'overall', 'elements', 'grown', 'expression', 'appears', 'total', 'have', 'here', 'of', 'just', 'type', 'transcriptomes', 'transcriptome', 'transcriptomic', 'transcription', 'transcriptional', 'report', 'during', 'diversity', 'investigated', 'library', 'per', 'libraries', '2500', '2000', '1210', '1001', '1107', 'dna', 'nextseq', 'nextseq500', 'seq', 'sequencing', 'sequencing;', 'end', 'al;', 'whereas', 'which', 'analyse', 'analyze', 'analyzer', 'quality', 'analysis', 'analyses', 'whole-genome', 'thus', 'plant', 'plants', 'future', 'such', 'not', 'alone', 'most', 'molecular', 'within', 'into', 'but', 'however', 'between', 'we', 'is', 'origin', 'or', 'also', 'was', 'can', 'be', 'been', 'use', 'kit', 'used', 'et', 'al', 'by', 'this', 'that', 'these', 'the', 'their', 'at', 'in', 'to', 'on', 'with', 'mrna', 'rna', 'rnas', 'rna-seq', 'rnaseq', 'of', 'hiseq', 'hiseq2000', 'illumina', 'arabidopsis', 'thaliana', 'from', 'roles', 'title', 'description', 'using', 'were', 'are', 'and', 'unknown', 'under', 'a', 'an', 'one', 'two', 'three', 'as', 'for', 'found', 'after', 'none', 'mapping', 'na', 'whole', 'chip-seq', 'play', 'paired', 'br', 'future', 'rowan', 'study', 'studies', 'may', 'sample', 'truseq', 'until', 'gene', 'genes', 'genetic', 'genome', 'genomes', 'units', 'its', 'yelina', 'data', 'set', 'tube', 'single-base', 'size', 'room', 'along', 'before', 'several', 'less', 'protocol', 'profiling', 'profiles', 'conditions', 'collection', 'complete', 'reveal', 'given', 'ii', 'isolated', 'described', 'describe', 'na', 'worldwide', 'accessions', 'identify', 'identification'] # exclude these words
    first_items = [x[0] + ' (' + str(x[1]) + ')' for x in sorted_tuples if x[1] > 2 and len(x[0]) > 1 and not x[0].startswith('SRR') and not x[0].startswith('ERR') and not x[0].startswith('DRR') and not x[0].isdigit() and not ':' in x[0] and not x[0].lower() in exclude_lst]
    return ' '.join(first_items)


def get_rna_signal(s, d):
    ''' extract RNA-seq signal information, and return the words ordered by frequency '''    
    lst = s.split()
    result = []
    MAX_WORDS = 60
    if lst[0] == '.': # all RNA samples
        return 'all available signals'
    for x in lst: # x is an RNA sample ID, words by frequency
        if x in d:
            desc = d[x]['description']
            desc_lst = re.split('<br>', desc)
            short_lst = []
            for x in desc_lst:
                short_lst.extend(x.split())
                if len(short_lst) > MAX_WORDS: # average english words 5.1, take the first 100 words, should be informative enough. Longer desc require more computation time.
                    short_lst = short_lst[:MAX_WORDS]
                    break
            # index = desc.find('<br>')
            # if index > 0:
            #     desc = desc[:index]
            result.append((' '.join(short_lst)).strip())
    return word_freq3(result)


def get_rna_signal2(s, d): # not very successful, and slow, so NOT used
    ''' extract RNA-seq signal information, and return the words ordered by frequency '''    

    lst = s.split()

    if lst[0] == '.': # all RNA samples
        return 'all available signals'

    text = ''
    for x in lst: # x is an RNA sample ID, words by frequency
        if x in d:
            desc = d[x]['description']
            text += desc.strip().rstrip('.') + '. '

    rake = Rake(RAKE_STOPLIST_FILE)
    keywords = rake.run(text)
    return '<br>'.join( [ t[0] + ' (' + str(int(t[1])) + ')' for t in keywords ] )


def replace_old_html_page(fname, edge_date):
    ''' If the file fname needs updating, return True.  '''
    if not os.path.exists(fname): # if the file does not exist, it needs updating
        return True

    # Check all files AT2G43790_AT1G03080_0.html,  AT2G43790_AT1G03080_1.html, AT2G43790_AT1G03080_2.html, etc.  If any of them is too old, create a new one.    
    index = fname.rfind('_') 
    if index < 0:
        print('html_network.py: %s has no underscore.' % (fname))
        sys.exit()
    fname_part = fname[:index]
    for fn in glob.glob(os.path.join(fname_part, '*.html')):
        file_date = datetime.fromtimestamp(os.path.getmtime(fn)).strftime('%Y%m%d')
        if int(edge_date) - int(file_date) > 1: # edge_date is at least 1 day newer than edge file date
            return True

    return False


def format_date(s):
    ''' s in the form of 20170419.  Return 2017-04-19 '''
    s = s.strip()
    if len(s) != 8:
        return s
    return s[0:4] + '-' + s[4:6] + '-' + s[6:]


def make_html_page_for_condition(fname, tf_name, target_name, condRstr, condCstr, edge_date, subset): # important page ***

    ### if the page already exists, and its information is up-to-date, then don't create it again (to save time)
    if REGENERATE_ALL_EDGE_FILES == 'NO' and not replace_old_html_page(fname, edge_date):
        return
    
    d3_library =  '<link href=\"./c3.min.css\" rel=\"stylesheet\" /><script src=\"./d3.min.js\"></script><script src=\"./c3.min.js\"></script><script src=\"./scatterplot.js\"></script><script src=\"./barchart.js\"></script>'
    f = open(fname, 'w')
    f.write('<html><head> %s  <style> body {font-family:\"HelveticaNeue-Light\", \"Helvetica Neue Light\", \"Helvetica neue\"} </style></head><body>' % (d3_library))

    ### RNA-seq
    f.write('<h2>RNA-seq experiments</h2>')
    part = os.path.splitext( os.path.basename(fname) )[0] # get file name without extension
    id_lst = part.split('_')
    gene1_file = os.path.join('json', id_lst[0] + '.json') # TF
    gene2_file = os.path.join('json', id_lst[1] + '.json') # target

    f.write('<p>TF is %s %s.  Target is %s %s.  Edge made on %s.  Method: %s.</p>'% (id_lst[0], '' if tf_name == id_lst[0] else tf_name, id_lst[1], '' if target_name == id_lst[1] else target_name, format_date(edge_date), subset))
    cond_lst_str = str(condRstr.split()) # insert to javascript function call code
    rnaseq_info_file = os.path.basename(RNA_SEQ_INFO_DATABASE_JSON)
    s = '<p><a id=\"myLink\" href=\"javascript:void(0);\" onclick=\"drawScatterPlot(\'%s\',\'%s\', \'%s\', %s);\">Click for gene expression scatter-plot</a></p>  <p id=\"chart\"></p>' % (gene1_file, gene2_file, rnaseq_info_file, cond_lst_str)
    f.write(s)

    global glb_rna_seq_info_dict
    #s = get_rna_signal(condRstr, glb_rna_seq_info_dict) # DISABLED since this is SLOWEST part
    # if s.startswith('all available'):
    #     f.write('<h3>Signal</h3>' + '<p>' + s + '</p>')
    # else:
    #     f.write('<h3>Signal</h3> <p>Note: words are ordered by frequency.</p>' + '<p>' + s + '</p>')
    
    # f.write('<p>%s<p>' % (make_link_string_for_cond(condRstr, 'rnaseq')))
    
    ### ChIP-seq
    f.write('<h2>ChIP-seq experiments</h2>')
    gene1_file = os.path.join('json2', id_lst[0] + '.json') # TF
    gene2_file = os.path.join('json2', id_lst[1] + '.json' ) # target
    cond_lst_str = str(condCstr.split())
    s = '<a id=\"myLink2\" href=\"javascript:void(0);\" onclick=\"drawBarChart(\'%s\',%s);\">Click for plot</a>  <p id=\"chart_bind\"></p>' % (gene2_file, cond_lst_str) # display binding strength
    f.write(s)

    global glb_chip_seq_info_dict
    s = get_chip_signal(condCstr, glb_chip_seq_info_dict)
    if s != '':
        f.write('<h3>Signal</h3> <p>Note: words are ordered by frequency.</p>' + '<p>' + s + '</p>')
    else:
        f.write('<h3>Signal</h3>' + '<p>None.</p>')

    s = get_chip_phenotype(condCstr, glb_chip_seq_info_dict)
    f.write('<h3>Phenotype</h3>' + '<p>' + s + '</p>')
    
    f.write('<p>%s</p>' % (make_link_string_for_cond(condCstr, 'chipseq')))
    f.write('</body></html>')   
    f.close()


def make_w2ui_table_page(fname, gene_str, download_str, dict_lst_regulates, dict_lst_regulatedby):
    ''' each element in dict_lst_* must have the form  {'strength': '', 'metric': '', 'geneid': '', 'genename': ''} '''
    start_part = '''
    <html>
      <head>
        <title>%s</title>
        <script src="./jquery.min.for.w2ui.js"></script>
        <script src="./w2ui.min.js"></script>
        <link rel="stylesheet" type="text/css" href="./w2ui.min.css" />    
        <script>
            $(function() {
    ''' % (
        gene_str)
    
    # the first table showing targets of a TF
    grid1 = '''
        $('#grid1').w2grid({ 
            name:'grid1', 
            header:'%s regulates',
            show:{ footer:true, toolbar:true, header:true },
            columns:[	
                { field:'recid', caption:'No.', size:'50px', sortable:true, resizable:true},
                { field:'strength', caption:'Corr', size:'150px', sortable:true, resizable:true, searchable:true },
                { field:'metric', caption:'Metric', size:'150px', sortable:true, resizable:true, searchable:true },
                { field:'geneid', caption:'Gene ID', size:'150px', sortable:true, resizable:true, searchable:true },
                { field:'genename', caption:'Gene name', size:'150px', sortable:true, resizable:true, searchable:true }
            ],
           records: 
    ''' % (
        gene_str)

    grid1 += '[\n'
    i = 1
    for d in dict_lst_regulates:
        grid1 += '    {recid:%d, strength:\'%s\', metric:\'%s\', geneid:\'%s\', genename:\'%s\'},\n' % (i, d['strength'], d['metric'], d['geneid'], d['genename'])
        i += 1
    grid1 = grid1.rstrip('\n').rstrip(',')
    grid1 += ']\n'
    grid1 += '});\n'

    # the second table showing TF's regulators
    grid2 = '''
        $('#grid2').w2grid({ 
            name:'grid2', 
            header:'%s is regulated by',
            show:{ footer:true, toolbar:true, header:true },
            columns:[	
                { field:'recid', caption:'No.', size:'50px', sortable:true, resizable:true},
                { field:'strength', caption:'Corr', size:'150px', sortable:true, resizable:true, searchable:true },
                { field:'metric', caption:'Metric', size:'150px', sortable:true, resizable:true, searchable:true },
                { field:'geneid', caption:'Gene ID', size:'150px', sortable:true, resizable:true, searchable:true },
                { field:'genename', caption:'Gene name', size:'150px', sortable:true, resizable:true, searchable:true }
            ],
           records: 
    ''' % (
        gene_str)

    grid2 += '[\n'
    i = 1
    for d in dict_lst_regulatedby:
        grid2 += '    {recid:%d, strength:\'%s\', metric:\'%s\', geneid:\'%s\', genename:\'%s\'},\n' % (i, d['strength'], d['metric'], d['geneid'], d['genename'])        
        i += 1
    grid2 = grid2.rstrip('\n').rstrip(',')
    grid2 += ']\n'
    grid2 += '});\n'

    end_part = '''
    });
        </script>
      </head>
      <body>
        <div id="grid1" style="position:absolute; left:0px; width:49.9%%; height:99%%;">regulatee table</div>
        <div id="grid2" style="position:absolute; right:0px; width:49.9%%; height:99%%;">regulator table</div>
        <br/>
        <div id="download">%s</div>
      </body>
    </html>
    ''' % (
        download_str)
    
    result = start_part + grid1 + grid2 + end_part
    # minify html 
    lst = re.split(r'\s{2,}', result)
    result = ''.join(lst)
    f = open(fname, 'w')
    f.write(result)
    f.close()


def make_html_page(node, G, fname, agi2name_dict):
    ''' Make html pages for node's successors and predecessors.  '''
    #f.write('<p><a href=%s>Go to index page</a></p>' % ('../summary.html'))
    #download_str = '<a href=\'%s\'>Download all edges</a>' % ('./edges.txt.zip') add in future
    download_str = ''
    gname = get_name(node, agi2name_dict)
    if node.strip() == gname.strip(): # id only
        gene_str = node
    else:
        gene_str = '%s' % (node + ' ' + gname)
    N = G.graph['max_rsubset_size']
    
    predecessors = G.predecessors(node)
    successors = G.successors(node)

    d1 = {}
    d2 = {}
    for n in successors:
        name = n.split()[0] + '.html'
        d = G.get_edge_data(node, n) # n is node's target
        for k in d.keys(): # can have multiple edges between two nodes
            t = d[k]['action']
            t = int(np.abs(d[k]['weight'])*10) * t # edge strength
            R = '  '.join(d[k]['conditionR'])
            C = '  '.join(d[k]['conditionC'])
            RMSE = d[k]['rmse']
            edge_date = d[k]['edge_date']
            subset = d[k]['subset']
            info_page = get_id(node) + '_' + get_id(n) + '_' + str(k) + '.html' # node is TF, n is target
            info_page_path = os.path.join(os.path.dirname(fname), info_page)
            tf_name = get_name(node, agi2name_dict)
            target_name = get_name(n, agi2name_dict)
            make_html_page_for_condition(info_page_path, tf_name, target_name, R, C, edge_date, subset)  # ***

            d1[info_page] = float(d[k]['metric'])
            display_name = n + ' ' + ('' if target_name == n else target_name)
            d2[info_page] = (t, name, display_name, RMSE)

    # order edges by strength
    regulatee_dict_lst = []
    for tpl in sorted(d1.items(), key=operator.itemgetter(1), reverse=True):        
        k = tpl[0]
        info_page = k
        t = d2[k][0]
        name = d2[k][1]
        n = d2[k][2] # display name
        RMSE = d2[k][3]
        #s1 += '<a href=\'%s\' title=\'%s\'>%s</a> <a href=\'%s\'>%s</a><br/>' % (info_page, RMSE, t.rjust(12, '_'), name, n)
        lst = n.split()
        geneid = lst[0]
        genename = '-'
        if len(lst) > 1:
            genename = lst[1]
        regulatee_dict_lst.append({'strength': '<a href=%s title=%s>%s</a>' % (info_page, RMSE, t.rjust(12, '_')), 'geneid': '<a href=%s>%s</a>' % (name, geneid), 'genename': '%s' % (genename), 'metric': '%4.2f' % (d1[k])})


    d1 = {}
    d2 = {}
    for n in predecessors:
        name = n.split()[0] + '.html'
        d = G.get_edge_data(n, node)
        for k in d.keys():
            t = d[k]['action']
            t = int(np.abs(d[k]['weight'])*10) * t # edge strength
            R = '  '.join(d[k]['conditionR'])
            C = '  '.join(d[k]['conditionC'])
            RMSE = d[k]['rmse']
            edge_date = d[k]['edge_date']
            subset = d[k]['subset']            
            info_page = get_id(n) + '_' + get_id(node) + '_' + str(k) + '.html' # n is TF, node is target
            info_page_path = os.path.join(os.path.dirname(fname), info_page)
            tf_name = get_name(n, agi2name_dict)
            target_name = get_name(node, agi2name_dict)            
            #if not os.path.exists(info_page_path):  # tf->target may already exits, if so don't need to make it again
            make_html_page_for_condition(info_page_path, tf_name, target_name, R, C, edge_date, subset)  # CHANGE ***

            d1[info_page]  = float(d[k]['metric'])
            display_name = n + ' ' + ('' if tf_name == n else tf_name)
            d2[info_page] = (t, name, display_name, RMSE)

    # order edges by strength
    regulator_dict_lst = []
    for tpl in sorted(d1.items(), key=operator.itemgetter(1), reverse=True):        
        k = tpl[0]
        info_page = k
        t = d2[k][0]
        name = d2[k][1]
        n = d2[k][2]
        RMSE = d2[k][3]
        lst = n.split()
        geneid = lst[0]
        genename = '-'
        if len(lst) > 1:
            genename = lst[1]
        regulator_dict_lst.append({'strength': '<a href=%s title=%s>%s</a>' % (info_page, RMSE, t.rjust(12, '_')), 'geneid': '<a href=%s>%s</a>' % (name, geneid), 'genename': '%s' % (genename), 'metric': '%4.2f' % (d1[k])})

    make_w2ui_table_page(fname, gene_str, download_str, regulatee_dict_lst, regulator_dict_lst) # ***


def num_lines(fname):
    ''' Return number of lines in file fname. '''
    f = open(fname)
    n = len(f.readlines())
    f.close()
    return n


## main program
parser = OptionParser()
parser.add_option('-f', '--file', dest='edge_file', help='edge file', metavar='FILE')
parser.add_option('-r', '--rnaseqinfo', dest='rna_seq_info_file', default='', help='RNA-seq information file', metavar='FILE')
parser.add_option('-c', '--chipseqinfo', dest='chip_seq_info_file', default='', help='ChIP-seq information file', metavar='FILE')
parser.add_option('-n', '--networkpara', dest='network_para_file', default='', help='Network parameter file', metavar='FILE')
parser.add_option('-i', '--includeedgetype', dest='include',  default='all', help='include edge types')
parser.add_option('-s', '--showcondition', dest='cond',  action="store_true", default=False, help='show correlated conditions')
(options, args) = parser.parse_args()

glb_param_dict = make_global_param_dict(options.network_para_file)

agi2name_dict = make_gene_name_AGI_map_dict(glb_param_dict['GENE_ID_AND_GENE_NAME'])

total_num_edges = num_lines(options.edge_file)


# Make summary.html page
G = build_network_from_file(options.edge_file)
if not os.path.isdir(DIR_NAME):
    os.makedirs(DIR_NAME)

# Make RNA-seq information page
if options.rna_seq_info_file != '':
    glb_rna_seq_info_dict = make_rna_seq_info_dict(options.rna_seq_info_file)
    make_rna_seq_info_html_page(os.path.join(DIR_NAME, RNA_SEQ_INFO_HTML_PAGE), glb_rna_seq_info_dict)

# Make ChIP-seq information page
if options.chip_seq_info_file != '':
    glb_chip_seq_info_dict = make_chip_seq_info_dict(options.chip_seq_info_file)
    make_chip_seq_info_html_page(os.path.join(DIR_NAME, CHIP_SEQ_INFO_HTML_PAGE), glb_chip_seq_info_dict)

# Fill in static index page
findex = open(INDEX_PAGE, 'w')
findex.write('<html><head><style> body {font-family:\"HelveticaNeue-Light\", \"Helvetica Neue Light\", \"Helvetica neue\"} table {table-layout: fixed; width: 800px;}</style></head><body>')
curr_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
s = '<h2>All genes considered</h2>'
s +=  '<p>Last updated at %s. A total of %d edges.</p>' % (curr_time, total_num_edges)
for n in sorted(G.nodes()): # for each node in the network, find its neighbours.
    t = n.split()[0] + '.html'
    filepath = os.path.join(DIR_NAME, t)

    successors = G.successors(n)
    predecessors = G.predecessors(n)

    s1 = ''
    for sn in successors:
        t1 = sn.split()[0] + '.html'
        filepath1 = os.path.join(DIR_NAME.split('/')[-1], t1)
        s1 += '<a href=\'%s\'>%s</a><br>' % (filepath1, sn)

    s2 = ''
    for pn in predecessors:
        t2 = pn.split()[0] + '.html'
        filepath2 = os.path.join(DIR_NAME.split('/')[-1], t2)
        s2 += '<a href=\'%s\'>%s</a><br>' % (filepath2, pn)
    
    s += '<p>Gene:<a href=\'%s\'>%s</a><br>' % (filepath, n)
    s += '<table border=1><tr><td width=400px>Regulated by %d</td><td width=400px>Regulates %d</td></tr>' % (len(predecessors), len(successors))
    s += '<tr> <td valign=\"top\">%s</td> <td valign=\"top\">%s</td></tr>' % (s2, s1)
    s += '</table>'
    s += '</p>'

    make_html_page(n, G, filepath, agi2name_dict)

findex.write(s)
findex.write('</body></html>')
findex.close()

# copy auxiliary folders and files
if os.path.isdir(JSON_DIR):
    cmd = 'cp -r %s %s' % (JSON_DIR, DIR_NAME)
    os.system(cmd)
else:
    print('[WARNING] html_network.py: Omit JSON directory (for displaying gene expression).')

if os.path.isdir(JSON_DIR2):
    cmd = 'cp -r %s %s' % (JSON_DIR2, DIR_NAME)
    os.system(cmd)
else:
    print('[WARNING] html_network.py: Omit JSON directory 2 (for displaying binding).')

if os.path.exists(RNA_SEQ_INFO_DATABASE_JSON):
    cmd = 'cp %s %s' % (RNA_SEQ_INFO_DATABASE_JSON, DIR_NAME)
    os.system(cmd)
else:
    print('[WARNING] html_network.py: %s does not exists. Scatterplots may not work properly.' % (RNA_SEQ_INFO_DATABASE_JSON))

for fname in C3_FILES:
    fpath = os.path.join(C3_DIR, fname)
    if os.path.exists(fpath):
        cmd = 'cp %s %s' % (fpath, DIR_NAME)
        os.system(cmd)
    else:
        print('[WARNING] html_network.py: Omitted %s. Scatter plot may not work without this file. ' % (fpath))

for fname in W2UI_FILES:
    fpath = os.path.join(W2UI_DIR, fname)
    if os.path.exists(fpath):
        cmd = 'cp %s %s' % (fpath, DIR_NAME)
        os.system(cmd)
    else:
        print('[WARNING] html_network.py: Omit %s. Table may not work without this file. ' % (fpath))
        
#print('html_network.py done!')