如何使用python读取和修改(.gct)文件?

2024-06-06 20:00:23 发布

您现在位置:Python中文网/ 问答频道 /正文

哪些库可以帮助我读取python中的gct文件,并像删除带有NaN值的行一样对其进行编辑。如果我将下面的代码应用于.gct文件,它将如何更改

data = pd.read_csv('PAAD1.csv')
new_data = data.dropna(axis = 0, how ='any')
print("Old data frame length:", len(data), "\nNew data frame length:",  
       len(new_data), "\nNumber of rows with at least 1 NA value: ", 
       (len(data)-len(new_data)))
new_data.to_csv('EditedPAAD.csv')

Tags: 文件csv代码编辑newreaddatalen
2条回答

谷歌快速搜索将为您提供以下信息: https://pypi.org/project/cmapPy/

关于代码,如果您不关心前两行中的元数据,这似乎符合您的目的,但是您应该首先指出分隔符是TAB,并跳过前两行-pandas.read_csv(PATH_TO_GCT_FILE, sep='\t',skiprows=2)

您应该为此使用^{}包。与read_csv相比,它提供了更多的自由度和特定于域的实用程序。例如,如果你的*.gct看起来像这样

#1.2            
22215   2       
Name    Description Tumor_One   Normal_One
1007_s_at   na  -0.214548   -0.18069
1053_at "RFC2 : replication factor C (activator 1) 2, 40kDa |@RFC2|"    0.868853    -1.330921
117_at  na  1.124814    0.933021
121_at  PAX8 : paired box gene 8 |@PAX8|    -0.825381   0.102078
1255_g_at   GUCA1A : guanylate cyclase activator 1A (retina) |@GUCA1A|  -0.734896   -0.184104
1294_at UBE1L : ubiquitin-activating enzyme E1-like |@UBE1L|    -0.366741   -1.209838
1316_at "THRA : thyroid hormone receptor, alpha (erythroblastic leukemia viral (v-erb-a) oncogene homolog, avian) |@THRA|"  -0.126108   1.486972
1320_at "PTPN21 : protein tyrosine phosphatase, non-receptor type 21 |@PTPN21|" 3.083681    -0.086705
...

您只能提取具有所需probeset id(行id)的行,例如['1007_s_at', '1053_at', '117_at', '121_at', '1255_g_at', '1294_at UBE1L']

因此,要读取文件,请删除description中的nan并再次保存,请执行以下操作:

from cmapPy.pandasGEXpress.parse_gct import parse
from cmapPy.pandasGEXpress.write_gct import write

data = parse('example.gct', rid=['1007_s_at', '1053_at',
                                 '117_at', '121_at',
                                 '1255_g_at', '1294_at  UBE1L'])
# remove nan values from row_metadata (description column)
data.row_metadata_df.dropna(inplace=True)
# remove the entries of .data_df where nan values are in row_metadata
data.data_df = data.data_df.loc[data.row_metadata_df.index]

# Can only write GCT version 1.3
write(data, 'new_example.gct')

那么new_example.gct看起来是这样的:

#1.3
3   2   1   0
id  Description Tumor_One   Normal_One

1053_at RFC2 : replication factor C (activator 1) 2, 40kDa |@RFC2|  0.8689  -1.3309

121_at  PAX8 : paired box gene 8 |@PAX8|    -0.8254 0.1021

1255_g_at   GUCA1A : guanylate cyclase activator 1A (retina) |@GUCA1A|  -0.7349 -0.1841

相关问题 更多 >