반응형

 

import pandas as pd

df = pd.DataFrame()

#make dataframe from dictionary
tmp_df = pd.DataFrame([foo_dic], index=id)

#sum data by raw
total_count = tmp_df.sum(axis=1)[0]
tmp_df = tmp_df.div(total_count)

#concat multiple dataframe parellel
concat_df = pd.concat([df1,df2],axis=1).fillna(0)

#specific columns contain letter 'test'
df = df.loc[:,df.columns.str.contains('test', regex=True)]

#merge dataframe consider index
merge_df = pd.merge(df1, df2, left_index=True, right_index=True)
반응형

'Computer Science > python' 카테고리의 다른 글

python 설치  (0) 2022.04.06
Progress bar 모듈 tqdm  (0) 2022.03.07
logging 모듈 사용하기  (0) 2022.02.17
f-string을 활용한 regex 사용법  (0) 2022.02.15
Primer 서열 분석을 위한 python 코드  (0) 2021.08.17
반응형

log 파일 작성 모듈 logging.

 

import logging

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('[%(levelname)s %(asctime)s] %(message)s',"%Y-%m-%d %H:%M:%S")

stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
stream_handler.setLevel(logging.INFO)
logger.addHandler(stream_handler)

file_handler = logging.FileHandler(f'my.log')
file_handler.setFormatter(formatter)
file_handler.setLevel(logging.DEBUG)
logger.addHandler(file_handler)

logging.info(f'Read Database File')
logging.debug(f'Read Database File')

 

handler를 여러 개 만들어서 하나는 stdout 다른 하나는 my.log 파일로 만들고 level에 따라 출력 범위를 다르게 조절한다.

 

위의 예시에서는 'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL', 중에 debug는 파일로만 생성되도록 설정되었다.

 

개발 단계에서는 stream_handler를 DEBUG로 놓고 진행하다가 개발 완료시 INFO로 수정하면 원하는 부분만 출력하도록 조정 가능하다.

 

a = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT).communicate()[0].decode('UTF-8')
logging.debug(f'{cmd}\n{a}')

subprocess와 연결해서 command를 입력하고 나오는 출력물까지 debug로 한 번에 연결 할 수 있다.

반응형

'Computer Science > python' 카테고리의 다른 글

Progress bar 모듈 tqdm  (0) 2022.03.07
pandas 활용하기  (0) 2022.02.18
f-string을 활용한 regex 사용법  (0) 2022.02.15
Primer 서열 분석을 위한 python 코드  (0) 2021.08.17
String Format으로 길이 고정하기  (0) 2020.06.24
반응형

 

read 서열에서 error를 1이하로 허용하는 내에 BESTMATCH를 찾아 시작과 종료지점 그리고 매치되는 서열을 확인하는 코드. error는 mismatch, insertion, deletion을 의미한다.

 

import regex

primer_seq, read_seq

regex_primer_seq = fr'({primer_seq}{{e<=1}})'
match_object = regex.search(regex_primer_seq, read_seq, regex.BESTMATCH)

match_start, match_end = match_object.span()
match_seq = match_object.captures()
반응형

'Computer Science > python' 카테고리의 다른 글

pandas 활용하기  (0) 2022.02.18
logging 모듈 사용하기  (0) 2022.02.17
Primer 서열 분석을 위한 python 코드  (0) 2021.08.17
String Format으로 길이 고정하기  (0) 2020.06.24
python multi-level argparse  (0) 2019.07.12
반응형

anaconda의 버전 업데이트 후 아래와 같은 에러 발생

Collecting package metadata (current_repodata.json): failed

ProxyError: Conda cannot proceed due to an error in your proxy configuration.
Check for typos and other configuration errors in any '.netrc' file in your home directory,
any environment variables ending in '_PROXY', and any other system-wide proxy
configuration settings.

 

https://github.com/conda/conda/issues/9497 페이지에서 확인해보니 urllib3 의 버전 변경으로 인한 문제로 보임.

 

conda config --set ssl_verify False

위 명령어를 작성하면 .condarc 파일이 home directory 밑에 생성되는데 파일을 아래와 같이 수정한다

 

ssl_verify: False
proxy_servers:
  https: http://[PROXY]:[PORT]/

 

https: https: 에서 https: http로의 변경.

반응형

'Computer Science > linux' 카테고리의 다른 글

centos 8에 slurm 설치하기  (0) 2022.05.11
conda 채널 추가  (0) 2022.02.22
Jupyter notebook 설정  (0) 2020.11.03
Centos yum repo 변경  (0) 2020.08.03
github로 스크립트 관리하기 AtoZ  (0) 2020.07.29
반응형

NGS 데이터에서 adapter 서열과 primer 서열에 따른 read 분류.

 

아래는 pseudo code 이므로 적절한 변환이 필요하다.

import regex

#ambiguous base list
ambiguous_base_dic = {"N":"ATGC","R":"AG","Y":"TC","K":"GT","M":"AC","S":"GC","W":"AT","B":"CGT","D":"AGT","H":"ACT","V":"ACG"}

#if ambiguous base occured, change to regular expression format
for word, initial in ambiguous_dic.items():
	primer_f = primer_f.replace(word, "["+initial+"]")
    
#index sequences are allowed one mismatch
primer = r"(^{0})".format(primer_f)+"{e<=1}"

#if primer sequence is inside sequence, print out.
if regex.findall(primer, sequence):
	print(sequence)

 

 

 

반응형

'Computer Science > python' 카테고리의 다른 글

logging 모듈 사용하기  (0) 2022.02.17
f-string을 활용한 regex 사용법  (0) 2022.02.15
String Format으로 길이 고정하기  (0) 2020.06.24
python multi-level argparse  (0) 2019.07.12
python 파일 입출력  (0) 2019.07.12

+ Recent posts