#!/usr/local/bin/perl #-wT # Neurosurgery Find a Specialist script # written by Aixian Lin # URL: http://www.stanford.edu/dept/neurosurgery/find_specialist/index.html # Program takes in user query, # Searches db by doctor's name, type of disease or disorder, disease. use CGI qw/:standard/; use strict; use lib "/afs/ir/dept/neurosurgery/cgi-bin/perl-lib/"; use CGIBook::Error; my $VIRTUAL_PATH = 'http://www.stanford.edu/dept/neurosurgery/'; my $disease_dir = '/afs/ir/dept/neurosurgery/WWW/find_specialist/dbsearch/diseases'; my $dir_doctors = '/afs/ir/dept/neurosurgery/WWW/find_specialist/dbsearch/doctors/'; my $db = '/afs/ir/dept/neurosurgery/WWW/find_specialist/dbsearch/neurodb'; my $q = new CGI; my $action = $q->param("action"); my $doctor = $q->param("doctor"); my $category = $q->param("category"); my $keyword = $q->param("keyword"); my $disease = $q->param("disease"); #----------------------tests------------ #my $doctor ="Select a specialist"; #my $category = "pain"; #my $keyword = "toys"; #my $disease = "arachnoidcyst"; #--------------------------------------- my @data = get_db($db); #print '$action is : '.$action."\n"; print $q->header( "text/html" ); if ( $action eq '' || $action eq ' ') { &start; } elsif ( $action eq "get_keyword" ) { get_keyword( $q, $keyword, $VIRTUAL_PATH, $disease_dir, $dir_doctors, $db, @data); } elsif ( $action eq "get_doctor" ) { get_doctor($q,$doctor,$db,@data); } elsif ( $action eq "get_category" ) { get_category($q, $category, $db, @data); } elsif ( $action eq "get_description" ) { get_description($q, $disease, $db,$disease_dir,@data); } else { &redirectweb; } ######################## start running #################### sub start{ #my( $q, $keyword, $category, $doctor, @data ) = @_; if ($keyword ne "" && $keyword ne " ") { get_keyword( $q, $keyword, $VIRTUAL_PATH, $disease_dir, $dir_doctors, $db, @data); } elsif ($doctor ne "Select a doctor") { get_doctor($q, $doctor, $db, @data); } elsif ($category ne "Select a category") { #print "---------how can this bw----------"; get_category($q, $category, $db, @data); } else { &redirectweb; # &printresult; } } ####################### get description ################## sub get_description(){ my ($q, $disease, $db,$disease_dir,@data) = @_; my $dis_column = 0; &faculty_header; &print_title($q, $dis_column, $disease, @data); &match_desc($q, $disease, $db,$disease_dir,@data); print $q->br; print $q->br; print $q->br; &faculty_footer; } ########################### match_desc for get_description ########################## sub match_desc() { my ($q, $disease, $db, $disease_dir, @data) = @_; my ($r, $d, $descdoc, $row_num, $cured, $idx, $descdoc_pos, $first_doc_col, $doc_num); my $desc_column = 2; my $dis_column = 0; $cured = strip($disease); $row_num = scalar(@data); $first_doc_col = 4; $doc_num=countDr(); #last column is 6 which is index < 7 my $last_col = ($first_doc_col + $doc_num); # print " ------cured is $cured --------"; #go through all the rows to find dis match for ($r=0; $r<$row_num; $r++) { #init disease var $d = $data[$r][$dis_column]; $d = strip($d); # print "match found ------d is $d --------"; #check if input matches disease if ($cured eq $d) { #find descript file of the row my $descript = $data[$r][$desc_column]; # print "\nmatch found ------ data is $data[$r][$desc_column]--------\n"; $descript =~ s/^\s*(.*?)\s*$/$1/; # print "\nmatch found ------descript is $descript--------\n"; # print "\n------descript should be $data[$r][2]--------\n"; # print "\n------desc_column should be $desc_column-----\n"; #### get disease descript &print_descripts($descript, $disease_dir); my $inx; #get all the doctors marked with x for ($inx=$first_doc_col; $inx<$last_col; $inx++) { my $desc_doc = $data[0][$inx]; my $descdoc_pos = $data[$r][$inx]; if ($descdoc_pos =~ /x/) { #### print doctor's url &print_dr_url($q,$desc_doc); #&print_dr_url; } } } } } # end sub #################### print dr url ########## sub print_dr_url() { my ($q,$desc_doc) = @_; my ($doc_url); $desc_doc =~ s/:/,/g; $doc_url = strip($desc_doc); print $q->a( { -href => "http://cgi.stanford.edu/dept/neurosurgery/cgi-bin/find_specialist?action=get_doctor&doctor=$doc_url" }, "$desc_doc" ); print $q->br; } ######################## print descripts of page ################# sub print_descripts { my ($descript, $disease_dir) = @_; my ($desc_content); my $filename = "$disease_dir/$descript"; open(DISEASEDIR, "$filename") || die "cannot find descript file"; while () { print $q->p("$_"); } close(DISEASEDIR); #print $q->p("$desc_content"); #print "file name is $filename-------"; #open(DISEASEDIR, "$filename") || die "cannot find descript file"; #$desc_content = ; #close(DISEASEDIR); #print $q->p("$desc_content"); } ####################### get doctor ################## sub get_doctor() { my($q,$doctor,$db,@data) = @_; my @firstline = &get_first_line($db); my $totalfields = @firstline; my $clean_doc = strip($doctor); my ($header, $newheader); #print "\nfirstline: @firstline\n"; #print "\ncleandoc: $clean_doc\n"; for($a=0; $a<$totalfields; $a++){ $header = $data[0][$a]; $newheader = &strip($header); #print "\nnew header is: $newheader\n"; if ($clean_doc eq $newheader) { $doctor = $header; $doctor =~ s/:/,/g; &faculty_header; &match_doc($q, $dir_doctors, $db, $doctor, @data); print $q->br; print $q->br; print $q->br; &faculty_footer; } } } ######## generates doctor profile page ######## ######## figure out to match more words, ######## ######## ex glioma vs malignant glioma !!!!!!!!!!!!### ############ match $disease for doc profile page ################# sub match_doc() { my ($q, $dir_doctors, $db, $doctor, @data) = @_; my $doc_file = &trimDocName($dir_doctors,$doctor); my $specialty_file = "$dir_doctors/$doc_file.txt"; my $doctor_profile = "$dir_doctors/$doc_file.html"; my ( @bio, $bio, @spec, $spec, @newspec, @specialties, @diseases, $specialty, $cleaned_dis, $spec_dis) ; ####### open doctor specialty and bio files for process ####### open(BIO, $doctor_profile) || die "cannot open bio file, fiel is $doctor_profile"; @bio = ; $bio = join '', @bio; close (BIO); open(SPEC, $specialty_file) || die "cannot open specialty file"; @spec=; close (SPEC); chomp @spec; print "$bio"; ###### get the lines without the extra newline foreach $spec (@spec) { push(@newspec, $spec) if $spec; } @specialties = split(/,/, $newspec[1]); @diseases = split(/,/, $newspec[3]); print $q->b( "Doctor's Speciality:"); print $q->br; my $found_specialty; my $found_disease; my $cleaned_spec; foreach $specialty(@specialties) { ####### match dr's specialty or disease to db file; ############### ####### prints link if specialty is found, else print text############# ####### returned string is trimmed using db values column 1 or 3############# $cleaned_spec = $specialty; $cleaned_spec = strip($cleaned_spec); $found_specialty = &match_specialty($db,$cleaned_spec,@data); if ($found_specialty ne '') { print $q->a( { -href => "http://cgi.stanford.edu/dept/neurosurgery/cgi-bin/find_specialist?action=get_category&category=$found_specialty" }, "$specialty" ); print $q->br; } else { $found_disease = &match_disease($db,$cleaned_spec, @data); if ($found_disease ne '') { print $q->a( { -href => "http://cgi.stanford.edu/dept/neurosurgery/cgi-bin/find_specialist?action=get_description&disease=$found_disease" }, "$specialty" ); print $q->br; } else { print "$specialty"; print $q->br; } } } print '

'; print 'Diseases Doctor Treats:
'; print "\n"; my $found_disease2; my $found_specialty2; foreach $spec_dis(@diseases) { $cleaned_dis = $spec_dis; $cleaned_dis = &strip($cleaned_dis); $found_disease2 = &match_disease($db,$cleaned_dis,@data); if ($found_disease2 ne ''){ print $q->a( { -href => "http://cgi.stanford.edu/dept/neurosurgery/cgi-bin/find_specialist?action=get_description&disease=$found_disease2" }, "$spec_dis" ); print $q->br; } else { $found_disease2 = &match_specialty($db,$cleaned_dis,@data); if ($found_disease2 ne '') { print $q->a( { -href => "http://cgi.stanford.edu/dept/neurosurgery/cgi-bin/find_specialist?action=get_category&category=$found_disease2" }, "$spec_dis" ); print $q->br; } else { print "$spec_dis"; print $q->br; } } } }#end match_doc ########### strip querry string to match doctor file names ##### sub trimDocName { my ($dir_doctors, $qdoc) = @_; my ($file, $querrydoc); #print "my doc name is $qdoc\n"; $qdoc =lc($qdoc); $qdoc =~tr/,//d; my @qdoc = split(/\s/, $qdoc); my $ext = ".txt"; opendir(DOCDIR, $dir_doctors); my @doc = readdir(DOCDIR); foreach $querrydoc (@qdoc) { foreach $file (@doc) { if ($file eq $querrydoc.$ext) { #print "=====match found: doctor is @qdoctor, file is $file\n"; #print "\$querrydoc is $querrydoc\n"; return $querrydoc; } } } closedir(DOCDIR); } #################### get first line of 2d array ############333 sub get_first_line { my ($db) = @_; my (@file); open (DB2, "$db") || die "cannot find neuro db"; @file = ; close(DB2); my @header_row = split(/,/, $file[0]); return @header_row; } ########## match $specialty for doc profile page ################# sub match_specialty(){ my ($db,$cleaned_spec, @data) = @_; my ($i, $link, $row_num); my $temp =''; $row_num = scalar(@data); for ($i=0; $i<$row_num; $i++) { $temp = $data[$i][3]; $temp = strip($temp); ##do exact match for specialty if ($cleaned_spec eq $temp || $cleaned_spec eq $temp."s" || $cleaned_spec."s" eq $temp) { return $temp;} } } ############ match $disease for doc profile page ################# sub match_disease(){ my ($db,$cleaned_dis,@data) = @_; my ($k, $link, $row_num); my $tempdis =''; $row_num = scalar(@data); for ($k=0; $k<$row_num; $k++) { $tempdis= $data[$k][0]; $tempdis= strip($tempdis); #print "\n------------------cleaned dis in sub is $cleaned_dis------\n"; #print "\n------------------tempdis in sub is $tempdis------\n"; ##do exact match for specialty if ( $cleaned_dis eq $tempdis || $cleaned_dis eq $tempdis."s" || $cleaned_dis."s" eq $tempdis) { return $tempdis; } } } ######################## get keyword #################### sub get_keyword(){ my( $q, $key, $VIRTUAL_PATH, $disease_dir, $dir_doctors, $db, @data) = @_; unless ( defined $keyword and length $keyword ) { error( $q, "Please specify a valid query!" ); } $keyword = quotemeta( $key ); #my @data = &get_db($db); my $results = search( $q, $keyword, $db, $disease_dir,@data ); my $results_syn = &match_synonym($db,$keyword,@data); &faculty_header; print $q->h2( "Search for: $key" ); if (defined $results || defined $results_syn) { print $q->ul( $results ); print $q->ul( $results_syn ); } else { print $q->ul("No matches found"); } #print $q->ul( $results || "No matches found" ); #print $q->ul( $results_syn ); print $q->br; print $q->br; &faculty_footer; } #################### search ######################## sub search { my( $q, $keyword,$db,$disease_dir,@data ) = @_; my( %matches, @files, @sorted_paths, $results, @searchdis ); my ($file, $filename, $searchdis); local( *DIR, *FILE ); opendir DIR, $disease_dir or error( $q, "Cannot access search dir!" ); @files = grep { -T "$disease_dir/$_" } readdir DIR; closedir DIR; #print "\n==========file is @files===================\n"; foreach $file ( @files ) { my $full_path = "$disease_dir/$file"; open FILE, $full_path or error( $q, "Cannot process $file!" ); while ( ) { if ( /$keyword/io ) { $_ = html_escape( $_ ); s|$keyword|$1|gio; push @{ $matches{$full_path}{content} }, $_; $matches{$full_path}{file} = $file; ####----------get links for discript files---------------------### $matches{$full_path}{link} = &match_descript($db,$file,@data); #print $q->p("--------- synlink is $matches{$full_path}{synlink} -----------"); ####-----------------------------------------------------------### $matches{$full_path}{num_matches}++; } } close FILE; } @sorted_paths = sort { $matches{$b}{num_matches} <=> $matches{$a}{num_matches} || $a cmp $b } keys %matches; my $full_path; foreach $full_path ( @sorted_paths ) { my $file = $matches{$full_path}{file}; my $num_matches = $matches{$full_path}{num_matches}; #print $q->p("--------- file is $file-----------"); my $desclink = $matches{$full_path}{link}; my $synlink = $matches{$full_path}{synlink}; #print $q->p("--------- num matches is $num_matches-----------"); my $link = $q->a( { -href => "http://cgi.stanford.edu/dept/neurosurgery/cgi-bin/find_specialist?action=get_description&disease=$desclink" }, $file ); #my $content = join $q->br, @{ $matches{$full_path}{content} }; $results .= $q->p( $q->b( $link ) . " ( matches: $num_matches)" ); } return $results; } ######################## return keyword links for synonyms ################### sub match_synonym() { my ($db, $keyword, @data) = @_; my ($k, $row_num, $tempsyn, $tempkey, $dis ); my $syn_column = 1; my $dis_column = 0; my $desc_column = 2; my $desc_file; #print $q->p("--------- db is $db -----------"); #print $q->p("--------- keyword is $keyword -----------"); #print $q->p("--------- -----------"); $tempkey = strip($keyword); $row_num = scalar(@data); for ($k=0; $k<$row_num; $k++) { $tempsyn = $data[$k][$syn_column]; $tempsyn = strip($tempsyn); if ( $tempsyn =~ /$tempkey/) { $dis = $data[$k][$dis_column]; $dis = &strip($dis); $desc_file = $data[$k][$desc_column]; my $synlink = $q->a( { -href => "http://cgi.stanford.edu/dept/neurosurgery/cgi-bin/find_specialist?action=get_description&disease=$dis" }, $desc_file ); my $synresults .= $q->p( $q->b( $synlink ) . " ( matches: 1)" ); return $synresults; } } } ######################## don't touch ############### sub html_escape { my( $text ) = @_; $text =~ s/&/&/g; $text =~ s//>/g; return $text; } ######################## return keyword links for get_descripts ################### sub match_descript(){ my ($db,$file,@data) = @_; my ($k, $row_num, $tempdesc, $tempfile, $dis); my $desc_column = 2; my $dis_column =0; $tempfile = strip($file); $row_num = scalar(@data); for ($k=0; $k<$row_num; $k++) { $tempdesc = $data[$k][$desc_column]; $tempdesc = strip($tempdesc); if ( $tempdesc eq $tempfile) { $dis = $data[$k][$dis_column]; $dis = &strip($dis); return $dis; } } } ############# get category stuff ##################### sub get_category(){ my ($q, $category, $db, @data) = @_; # get list of diseases from db under category my $cat_column = 3; &faculty_header; &print_title($q, $cat_column, $category, @data); &match_cat($q,$category, $db,@data); print $q->br; print $q->br; &faculty_footer; } ######################### match category ############### sub match_cat() { my ($q, $category, $db, @data) = @_; my ($n, $link, $row_num, $cleaned_cat, $inx, $doc, $first_doc_col); my (@docs, $doc_num, $last_col, $cat, $dis, $dislink, $doc_pos, $doclink); #my $cat =''; $cleaned_cat = strip($category); $row_num = scalar(@data); $first_doc_col = 4; $doc_num=countDr(); #last column is 6 which is index < 7 $last_col = ($first_doc_col + $doc_num); print ''; print "\n"; #go through all the rows to find cat match for ($n=0; $n<$row_num; $n++) { #init cat var $cat = $data[$n][3]; $cat = strip($cat); #check if input matches cat if ($cleaned_cat eq $cat) { print ''; } } print '
'; #find disease of the row $dis = $data[$n][0]; #### get disease descript $dislink = strip($dis); print $q->a( { -href => "http://cgi.stanford.edu/dept/neurosurgery/cgi-bin/find_specialist?action=get_description&disease=$dislink" }, "$dis" ); print "\n"; print ''; print "\n"; #get all the doctors marked with x for ($inx=$first_doc_col; $inx<$last_col; $inx++) { $doc = $data[0][$inx]; $doc_pos = $data[$n][$inx]; if ($doc_pos =~ /x/) { #### get doctor's name $doc =~ s/:/,/g; $doclink = strip($doc); print $q->a( { -href => "http://cgi.stanford.edu/dept/neurosurgery/cgi-bin/find_specialist?action=get_doctor&doctor=$doclink" }, "$doc" ); print $q->br; } } print "\n"; print '
'; } #################### count num of doctors ################ sub countDr { #my ($db) = @_; my (@file); open (DB2, "$db") || die "cannot find neuro db"; @file = ; close(DB2); #splits each line by newline, figure out how many doctors my @header_row = split(/,/, $file[0]); my $field_num = @header_row; ######## there are 4 fields bef doctors ####### my $doc_num = ($field_num - 4); return $doc_num; } ################## strips off funny characters, spaces, dots ########### sub strip() { my ($word) = @_ ; ## trailing, leading and spaces in between $word =~ s/^\s*(.*?)\s*$/$1/; $word =~ tr/ //d; ## get alphanumeric only $word =~ s/[^\w ]//g; $word =~ /([\w ]+)/; ## lower case $word = lc($word); $word =~ s/\n//g; $word =~ s/\^//g; return $word; } ####################### open get and return db ################# sub get_db() { my ($db) = @_; my ($row, $line, $i, $j, $disease, $descripts, $category, $doctor, $qdoctor); my (@line, @file, @data, @doctors); ###print "neurodb is at $db\n"; open(DB, $db) || die "cannot find neuro db"; @file = ; close(DB); #splits each line by newline, figure out how many doctors my @header_row = split(/,/, $file[0]); my $field_num = @header_row; my $doc_num = ($field_num - 3); $row = 0; foreach $line (@file) { chomp $line; ($disease, $descripts, $category, @doctors) = split(/,/, $line); #print "$disease, $descripts, $category, @doctors\n"; $data[$row][0] = $disease; $data[$row][1] = $descripts; $data[$row][2] = $category; # init doctors list, doctors could increase; $i=3; #start from column 4 (index 3) foreach $doctor (@doctors) { $data[$row][$i] = $doctor; $i++; } $row++; } return @data; } ########################## format html top ######################### sub faculty_header { ##### so that netscape won't screw up...#### #print $q->header; print <<'FAC_HEA'; Neurosurgery: Find a specialist
Contact Us Search Site Site Map Intranet
Department of Neurosurgery Stanford University
Find a Specialist Get a Referral
About Our Department
Information for Patients
Referring Physicians
Residency and Fellowships
Research Programs

Find a specialist

FAC_HEA } ######################## print title of page ################# sub print_title{ my ($q,$column,$name, @data) = @_; #my ($name, @data) = @_; my ($n, $c_cat, $catvar, $newcat, $c, @temp); $c_cat = &strip($name); my $row_num = scalar(@data); my $first_doc_col = 4; my $doc_num=countDr(); #last column is 6 which is index < 7 my $last_col = ($first_doc_col + $doc_num); #go through all the rows to find cat match for ($n=0; $n<$row_num; $n++) { #init cat var $catvar = $data[$n][$column]; $newcat = &strip($catvar); #check if input matches cat if ($c_cat eq $newcat) { push(@temp, $catvar); } } print $q->h2("$temp[0]"); } ########################## format html bottom ######################### sub faculty_footer { print <<"FAC_FOO";
FAC_FOO } ####################### select something ################## sub redirectweb{ print "Content-type: text\/html\n\n"; print <<"WEB";

please make a selection or enter a keyword. WEB } __END__
dr is $doctor
cat is $category
dis is $disease
key is $keyword
action is $action