A Pipeline for Automated Phenotype Extraction from Medical Reports using Large Language Models
Abstract
Replication material for the paper "A Pipeline for Automated Phenotype Extraction from Medical Reports using Large Language Models"
Full text
✦ 1 / 4 ✦ C l i n ica l R e port P he notyp e E xtr ac t i on P i p e l i n e T hi s r e pos i tory c ont ai ns a s e t o f s c r i pts to b u i l d a n e n d - to - e n d p i p e l i n e f or pro ce ss i n g c l i n ica l r e ports , e xtr ac t i n g stru c tur ed i n f orm a t i on w i t h LLM s , a n d m a pp i n g p he notyp e s to t he H um a n P he notyp e O ntolo g y ( HPO ). T he m ai n ca p abi l i t ie s a r e : OCR + tr a nsl a t i on o f PDF c l i n ica l r e ports . T e xt fi lt e r i n g to kee p only p a t ie nt - r e l e v a nt i n f orm a t i on . P he notyp e e xtr ac t i on us i n g a n LLM ( v ia O ll a m a ). M a pp i n g e xtr ac t ed p he notyp e s to HPO t e rms us i n g e m beddi n g s a n d f uzzy m a t chi n g . E xtr ac t i on o f diag nos i s c o de s f rom r e ports us i n g OMIM . A highe r - l e v e l p i p e l i n e s c r i pt to pro ce ss .docx r e ports e n d to e n d . R e pos i tory S tru c tur e . ├── Pipeline_Script.sh ├── start_ollama.sh ├── DataExtractorPipeline.py ├── PDFTranslator.py ├── PDFTextExtractor.py ├── PhenotypeExtractor.py ├── HPOMapper.py ├── WordTextExtractor.py ├── InfoExtractor.py ├── DiagnosisExtractor.py ├── utils.py ├── concepts/ ├── prompts_and_config/ └── OutputFiles/ R e qu i r e m e nts P yt h on 3.9+ O ll a m a i nst a ll ed a n d runn i n g T e ss e r ac t OCR P yt h on p ackage s : deep_translator==1.11.4 docx==0.2.4 Flask==3.1.2 fuzzywuzzy==0.18.0 InquirerPy==0.3.4
✦ 2 / 4 ✦ json_repair==0.48.0 matplotlib==3.10.8 more_itertools==10.8.0 numpy==2.3.5 ollama==0.6.1 pandas==2.3.3 pdf2image==1.17.0 pronto==2.7.0 psycopg2==2.9.11 PyJWT==2.10.1 pytesseract==0.3.13 python_Levenshtein==0.27.3 Requests==2.32.5 scikit_learn==1.8.0 torch==2.8.0 transformers==4.55.0 S t a rt i n g t he LLM B acke n d ./start_ollama.sh # or ollama serve I n our e xp e r i m e nts , w e us e t he gpt-oss:120b mo de l , w hich i s a v ai l ab l e on O ll a m a H u b a n d , ba s ed on our pr e l i m i n a ry t e sts , p e r f orms w e ll f or p he notyp e e xtr ac t i on t a s k s . M ai n P yt h on mo d ul e s I n t he f ollow i n g , w e r e port t he m ai n mo d ul e s t ha t ca n be us ed i n de p e n de ntly or a s p a rt o f t he e n d - to - e n d p i p e l i n e , a n d w e r e port t hei r m ai n i nputs a n d outputs . PDFTranslator.py : T r a nsl a t e s a n d e xtr ac ts t e xt f rom PDF c l i n ica l r e ports us i n g OCR . M ai n i nputs : PDF fi l e p a t h P rompt fi l e f or fi l e tr a nsl a t i on ( b y defa ult prompts_and_config/translation_prompt.txt ) M ai n outputs : T r a nsl a t ed t e xt fi l e ( b y defa ult i n OutputFiles/ClinicalReport_TRANSLATED.txt ) PDFTextExtractor.py : F i lt e rs t e xt f rom PDF fi l e s b y r e mov i n g non - p a t ie nt - r e l a t ed i n f orm a t i on . M ai n i nputs : T r a nsl a t ed t e xt fi l e ( output o f PDFTranslator.py ) M ai n outputs : F i lt e r ed t e xt fi l e ( b y defa ult i n OutputFiles/ClinicalReport_FILTERED.txt ) PhenotypeExtractor.py : E xtr ac ts p he notyp e s f rom t e xt us i n g a n LLM . M ai n i nputs :
✦ 3 / 4 ✦ T e xt fi l e w i t h c l i n ica l r e port ( b y defa ult i n OutputFiles/ClinicalReport_FILTERED.txt ) P rompt fi l e f or p he notyp e e xtr ac t i on ( b y defa ult prompts_and_config/phenotypes_prompt.txt ) M ai n outputs : JSON fi l e w i t h e xtr ac t ed p he notyp e s ( b y defa ult i n OutputFiles/phenotypes.json ) HPOMapper.py : M a ps e xtr ac t ed p he notyp e s to HPO t e rms us i n g e m beddi n g s a n d f uzzy m a t chi n g . M ai n i nputs : JSON fi l e w i t h e xtr ac t ed p he notyp e s ( output o f PhenotypeExtractor.py ). I f not prov ided , t he i nput fi l e i s a ssum ed to be OutputFiles/phenotypes.json M ai n outputs : JSON fi l e w i t h m a pp ed HPO t e rms ( b y defa ult i n OutputFiles/phenotypes_with_score.json ) DataExtractorPipeline.py : H igh - l e v e l p i p e l i n e to pro ce ss .docx r e ports e n d to e n d . M ai n i nputs : t he p a t h to a .docx fi l e or f ol de r c ont ai n i n g mult i pl e .docx fi l e s M ai n outputs : JSON fi l e s w i t h e xtr ac t ed p he notyp e s a n d m a pp ed HPO t e rms ( b y defa ult i n OutputFiles/ ), w i t h n a m e < ca s e _ id >_ p he notyp e s . j son T yp ica l W or kf lows 1. PDF → P he notyp e s → HPO T hi s s c r i pt pro ce ss e s a c l i n ica l r e port i n PDF f orm a t , e xtr ac ts p he notyp e s us i n g a n LLM , a n d m a ps t he m to HPO t e rms . I t r e qu i r e s two a r g um e nts : t he p a t h to t he c l i n ica l r e port PDF a n d t he n a m e o f t he O ll a m a mo de l to us e . ./Pipeline_Script.sh <clinical_report.pdf> <ollama_model> 2. DOC X E n d - to - E n d P i p e l i n e T hi s p i p e l i n e pro ce ss e s ge n e t ic r e ports i n .docx f orm a t e n d to e n d . I t i n c lu de s t e xt e xtr ac t i on , p he notyp e e xtr ac t i on , a n d HPO m a pp i n g . T o m ake i t wor k , i t i s n eeded to c orr ec tly fi ll t he prompts_and_config/word_sections.json fi l e w i t h t he s ec t i ons pr e s e nt i n t he r e ports . python DataExtractorPipeline.py -i <file_or_folder> -m <ollama_model> 3. T e xt → P he notyp e s → HPO
✦ 4 / 4 ✦ T hi s p i p e l i n e ca n be us ed f or t e st i n g purpos e s , to e xtr ac t p he notyp e s f rom pl ai n t e xt fi l e s a n d m a p t he m to HPO t e rms . I t r e qu i r e s t e xt to be a lr ead y i n E n g l i s h . python PhenotypeExtractor.py -f <txt_file_path> -m <ollama_model> -p <phenotype_prompt_file> python HPOMapper.py -f <json_file_path> N ot e t ha t t he p he notyp e prompt fi l e ca n be t he on e w e prov ide i n prompts_and_config/phenotypes_prompt.txt or a c ustom on e . T he JSON fi l e f or HPO m a pp i n g s h oul d be t he output o f t he p he notyp e e xtr ac t i on st e p . 4. I n di v id u a l S c r i pts E ach s c r i pt ca n be run i n de p e n de ntly ( PDF T r a nsl a tor , PDF T e xt E xtr ac tor , P he notyp e E xtr ac tor , HPOM a pp e r , e t c .) C on fig ur a t i on F i l e s prompts _ a n d _ c on fig / p he notyp e s _ prompt . txt prompts _ a n d _ c on fig / wor d _ s ec t i ons . j son c on ce pts / h po _ c on ce pts _ fi lt e r ed . j son c on ce pts / m i m T i tl e s . txt L o ggi n g a n d O utput A ll i nt e rm edia t e a n d fi n a l outputs a r e wr i tt e n to OutputFiles/ , if no diffe r e nt p a t h i s gi v e n .